Start writing systematic 'dnahelper' tests

This commit is contained in:
Andrew Bettison 2012-07-23 12:23:17 +09:30
parent 0e5c5e0e98
commit 30567c1d34
4 changed files with 145 additions and 67 deletions

View File

@ -428,6 +428,11 @@ int app_echo(int argc, const char *const *argv, struct command_line_option *o)
return 0; return 0;
} }
int cli_lookup_did(const char *text)
{
return text[0] == '\0' || strcmp(text, "*") || str_is_did(text);
}
int app_dna_lookup(int argc, const char *const *argv, struct command_line_option *o) int app_dna_lookup(int argc, const char *const *argv, struct command_line_option *o)
{ {
int i; int i;
@ -441,7 +446,7 @@ int app_dna_lookup(int argc, const char *const *argv, struct command_line_option
char uris[MAXREPLIES][MAXURILEN]; char uris[MAXREPLIES][MAXURILEN];
const char *did; const char *did;
if (cli_arg(argc, argv, o, "did", &did, NULL, "*") == -1) if (cli_arg(argc, argv, o, "did", &did, cli_lookup_did, "*") == -1)
return -1; return -1;
/* Bind to MDP socket and await confirmation */ /* Bind to MDP socket and await confirmation */
@ -1407,15 +1412,19 @@ int app_keyring_add(int argc, const char *const *argv, struct command_line_optio
return 0; return 0;
} }
int cli_optional_did(const char *text)
{
return text[0] == '\0' || str_is_did(text);
}
int app_keyring_set_did(int argc, const char *const *argv, struct command_line_option *o) int app_keyring_set_did(int argc, const char *const *argv, struct command_line_option *o)
{ {
const char *sid, *did, *pin, *name; const char *sid, *did, *pin, *name;
cli_arg(argc, argv, o, "sid", &sid, NULL, ""); cli_arg(argc, argv, o, "sid", &sid, str_is_subscriber_id, "");
cli_arg(argc, argv, o, "did", &did, NULL, ""); cli_arg(argc, argv, o, "did", &did, str_is_did, "");
cli_arg(argc, argv, o, "name", &name, NULL, ""); cli_arg(argc, argv, o, "name", &name, NULL, "");
cli_arg(argc, argv, o, "pin", &pin, NULL, ""); cli_arg(argc, argv, o, "pin", &pin, NULL, "");
if (strlen(did)>31) return WHY("DID too long (31 digits max)");
if (strlen(name)>63) return WHY("Name too long (31 char max)"); if (strlen(name)>63) return WHY("Name too long (31 char max)");
if (!(keyring = keyring_open_with_pins(pin))) if (!(keyring = keyring_open_with_pins(pin)))

View File

@ -99,7 +99,8 @@ int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary)
int str_is_subscriber_id(const char *sid) int str_is_subscriber_id(const char *sid)
{ {
return strcasecmp(sid, "broadcast") == 0 || _is_xstring(sid, SID_STRLEN); size_t len = 0;
return strn_is_subscriber_id(sid, &len) && sid[len] == '\0';
} }
int strn_is_subscriber_id(const char *sid, size_t *lenp) int strn_is_subscriber_id(const char *sid, size_t *lenp)
@ -157,6 +158,32 @@ int rhizome_str_is_file_hash(const char *hash)
return _is_xstring(hash, RHIZOME_FILEHASH_STRLEN); return _is_xstring(hash, RHIZOME_FILEHASH_STRLEN);
} }
int str_is_did(const char *did)
{
size_t len = 0;
return strn_is_did(did, &len) && did[len] == '\0';
}
int strn_is_did(const char *did, size_t *lenp)
{
int i;
for (i = 0; i < DID_MAXSIZE; ++i) {
switch (did[i]) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '*': case '#': case '+':
break;
default:
return 0;
}
}
if (i < DID_MINSIZE)
return 0;
if (lenp)
*lenp = i;
return 1;
}
int extractDid(unsigned char *packet,int *ofs,char *did) int extractDid(unsigned char *packet,int *ofs,char *did)
{ {
int d=0; int d=0;

View File

@ -300,6 +300,7 @@ void keyring_identity_extract(const keyring_identity *id, const unsigned char **
*/ */
#define SID_SIZE 32 #define SID_SIZE 32
#define DID_MINSIZE 5
#define DID_MAXSIZE 32 #define DID_MAXSIZE 32
#define SIDDIDFIELD_LEN (SID_SIZE+1) #define SIDDIDFIELD_LEN (SID_SIZE+1)
#define PINFIELD_LEN 32 #define PINFIELD_LEN 32
@ -688,6 +689,8 @@ char *str_toupper_inplace(char *s);
int str_is_subscriber_id(const char *sid); int str_is_subscriber_id(const char *sid);
int strn_is_subscriber_id(const char *sid, size_t *lenp); int strn_is_subscriber_id(const char *sid, size_t *lenp);
int str_is_did(const char *did);
int strn_is_did(const char *did, size_t *lenp);
int stowSid(unsigned char *packet, int ofs, const char *sid); int stowSid(unsigned char *packet, int ofs, const char *sid);
int stowDid(unsigned char *packet,int *ofs,char *did); int stowDid(unsigned char *packet,int *ofs,char *did);

View File

@ -43,64 +43,78 @@ configure_servald_server() {
setup_dnahelper() { setup_dnahelper() {
dnahelper="$TFWTMP/dnahelper" dnahelper="$TFWTMP/dnahelper"
cat >"$dnahelper" <<EOF cat >"$dnahelper" <<'EOF'
#!/usr/bin/env python #!/bin/sh
# Sample DNA Helper application for testing echo STARTED
import sys while read line
def main(): do
print "STARTED" token="${line%%|*}"
sys.stdout.flush() line="${line#*|}"
while True: did="${line%%|*}"
#sys.stderr.write("wah\n") line="${line#*|}"
line = sys.stdin.readline().strip() case "$token|$did|$line" in
if line == "": '|'*'|')
# EOF detection is broken :( echo "empty token" >&2
break ;;
s = line.split('|') *'||')
if len(s) != 3: echo "empty DID" >&2
print "ERROR" ;;
continue *'|00000|')
(token, number, xxx) = s echo "$token|A|$did|B|"
if number == "12345": ;;
# Multiple results (SID/VoMP results) *'|00001|')
print "%s|sid:%s|%s|%s|" % (token, token, number, "Agent A. Smith") echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
print "%s|sid:%s|%s|%s|" % (token, token, number, "Agent B. Smith") ;;
elif number == "5551234": *'|00002|')
# Single result, SIP URI echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
print "%s|sip://5551234@10.1.2.3|%s|%s|" % (token, number, "Will Smith") sleep 0.1
elif number == "5551001": echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|"
# Empty URI field sleep 0.1
print "%s||%s|%s|" % (token, number, "Empty URI") ;;
elif number == "5551002": *'|00003|')
# Empty DID field echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
print "%s|sip://123@1.2.3.4||%s|" % (token, "Empty DID") sleep 0.1
elif number == "5551003": echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|"
# Empty CALLERID field sleep 0.1
print "%s|sip://empty-callerid@1.2.3.4|%s||" % (token, number) echo "$token|sip://$token@10.1.0.3|$did|Joe C. Bloggs|"
elif number == "5551004": sleep 0.1
# Excessively long callerid ;;
print "%s|sip://long-callerid@1.2.3.4|%s|%s|" % (token, 'x' * 200, number) *'|00004|')
elif number == "5551005": # Empty URI
# Excessively long DID echo "$token||$did|Eccles|"
print "%s|sip://long-did@1.2.3.4|%s|%s|" % (token, 'x' * 200, "Agent Smith") ;;
elif number == "5551006": # Test malformed URI
# Excessively long URI # Test mismatched token
print "%s|sip://%s|%s|%s|" % (token, 'x' * 1000, number, "Agent Smith") # Test empty token
elif number == "5551007": # Test long token
# Incorrect token # Test empty DID
print "cheeseburger|sip://incorrect-token@1.2.3.4|%s||" % (token, number) # Test mismatched DID
elif number == "11111": # Test long DID
print "%s|A|%s|B|" % (token, number) # Test malformed line
print "DONE" # Test long reply
sys.stdout.flush() # Test DONE timeout
if __name__ == "__main__": # Test die
main() # Test fork and die
*'|'*'|')
;;
*)
echo "garbage line" >&2
;;
esac
echo DONE
done
EOF EOF
chmod 0755 "$dnahelper" chmod 0755 "$dnahelper"
executeOk "$dnahelper" <<EOF executeOk "$dnahelper" <<EOF
ToKeN|11111| ToKeN|00000|
EOF EOF
assertStdoutIs -e "STARTED\nToKeN|A|11111|B|\nDONE\n" assertStdoutIs -e "STARTED\nToKeN|A|00000|B|\nDONE\n"
}
setup_single_instance() {
setup
setup_dnahelper
start_servald_instances +A
} }
doc_ExecError="Non-existent DNA helper executable" doc_ExecError="Non-existent DNA helper executable"
@ -114,15 +128,40 @@ test_ExecError() {
executeOk_servald dna lookup 12345 executeOk_servald dna lookup 12345
} }
doc_Simple="Simple DNA helper test" doc_OneReply="DNA helper returns one line"
setup_Simple() { setup_OneReply() {
setup setup_single_instance
setup_dnahelper
start_servald_instances +A
} }
test_Simple() { test_OneReply() {
executeOk_servald dna lookup 12345 executeOk_servald dna lookup 00001
tfw_cat --stdout --stderr assertStdoutIs -e "sip://$SIDA@10.1.0.1:00001:Joe A. Bloggs\n"
}
doc_TwoReplies="DNA helper returns two lines"
setup_TwoReplies() {
setup_single_instance
}
test_TwoReplies() {
executeOk_servald dna lookup 00002
assertStdoutIs -e "sip://$SIDA@10.1.0.1:00002:Joe A. Bloggs\nsip://$SIDA@10.1.0.2:00002:Joe B. Bloggs\n"
}
doc_ThreeReplies="DNA helper returns three lines"
setup_ThreeReplies() {
setup_single_instance
}
test_ThreeReplies() {
executeOk_servald dna lookup 00003
assertStdoutIs -e "sip://$SIDA@10.1.0.1:00003:Joe A. Bloggs\nsip://$SIDA@10.1.0.2:00003:Joe B. Bloggs\nsip://$SIDA@10.1.0.3:00003:Joe C. Bloggs\n"
}
doc_EmptyURI="DNA helper returns empry URI"
setup_EmptyURI() {
setup_single_instance
}
test_EmptyURI() {
executeOk_servald dna lookup 00004
assertStdoutIs ""
} }
runTests "$@" runTests "$@"