aboutsummaryrefslogblamecommitdiff
path: root/tools/jhbtest.pl
blob: 854e09ee77f3649e93c16b47a11079a74285a6e1 (plain) (tree)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451


































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                   
#!/usr/bin/perl -w

use strict; # har har

use constant ERR => 0;
use constant WARN => 1;
use constant INFO => 2;
use constant DEBUG => 3;

use constant RID => 31974;

### ### ### conf ### ### ###

my $BASE_ADDRESS = "http://localhost:5280/http-bind/";
my $JABBER_SERVER = "localhost";

my $RID = RID;

my $WAIT = 60;

my $USER = "tester";
my $UPW = "mysecret";

my $DEBUG = INFO;

### ### ### END conf ### ### ###

# create an agent we can use for our requests
use LWP::UserAgent;
my $ua = new LWP::UserAgent();

# create a tree parser to parse response content
use XML::Parser;
my $p = new XML::Parser(Style => 'Tree');


### ### ### subs ### ### ###
sub doSend() {
	my $content = shift;
	
	# create a request
	my $req = new HTTP::Request(POST => $BASE_ADDRESS);
	$req->content_type('text/xml; charset=utf-8');
	$req->content($content);

	debug(DEBUG,"<< Request\n".$req->as_string."<< END Request");

	# send request
	my $res = $ua->request($req);

	debug(DEBUG,">> Response\n" . $res->as_string .">> END Response");
	return $res;
}

# getChildEls
# used to strip enclosing body element
# PARAMS: @tree - tree style array from XML::Parser
# RETURN: @children - child elements of top level element
sub getChildEls 
{
    my $t = $_[0];

    shift @{$t->[1]};
    return @{$t->[1]};
}

sub debug
{
    my $lvl = shift;
    my $msg = shift;

    return if ($DEBUG < $lvl);

    my $prefix = "[";
    $prefix .= "ERROR" if ($lvl == ERR);
    $prefix .= "WARNING" if ($lvl == WARN);
    $prefix .= "INFO" if ($lvl == INFO);
    $prefix .= "DEBUG" if ($lvl == DEBUG);
    $prefix .= "] ";

    $msg =~ s/\n/\n$prefix/g;
    print STDERR $prefix . $msg . "\n";
}
### ### ### main ### ### ###

$| = 1; # set streaming output

# no body
print "Sending some 'foo': ";
my $res = &doSend("foo");
if ($res->code == 400) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}
# no body
print "Sending some '<foo />': ";
$res = &doSend("<foo />");
if ($res->code == 400) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}

# empty body
print "Sending empty body: ";
$res = &doSend("<body />");
if ($res->code == 400) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}

# fake a sid
print "Sending wrong sid: ";
$res = &doSend("<body sid='08154711' rid='$RID' xmlns='http://jabber.org/protocol/httpbind'/>");
if ($res->code == 404) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}

# forget to send 'to'
print "Missing 'to' attribute at session creation request: ";
$res = &doSend("<body content='text/xml; charset=utf-8' hold='0' rid='$RID' wait='60' xml:lang='en' xmlns='http://jabber.org/protocol/httpbind'/>");
if ($res->is_success && $res->content =~ /<body .*type=["']terminate['"]/ && $res->content =~/<body .*condition=["']improper-addressing['"] /) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}

# sending empty 'to' attribute
print "Empty 'to' attribute at session creation request: ";
$res = &doSend("<body content='text/xml; charset=utf-8' hold='0' rid='$RID' to='' wait='60' xml:lang='en' xmlns='http://jabber.org/protocol/httpbind'/>");
if ($res->is_success && $res->content =~ /<body .*type=["']terminate['"]/ && $res->content =~/<body .*condition=["']improper-addressing['"] /) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}

# forget to send a rid
print "Missing 'rid' attribute at session creation request: ";
$res = &doSend("<body content='text/xml; charset=utf-8' hold='0' to='$JABBER_SERVER' wait='60' xml:lang='en' xmlns='http://jabber.org/protocol/httpbind'/>");
if ($res->code == 404) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}
# trying to connect to non-existent domain
print "Connecting to non-existent domain: ";
$res = &doSend("<body content='text/xml; charset=utf-8' hold='0' rid='$RID' to='foo.bar' wait='60' xml:lang='en' xmlns='http://jabber.org/protocol/httpbind'/>");
if ($res->is_success && $res->content =~ /<body .*type=["']terminate['"]/ && $res->content =~/<body .*condition=["']host-unknown['"] /) {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}

