forked from servalproject/serval-dna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meshmsjava
executable file
·333 lines (316 loc) · 13.2 KB
/
meshmsjava
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
# Tests for MeshMS Java API.
#
# Copyright 2014 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_java.sh"
source "${0%/*}/../testdefs_meshms.sh"
setup() {
setup_servald
setup_servald_so
compile_java_classes
set_instance +A
executeOk_servald config \
set log.console.level debug \
set debug.httpd on \
set debug.meshms on
set_extra_config
create_identities 4
export SERVALD_RHIZOME_DB_RETRY_LIMIT_MS=60000
start_servald_server
}
teardown() {
stop_all_servald_servers
kill_all_servald_processes
assert_no_servald_processes
report_all_servald_servers
}
set_extra_config() {
:
}
doc_MeshmsListConversations="Java API list MeshMS conversations"
setup_MeshmsListConversations() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message One"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message Two"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message Three"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message Four"
}
test_MeshmsListConversations() {
executeJavaOk org.servalproject.test.Meshms meshms-list-conversations $SIDA1
tfw_cat --stderr
assertStdoutLineCount '==' 3
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA2, read=true, last_message=0, read_offset=0"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA3, read=false, last_message=14, read_offset=0"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA4, read=false, last_message=24, read_offset=0"
executeOk_servald meshms read messages $SIDA1
executeJavaOk org.servalproject.test.Meshms meshms-list-conversations $SIDA1
assertStdoutLineCount '==' 3
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA2, read=true, last_message=0, read_offset=0"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA3, read=true, last_message=14, read_offset=14"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA4, read=true, last_message=24, read_offset=24"
}
doc_MeshmsListMessages="Java API list MeshMS messages in one conversation"
setup_MeshmsListMessages() {
setup
meshms_add_messages $SIDA1 $SIDA2 '><>>A>A<>><><><>>>A>A><<<<A<>><>>A<<>'
let NROWS=NSENT+NRECV+(NACK?1:0)
executeOk_servald meshms list messages $SIDA1 $SIDA2
delivered_offset=$(sed -n -e '/^[0-9]\+:[0-9]\+:[0-9]\+:ACK:delivered$/{n;s/^[0-9]\+:\([0-9]\+\):[0-9]\+:>:.*/\1/p;q}' "$TFWSTDOUT")
[ -z "$delivered_offset" ] && delivered_offset=0
read_offset=$(sed -n -e 's/^[0-9]\+:\([0-9]\+\):[0-9]\+MARK:read$/\1/p' "$TFWSTDOUT")
[ -z "$read_offset" ] && read_offset=0
tfw_log delivered_offset="$delivered_offset" read_offset="$read_offset"
}
test_MeshmsListMessages() {
executeJavaOk org.servalproject.test.Meshms meshms-list-messages $SIDA1 $SIDA2
assertStdoutLineCount '==' $(($NROWS + 2))
assertStdoutIs --line=1 -e "read_offset=$read_offset\n"
assertStdoutIs --line=2 -e "latest_ack_offset=$delivered_offset\n"
seen_ack=false
let lnum=3
for ((j = NMESSAGE-1; j >= 0; --j)); do
case ${MESSAGE[$j]} in
'ACK') $seen_ack && continue
esac
assertStdoutGrep --line=$lnum 'token=[-_A-Za-z0-9=]\+,'
assertStdoutGrep --line=$lnum "my_sid=$SIDA1,"
assertStdoutGrep --line=$lnum "their_sid=$SIDA2,"
text="$(sed -n -e $lnum's/.*\<text=\([^ ]*\), .*/\1/p' "$TFWSTDOUT")"
offset="$(sed -n -e $lnum's/.*\<offset=\([0-9]\+\).*/\1/p' "$TFWSTDOUT")"
is_delivered="$(sed -n -e $lnum's/.*\<delivered=\(true\|false\).*/\1/p' "$TFWSTDOUT")"
is_read="$(sed -n -e $lnum's/.*\<read=\(true\|false\).*/\1/p' "$TFWSTDOUT")"
ack_offset="$(sed -n -e $lnum's/.*\<ack_offset=\(null\|[0-9]\+\).*/\1/p' "$TFWSTDOUT")"
tfw_log text="$text" offset="$offset" is_delivered="$is_delivered" is_read="$is_read" ack_offset="$ack_offset"
case ${MESSAGE[$j]} in
'>'|'<')
echo -n "${TEXT[$j]}" | tr ' \n' . >text_fixture
echo -n "$text" >text_list
assert --dump-on-fail=text_fixture --dump-on-fail=text_list cmp text_fixture text_list
assert [ "$ack_offset" = null ]
;;
esac
case ${MESSAGE[$j]} in
'>')
assertStdoutGrep --line=$lnum 'type=MESSAGE_SENT,'
if [ "$offset" -le "$delivered_offset" ]; then
assert [ "$is_delivered" = true ]
else
assert [ "$is_delivered" = false ]
fi
let ++lnum
;;
'<')
assertStdoutGrep --line=$lnum 'type=MESSAGE_RECEIVED,'
if [ "$offset" -le "$read_offset" ]; then
assert [ "$is_read" = true ]
else
assert [ "$is_read" = false ]
fi
let ++lnum
;;
'ACK')
assertStdoutGrep --line=$lnum 'type=ACK_RECEIVED,'
assert [ "$text" = null ]
assert [ "$ack_offset" = "$delivered_offset" ]
seen_ack=true
let ++lnum
;;
esac
done
}
doc_MeshmsListMessagesNewSince="Java API list MeshMS messages in one conversation since token"
setup_MeshmsListMessagesNewSince() {
set_extra_config() {
executeOk_servald config set api.restful.newsince_timeout 1s
}
setup
meshms_add_messages $SIDA1 $SIDA2 '><>>A>A<>><><><>>>A>A><<<<A<>><>>A<<>'
let NROWS=NSENT+NRECV+(NACK?1:0)
executeJavaOk org.servalproject.test.Meshms meshms-list-messages "$SIDA1" "$SIDA2"
tfw_cat --stdout --stderr
assertStdoutLineCount == $(($NROWS + 2))
replayStdout | sed -n -e '3,$p' >messages
tfw_preserve messages
for ((i = 0; i < NROWS; i += 3)); do
let lnum=i+1
token[$i]=$(sed -n -e $lnum's/.*\<token=\([^ ]*\), .*/\1/p' messages)
assert [ -n "${token[$i]}" ]
done
}
test_MeshmsListMessagesNewSince() {
list() {
executeJavaOk org.servalproject.test.Meshms meshms-list-messages-since "$SIDA1" "$SIDA2" "${token[$i]}"
replayStdout >messages$i
tfw_preserve messages$i
}
for ((i = 0; i < NROWS; i += 3)); do
# At most fifteen requests going at once
[ $i -ge 15 ] && fork_wait %list$((i-15))
fork %list$i list
done
fork_wait_all
for ((i = 0; i < NROWS; i += 3)); do
>tmp
[ $i -gt 0 ] && sed -n -e "1,${i}p" messages >tmp
tfw_preserve tmp
# If tmp contains an ACK_RECEIVED, then so should messages$i. Otherwise,
# ignore any ACK_RECEIVED in messages$i.
grep -q 'type=ACK_RECEIVED' tmp || sed -i -e '/type=ACK_RECEIVED/d' messages$i
assert cmp tmp messages$i
done
}
grepall() {
local pattern="$1"
shift
for file; do
grep "$pattern" "$file" || return $?
done
return 0
}
doc_MeshmsListMessagesNewSinceArrival="Java API list newly arriving MeshMS messages in one conversation"
setup_MeshmsListMessagesNewSinceArrival() {
set_extra_config() {
executeOk_servald config set api.restful.newsince_timeout 360s \
set api.restful.users.harry.password potter
}
setup
# Use REST interface to send messages, not CLI, in order to avoid a database
# locking storm
meshms_use_restful harry potter
meshms_add_messages $SIDA1 $SIDA2 '><>A>'
let NROWS=NSENT+NRECV+(NACK?1:0)
executeJavaOk org.servalproject.test.Meshms meshms-list-messages "$SIDA1" "$SIDA2"
tfw_cat --stdout --stderr
assertStdoutLineCount == $(($NROWS + 2))
token=$(sed -n -e '3s/.*\<token=\([^ ]*\), .*/\1/p' "$TFWSTDOUT")
assert [ -n "$token" ]
}
test_MeshmsListMessagesNewSinceArrival() {
list() {
runJava org.servalproject.test.Meshms meshms-list-messages-since "$SIDA1" "$SIDA2" "$token" >messages$i
}
for i in 1 2 3; do
fork %list$i list
done
wait_until [ -e messages1 -a -e messages2 -a -e messages3 ]
for message in '>Rumplestiltskin' 'A' '<Howdydoody' '>Eulenspiegel'; do
meshms_add_messages $SIDA1 $SIDA2 "${message:0:1}" "${message:1}"
case ${message:0:1} in
'<'|'>') waitfor="${message:1}";;
'A') waitfor="ACK";;
*) error "message=${message}";;
esac
wait_until grepall "$waitfor" messages{1,2,3}
done
fork_terminate_all
fork_wait_all
}
teardown_MeshmsListMessagesNewSinceArrival() {
tfw_preserve messages{1,2,3}
teardown
}
doc_MeshmsListMessagesNoIdentity="Java API list MeshMS messages from unknown identity"
test_MeshmsListMessagesNoIdentity() {
SIDX=0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
executeJavaOk org.servalproject.test.Meshms meshms-list-messages $SIDX $SIDA2
assertStdoutGrep 'MeshMSUnknownIdentityException'
tfw_cat --stdout --stderr
}
doc_MeshmsSend="Java API send MeshMS message"
test_MeshmsSend() {
executeJavaOk org.servalproject.test.Meshms meshms-send-message $SIDA1 $SIDA2 "Hello World"
executeOk_servald meshms list messages $SIDA1 $SIDA2
assertStdoutGrep --matches=1 ':>:Hello World'
executeJavaOk org.servalproject.test.Meshms meshms-send-message $SIDA2 $SIDA1 "Hello Back!"
executeOk_servald meshms list messages $SIDA1 $SIDA2
assertStdoutGrep --matches=1 ':>:Hello World$'
assertStdoutGrep --matches=1 ':<:Hello Back!$'
}
doc_MeshmsSendNoIdentity="Java API send MeshMS message from unknown identity"
test_MeshmsSendNoIdentity() {
SIDX=0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
executeJavaOk org.servalproject.test.Meshms meshms-send-message $SIDX $SIDA2 "Hello World"
assertStdoutGrep 'MeshMSUnknownIdentityException'
tfw_cat --stdout --stderr
}
doc_MeshmsReadAllConversations="Java API MeshMS mark all conversations read"
setup_MeshmsReadAllConversations() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message1"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message2"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message3"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message4"
executeOk_servald meshms list conversations $SIDA1
assertStdoutGrep --stderr --matches=1 ":$SIDA2::0:0\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA3:unread:11:0\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA4:unread:20:0\$"
}
test_MeshmsReadAllConversations() {
executeJavaOk org.servalproject.test.Meshms meshms-mark-all-conversations-read $SIDA1
assertStdoutIs -e 'UPDATED\n'
executeOk_servald meshms list conversations $SIDA1
assertStdoutGrep --stderr --matches=1 ":$SIDA2::0:0\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA3::11:11\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA4::20:20\$"
}
doc_MeshmsReadAllMessages="Java API MeshMS mark all conversations read"
setup_MeshmsReadAllMessages() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message1"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message2"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message3"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message4"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message5"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message6"
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:45:0\$"
}
test_MeshmsReadAllMessages() {
executeJavaOk org.servalproject.test.Meshms meshms-mark-all-messages-read $SIDA2 $SIDA1
assertStdoutIs -e 'UPDATED\n'
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1::45:45\$"
}
doc_MeshmsReadMessage="Java API MeshMS mark a message as read"
setup_MeshmsReadMessage() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message1"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message2"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message3"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message4"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message5"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message6"
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:45:0\$"
}
test_MeshmsReadMessage() {
executeJavaOk org.servalproject.test.Meshms meshms-advance-read-offset $SIDA2 $SIDA1 22
assertStdoutIs -e 'UPDATED\n'
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:45:22\$"
executeJavaOk org.servalproject.test.Meshms meshms-advance-read-offset $SIDA2 $SIDA1 11
assertStdoutIs -e 'OK\n'
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:45:22\$"
}
runTests "$@"