mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 02:39:44 +00:00
Start writing systematic 'dnahelper' tests
This commit is contained in:
parent
0e5c5e0e98
commit
30567c1d34
@ -428,6 +428,11 @@ int app_echo(int argc, const char *const *argv, struct command_line_option *o)
|
||||
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 i;
|
||||
@ -441,7 +446,7 @@ int app_dna_lookup(int argc, const char *const *argv, struct command_line_option
|
||||
char uris[MAXREPLIES][MAXURILEN];
|
||||
|
||||
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;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
const char *sid, *did, *pin, *name;
|
||||
cli_arg(argc, argv, o, "sid", &sid, NULL, "");
|
||||
cli_arg(argc, argv, o, "did", &did, NULL, "");
|
||||
cli_arg(argc, argv, o, "sid", &sid, str_is_subscriber_id, "");
|
||||
cli_arg(argc, argv, o, "did", &did, str_is_did, "");
|
||||
cli_arg(argc, argv, o, "name", &name, 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 (!(keyring = keyring_open_with_pins(pin)))
|
||||
|
@ -99,7 +99,8 @@ int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary)
|
||||
|
||||
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)
|
||||
@ -157,6 +158,32 @@ int rhizome_str_is_file_hash(const char *hash)
|
||||
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 d=0;
|
||||
|
3
serval.h
3
serval.h
@ -300,6 +300,7 @@ void keyring_identity_extract(const keyring_identity *id, const unsigned char **
|
||||
|
||||
*/
|
||||
#define SID_SIZE 32
|
||||
#define DID_MINSIZE 5
|
||||
#define DID_MAXSIZE 32
|
||||
#define SIDDIDFIELD_LEN (SID_SIZE+1)
|
||||
#define PINFIELD_LEN 32
|
||||
@ -688,6 +689,8 @@ char *str_toupper_inplace(char *s);
|
||||
|
||||
int str_is_subscriber_id(const char *sid);
|
||||
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 stowDid(unsigned char *packet,int *ofs,char *did);
|
||||
|
163
tests/dnahelper
163
tests/dnahelper
@ -43,64 +43,78 @@ configure_servald_server() {
|
||||
|
||||
setup_dnahelper() {
|
||||
dnahelper="$TFWTMP/dnahelper"
|
||||
cat >"$dnahelper" <<EOF
|
||||
#!/usr/bin/env python
|
||||
# Sample DNA Helper application for testing
|
||||
import sys
|
||||
def main():
|
||||
print "STARTED"
|
||||
sys.stdout.flush()
|
||||
while True:
|
||||
#sys.stderr.write("wah\n")
|
||||
line = sys.stdin.readline().strip()
|
||||
if line == "":
|
||||
# EOF detection is broken :(
|
||||
break
|
||||
s = line.split('|')
|
||||
if len(s) != 3:
|
||||
print "ERROR"
|
||||
continue
|
||||
(token, number, xxx) = s
|
||||
if number == "12345":
|
||||
# Multiple results (SID/VoMP results)
|
||||
print "%s|sid:%s|%s|%s|" % (token, token, number, "Agent A. Smith")
|
||||
print "%s|sid:%s|%s|%s|" % (token, token, number, "Agent B. Smith")
|
||||
elif number == "5551234":
|
||||
# Single result, SIP URI
|
||||
print "%s|sip://5551234@10.1.2.3|%s|%s|" % (token, number, "Will Smith")
|
||||
elif number == "5551001":
|
||||
# Empty URI field
|
||||
print "%s||%s|%s|" % (token, number, "Empty URI")
|
||||
elif number == "5551002":
|
||||
# Empty DID field
|
||||
print "%s|sip://123@1.2.3.4||%s|" % (token, "Empty DID")
|
||||
elif number == "5551003":
|
||||
# Empty CALLERID field
|
||||
print "%s|sip://empty-callerid@1.2.3.4|%s||" % (token, number)
|
||||
elif number == "5551004":
|
||||
# Excessively long callerid
|
||||
print "%s|sip://long-callerid@1.2.3.4|%s|%s|" % (token, 'x' * 200, number)
|
||||
elif number == "5551005":
|
||||
# Excessively long DID
|
||||
print "%s|sip://long-did@1.2.3.4|%s|%s|" % (token, 'x' * 200, "Agent Smith")
|
||||
elif number == "5551006":
|
||||
# Excessively long URI
|
||||
print "%s|sip://%s|%s|%s|" % (token, 'x' * 1000, number, "Agent Smith")
|
||||
elif number == "5551007":
|
||||
# Incorrect token
|
||||
print "cheeseburger|sip://incorrect-token@1.2.3.4|%s||" % (token, number)
|
||||
elif number == "11111":
|
||||
print "%s|A|%s|B|" % (token, number)
|
||||
print "DONE"
|
||||
sys.stdout.flush()
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
cat >"$dnahelper" <<'EOF'
|
||||
#!/bin/sh
|
||||
echo STARTED
|
||||
while read line
|
||||
do
|
||||
token="${line%%|*}"
|
||||
line="${line#*|}"
|
||||
did="${line%%|*}"
|
||||
line="${line#*|}"
|
||||
case "$token|$did|$line" in
|
||||
'|'*'|')
|
||||
echo "empty token" >&2
|
||||
;;
|
||||
*'||')
|
||||
echo "empty DID" >&2
|
||||
;;
|
||||
*'|00000|')
|
||||
echo "$token|A|$did|B|"
|
||||
;;
|
||||
*'|00001|')
|
||||
echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
|
||||
;;
|
||||
*'|00002|')
|
||||
echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
|
||||
sleep 0.1
|
||||
echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|"
|
||||
sleep 0.1
|
||||
;;
|
||||
*'|00003|')
|
||||
echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
|
||||
sleep 0.1
|
||||
echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|"
|
||||
sleep 0.1
|
||||
echo "$token|sip://$token@10.1.0.3|$did|Joe C. Bloggs|"
|
||||
sleep 0.1
|
||||
;;
|
||||
*'|00004|')
|
||||
# Empty URI
|
||||
echo "$token||$did|Eccles|"
|
||||
;;
|
||||
# Test malformed URI
|
||||
# Test mismatched token
|
||||
# Test empty token
|
||||
# Test long token
|
||||
# Test empty DID
|
||||
# Test mismatched DID
|
||||
# Test long DID
|
||||
# Test malformed line
|
||||
# Test long reply
|
||||
# Test DONE timeout
|
||||
# Test die
|
||||
# Test fork and die
|
||||
*'|'*'|')
|
||||
;;
|
||||
*)
|
||||
echo "garbage line" >&2
|
||||
;;
|
||||
esac
|
||||
echo DONE
|
||||
done
|
||||
EOF
|
||||
chmod 0755 "$dnahelper"
|
||||
executeOk "$dnahelper" <<EOF
|
||||
ToKeN|11111|
|
||||
ToKeN|00000|
|
||||
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"
|
||||
@ -114,15 +128,40 @@ test_ExecError() {
|
||||
executeOk_servald dna lookup 12345
|
||||
}
|
||||
|
||||
doc_Simple="Simple DNA helper test"
|
||||
setup_Simple() {
|
||||
setup
|
||||
setup_dnahelper
|
||||
start_servald_instances +A
|
||||
doc_OneReply="DNA helper returns one line"
|
||||
setup_OneReply() {
|
||||
setup_single_instance
|
||||
}
|
||||
test_Simple() {
|
||||
executeOk_servald dna lookup 12345
|
||||
tfw_cat --stdout --stderr
|
||||
test_OneReply() {
|
||||
executeOk_servald dna lookup 00001
|
||||
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 "$@"
|
||||
|
Loading…
Reference in New Issue
Block a user