# trying to connect to non-existent jabber server
print "Connecting to non-existent jabber server: ";
$res = &doSend("<body content='text/xml; charset=utf-8' hold='0' rid='$RID' to='www.jabber.org' wait='60' xml:lang='en' xmlns='http://jabber.org/protocol/httpbind'/>");
if ($res->is_success && $res->content =~ /<body .*type=["']terminate['"]/ && ($res->content =~/<body .*condition=["']remote-connection-failed['"] / || $res->content =~/<body .*condition=["']host-unknown['"] /))  {
	print "OK.\n";
} else {
	print "Failed!\n";
	print $res->as_string, "\n";
}

# connection to foreign server
#print "Connecting to foreign jabber server: ";
#$res = &doSend("<body content='text/xml; charset=utf-8' hold='0' rid='$RID' to='jwchat.org' wait='60' xml:lang='en' xmlns='http://jabber.org/protocol/httpbind'/>");
#if ($res->is_success && $res->content =~ /<body .*type=["']terminate['"]/ && $res->content =~/<body .*condition=["']host-unknown['"] /) {
#	print "OK.\n";
#} else {
#	print "Failed!\n";
#	print $res->as_string, "\n";
#}

my %sess;
sub getSess
{
    $sess{rid} = RID; # a rid to start
    $res = &doSend("<body content='text/xml; charset=utf-8' hold='0' rid='$sess{rid}' to='$JABBER_SERVER' wait='$WAIT' xml:lang='en' xmlns='http://jabber.org/protocol/httpbind'/>");
    if ($res->is_success) {
	my $t = $p->parse($res->content);
	%sess = %{$t->[1]->[0]};
	$sess{rid} = RID; # a rid to start

	if (defined($sess{sid}) && $sess{sid} ne "" && defined($sess{wait}) && $sess{wait} ne '' && $sess{wait} <= $WAIT) {
	    debug(INFO,"sid: $sess{sid}");
	    debug(INFO, "authid: $sess{authid}") if (defined($sess{authid}));
	    debug(INFO, "wait: $sess{wait}");
	    debug(INFO, "inactivity: $sess{inactivity}") if (defined($sess{inactivity}));
	    debug(INFO, "polling: $sess{polling}") if (defined($sess{polling}));
	    debug(INFO, "requests: $sess{requests}") if (defined($sess{requests}));
	    debug(INFO, "accept: $sess{accept}") if (defined($sess{accept}));
	    debug(INFO, "charsets: $sess{charsets}") if (defined($sess{charsets}));

	    debug (WARN, "authid missing") unless (defined($sess{authid}) && $sess{authid} ne '');
	    debug (WARN, "server indicates polling mode") if (defined($sess{requests}) && $sess{requests} == 1);
	    return 1;
	} 

    }

    debug(ERR, "sid missing") unless (defined($sess{sid}) && $sess{sid} ne "");
    debug(ERR, "wait missing") unless (defined($sess{wait}) && $sess{wait} ne '');
    debug(ERR, "wait bigger then requested") unless (defined($sess{wait}) && $sess{wait} ne '' && $sess{wait} <= $WAIT);

    debug(DEBUG, $res->as_string);
    return 0;
}

# try to get a real sid
print "Create a new session: ";
if (&getSess()) {
    print "OK.\n";
} else {
    debug(ERR, "Aborting.");
    exit(1);
}

# checking wait attribute
print "Creating another session with smaller 'wait' then before: ";
$WAIT = $sess{wait} - 1;
if (&getSess()) {
    print "OK.\n";
} else {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);
}

sub doAuth
{
# query auth
    $sess{rid}++;
    $res = &doSend("<body rid='$sess{rid}' sid='$sess{sid}' xmlns='http://jabber.org/protocol/httpbind'><iq type='get' id='auth1'><query xmlns='jabber:iq:auth'><username>$USER</username></query></iq></body>");
    my @els = (&getChildEls($p->parse($res->content)));
    unless ($els[0] eq 'iq' && $els[1]->[0]->{'type'} eq 'result') {
	debug(ERR, $res->content);
	return 0;
    }
       
# send auth
    $sess{rid}++;
    $res = &doSend("<body rid='$sess{rid}' sid='$sess{sid}' xmlns='http://jabber.org/protocol/httpbind'><iq type='set' id='auth2'><query xmlns='jabber:iq:auth'><username>$USER</username><resource>test</resource><password>$UPW</password></query></iq></body>");
    @els = (&getChildEls($p->parse($res->content)));
    unless ($els[0] eq 'iq' && $els[1]->[0]->{'type'} eq 'result') {
	debug(ERR, $res->content);
	return 0;
    }

    return 1;
}

