#!/bin/bash # Tests for Serval rhizome operations. # # Copyright 2013 Serval Project, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. source "${0%/*}/../testframework.sh" source "${0%/*}/../testdefs.sh" source "${0%/*}/../testdefs_rhizome.sh" shopt -s extglob setup() { CR=' ' setup_curl 7 setup_jq 1.2 setup_servald set_instance +A set_rhizome_config executeOk_servald config \ set rhizome.api.restful.users.harry.password potter \ set rhizome.api.restful.users.ron.password weasley \ set rhizome.api.restful.users.hermione.password grainger create_single_identity echo "$SIDA1" >sids start_servald_instances +A wait_until rhizome_http_server_started +A get_rhizome_server_port PORTA +A } finally() { stop_all_servald_servers } teardown() { kill_all_servald_processes assert_no_servald_processes report_all_servald_servers } set_rhizome_config() { executeOk_servald config \ set debug.httpd on \ set debug.rhizome_httpd on \ set debug.rhizome on \ set debug.verbose on \ set log.console.level debug } doc_AuthBasicMissing="Basic Authentication credentials are required" test_AuthBasicMissing() { executeOk curl \ --silent --show-error --write-out '%{http_code}' \ --output http.output \ --dump-header http.headers \ "http://$addr_localhost:$PORTA/restful/rhizome/bundlelist.json" assertStdoutIs '401' assertGrep http.headers "^WWW-Authenticate: Basic realm=\"Serval Rhizome\"$CR$" } teardown_AuthBasicMissing() { tfw_cat http.headers http.output teardown } doc_AuthBasicWrong="Basic Authentication credentials must be correct" test_AuthBasicWrong() { executeOk curl \ --silent --show-error --write-out '%{http_code}' \ --output http.output \ --dump-header http.headers \ --basic --user fred:nurks \ "http://$addr_localhost:$PORTA/restful/rhizome/bundlelist.json" assertStdoutIs '401' assertGrep http.headers "^WWW-Authenticate: Basic realm=\"Serval Rhizome\"$CR$" executeOk curl \ --silent --fail --show-error --write-out '%{http_code}' \ --output http.output \ --dump-header http.headers \ --basic --user ron:weasley \ "http://$addr_localhost:$PORTA/restful/rhizome/bundlelist.json" assertStdoutIs '200' } teardown_AuthBasicWrong() { tfw_cat http.headers http.output teardown } doc_RhizomeList="Fetch small Rhizome bundle list in JSON format" setup_RhizomeList() { setup NBUNDLES=100 for ((n = 0; n != NBUNDLES; ++n)); do create_file file$n $((1000 + $n)) executeOk_servald rhizome add file $SIDA file$n file$n.manifest extract_stdout_manifestid BID[$n] extract_stdout_version VERSION[$n] extract_stdout_filesize SIZE[$n] extract_stdout_filehash HASH[$n] extract_stdout_date DATE[$n] extract_stdout_rowid ROWID[$n] done } test_RhizomeList() { executeOk curl \ --silent --fail --show-error \ --output bundlelist.json \ --dump-header http.headers \ --basic --user harry:potter \ "http://$addr_localhost:$PORTA/restful/rhizome/bundlelist.json" tfw_cat http.headers bundlelist.json tfw_preserve bundlelist.json assert [ "$(jq '.rows | length' bundlelist.json)" = $NBUNDLES ] # The following jq(1) incantation transforms the JSON array in # bundlelist.json from the following form (which is optimised for # transmission size): # { # "header":[ "_id", "service", "id", "version","date",".inserttime", # ".author",".fromhere","filesize","filehash","sender","recipient", # "name" # ], # "token":"xxx", # "rows":[ # [ rowid1, "service1", bundleid1, version1, .... ], # [ rowid2, "service2", bundleid2, version2, .... ], # ... # [ rowidN, "serviceN", bundleidN, versionN, .... ] # ] # } # # into an array of JSON objects: # { # "token":"xxx", # "bundles":[ # { # "_id": rowid1, # "service": service1, # "id": bundleid1, # "version": version1, # ... # }, # { # "_id": rowid2, # "service": service2, # "id": bundleid2, # "version": version2, # ... # }, # ... # { # "_id": rowidN, # "service": serviceN, # "id": bundleidN, # "version": versionN, # ... # } # ] # } # which is much easier to test with jq(1) expressions. jq \ '[ .header as $h | .rows[] as $d | [ $d | keys | .[] as $i | {key:$h[$i], value:$d[$i]} ] | from_entries ] as $bundles | .token as $token | { token:$token, bundles:$bundles }' \ bundlelist.json >array_of_objects.json tfw_preserve array_of_objects.json for ((n = 0; n != NBUNDLES; ++n)); do jqscript=".bundles | contains([ { name:\"file$n\", service:\"file\", id:\"${BID[$n]}\", version:${VERSION[$n]}, filesize:${SIZE[$n]}, filehash:\"${HASH[$n]}\", date:${DATE[$n]}, _id:${ROWID[$n]}, \".fromhere\":1, \".author\":\"$SIDA\", } ])" assert [ "$(jq "$jqscript" array_of_objects.json)" = true ] done } doc_RhizomeListSince="Fetch Rhizome bundle list since token in JSON format" test_RhizomeListSinceJSON() { : } doc_RhizomeManifest="Fetch Rhizome bundle manifest" test_RhizomeManifest() { : } doc_RhizomePayloadRaw="Fetch Rhizome raw payload" test_RhizomePayloadRaw() { : } doc_RhizomePayloadDecrypted="Fetch Rhizome decrypted payload" test_RhizomePayloadDecrypted() { : } doc_RhizomeInsert="Insert new Rhizome bundle" test_RhizomeInsert() { : } doc_MeshmsListConversations="List MeshMS conversations" test_MeshmsListConversations() { : } doc_MeshmsListMessages="List all MeshMS messages in a single conversation" test_MeshmsListMessages() { : } doc_MeshmsListMessagesSince="List MeshMS messages in a single conversation since token" test_MeshmsListMessagesSince() { : } doc_MeshmsSend="Send MeshMS message" test_MeshmsSend() { : } runTests "$@"