Fix failing Rhizome Java API tests

Update the Java API to be consistent with recent changes to the Rhizome
REST API was that regularised the MIME types used in Content-Type
headers.

Add some missing CONTENT_TYPE_ constants to "httpd.h", and ensure they
are consistent with the Java getMimeType() methods in subclasses of
AbstractId.

Add the --stderr option to all executeJavaOk statements in the
'rhizomejava' test script, to reveal the exception message in the case
of failure.
This commit is contained in:
Andrew Bettison 2017-12-20 13:52:44 +10:30
parent 16d4a6d18e
commit e9d91d04ca
9 changed files with 95 additions and 37 deletions

23
httpd.c
View File

@ -27,21 +27,40 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define RHIZOME_SERVER_MAX_LIVE_REQUESTS 32
/* These must agree with the values returned by the getMimeType() methods in all the Java
* subclasses of org.servalproject.servaldna.AbstractId.
*/
const struct mime_content_type CONTENT_TYPE_SID_HEX = {
.type = "serval",
.subtype = "sid",
.format = "hex"
};
const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_ID = {
const struct mime_content_type CONTENT_TYPE_IDENTITY_HEX = {
.type = "serval",
.subtype = "identity",
.format = "hex"
};
const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_ID_HEX = {
.type = "rhizome",
.subtype = "bid",
.format = "hex"
};
const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_SECRET = {
const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_KEY_HEX = {
.type = "rhizome",
.subtype = "bundlekey",
.format = "hex"
};
const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_SECRET_HEX = {
.type = "rhizome",
.subtype = "bundlesecret",
.format = "hex"
};
const struct mime_content_type CONTENT_TYPE_RHIZOME_FILEHASH_HEX = {
.type = "rhizome",
.subtype = "filehash",
.format = "hex"
};
const struct mime_content_type CONTENT_TYPE_RHIZOME_MANIFEST = {
.type = "rhizome",
.subtype = "manifest",

View File

@ -36,8 +36,11 @@ extern unsigned int current_httpd_request_count;
// Some non-standard MIME types for the Content-Type header
extern const struct mime_content_type CONTENT_TYPE_SID_HEX;
extern const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_ID;
extern const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_SECRET;
extern const struct mime_content_type CONTENT_TYPE_IDENTITY_HEX;
extern const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_ID_HEX;
extern const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_KEY_HEX;
extern const struct mime_content_type CONTENT_TYPE_RHIZOME_BUNDLE_SECRET_HEX;
extern const struct mime_content_type CONTENT_TYPE_RHIZOME_FILEHASH_HEX;
extern const struct mime_content_type CONTENT_TYPE_RHIZOME_MANIFEST;
enum list_phase { LIST_HEADER = 0, LIST_FIRST, LIST_ROWS, LIST_END, LIST_DONE };

View File

@ -32,7 +32,7 @@ public class BundleKey extends AbstractId {
@Override
public String getMimeType() {
return null; //TODO?
return "rhizome/bundlekey";
}
public BundleKey(String hex) throws InvalidHexException {

View File

@ -34,7 +34,7 @@ public class FileHash extends AbstractId {
@Override
public String getMimeType() {
return null;
return "rhizome/filehash";
}
public FileHash(String hex) throws InvalidHexException {

View File

@ -44,7 +44,7 @@ public class SigningKey extends AbstractId {
@Override
public String getMimeType() {
return "serval-mesh/id";
return "serval/identity";
}
@Override

View File

@ -48,9 +48,22 @@ import java.lang.reflect.Modifier;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Enumeration;
import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
public class RhizomeCommon
{
public static final MimeType MIME_TYPE_RHIZOME_MANIFEST;
static {
try {
MIME_TYPE_RHIZOME_MANIFEST = new MimeType("rhizome/manifest; format=text+binarysig");
}
catch (MimeTypeParseException ex) {
throw new IllegalStateException(ex);
}
}
private static class Status {
URL url;
@ -64,6 +77,17 @@ public class RhizomeCommon
String payload_status_message;
}
protected static boolean mimeTypeMatches(MimeType required, MimeType candidate) {
if (!candidate.match(required))
return false;
for (Enumeration e = required.getParameters().getNames(); e.hasMoreElements(); ) {
String name = (String) e.nextElement();
if (!required.getParameter(name).equals(candidate.getParameter(name)))
return false;
}
return true;
}
protected static Status receiveResponse(HttpURLConnection conn, int expected_response_code) throws IOException, ServalDInterfaceException
{
int[] expected_response_codes = { expected_response_code };
@ -231,11 +255,18 @@ public class RhizomeCommon
case NEW:
return null;
case SAME:
if (!RhizomeManifest.MIME_TYPE.equals(conn.getContentType()))
throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
try {
MimeType content_type = new MimeType(conn.getContentType());
if (mimeTypeMatches(MIME_TYPE_RHIZOME_MANIFEST, content_type)) {
RhizomeManifest manifest = RhizomeManifest.fromTextFormat(status.input_stream);
BundleExtra extra = bundleExtraFromHeaders(conn);
return new RhizomeManifestBundle(manifest, extra.rowId, extra.insertTime, extra.author, extra.secret);
}
throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + content_type);
}
catch (MimeTypeParseException ex) {
throw new ServalDInterfaceException("invalid HTTP Content-Type: " + conn.getContentType());
}
case ERROR:
throw new ServalDFailureException("received rhizome_bundle_status_code=ERROR(-1) from " + conn.getURL());
}
@ -453,8 +484,10 @@ public class RhizomeCommon
decodeHeaderBundleStatus(status, conn);
checkBundleStatus(status);
if (!RhizomeManifest.MIME_TYPE.equals(status.contentType))
throw new ServalDInterfaceException("unexpected HTTP Content-Type " + status.contentType + " from " + status.url);
MimeType content_type = new MimeType(status.contentType);
if (!mimeTypeMatches(MIME_TYPE_RHIZOME_MANIFEST, content_type))
throw new ServalDInterfaceException("unexpected HTTP Content-Type " + content_type + " from " + status.url + ", expecting " + MIME_TYPE_RHIZOME_MANIFEST);
RhizomeManifest returned_manifest = RhizomeManifest.fromTextFormat(status.input_stream);
BundleExtra extra = bundleExtraFromHeaders(conn);
return new RhizomeInsertBundle(status.bundle_status_code, status.payload_status_code, returned_manifest, extra.rowId, extra.insertTime, extra.author, extra.secret);
@ -462,6 +495,9 @@ public class RhizomeCommon
catch (RhizomeManifestParseException e) {
throw new ServalDInterfaceException("malformed manifest from daemon", e);
}
catch (MimeTypeParseException ex) {
throw new ServalDInterfaceException("invalid HTTP Content-Type: " + status.contentType);
}
finally {
if (status.input_stream != null)
status.input_stream.close();

View File

@ -641,7 +641,7 @@ static int insert_mime_part_header(struct http_request *hr, const struct mime_pa
if (!h->content_type.type[0])
; // missing Content-Type defaults to CONTENT_TYPE_RHIZOME_BUNDLE_SECRET
// TODO deprecate this default and insist that Content-Type be supplied
else if (!mime_content_types_are_equal(&h->content_type, &CONTENT_TYPE_RHIZOME_BUNDLE_SECRET))
else if (!mime_content_types_are_equal(&h->content_type, &CONTENT_TYPE_RHIZOME_BUNDLE_SECRET_HEX))
return http_response_form_part(r, 415, "Unsupported Content-Type in", PART_SECRET, NULL, 0);
r->u.insert.current_part = PART_SECRET;
assert(r->u.insert.secret_text_len == 0);
@ -655,7 +655,7 @@ static int insert_mime_part_header(struct http_request *hr, const struct mime_pa
if (!h->content_type.type[0])
; // missing Content-Type defaults to CONTENT_TYPE_RHIZOME_BUNDLE_ID
// TODO deprecate this default and insist that Content-Type be supplied
else if (!mime_content_types_are_equal(&h->content_type, &CONTENT_TYPE_RHIZOME_BUNDLE_ID))
else if (!mime_content_types_are_equal(&h->content_type, &CONTENT_TYPE_RHIZOME_BUNDLE_ID_HEX))
return http_response_form_part(r, 415, "Unsupported Content-Type in", PART_BUNDLEID, NULL, 0);
r->u.insert.current_part = PART_BUNDLEID;
assert(r->u.insert.bid_text_len == 0);

View File

@ -58,7 +58,7 @@ setup_RhizomeList() {
assert [ "$ROWID_MAX" -ge "$NBUNDLES" ]
}
test_RhizomeList() {
executeJavaOk org.servalproject.test.Rhizome rhizome-list
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-list
tfw_cat --stdout --stderr
assertStdoutLineCount == $NBUNDLES
let lnum=NBUNDLES
@ -97,7 +97,7 @@ setup_RhizomeListNewSince() {
# locking storm
rhizome_use_restful harry potter
rhizome_add_bundles $SIDA1 0 5
executeJavaOk org.servalproject.test.Rhizome rhizome-list
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-list
assertStdoutLineCount == 6
unset_vars_with_prefix XX_
unpack_vars XX_ "$(sed -n -e 1p "$TFWSTDOUT")"
@ -165,7 +165,7 @@ setup_RhizomeManifest() {
}
test_RhizomeManifest() {
for n in 0 1 2; do
executeJavaOk org.servalproject.test.Rhizome rhizome-manifest "${BID[$n]}" bundle$n.rhm
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-manifest "${BID[$n]}" bundle$n.rhm
tfw_cat --stdout --stderr
assert_metadata $n
ls -l file$n.manifest bundle$n.rhm
@ -179,7 +179,7 @@ setup_RhizomeManifestNonexist() {
setup
}
test_RhizomeManifestNonexist() {
executeJavaOk org.servalproject.test.Rhizome rhizome-manifest "$BID_NONEXISTENT" ''
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-manifest "$BID_NONEXISTENT" ''
tfw_cat --stdout --stderr
assertStdoutLineCount == 1
assertStdoutGrep --ignore-case '^not found$'
@ -193,7 +193,7 @@ setup_RhizomePayloadRaw() {
}
test_RhizomePayloadRaw() {
for n in 0 1 2 3; do
executeJavaOk org.servalproject.test.Rhizome rhizome-payload-raw "${BID[$n]}" raw.bin$n
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-payload-raw "${BID[$n]}" raw.bin$n
tfw_cat --stdout --stderr
assert_metadata $n
done
@ -207,7 +207,7 @@ setup_RhizomePayloadRawNonexistManifest() {
setup
}
test_RhizomePayloadRawNonexistManifest() {
executeJavaOk org.servalproject.test.Rhizome rhizome-payload-raw "$BID_NONEXISTENT" ''
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-payload-raw "$BID_NONEXISTENT" ''
tfw_cat --stdout --stderr
assertStdoutLineCount == 1
assertStdoutGrep --ignore-case '^not found$'
@ -223,7 +223,7 @@ setup_RhizomePayloadRawNonexistPayload() {
rhizome_delete_payload_blobs "${HASH[0]}"
}
test_RhizomePayloadRawNonexistPayload() {
executeJavaOk org.servalproject.test.Rhizome rhizome-payload-raw "${BID[0]}" raw.bin
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-payload-raw "${BID[0]}" raw.bin
tfw_cat --stdout --stderr
assertStdoutGrep --ignore-case '^no payload$'
assert_metadata 0
@ -237,7 +237,7 @@ setup_RhizomePayloadDecrypted() {
}
test_RhizomePayloadDecrypted() {
for n in 0 1 2 3; do
executeJavaOk org.servalproject.test.Rhizome rhizome-payload-decrypted "${BID[$n]}" decrypted.bin$n
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-payload-decrypted "${BID[$n]}" decrypted.bin$n
tfw_cat --stdout --stderr
assert_metadata $n
done
@ -256,7 +256,7 @@ setup_RhizomePayloadDecryptedNonexistManifest() {
rhizome_delete_payload_blobs "${HASH[0]}"
}
test_RhizomePayloadDecryptedNonexistManifest() {
executeJavaOk org.servalproject.test.Rhizome rhizome-payload-decrypted "${BID[0]}" ''
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-payload-decrypted "${BID[0]}" ''
tfw_cat --stdout --stderr
assertStdoutGrep --ignore-case '^no payload$'
assert_metadata 0
@ -274,7 +274,7 @@ setup_RhizomePayloadDecryptedForeign() {
executeOk_servald rhizome import bundle raw1 file1.manifest
}
test_RhizomePayloadDecryptedForeign() {
executeJavaOk org.servalproject.test.Rhizome rhizome-payload-decrypted "${BID[1]}" decrypted.bin$n
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-payload-decrypted "${BID[1]}" decrypted.bin$n
tfw_cat --stdout --stderr
assertStdoutGrep RhizomeDecryptionException
}
@ -303,7 +303,7 @@ setup_RhizomeInsert() {
}
test_RhizomeInsert() {
for n in 1 2 3 4; do
executeJavaOk org.servalproject.test.Rhizome rhizome-insert "${author[$n]}" manifest$n file$n file$n.manifest "${payload_filename[$n]}"
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert "${author[$n]}" manifest$n file$n file$n.manifest "${payload_filename[$n]}"
tfw_cat --stdout --stderr -v file$n.manifest
assertStdoutGrep '^_status=NEW$'
replayStdout >stdout-insert
@ -349,7 +349,7 @@ test_RhizomeInsert() {
for n in 1 2 3 4; do
$SED -e '/^version=/d;/^date=/d;/^filehash=/d;/^filesize=/d;/^[^a-zA-Z]/,$d' xfile$n.manifest >nmanifest$n
assert_manifest_fields nmanifest$n id !version !date !filehash !filesize
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' nmanifest$n nfile$n nfile$n.manifest "nfile$n"
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert '' nmanifest$n nfile$n nfile$n.manifest "nfile$n"
tfw_cat --stdout --stderr -v nfile$n.manifest
if [ -n "${author[$n]}" ]; then
assertStdoutGrep '^_status=NEW$'
@ -371,11 +371,11 @@ setup_RhizomeImport() {
set_instance +A
}
test_RhizomeImport() {
executeJavaOk org.servalproject.test.Rhizome rhizome-import 'file1.manifest' 'file1'
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-import 'file1.manifest' 'file1'
tfw_cat --stdout --stderr
assertStdoutGrep '^_status=NEW$'
assertStdoutGrep '^_payload_status=NEW$'
executeJavaOk org.servalproject.test.Rhizome rhizome-import 'file1.manifest' 'file1'
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-import 'file1.manifest' 'file1'
tfw_cat --stdout --stderr
assertStdoutGrep '^_status=SAME$'
}
@ -393,7 +393,7 @@ setup_RhizomeInsertAnon() {
create_file file2 1002
}
test_RhizomeInsertAnon() {
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' file2.manifest file2 ifile2.manifest "file2" "$SECRET"
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert '' file2.manifest file2 ifile2.manifest "file2" "$SECRET"
tfw_cat --stdout --stderr -v ifile2.manifest
assertStdoutGrep '^_status=NEW$'
assertStdoutGrep '^_payload_status=NEW$'
@ -409,7 +409,7 @@ setup_RhizomeInsertEmptyNew() {
assert [ ! -s empty ]
}
test_RhizomeInsertEmptyNew() {
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' '' empty empty.manifest "lucky"
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert '' '' empty empty.manifest "lucky"
tfw_cat --stdout --stderr -v empty.manifest
extract_manifest_id BID empty.manifest
assertStdoutGrep '^_status=NEW$'
@ -437,7 +437,7 @@ setup_RhizomeInsertEmptyUpdate() {
echo "BK=$BK" >>iempty.manifest
}
test_RhizomeInsertEmptyUpdate() {
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' iempty.manifest empty empty.manifest
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert '' iempty.manifest empty empty.manifest
tfw_cat --stdout --stderr -v empty.manifest
assertStdoutGrep '^_status=NEW$'
assertStdoutGrep '^_payload_status=EMPTY$'
@ -458,7 +458,7 @@ setup_RhizomeInsertJournal() {
echo 'tail=0' >file1.manifest
}
test_RhizomeInsertJournal() {
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' file1.manifest file1 ifile1.manifest
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert '' file1.manifest file1 ifile1.manifest
tfw_cat --stdout --stderr
# TODO: need special exception for this case, not RhizomeInvalidManifestException
assertStdoutGrep RhizomeInvalidManifestException
@ -476,7 +476,7 @@ setup_RhizomeInsertIncorrectFilesize() {
}
test_RhizomeInsertIncorrectFilesize() {
for n in 1 2; do
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' file$n.manifest file$n ifile$n.manifest
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert '' file$n.manifest file$n ifile$n.manifest
tfw_cat --stdout --stderr
assertStdoutGrep RhizomeInconsistencyException
assertStdoutGrep --ignore-case 'payload size.*contradicts manifest'
@ -492,7 +492,7 @@ setup_RhizomeInsertIncorrectFilehash() {
echo 'filehash=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' >file1.manifest
}
test_RhizomeInsertIncorrectFilehash() {
executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' file1.manifest file1 ifile1.manifest
executeJavaOk --stderr org.servalproject.test.Rhizome rhizome-insert '' file1.manifest file1 ifile1.manifest
tfw_cat --stdout --stderr
assertStdoutGrep RhizomeInconsistencyException
assertStdoutGrep --ignore-case 'payload hash.*contradicts manifest'