Merge remote-tracking branch 'origin/master' into sid_handling

This commit is contained in:
Jeremy Lakeman 2012-09-17 15:38:05 +09:30
commit ac4567d48a
8 changed files with 183 additions and 101 deletions

View File

@ -1344,7 +1344,7 @@ int keyring_find_sid(const keyring_file *k, int *cn, int *in, int *kp, const uns
void keyring_identity_extract(const keyring_identity *id, const unsigned char **sidp, const char **didp, const char **namep)
{
int todo = (sidp ? 1 : 0) | (didp ? 2 : 0) || (namep ? 4 : 0);
int todo = (sidp ? 1 : 0) | (didp ? 2 : 0) | (namep ? 4 : 0);
int kpn;
for (kpn = 0; todo && kpn < id->keypair_count; ++kpn) {
keypair *kp = id->keypairs[kpn];

View File

@ -77,8 +77,7 @@ srandomdev(void)
{
gettimeofday(&tv, NULL);
/* NOTE: intentional use of uninitialized variable */
seed ^= (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
seed = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
}
srandom(seed);
}

View File

@ -131,7 +131,8 @@ set_instance() {
error "missing instance name argument"
;;
+[A-Z])
instance_name="${1#+}"
instance_arg="${1}"
instance_name="${instance_arg#+}"
instance_number=$((36#$instance_name - 9))
tfw_log "# set instance = $instance_name, number = $instance_number"
export instance_dir="${servald_instances_dir?:}/$instance_name"
@ -146,6 +147,48 @@ set_instance() {
esac
}
# Composition function:
# - invoke a command once in many instances
# - in "--all" mode (default), returns the count of commands that returned
# nonzero (ie, failure count); this returns zero only if all commands in all
# instances return zero, ie, is an AND relation on success; this is
# guaranteed to invoke the command in all instances (unless terminated
# by a failed assertion)
# - in "--any" mode, returns zero as soon as any command returns zero; ie, is
# an OR relation on success; N.B. this may not invoke the command in all
# instances
foreach_instance() {
mode=all
case "$1" in
--any) mode=any; shift;;
--all) mode=all; shift;;
esac
local -a instances=()
while [ $# -ne 0 ]; do
case "$1" in
+[A-Z]) instances+=("$1"); shift;;
*) break;;
esac
done
push_instance
local ret=0
local I
for I in ${instances[*]}; do
set_instance $I
if "$@"; then
case $mode in
any) break;;
esac
else
case $mode in
all) let ++ret;;
esac
fi
done
pop_instance
return $ret
}
# Utility function for setting up servald JNI fixtures:
# - check that libservald.so is present
# - set LD_LIBRARY_PATH so that libservald.so can be found
@ -416,41 +459,97 @@ assert_all_servald_servers_no_errors() {
}
# Utility function
# - create an identity
# - create an identity in the current instance {I}
# - assign a phone number (DID) and name to the new identity, use defaults
# if not specified by arg1 and arg2
# - set the SID variable to the SID of the new identity
# - set the DID variable to the phone number of the new identity
# - set the NAME variable to the name of the new identity
create_identity() {
executeOk_servald keyring add
assert [ -e "$SERVALINSTANCE_PATH/serval.keyring" ]
# - assert the new identity is the only one in this instance
# - set the SID{I} variable, eg SIDA, to the SID of the new identity
# - set the DID{I} variable, eg DIDA, to the phone number of the new identity
# - set the NAME{I} variable, eg NAMEA, to the name of the new identity
create_single_identity() {
local sidvar=SID${instance_name}1
local didvar=DID${instance_name}1
local namevar=NAME${instance_name}1
eval "$didvar=\"\${1-\$((5550000 + \$instance_number))}\""
eval "$namevar=\"\${2-Agent \$instance_name Smith}\""
create_identities 1
eval "SID$instance_name=\"\${!sidvar}\""
eval "DID$instance_name=\"\${!didvar}\""
eval "NAME$instance_name=\"\${!namevar}\""
sidvar=SID${instance_name}
didvar=DID${instance_name}
namevar=NAME${instance_name}
tfw_log "SID$instance_name=$(shellarg "${!sidvar}")"
tfw_log "DID$instance_name=$(shellarg "${!didvar}")"
tfw_log "NAME$instance_name=$(shellarg "${!namevar}")"
}
# Utility function:
# - create N identities in the current instance {I}
# - if variables DID{I}{1..N} and/or NAME{I}{1..N} are already set, then use
# them to set the DIDs and names of each identity
# - assert that all SIDs are unique
# - assert that all SIDs appear in keyring list
# - set variables SID{I}{1..N} to SIDs of identities, eg, SIDA1, SIDA2...
# - set variables DID{I}{1..N} to DIDs of identities, eg, DIDA1, DIDA2...
# - set variables NAME{I}{1..N} to names of identities, eg, NAMEA1, NAMEA2...
create_identities() {
local N="$1"
case "$N" in
+([0-9]));;
*) error "invalid arg1: $N";;
esac
local i j
for ((i = 1; i <= N; ++i)); do
executeOk_servald keyring add
assert [ -e "$SERVALINSTANCE_PATH/serval.keyring" ]
local sidvar=SID$instance_name$i
local didvar=DID$instance_name$i
local namevar=NAME$instance_name$i
extract_stdout_keyvalue $sidvar sid "$rexp_sid"
tfw_log "$sidvar=${!sidvar}"
# If the DID and/or NAME is already specified in the variables, then use
# them, otherwise extract the DID and NAME automatically generated by
# servald.
if [ -n "${!didvar}" -o -n "${!namevar}" ]; then
executeOk_servald set did "${!sidvar}" "${!didvar}" "${!namevar}"
eval "$didvar=\${!didvar}"
eval "$namevar=\${!namevar}"
tfw_log "$didvar=$(shellarg "${!didvar}")"
tfw_log "$namevar=$(shellarg "${!namevar}")"
else
extract_stdout_keyvalue_optional $didvar did "$rexp_did" && tfw_log "$didvar=$(shellarg "${!didvar}")"
extract_stdout_keyvalue_optional $namevar name ".*" && tfw_log "$namevar=$(shellarg "${!namevar}")"
fi
done
for ((i = 1; i <= N; ++i)); do
for ((j = 1; j <= N; ++j)); do
[ $i -ne $j ] && eval assert [ "\$SID$instance_name$i" != "\$SID$instance_name$j" ]
done
done
executeOk_servald keyring list
SID=$(replayStdout | sed -ne "1s/^\($rexp_sid\):.*\$/\1/p")
assert --message='main identity known' [ -n "$SID" ]
DID="${1-$((5550000 + $instance_number))}"
NAME="${2-Agent $instance_name Smith}"
executeOk_servald set did $SID "$DID" "$NAME"
tfw_log "Identity $instance_name: $SID $DID $NAME"
assertStdoutLineCount '==' $N
for ((i = 1; i <= N; ++i)); do
local sidvar=SID$instance_name$i
local didvar=DID$instance_name$i
local namevar=NAME$instance_name$i
local re_name=$(escape_grep_basic "${!namevar}")
assertStdoutGrep --matches=1 "^${!sidvar}:${!didvar}:${re_name}\$"
done
}
# Utility function, to be overridden as needed:
# - set up the configuration immediately prior to starting a servald server
# process
# - set up the configuration immediately prior to starting a servald server process
# - called by start_servald_instances
configure_servald_server() {
:
}
# Utility function:
# - start a set of servald server processes running on a shared dummy interface
# and with its own private monitor and MDP abstract socket names
# - start a set of servald server processes running on a shared dummy interface and with its own
# private monitor and MDP abstract socket names
# - set variable DUMMYNET to the full path name of shared dummy interface
# - set variables SIDx to the SID of instance x: SIDA, SIDB, etc.
# - set variables DIDx to the DID of instance x: DIDA, DIDB, etc.
# - set variables NAMEx to the names of instance x: NAMEA, NAMEB, etc.
# - set variables LOGx to the full path of server log file for instance x: LOGA,
# LOGB, etc,
# - set variables LOGx to the full path of server log file for instance x: LOGA, LOGB, etc,
# - wait for all instances to detect each other
# - assert that all instances are in each others' peer lists
start_servald_instances() {
@ -458,44 +557,52 @@ start_servald_instances() {
tfw_log "# start servald instances $*"
DUMMYNET=$SERVALD_VAR/dummy
>$DUMMYNET
local I J
local I
for I; do
set_instance $I
create_identity
# These config settings can be overridden in a caller-supplied configure_servald_server().
# They are extremely useful for the majority of fixtures.
executeOk_servald config set interfaces "+>$DUMMYNET"
executeOk_servald config set monitor.socket "org.servalproject.servald.monitor.socket.$TFWUNIQUE.$instance_name"
executeOk_servald config set mdp.socket "org.servalproject.servald.mdp.socket.$TFWUNIQUE.$instance_name"
configure_servald_server
start_servald_server
eval SID$instance_name="$SID"
eval DID$instance_name="$(shellarg "$DID")"
eval NAME$instance_name="$(shellarg "$NAME")"
eval LOG$instance_name="$(shellarg "$instance_servald_log")"
done
# Now wait until they see each other.
wait_until --sleep=0.25 instances_see_each_other "$@"
tfw_log "# dummynet file:" $(ls -l $DUMMYNET)
# Assert that all instances report complete all-peer lists.
for I; do
set_instance $I
executeOk_servald id allpeers
assertStdoutLineCount '==' $(($# - 1))
for J; do
[ $I = $J ] && continue
local sidvar=SID${J#+}
assertStdoutGrep "${!sidvar}"
done
done
pop_instance
}
instances_see_each_other() {
local I J
# Assertion function:
# - asserts that the current instance reports a peer list that contains all the
# SIDs of all the other instances
# - uses the SID{I}{1..N} variables set by create_instances()
assert_peers_are_instances() {
local I N
executeOk_servald id allpeers
for I; do
for J; do
[ $I = $J ] && continue
local logvar=LOG${I#+}
local sidvar=SID${J#+}
for ((N=1; 1; ++N)); do
local sidvar=SID${I#+}$N
[ -n "${!sidvar}" ] || break
assertStdoutGrep "${!sidvar}"
done
done
}
# Predicate function:
# - useful in combination with assert() and wait_until()
# - return true if the current instance has logged that it has seen all other instances via the
# selfannounce mechanism
has_seen_instances() {
local I N
local logvar=LOG$instance_name
for I; do
[ $I = $instance_arg ] && continue
for ((N=1; 1; ++N)); do
local sidvar=SID${I#+}$N
[ -n "${!sidvar}" ] || break
if ! grep "ADD OVERLAY NODE sid=${!sidvar}" "${!logvar}"; then
return 1
fi
@ -503,3 +610,11 @@ instances_see_each_other() {
done
return 0
}
# Predicate function:
# - useful in combination with assert() and wait_until()
# - return true if all instances have logged that they have seen all other instances via the
# selfannounce mechanism
instances_see_each_other() {
foreach_instance "$@" has_seen_instances "$@"
}

View File

@ -26,52 +26,6 @@ rexp_filesize='[0-9]\{1,\}'
rexp_version='[0-9]\{1,\}'
rexp_date='[0-9]\{1,\}'
# Utility function:
# - create N identities in the current instance (I)
# - set variable SID to SID of first identity
# - set variable SID{I} to SID of first identity, eg, SIDA
# - set variables SID{I}{1..N} to SIDs of identities, eg, SIDA1, SIDA2...
# - set variables DID{I}{1..N} to DIDs of identities, eg, DIDA1, DIDA2...
# - set variables NAME{I}{1..N} to names of identities, eg, NAMEA1, NAMEA2...
# - assert that all SIDs are unique
# - assert that all SIDs appear in keyring list
create_rhizome_identities() {
local N="$1"
case "$N" in
+([0-9]));;
*) error "invalid arg1: $N";;
esac
local i j
for ((i = 1; i <= N; ++i)); do
executeOk_servald keyring add
local sidvar=SID$instance_name$i
local didvar=DID$instance_name$i
local namevar=NAME$instance_name$i
extract_stdout_keyvalue $sidvar sid "$rexp_sid"
tfw_log "$sidvar=${!sidvar}"
if [ $i -eq 1 ]; then
SID="${!sidvar}"
eval SID$instance_name="$SID"
fi
extract_stdout_keyvalue_optional DID$instance_name$i did "$rexp_did" && tfw_log "$didvar=${!didvar}"
extract_stdout_keyvalue_optional NAME$instance_name$i name ".*" && tfw_log "$namevar=${!namevar}"
done
for ((i = 1; i <= N; ++i)); do
for ((j = 1; j <= N; ++j)); do
[ $i -ne $j ] && eval assert [ "\$SID$instance_name$i" != "\$SID$instance_name$j" ]
done
done
executeOk_servald keyring list
assertStdoutLineCount '==' $N
for ((i = 1; i <= N; ++i)); do
local sidvar=SID$instance_name$i
local didvar=DID$instance_name$i
local namevar=NAME$instance_name$i
local re_name=$(escape_grep_basic "${!namevar}")
assertStdoutGrep --matches=1 "^${!sidvar}:${!didvar}:${re_name}\$"
done
}
assert_manifest_complete() {
local manifest="$1"
tfw_cat -v "$manifest"

View File

@ -26,6 +26,7 @@ setup() {
assert_no_servald_processes
setup_dnahelper
start_servald_instances +A
assert_all_instance_peers_complete +A
}
teardown() {
@ -221,6 +222,7 @@ setup_ExecError() {
dnahelper=/non/existent
assert [ ! -e "$dnahelper" ]
start_servald_instances +A
assert_all_instance_peers_complete +A
}
test_ExecError() {
executeOk_servald dna lookup 12345
@ -246,6 +248,7 @@ done
EOF
chmod 0755 "$dnahelper"
start_servald_instances +A
assert_all_instance_peers_complete +A
}
test_ExecArg1() {
executeOk_servald dna lookup 12345

View File

@ -39,6 +39,7 @@ instances_reach_each_other() {
setup() {
setup_servald
assert_no_servald_processes
foreach_instance +A +B create_single_identity
configure_servald_server() { set_server_vars; }
start_servald_instances +A +B
wait_until --sleep=0.25 instances_reach_each_other +A +B
@ -70,6 +71,7 @@ doc_MultiServer="Start three servald servers with dummy interfaces"
setup_MultiServer() {
setup_servald
assert_no_servald_processes
foreach_instance +A +B +C create_single_identity
configure_servald_server() { set_server_vars; }
}
test_MultiServer() {
@ -80,8 +82,8 @@ doc_LookupWildcard="Lookup by wildcard"
test_LookupWildcard() {
executeOk_servald dna lookup "*"
assertStdoutLineCount '==' 2
assertStdoutGrep --matches=1 "^sid://$SIDA/$DIDA:$DIDA:$NAMEA$"
assertStdoutGrep --matches=1 "^sid://$SIDB/$DIDB:$DIDB:$NAMEB$"
assertStdoutGrep --matches=1 "^sid://$SIDA/$DIDA:$DIDA:$NAMEA\$"
assertStdoutGrep --matches=1 "^sid://$SIDB/$DIDB:$DIDB:$NAMEB\$"
}
doc_LookupEmpty="Lookup by empty string"
@ -164,6 +166,7 @@ do
done
EOF
chmod 0755 "$dnahelper"
foreach_instance +A +B +C +D create_single_identity
configure_servald_server() {
set_server_vars
executeOk_servald config set debug.dnahelper on

View File

@ -27,10 +27,10 @@ shopt -s extglob
setup_rhizome() {
set_instance +A
executeOk_servald config set debug.rhizome on
create_rhizome_identities 1
create_identities 1
set_instance +B
executeOk_servald config set debug.rhizome on
create_rhizome_identities 4
create_identities 4
assert [ $SIDB1 != $SIDA1 ]
assert [ $SIDB2 != $SIDA1 ]
assert [ $SIDB3 != $SIDA1 ]

View File

@ -31,10 +31,8 @@ teardown() {
}
setup_rhizome() {
set_instance +A
create_rhizome_identities 1
foreach_instance +A +B create_single_identity
set_instance +B
create_rhizome_identities 1
}
# Called by start_servald_instances for each instance.
@ -110,6 +108,8 @@ setup_FileTransfer() {
set_instance +A
add_file file1
start_servald_instances +A +B
foreach_instance +A assert_peers_are_instances +B
foreach_instance +B assert_peers_are_instances +A
}
test_FileTransfer() {
wait_until bundle_received_by +B
@ -131,6 +131,8 @@ setup_FileTransferBig() {
ls -l file1
add_file file1
start_servald_instances +A +B
foreach_instance +A assert_peers_are_instances +B
foreach_instance +B assert_peers_are_instances +A
}
test_FileTransferBig() {
wait_until bundle_received_by +B
@ -156,6 +158,10 @@ setup_FileTransferMulti() {
set_instance +A
add_file file1
start_servald_instances +A +B +C +D +E
foreach_instance +A assert_peers_are_instances +B +C +D +E
foreach_instance +B assert_peers_are_instances +A +C +D +E
foreach_instance +C assert_peers_are_instances +A +B +D +E
foreach_instance +D assert_peers_are_instances +A +B +C +E
}
test_FileTransferMulti() {
wait_until bundle_received_by +B +C +D +E
@ -172,6 +178,8 @@ setup_FileTransferDelete() {
set_instance +A
add_file file1
start_servald_instances +A +B
foreach_instance +A assert_peers_are_instances +B
foreach_instance +B assert_peers_are_instances +A
wait_until bundle_received_by +B
set_instance +A
>file1_2