print "Authenticating: ";
if (&doAuth()) {
    print "OK.\n";
} else {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);    
}


sub doPoll
{
    $sess{rid}++;
    return &doSend("<body rid='$sess{rid}' sid='$sess{sid}' xmlns='http://jabber.org/protocol/httpbind'/>");
 }


print "Polling with wrong 'rid': ";
$sess{rid}--;
$res = &doPoll();
if ($res->code != 404) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
 #   exit(1);    
}
print "OK.\n";
print "Checking if session terminated: ";
$res = &doPoll();
if ($res->code != 404) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
  #  exit(1);    
}
print "OK.\n";

print "Create a new session: ";
if (&getSess()) {
    print "OK.\n";
} else {
    debug(ERR, "Aborting.");
    exit(1);
}

print "Authenticating: ";
if (&doAuth()) {
    print "OK.\n";
} else {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
   # exit(1);    
}

print "Polling too frequently: ";
$res = &doPoll();
if ($res->code != 200) {
    print "First poll failed unexpectedly!\n";
    debug(ERR, "Aborting.");
    #exit(1);    
}
$res = &doPoll();
if ($res->code != 403) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    #exit(1);    
}
print "OK.\n";

print "Checking if session terminated: ";
$res = &doPoll();
if ($res->code != 404) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    #exit(1);    
}
print "OK.\n";

print "Create a new session: ";
if (&getSess()) {
    print "OK.\n";
} else {
    debug(ERR, "Aborting.");
    exit(1);
}

print "Authenticating: ";
if (&doAuth()) {
    print "OK.\n";
} else {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);
}

print "Test if proper polling is allowed: ";
$res = &doPoll();
if ($res->code != 200) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);    
}
sleep $sess{polling};
$res = &doPoll();
if ($res->code != 200) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);    
}
print "OK.\n";

print "Waiting for session to timeout: ";
my $STEP=10;
for (my $i=0; $i<$sess{inactivity}; $i+=$STEP) {
    print ".";
    sleep $STEP;
}
sleep 1; # take another nap
$res = &doPoll();
if ($res->code != 404) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);    
}
print "OK.\n";

print "Create a new session: ";
if (&getSess()) {
    print "OK.\n";
} else {
    debug(ERR, "Aborting.");
    exit(1);
}

print "Authenticating: ";
if (&doAuth()) {
    print "OK.\n";
} else {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);
}


# [TODO]
# Check for
# * KEY Sequence Algorithm Compliance
# * Too Many Simultaneous Connections (probably hard to achieve for polling mode)
# * request custom content-type/-encoding

# get roster
print "getting roster\n";
$sess{rid}++;
$res = &doSend("<body rid='$sess{rid}' sid='$sess{sid}' xmlns='http://jabber.org/protocol/httpbind'><iq type='get'><query xmlns='jabber:iq:roster' /></iq></body>");
debug(INFO, $res->content);

# send presence
print "sending presence\n";
$sess{rid}++;
$res = &doSend("<body rid='$sess{rid}' sid='$sess{sid}' xmlns='http://jabber.org/protocol/httpbind'><presence /></body>");
debug(INFO, $res->content);

# sending bullshit
print "sending bullshit\n";
$sess{rid}++;
$res = &doSend("<body rid='$sess{rid}' sid='$sess{sid}' xmlns='http://jabber.org/protocol/httpbind'>sending bullshit</body>");
debug(INFO, $res->content);

# send presence
print "sending xa presence\n";
$sess{rid}++;
$res = &doSend("<body rid='$sess{rid}' sid='$sess{sid}' xmlns='http://jabber.org/protocol/httpbind'><presence><show>xa</show></presence></body>");
debug(INFO, $res->content);

# disconnect
sleep 3;
print "logout\n";
$sess{rid}++;
$res = &doSend("<body rid='$sess{rid}' sid='$sess{sid}' type='terminate' xmlns='http://jabber.org/protocol/httpbind'><presence type='unavailable'/></body>");
debug(INFO, $res->content);


print "Checking if session terminated: ";
$res = &doPoll();
if ($res->code != 404) {
    print "FAILED!\n";
    debug(ERR, "Aborting.");
    exit(1);    
}
print "OK.\n";