Debian Codegraph @

tests/httpTest.tcl

raw
1 # httpTest.tcl
2 #
3 # Test HTTP/1.1 concurrent requests including
4 # queueing, pipelining and retries.
5 #
6 # Copyright (C) 2018 Keith Nash <[email protected]>
7 #
8 # See the file "license.terms" for information on usage and redistribution
9 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
11 # ------------------------------------------------------------------------------
12 # "Package" httpTest for analysis of Log output of http requests.
13 # ------------------------------------------------------------------------------
14 # This is a specialised test kit for examining the presence, ordering, and
15 # overlap of multiple HTTP transactions over a persistent ("Keep-Alive")
16 # connection; and also for testing reconnection in accordance with RFC 7230 when
17 # the connection is lost.
18 #
19 # This kit is probably not useful for other purposes. It depends on the
20 # presence of specific Log commands in the http library, and it interprets the
21 # logs that these commands create.
22 # ------------------------------------------------------------------------------
23
24 package require http
25
26 namespace eval ::http {
27 variable TestStartTimeInMs [clock milliseconds]
28 # catch {puts stdout "Start time (zero ms) is $TestStartTimeInMs"}
29 }
30
31 namespace eval ::httpTest {
32 variable testResults {}
33 variable testOptions
34 array set testOptions {
35 -verbose 0
36 -dotted 1
37 }
38 # -verbose - 0 quiet 1 write to stdout 2 write more
39 # -dotted - (boolean) use dots for absences in lists of transactions
40 }
41
42 proc httpTest::Puts {txt} {
43 variable testOptions
44 if {$testOptions(-verbose) > 0} {
45 puts stdout $txt
46 flush stdout
47 }
48 return
49 }
50
51 # http::Log
52 #
53 # A special-purpose logger used for running tests.
54 # - Processes Log calls that have "^" in their arguments, and records them in
55 # variable ::httpTest::testResults.
56 # - Also writes them to stdout (using Puts) if ($testOptions(-verbose) > 0).
57 # - Also writes Log calls that do not have "^", if ($testOptions(-verbose) > 1).
58
59 proc http::Log {args} {
60 variable TestStartTimeInMs
61 set time [expr {[clock milliseconds] - $TestStartTimeInMs}]
62 set txt [list $time {*}$args]
63 if {[string first ^ $txt] >= 0} {
64 ::httpTest::LogRecord $txt
65 ::httpTest::Puts $txt
66 } elseif {$::httpTest::testOptions(-verbose) > 1} {
67 ::httpTest::Puts $txt
68 }
69 return
70 }
71
72
73 # Called by http::Log (the "testing" version) to record logs for later analysis.
74
75 proc httpTest::LogRecord {txt} {
76 variable testResults
77
78 set pos [string first ^ $txt]
79 set len [string length $txt]
80 if {$pos > $len - 3} {
81 puts stdout "Logging Error: $txt"
82 puts stdout "Fix this call to Log in http-*.tm so it has ^ then\
83 a letter then a numeral."
84 flush stdout
85 } elseif {$pos < 0} {
86 # Called by mistake.
87 } else {
88 set letter [string index $txt [incr pos]]
89 set number [string index $txt [incr pos]]
90 # Max 9 requests!
91 lappend testResults [list $letter $number]
92 }
93
94 return
95 }
96
97
98 # ------------------------------------------------------------------------------
99 # Commands for analysing the logs recorded when calling http::geturl.
100 # ------------------------------------------------------------------------------
101
102 # httpTest::TestOverlaps --
103 #
104 # The main test for correct behaviour of pipelined and sequential
105 # (non-pipelined) transactions. Other tests should be run first to detect
106 # any inconsistencies in the data (e.g. absence of the elements that are
107 # examined here).
108 #
109 # Examine the sequence $someResults for each transaction from 1 to $n,
110 # ignoring any that are listed in $badTrans.
111 # Determine whether the elements "B" to $term for one transaction overlap
112 # elements "B" to $term for the previous and following transactions.
113 #
114 # Transactions in the list $badTrans are not included in "clean" or
115 # "dirty", but their possible overlap with other transactions is noted.
116 # Transactions in the list $notPiped are a subset of $badTrans, and
117 # their possible overlap with other transactions is NOT noted.
118 #
119 # Arguments:
120 # someResults - list of results, each of the form {letter numeral}
121 # n - number of HTTP transactions
122 # term - letter that indicated end of search range. "E" for testing
123 # overlaps from start of request to end of response headers.
124 # "F" to extend to the end of the response body.
125 # msg - the cumulative message from sanity checks. Append to it only
126 # to report a test failure.
127 # badTrans - list of transaction numbers not to be assessed as "clean" or
128 # "dirty"
129 # notPiped - subset of badTrans. List of transaction numbers that cannot
130 # taint another transaction by overlapping with it, because it
131 # used a different socket.
132 #
133 # Return value: [list $msg $clean $dirty]
134 # msg - warning messages: nothing will be appended to argument $msg if there
135 # is an error with the test.
136 # clean - list of transactions that have no overlap with other transactions
137 # dirty - list of transactions that have YES overlap with other transactions
138
139 proc httpTest::TestOverlaps {someResults n term msg badTrans notPiped} {
140 variable testOptions
141
142 # Check whether transactions overlap:
143 set clean {}
144 set dirty {}
145 for {set i 1} {$i <= $n} {incr i} {
146 if {$i in $badTrans} {
147 continue
148 }
149 set myStart [lsearch -exact $someResults [list B $i]]
150 set myEnd [lsearch -exact $someResults [list $term $i]]
151
152 if {($myStart < 0 || $myEnd < 0)} {
153 set res "Cannot find positions of transaction $i"
154 append msg $res \n
155 Puts $res
156 }
157
158 set overlaps {}
159 for {set j $myStart} {$j <= $myEnd} {incr j} {
160 lassign [lindex $someResults $j] letter number
161 if {$number != $i && $letter ne "A" && $number ni $notPiped} {
162 lappend overlaps $number
163 }
164 }
165
166 if {[llength $overlaps] == 0} {
167 set res "Transaction $i has no overlaps"
168 Puts $res
169 lappend clean $i
170 if {$testOptions(-dotted)} {
171 # N.B. results from different segments are concatenated.
172 lappend dirty .
173 } else {
174 }
175 } else {
176 set res "Transaction $i overlaps with [join $overlaps { }]"
177 Puts $res
178 lappend dirty $i
179 if {$testOptions(-dotted)} {
180 # N.B. results from different segments are concatenated.
181 lappend clean .
182 } else {
183 }
184 }
185 }
186 return [list $msg $clean $dirty]
187 }
188
189 # httpTest::PipelineNext --
190 #
191 # Test whether prevPair, pair are valid as consecutive elements of a pipelined
192 # sequence (Start 1), (End 1), (Start 2), (End 2) ...
193 # Numbers are integers increasing (by 1 if argument "any" is false), and need
194 # not begin with 1.
195 # The first element of the sequence has prevPair {} and is always passed as
196 # valid.
197 #
198 # Arguments;
199 # Start - string that labels the start of a segment
200 # End - string that labels the end of a segment
201 # prevPair - previous "pair" (list of string and number) element of a
202 # sequence, or {} if argument "pair" is the first in the
203 # sequence.
204 # pair - current "pair" (list of string and number) element of a
205 # sequence
206 # any - (boolean) iff true, accept any increasing sequence of integers.
207 # If false, integers must increase by 1.
208 #
209 # Return value - boolean, true iff the two pairs are valid consecutive elements.
210
211 proc httpTest::PipelineNext {Start End prevPair pair any} {
212 if {$prevPair eq {}} {
213 return 1
214 }
215
216 lassign $prevPair letter number
217 lassign $pair newLetter newNumber
218 if {$letter eq $Start} {
219 return [expr {($newLetter eq $End) && ($newNumber == $number)}]
220 } elseif {$any} {
221 set nxt [list $Start [expr {$number + 1}]]
222 return [expr {($newLetter eq $Start) && ($newNumber > $number)}]
223 } else {
224 set nxt [list $Start [expr {$number + 1}]]
225 return [expr {($newLetter eq $Start) && ($newNumber == $number + 1)}]
226 }
227 }
228
229 # httpTest::TestPipeline --
230 #
231 # Given a sequence of "pair" elements, check that the elements whose string is
232 # $Start or $End form a valid pipeline. Ignore other elements.
233 #
234 # Return value: {} if valid pipeline, otherwise a non-empty error message.
235
236 proc httpTest::TestPipeline {someResults n Start End msg desc badTrans} {
237 set sequence {}
238 set prevPair {}
239 set ok 1
240 set any [llength $badTrans]
241 foreach pair $someResults {
242 lassign $pair letter number
243 if {($letter in [list $Start $End]) && ($number ni $badTrans)} {
244 lappend sequence $pair
245 if {![PipelineNext $Start $End $prevPair $pair $any]} {
246 set ok 0
247 break
248 }
249 set prevPair $pair
250 }
251 }
252
253 if {!$ok} {
254 set res "$desc are not pipelined: {$sequence}"
255 append msg $res \n
256 Puts $res
257 }
258 return $msg
259 }
260
261 # httpTest::TestSequence --
262 #
263 # Examine each transaction from 1 to $n, ignoring any that are listed
264 # in $badTrans.
265 # Check that each transaction has elements A to F, in alphabetical order.
266
267 proc httpTest::TestSequence {someResults n msg badTrans} {
268 variable testOptions
269
270 for {set i 1} {$i <= $n} {incr i} {
271 if {$i in $badTrans} {
272 continue
273 }
274 set sequence {}
275 foreach pair $someResults {
276 lassign $pair letter number
277 if {$number == $i} {
278 lappend sequence $letter
279 }
280 }
281 if {$sequence eq {A B C D E F}} {
282 } else {
283 set res "Wrong sequence for token ::http::$i - {$sequence}"
284 append msg $res \n
285 Puts $res
286 if {"X" in $sequence} {
287 set res "- and error(s) X"
288 append msg $res \n
289 Puts $res
290 }
291 if {"Y" in $sequence} {
292 set res "- and warnings(s) Y"
293 append msg $res \n
294 Puts $res
295 }
296 }
297 }
298 return $msg
299 }
300
301 #
302 # Arguments:
303 # someResults - list of elements, each a list of a letter and a number
304 # n - (positive integer) the number of HTTP requests
305 # msg - accumulated warning messages
306 # skipOverlaps - (boolean) whether to skip testing of transaction overlaps
307 # badTrans - list of transaction numbers not to be assessed as "clean" or
308 # "dirty" by their overlaps
309 # for 1/2 includes all transactions
310 # for 3/4 includes an increasing (with recursion) set that will not be included in the list because they are already handled.
311 # notPiped - subset of badTrans. List of transaction numbers that cannot
312 # taint another transaction by overlapping with it, because it
313 # used a different socket.
314 #
315 # Return value: [list $msg $cleanE $cleanF $dirtyE $dirtyF]
316 # msg - warning messages: nothing will be appended to argument $msg if there
317 # is no error with the test.
318 # cleanE - list of transactions that have no overlap with other transactions
319 # (not considering response body)
320 # dirtyE - list of transactions that have YES overlap with other transactions
321 # (not considering response body)
322 # cleanF - list of transactions that have no overlap with other transactions
323 # (including response body)
324 # dirtyF - list of transactions that have YES overlap with other transactions
325 # (including response body)
326
327 proc httpTest::MostAnalysis {someResults n msg skipOverlaps badTrans notPiped} {
328 variable testOptions
329
330 # Check that stages for "good" transactions are all present and correct:
331 set msg [TestSequence $someResults $n $msg $badTrans]
332
333 # Check that requests are pipelined:
334 set msg [TestPipeline $someResults $n B C $msg Requests $notPiped]
335
336 # Check that responses are pipelined:
337 set msg [TestPipeline $someResults $n D F $msg Responses $notPiped]
338
339 if {$skipOverlaps} {
340 set cleanE {}
341 set dirtyE {}
342 set cleanF {}
343 set dirtyF {}
344 } else {
345 Puts "Overlaps including response body (test for non-pipelined case)"
346 lassign [TestOverlaps $someResults $n F $msg $badTrans $notPiped] msg cleanF dirtyF
347
348 Puts "Overlaps without response body (test for pipelined case)"
349 lassign [TestOverlaps $someResults $n E $msg $badTrans $notPiped] msg cleanE dirtyE
350 }
351
352 return [list $msg $cleanE $cleanF $dirtyE $dirtyF]
353 }
354
355 # httpTest::ProcessRetries --
356 #
357 # Command to examine results for socket-changing records [PQR],
358 # divide the results into segments for each connection, and analyse each segment
359 # individually.
360 # (Could add $sock to the logging to simplify this, but never mind.)
361 #
362 # In each segment, identify any transactions that are not included, and
363 # any that are aborted, to assist subsequent testing.
364 #
365 # Prepend A records (socket-independent) to each segment for transactions that
366 # were scheduled (by A) but not completed (by F). Pass each segment to
367 # MostAnalysis for processing.
368
369 proc httpTest::ProcessRetries {someResults n msg skipOverlaps notIncluded notPiped} {
370 variable testOptions
371
372 set nextRetry [lsearch -glob -index 0 $someResults {[PQR]}]
373 if {$nextRetry < 0} {
374 return [MostAnalysis $someResults $n $msg $skipOverlaps $notIncluded $notPiped]
375 }
376 set badTrans $notIncluded
377 set tryCount 0
378 set try $nextRetry
379 incr tryCount
380 lassign [lindex $someResults $try] letter number
381 Puts "Processing retry [lindex $someResults $try]"
382 set beforeTry [lrange $someResults 0 $try-1]
383 Puts [join $beforeTry \n]
384 set afterTry [lrange $someResults $try+1 end]
385
386 set dummyTry {}
387 for {set i 1} {$i <= $n} {incr i} {
388 set first [lsearch -exact $beforeTry [list A $i]]
389 set last [lsearch -exact $beforeTry [list F $i]]
390 if {$first < 0} {
391 set res "Transaction $i was not started in connection number $tryCount"
392 # So lappend it to badTrans and don't include it in the call below of MostAnalysis.
393 # append msg $res \n
394 Puts $res
395 if {$i ni $badTrans} {
396 lappend badTrans $i
397 } else {
398 }
399 } elseif {$last < 0} {
400 set res "Transaction $i was started but unfinished in connection number $tryCount"
401 # So lappend it to badTrans and don't include it in the call below of MostAnalysis.
402 # append msg $res \n
403 Puts $res
404 lappend badTrans $i
405 lappend dummyTry [list A $i]
406 } else {
407 set res "Transaction $i was started and finished in connection number $tryCount"
408 # So include it in the call below of MostAnalysis.
409 # So lappend it to notIncluded and don't include it in the recursive call of
410 # ProcessRetries which handles the later connections.
411 # append msg $res \n
412 Puts $res
413 lappend notIncluded $i
414 }
415 }
416
417 # Analyse the part of the results before the first replay:
418 set HeadResults [MostAnalysis $beforeTry $n $msg $skipOverlaps $badTrans $notPiped]
419 lassign $HeadResults msg cleanE1 cleanF1 dirtyE1 dirtyF1
420
421 # Pass the rest of the results to be processed recursively.
422 set afterTry [concat $dummyTry $afterTry]
423 set TailResults [ProcessRetries $afterTry $n $msg $skipOverlaps $notIncluded $notPiped]
424 lassign $TailResults msg cleanE2 cleanF2 dirtyE2 dirtyF2
425
426 set cleanE [concat $cleanE1 $cleanE2]
427 set cleanF [concat $cleanF1 $cleanF2]
428 set dirtyE [concat $dirtyE1 $dirtyE2]
429 set dirtyF [concat $dirtyF1 $dirtyF2]
430 return [list $msg $cleanE $cleanF $dirtyE $dirtyF]
431 }
432
433 # httpTest::logAnalyse --
434 #
435 # The main command called to analyse logs for a single test.
436 #
437 # Arguments:
438 # n - (positive integer) the number of HTTP requests
439 # skipOverlaps - (boolean) whether to skip testing of transaction overlaps
440 # notIncluded - list of transaction numbers not to be assessed as "clean" or
441 # "dirty" by their overlaps
442 # notPiped - subset of notIncluded. List of transaction numbers that cannot
443 # taint another transaction by overlapping with it, because it
444 # used a different socket.
445 #
446 # Return value: [list $msg $cleanE $cleanF $dirtyE $dirtyF]
447 # msg - warning messages: {} if there is no error with the test.
448 # cleanE - list of transactions that have no overlap with other transactions
449 # (not considering response body)
450 # dirtyE - list of transactions that have YES overlap with other transactions
451 # (not considering response body)
452 # cleanF - list of transactions that have no overlap with other transactions
453 # (including response body)
454 # dirtyF - list of transactions that have YES overlap with other transactions
455 # (including response body)
456
457 proc httpTest::logAnalyse {n skipOverlaps notIncluded notPiped} {
458 variable testResults
459 variable testOptions
460
461 # Check that each data item has the correct form {letter numeral}.
462 set ii 0
463 set ok 1
464 foreach pair $testResults {
465 lassign $pair letter number
466 if { [string match {[A-Z]} $letter]
467 && [string match {[0-9]} $number]
468 } {
469 # OK
470 } else {
471 set ok 0
472 set res "Error: testResults has bad element {$pair} at position $ii"
473 append msg $res \n
474 Puts $res
475 }
476 incr ii
477 }
478
479 if {!$ok} {
480 return $msg
481 }
482 set msg {}
483
484 Puts [join $testResults \n]
485 ProcessRetries $testResults $n $msg $skipOverlaps $notIncluded $notPiped
486 # N.B. Implicit Return.
487 }
488
489 proc httpTest::cleanupHttpTest {} {
490 variable testResults
491 set testResults {}
492 return
493 }
494
495 proc httpTest::setHttpTestOptions {key args} {
496 variable testOptions
497 if {$key ni {-dotted -verbose}} {
498 return -code error {valid options are -dotted, -verbose}
499 }
500 set testOptions($key) {*}$args
501 }
502
503 namespace eval httpTest {
504 namespace export cleanupHttpTest logAnalyse setHttpTestOptions
505 }