From 6e959fd34ce5d82bc6aa5266569cea702f50102d Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Wed, 2 May 2012 16:24:27 +0930 Subject: [PATCH] More tests for "rhizome extract manifest" Proper handling of unknown and invalid manifest IDs --- commandline.c | 22 +++++++++++----------- tests/dna_rhizome | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/commandline.c b/commandline.c index 8dcf585c..d1ad9a9f 100644 --- a/commandline.c +++ b/commandline.c @@ -1032,8 +1032,9 @@ int cli_manifestid(const char *arg) int app_rhizome_extract_manifest(int argc, const char *const *argv, struct command_line_option *o) { const char *manifestid, *manifestpath; - cli_arg(argc, argv, o, "manifestid", &manifestid, cli_manifestid, NULL); - cli_arg(argc, argv, o, "manifestpath", &manifestpath, NULL, ""); + if (cli_arg(argc, argv, o, "manifestid", &manifestid, cli_manifestid, NULL) + || cli_arg(argc, argv, o, "manifestpath", &manifestpath, NULL, "") == -1) + return -1; /* Ensure the Rhizome database exists and is open */ if (create_serval_instance_dir() == -1) return -1; @@ -1041,13 +1042,10 @@ int app_rhizome_extract_manifest(int argc, const char *const *argv, struct comma rhizome_opendb(); /* Extract the manifest from the database */ rhizome_manifest *m = NULL; - int ret = -1; - switch (rhizome_retrieve_manifest(manifestid, &m)) { - case 0: - ret = 0; - break; - case 1: - ret = 0; + int ret = rhizome_retrieve_manifest(manifestid, &m); + switch (ret) { + case 0: ret = 1; break; + case 1: ret = 0; if (manifestpath[0]) { if (rhizome_manifest_finalise(m, 1) == -1) ret = WHY("Could not overwrite manifest file."); @@ -1055,6 +1053,8 @@ int app_rhizome_extract_manifest(int argc, const char *const *argv, struct comma ret = WHY("Could not overwrite manifest file."); } break; + case -1: break; + default: ret = WHYF("Unsupported return value %d", ret); break; } if (m) rhizome_manifest_free(m); @@ -1370,9 +1370,9 @@ command_line_option command_line_options[]={ "Add a file to Rhizome and optionally write its manifest to the given path"}, {app_rhizome_list,{"rhizome","list","[]","[]",NULL},CLIFLAG_STANDALONE, "List all manifests and files in Rhizome"}, - {app_rhizome_extract_manifest,{"rhizome","extract","manifest","","",NULL},CLIFLAG_STANDALONE, + {app_rhizome_extract_manifest,{"rhizome","extract","manifest","","[]",NULL},CLIFLAG_STANDALONE, "Extract a manifest from Rhizome and write it to the given path"}, - {app_rhizome_extract_file,{"rhizome","extract","file","","",NULL},CLIFLAG_STANDALONE, + {app_rhizome_extract_file,{"rhizome","extract","file","","[]",NULL},CLIFLAG_STANDALONE, "Extract a file from Rhizome and write it to the given path"}, {app_keyring_create,{"keyring","create",NULL},0, "Create a new keyring file."}, diff --git a/tests/dna_rhizome b/tests/dna_rhizome index 49c25e28..9621cf59 100755 --- a/tests/dna_rhizome +++ b/tests/dna_rhizome @@ -189,6 +189,33 @@ test_AddThenExtractManifest() { assertStdoutGrep --matches=1 "^inserttime:[0-9]\+$" } +doc_ExtractMissingManifest="Extract non-existent manifest" +setup_ExtractMissingManifest() { + setup_dna_rhizome + manifestid=0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF +} +test_ExtractMissingManifest() { + execute --exit-status=1 $dna rhizome extract manifest $manifestid foo.manifest + assertStdoutLineCount '==' 0 + assert [ ! -e foo.manifest ] +} + +doc_ExtractManifestInvalidID="Extract manifest using invalid ID" +setup_ExtractManifestInvalidID() { + setup_dna_rhizome +} +test_ExtractManifestInvalidID() { + execute --exit-status=255 $dna rhizome extract manifest 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEx foo.manifest + assertStdoutLineCount '==' 0 + assert [ ! -e foo.manifest ] + execute --exit-status=255 $dna rhizome extract manifest 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE foo.manifest + assertStdoutLineCount '==' 0 + assert [ ! -e foo.manifest ] + execute --exit-status=255 $dna rhizome extract manifest '' foo.manifest + assertStdoutLineCount '==' 0 + assert [ ! -e foo.manifest ] +} + doc_AddDuplicate="Add same manifest detects duplicate" setup_AddDuplicate() { setup_dna_rhizome