summaryrefslogtreecommitdiffstats
path: root/test/src/test-http-debugger.pl
blob: fcc675f62eb41a1b2333eb8994c1844505bc590b (plain)
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
#!/usr/bin/perl

use lib './perl';
use strict;
use File::Spec;
use File::Basename;
use Cwd 'abs_path';
use JSON qw(from_json to_json);
use LWP;
use HTTP::Request::Common qw(POST GET);
use Data::Dumper;

my $scxmlBin = abs_path(shift);
die ("First argument needs to be path to uscxml-browser binary") if (!$scxmlBin);
die("'" . $scxmlBin . "' is not an executable file") if (! -x $scxmlBin);

my $baseDir = File::Spec->canonpath(dirname($0));
chdir $baseDir;

my $ua = LWP::UserAgent->new;
my $request;
my $response;
my $data;
my @breakpointSeq;

my $pid = fork;

if (!$pid) {
	exec("$scxmlBin -t4088 -d");
	exit;
}

my $baseURL = 'http://localhost:4088/debug';
# my $baseURL = 'http://localhost:5080/debug';

sub assertSuccess {
	my $response = shift;
	my $message = shift;
	print "-----\n";
	print $response->content();
	print "-----\n";
	from_json($response->content())->{'status'} eq "success" or die($message);
}

sub prepareSession {
	my $xml = shift;

	### Get a session
	my $request = GET $baseURL.'/connect';
	$response = $ua->request($request);
	assertSuccess($response, "Could not connect");

	my $session = from_json($response->content())->{'session'};
	die("Cannot acquire session from server") if (!$session);

	### Prepare an SCXML interpreter
	$request = POST $baseURL.'/prepare', 
		[
			'session' => $session,
			'url' => 'http://localhost/anonymous.scxml',
			'xml' => $xml
		];
	$response = $ua->request($request);
	assertSuccess($response, "Could not prepare SCXML");
	
	return $session;
}

sub finishSession {
	my $session = shift;
	
	### Prepare an SCXML interpreter
	$request = POST $baseURL.'/disconnect', 
		[
			'session' => $session,
		];
	$response = $ua->request($request);
	assertSuccess($response, "Could not disconnect session");
}

sub popAndCompare {
	my $qualified = shift;
	my $bp = shift(@breakpointSeq);
	for my $key (keys %{$bp}) {
		if (! exists($qualified->{$key}) || $qualified->{$key} ne $bp->{$key}) {
			print Dumper($qualified);
			print Dumper($bp);
			die("Expected different breakpoint");
		}
	}
	print "SUCCESS\n";
}

sub testSimpleStepping {

	my $xml = << 'END_SCXML';
	<scxml>
		<state id='s1'>
			<onentry>
				<log label="'foo'" />
			</onentry>
			<transition target='s2' />
		</state>
		<state id='s2'>
			<transition target='pass' />
		</state>
		<final id='pass' />
	</scxml>
END_SCXML

	@breakpointSeq = (
		{ subject => "microstep",  when => "before" },
		{ subject => "state",      when => "before", action => "enter" },
		{ subject => "state",      when => "after",  action => "enter"  },
		{ subject => "state",      when => "before", action => "enter", stateId => "s1" },
		{ subject => "executable", when => "before", execName => "log" },
		{ subject => "executable", when => "after", execName => "log" },
		{ subject => "state",      when => "after",  action => "enter", stateId => "s1" },
		{ subject => "microstep",  when => "after"  },
		{ subject => "microstep",  when => "before" },
		{ subject => "state",      when => "before", action => "exit",  stateId => "s1" },
		{ subject => "state",      when => "after",  action => "exit",  stateId => "s1" },
		{ subject => "transition", when => "before", source => "s1",    target => "s2"},
		{ subject => "transition", when => "after",  source => "s1",    target => "s2"},
		{ subject => "state",      when => "before", action => "enter", stateId => "s2" },
		{ subject => "state",      when => "after",  action => "enter", stateId => "s2" },
		{ subject => "microstep",  when => "after"  },
		{ subject => "microstep",  when => "before" },
		{ subject => "state",      when => "before", action => "exit",  stateId => "s2" },
		{ subject => "state",      when => "after",  action => "exit",  stateId => "s2" },
		{ subject => "transition", when => "before", source => "s2",    target => "pass"},
		{ subject => "transition", when => "after",  source => "s2",    target => "pass"},
		{ subject => "state",      when => "before", action => "enter", stateId => "pass" },
		{ subject => "state",      when => "after",  action => "enter", stateId => "pass" },
		{ subject => "microstep",  when => "after"  },
		
	);

	my $session = &prepareSession($xml);
	
	while(@breakpointSeq > 0) {
		### Take a step
		$request = POST $baseURL.'/step', ['session' => $session];
		$response = $ua->request($request);
		assertSuccess($response, "Could not step");
		# this will cause the interpreter to pause execution

		### Get the pending messages
		$request = POST $baseURL.'/poll', ['session' => $session];
		$response = $ua->request($request);
		assertSuccess($response, "Could not get breakpoint after step");

		# compare to what we expect
		$data = from_json($response->content());
		popAndCompare($data->{'qualified'});
	}
	
	### last step will finalize the interpreter
	$request = POST $baseURL.'/step', ['session' => $session];
	$response = $ua->request($request);
	assertSuccess($response, "Could not get breakpoint after step");

	### get the pending server push reply
	$request = POST $baseURL.'/poll', ['session' => $session];
	$response = $ua->request($request);
	assertSuccess($response, "Could not get breakpoint after step");

	$data = from_json($response->content());
	die("Machine not yet finished") if ($data->{'replyType'} ne "finished");

	&finishSession($session);
}

sub testBreakpoint {
	my $xml = << 'END_SCXML';
	<scxml>
		<state id='s1'>
			<onentry>
				<log label="'foo'" />
			</onentry>
			<transition target='s2' />
		</state>
		<state id='s2'>
			<transition target='pass' />
		</state>
		<final id='pass' />
	</scxml>
END_SCXML
	
	my $session = prepareSession($xml);
	
	### Skip to breakpoint
	$request = POST $baseURL.'/breakpoint/skipto', 
		[
			'session' => $session,
			'when' => 'after',
			'action' => 'enter',
			'subject' => 'state',
			'stateId' => 's1'
		];
	$response = $ua->request($request);
	assertSuccess($response, "Could not add breakpoint");
	
	### get the pending server push reply
	$request = POST $baseURL.'/poll', ['session' => $session];
	$response = $ua->request($request);
	assertSuccess($response, "Could not get breakpoint after step");
	
	$data = from_json($response->content());
	print Dumper($data);
	
	&finishSession($session);
}

sub testIssueReporting {
	my $xml = << 'END_SCXML';
	<scxml>
		<state id='s1'>
			<onentry>
				<log label="'foo'" />
			</onentry>
			<transition target='s2' />
		</state>
		<state id='s2'>
			<transition target='pass' />
		</state>
		<state id='s3'>
			<onentry>
				<log label="'Unreachable!'" />
			</onentry>
		</state>
		<final id='pass' />
	</scxml>
END_SCXML

	my $session = prepareSession($xml);
	
	### Get a list of issues
	$request = POST $baseURL.'/issues', 
		[
			'session' => $session
		];
	$response = $ua->request($request);
	assertSuccess($response, "Could not get issues for prepared SCXML document");
	
	$data = from_json($response->content());
	print Dumper($data);
	
	&finishSession($session);
	
}

&testSimpleStepping();
&testBreakpoint();
&testIssueReporting();

kill('TERM', $pid);