Debian Codegraph @

tests/iogt.test

raw
1 # -*- tcl -*-
2 # Commands covered: transform, and stacking in general
3 #
4 # This file contains a collection of tests for Giot
5 #
6 # See the file "license.terms" for information on usage and redistribution of
7 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
8 #
9 # Copyright (c) 2000 Ajuba Solutions.
10 # Copyright (c) 2000 Andreas Kupries.
11 # All rights reserved.
12
13 if {[catch {package require tcltest 2.1}]} {
14 puts stderr "Skipping tests in [info script]. tcltest 2.1 required."
15 return
16 }
17
18 ::tcltest::loadTestedCommands
19 catch [list package require -exact Tcltest [info patchlevel]]
20
21 namespace eval ::tcl::test::iogt {
22 namespace import ::tcltest::*
23
24 testConstraint testchannel [llength [info commands testchannel]]
25
26 set path(dummy) [makeFile {abcdefghijklmnopqrstuvwxyz0123456789,./?><;'\|":[]\}\{`~!@#$%^&*()_+-=
27 } dummy]
28
29 # " capture coloring of quotes
30
31 set path(dummyout) [makeFile {} dummyout]
32
33 set path(__echo_srv__.tcl) [makeFile {
34 #!/usr/local/bin/tclsh
35 # -*- tcl -*-
36 # echo server
37 #
38 # arguments, options: port to listen on for connections.
39 # delay till echo of first block
40 # delay between blocks
41 # blocksize ...
42
43 set port [lindex $argv 0]
44 set fdelay [lindex $argv 1]
45 set idelay [lindex $argv 2]
46 set bsizes [lrange $argv 3 end]
47 set c 0
48
49 proc newconn {sock rhost rport} {
50 variable fdelay
51 variable c
52 incr c
53 namespace upvar [namespace current] c$c conn
54
55 #puts stdout "C $sock $rhost $rport / $fdelay" ; flush stdout
56
57 set conn(after) {}
58 set conn(state) 0
59 set conn(size) 0
60 set conn(data) ""
61 set conn(delay) $fdelay
62
63 fileevent $sock readable [list echoGet $c $sock]
64 fconfigure $sock -translation binary -buffering none -blocking 0
65 }
66
67 proc echoGet {c sock} {
68 variable fdelay
69 namespace upvar [namespace current] c$c conn
70
71 if {[eof $sock]} {
72 # one-shot echo
73 exit
74 }
75 append conn(data) [read $sock]
76
77 #puts stdout "G $c $sock $conn(data) <<$conn(data)>>" ; flush stdout
78
79 if {$conn(after) == {}} {
80 set conn(after) [after $conn(delay) [list echoPut $c $sock]]
81 }
82 }
83
84 proc echoPut {c sock} {
85 variable idelay
86 variable fdelay
87 variable bsizes
88 namespace upvar [namespace current] c$c conn
89
90 if {[string length $conn(data)] == 0} {
91 #puts stdout "C $c $sock" ; flush stdout
92 # auto terminate
93 close $sock
94 exit
95 #set conn(delay) $fdelay
96 return
97 }
98
99 set conn(delay) $idelay
100 set n [lindex $bsizes $conn(size)]
101
102 #puts stdout "P $c $sock $n >>" ; flush stdout
103
104 #puts __________________________________________
105 #parray conn
106 #puts n=<$n>
107
108 if {[string length $conn(data)] >= $n} {
109 puts -nonewline $sock [string range $conn(data) 0 $n]
110 set conn(data) [string range $conn(data) [incr n] end]
111 }
112
113 incr conn(size)
114 if {$conn(size) >= [llength $bsizes]} {
115 set conn(size) [expr {[llength $bsizes]-1}]
116 }
117
118 set conn(after) [after $conn(delay) [list echoPut $c $sock]]
119 }
120
121 #fileevent stdin readable {exit ;#cut}
122
123 # main
124 socket -server newconn -myaddr 127.0.0.1 $port
125 vwait forever
126 } __echo_srv__.tcl]
127
128 ########################################################################
129
130 proc fevent {fdelay idelay blocks script data} {
131 # Start and initialize an echo server, prepare data transmission, then
132 # hand over to the test script. This has to start real transmission via
133 # 'flush'. The server is stopped after completion of the test.
134
135 upvar 1 sock sk
136
137 # Fixed port, not so good. Lets hope for the best, for now.
138 set port 4000
139
140 exec tclsh __echo_srv__.tcl $port $fdelay $idelay {*}$blocks >@stdout &
141 after 500
142
143 #puts stdout "> $port"; flush stdout
144
145 set sk [socket localhost $port]
146 fconfigure $sk -blocking 0 -buffering full \
147 -buffersize [expr {10+[llength $data]}]
148 puts -nonewline $sk $data
149
150 # The channel is prepared to go off.
151
152 #puts stdout ">>>>>"; flush stdout
153
154 set res [uplevel 1 $script]
155 catch {close $sk}
156 return $res
157 }
158
159 # --------------------------------------------------------------
160 # utility transformations ...
161
162 proc id {op data} {
163 switch -- $op {
164 create/write - create/read - delete/write - delete/read - clear_read {
165 #ignore
166 }
167 flush/write - flush/read - write - read {
168 return $data
169 }
170 query/maxRead {
171 return -1
172 }
173 }
174 }
175
176 proc id_optrail {var op data} {
177 variable $var
178 upvar 0 $var trail
179
180 lappend trail $op
181 switch -- $op {
182 create/write - create/read - delete/write - delete/read -
183 flush/read - clear/read {
184 #ignore
185 }
186 flush/write - write - read {
187 return $data
188 }
189 query/maxRead {
190 return -1
191 }
192 default {
193 lappend trail "error $op"
194 error $op
195 }
196 }
197 }
198
199 proc id_fulltrail {var op data} {
200 namespace upvar [namespace current] $var trail
201
202 #puts stdout ">> $var $op $data" ; flush stdout
203
204 switch -- $op {
205 create/write - create/read - delete/write - delete/read - clear_read {
206 set res *ignored*
207 }
208 flush/write - flush/read - write - read {
209 set res $data
210 }
211 query/maxRead {
212 set res -1
213 }
214 }
215
216 #catch {puts stdout "\t>* $res" ; flush stdout}
217 #catch {puts stdout "x$res"} msg
218
219 lappend trail [list $op $data $res]
220 return $res
221 }
222
223 proc id_torture {chan op data} {
224 switch -- $op {
225 create/write -
226 create/read -
227 delete/write -
228 delete/read -
229 clear_read {;#ignore}
230 flush/write -
231 flush/read {}
232 write {
233 global level
234 if {$level} {
235 return
236 }
237 incr level
238 testchannel unstack $chan
239 testchannel transform $chan \
240 -command [namespace code [list id_torture $chan]]
241 return $data
242 }
243 read {
244 testchannel unstack $chan
245 testchannel transform $chan \
246 -command [namespace code [list id_torture $chan]]
247 return $data
248 }
249 query/maxRead {return -1}
250 }
251 }
252
253 proc counter {var op data} {
254 namespace upvar [namespace current] $var n
255
256 switch -- $op {
257 create/write - create/read - delete/write - delete/read - clear_read {
258 #ignore
259 }
260 flush/write - flush/read {
261 return {}
262 }
263 write {
264 return $data
265 }
266 read {
267 if {$n > 0} {
268 incr n -[string length $data]
269 if {$n < 0} {
270 set n 0
271 }
272 }
273 return $data
274 }
275 query/maxRead {
276 return $n
277 }
278 }
279 }
280
281 proc counter_audit {var vtrail op data} {
282 namespace upvar [namespace current] $var n $vtrail trail
283
284 switch -- $op {
285 create/write - create/read - delete/write - delete/read - clear_read {
286 set res {}
287 }
288 flush/write - flush/read {
289 set res {}
290 }
291 write {
292 set res $data
293 }
294 read {
295 if {$n > 0} {
296 incr n -[string length $data]
297 if {$n < 0} {
298 set n 0
299 }
300 }
301 set res $data
302 }
303 query/maxRead {
304 set res $n
305 }
306 }
307
308 lappend trail [list counter:$op $data $res]
309 return $res
310 }
311
312 proc rblocks {var vtrail n op data} {
313 namespace upvar [namespace current] $var buf $vtrail trail
314
315 set res {}
316
317 switch -- $op {
318 create/write - create/read - delete/write - delete/read - clear_read {
319 set buf {}
320 }
321 flush/write {
322 }
323 flush/read {
324 set res $buf
325 set buf {}
326 }
327 write {
328 set data
329 }
330 read {
331 append buf $data
332 set b [expr {$n * ([string length $buf] / $n)}]
333 append op " $n [string length $buf] :- $b"
334 set res [string range $buf 0 [incr b -1]]
335 set buf [string range $buf [incr b] end]
336 #return $res
337 }
338 query/maxRead {
339 set res -1
340 }
341 }
342
343 lappend trail [list rblock | $op $data $res | $buf]
344 return $res
345 }
346
347 # --------------------------------------------------------------
348 # ... and convenience procedures to stack them
349
350 proc identity {-attach channel} {
351 testchannel transform $channel -command [namespace code id]
352 }
353 proc audit_ops {var -attach channel} {
354 testchannel transform $channel -command [namespace code [list id_optrail $var]]
355 }
356 proc audit_flow {var -attach channel} {
357 testchannel transform $channel -command [namespace code [list id_fulltrail $var]]
358 }
359
360 proc torture {-attach channel} {
361 testchannel transform $channel -command [namespace code [list id_torture $channel]]
362 }
363
364 proc stopafter {var n -attach channel} {
365 namespace upvar [namespace current] $var vn
366 set vn $n
367 testchannel transform $channel -command [namespace code [list counter $var]]
368 }
369 proc stopafter_audit {var trail n -attach channel} {
370 namespace upvar [namespace current] $var vn
371 set vn $n
372 testchannel transform $channel -command [namespace code [list counter_audit $var $trail]]
373 }
374 proc rblocks_t {var trail n -attach channel} {
375 testchannel transform $channel -command [namespace code [list rblocks $var $trail $n]]
376 }
377
378 # --------------------------------------------------------------
379 # serialize an array, with keys in sorted order.
380
381 proc array_sget {v} {
382 upvar $v a
383 set res [list]
384 foreach n [lsort [array names a]] {
385 lappend res $n $a($n)
386 }
387 set res
388 }
389 proc asort {alist} {
390 # sort a list of key/value pairs by key, removes duplicates too.
391 array set a $alist
392 array_sget a
393 }
394
395 ########################################################################
396
397 test iogt-1.1 {stack/unstack} testchannel {
398 set fh [open $path(dummy) r]
399 identity -attach $fh
400 testchannel unstack $fh
401 close $fh
402 } {}
403 test iogt-1.2 {stack/close} testchannel {
404 set fh [open $path(dummy) r]
405 identity -attach $fh
406 close $fh
407 } {}
408 test iogt-1.3 {stack/unstack, configuration, options} testchannel {
409 set fh [open $path(dummy) r]
410 set ca [asort [fconfigure $fh]]
411 identity -attach $fh
412 set cb [asort [fconfigure $fh]]
413 testchannel unstack $fh
414 set cc [asort [fconfigure $fh]]
415 close $fh
416 # With this system none of the buffering, translation and encoding option
417 # may change their values with channels stacked upon each other or not.
418 # cb == ca == cc
419 list [string equal $ca $cb] [string equal $cb $cc] [string equal $ca $cc]
420 } {1 1 1}
421 test iogt-1.4 {stack/unstack, configuration} -setup {
422 set fh [open $path(dummy) r]
423 } -constraints testchannel -body {
424 set ca [asort [fconfigure $fh]]
425 identity -attach $fh
426 fconfigure $fh -buffering line -translation cr -encoding shiftjis
427 testchannel unstack $fh
428 set cc [asort [fconfigure $fh]]
429 list [string equal $ca $cc] [fconfigure $fh -buffering] \
430 [fconfigure $fh -translation] [fconfigure $fh -encoding]
431 } -cleanup {
432 close $fh
433 } -result {0 line cr shiftjis}
434
435 test iogt-2.0 {basic I/O going through transform} -setup {
436 set fin [open $path(dummy) r]
437 set fout [open $path(dummyout) w]
438 } -constraints testchannel -body {
439 identity -attach $fin
440 identity -attach $fout
441 fcopy $fin $fout
442 close $fin
443 close $fout
444 set fin [open $path(dummy) r]
445 set fout [open $path(dummyout) r]
446 list [string equal [set in [read $fin]] [set out [read $fout]]] \
447 [string length $in] [string length $out]
448 } -cleanup {
449 close $fin
450 close $fout
451 } -result {1 71 71}
452 test iogt-2.1 {basic I/O, operation trail} {testchannel unix} {
453 set fin [open $path(dummy) r]
454 set fout [open $path(dummyout) w]
455 set ain [list]; set aout [list]
456 audit_ops ain -attach $fin
457 audit_ops aout -attach $fout
458 fconfigure $fin -buffersize 10
459 fconfigure $fout -buffersize 10
460 fcopy $fin $fout
461 close $fin
462 close $fout
463 set res "[join $ain \n]\n--------\n[join $aout \n]"
464 } {create/read
465 query/maxRead
466 read
467 query/maxRead
468 read
469 query/maxRead
470 read
471 query/maxRead
472 read
473 query/maxRead
474 read
475 query/maxRead
476 read
477 query/maxRead
478 read
479 query/maxRead
480 read
481 query/maxRead
482 flush/read
483 query/maxRead
484 delete/read
485 --------
486 create/write
487 write
488 write
489 write
490 write
491 write
492 write
493 write
494 write
495 flush/write
496 delete/write}
497 test iogt-2.2 {basic I/O, data trail} {testchannel unix} {
498 set fin [open $path(dummy) r]
499 set fout [open $path(dummyout) w]
500 set ain [list]; set aout [list]
501 audit_flow ain -attach $fin
502 audit_flow aout -attach $fout
503 fconfigure $fin -buffersize 10
504 fconfigure $fout -buffersize 10
505 fcopy $fin $fout
506 close $fin
507 close $fout
508 set res "[join $ain \n]\n--------\n[join $aout \n]"
509 } {create/read {} *ignored*
510 query/maxRead {} -1
511 read abcdefghij abcdefghij
512 query/maxRead {} -1
513 read klmnopqrst klmnopqrst
514 query/maxRead {} -1
515 read uvwxyz0123 uvwxyz0123
516 query/maxRead {} -1
517 read 456789,./? 456789,./?
518 query/maxRead {} -1
519 read {><;'\|":[]} {><;'\|":[]}
520 query/maxRead {} -1
521 read {\}\{`~!@#$} {\}\{`~!@#$}
522 query/maxRead {} -1
523 read %^&*()_+-= %^&*()_+-=
524 query/maxRead {} -1
525 read {
526 } {
527 }
528 query/maxRead {} -1
529 flush/read {} {}
530 query/maxRead {} -1
531 delete/read {} *ignored*
532 --------
533 create/write {} *ignored*
534 write abcdefghij abcdefghij
535 write klmnopqrst klmnopqrst
536 write uvwxyz0123 uvwxyz0123
537 write 456789,./? 456789,./?
538 write {><;'\|":[]} {><;'\|":[]}
539 write {\}\{`~!@#$} {\}\{`~!@#$}
540 write %^&*()_+-= %^&*()_+-=
541 write {
542 } {
543 }
544 flush/write {} {}
545 delete/write {} *ignored*}
546 test iogt-2.3 {basic I/O, mixed trail} {testchannel unix} {
547 set fin [open $path(dummy) r]
548 set fout [open $path(dummyout) w]
549 set trail [list]
550 audit_flow trail -attach $fin
551 audit_flow trail -attach $fout
552 fconfigure $fin -buffersize 20
553 fconfigure $fout -buffersize 10
554 fcopy $fin $fout
555 close $fin
556 close $fout
557 join $trail \n
558 } {create/read {} *ignored*
559 create/write {} *ignored*
560 query/maxRead {} -1
561 read abcdefghijklmnopqrst abcdefghijklmnopqrst
562 write abcdefghij abcdefghij
563 write klmnopqrst klmnopqrst
564 query/maxRead {} -1
565 read uvwxyz0123456789,./? uvwxyz0123456789,./?
566 write uvwxyz0123 uvwxyz0123
567 write 456789,./? 456789,./?
568 query/maxRead {} -1
569 read {><;'\|":[]\}\{`~!@#$} {><;'\|":[]\}\{`~!@#$}
570 write {><;'\|":[]} {><;'\|":[]}
571 write {\}\{`~!@#$} {\}\{`~!@#$}
572 query/maxRead {} -1
573 read {%^&*()_+-=
574 } {%^&*()_+-=
575 }
576 query/maxRead {} -1
577 flush/read {} {}
578 write %^&*()_+-= %^&*()_+-=
579 write {
580 } {
581 }
582 query/maxRead {} -1
583 delete/read {} *ignored*
584 flush/write {} {}
585 delete/write {} *ignored*}
586
587 test iogt-2.4 {basic I/O, mixed trail} {testchannel} {
588 set fh [open $path(dummy) r]
589 torture -attach $fh
590 chan configure $fh -buffersize 2
591 set x [read $fh]
592 testchannel unstack $fh
593 close $fh
594 set x
595 } {}
596 test iogt-2.5 {basic I/O, mixed trail} {testchannel} {
597 set ::level 0
598 set fh [open $path(dummyout) w]
599 torture -attach $fh
600 puts -nonewline $fh abcdef
601 flush $fh
602 testchannel unstack $fh
603 close $fh
604 } {}
605
606 test iogt-3.0 {Tcl_Channel valid after stack/unstack, fevent handling} -setup {
607 proc DoneCopy {n {err {}}} {
608 variable copy 1
609 }
610 } -constraints {testchannel knownBug} -body {
611 # This test to check the validity of acquired Tcl_Channel references is not
612 # possible because even a backgrounded fcopy will immediately start to
613 # copy data, without waiting for the event loop. This is done only in case
614 # of an underflow on the read size!. So stacking transforms after the
615 # fcopy will miss information, or are not used at all.
616 #
617 # I was able to circumvent this by using the echo.tcl server with a big
618 # delay, causing the fcopy to underflow immediately.
619 set fin [open $path(dummy) r]
620 fevent 1000 500 {20 20 20 10 1 1} {
621 variable copy
622 close $fin
623 set fout [open dummyout w]
624 flush $sock; # now, or fcopy will error us out
625 # But the 1 second delay should be enough to initialize everything
626 # else here.
627 fcopy $sock $fout -command [namespace code DoneCopy]
628 # Transform after fcopy got its handles! They should be still valid
629 # for fcopy.
630 set trail [list]
631 audit_ops trail -attach $fout
632 vwait [namespace which -variable copy]
633 } [read $fin]; # {}
634 close $fout
635 # Check result of copy.
636 set fin [open $path(dummy) r]
637 set fout [open $path(dummyout) r]
638 set res [string equal [read $fin] [read $fout]]
639 close $fin
640 close $fout
641 list $res $trail
642 } -cleanup {
643 rename DoneCopy {}
644 } -result {1 {create/write create/read write flush/write flush/read delete/write delete/read}}
645
646 test iogt-4.0 {fileevent readable, after transform} -setup {
647 set fin [open $path(dummy) r]
648 set data [read $fin]
649 close $fin
650 set trail [list]
651 set got [list]
652 proc Done {args} {
653 variable stop 1
654 }
655 proc Get {sock} {
656 variable trail
657 variable got
658 if {[eof $sock]} {
659 Done
660 lappend trail "xxxxxxxxxxxxx"
661 close $sock
662 return
663 }
664 lappend trail "vvvvvvvvvvvvv"
665 lappend trail "\tgot: [lappend got "\[\[[read $sock]\]\]"]"
666 lappend trail "============="
667 #puts stdout $__ ; flush stdout
668 #read $sock
669 }
670
671 } -constraints {testchannel knownBug} -body {
672 fevent 1000 500 {20 20 20 10 1} {
673 variable stop
674 audit_flow trail -attach $sock
675 rblocks_t rbuf trail 23 -attach $sock
676
677 fileevent $sock readable [namespace code [list Get $sock]]
678
679 flush $sock; # Now, or fcopy will error us out
680 # But the 1 second delay should be enough to initialize everything
681 # else here.
682 vwait [namespace which -variable stop]
683 } $data
684 join [list [join $got \n] ~~~~~~~~ [join $trail \n]] \n
685 } -cleanup {
686 rename Done {}
687 rename Get {}
688 } -result {[[]]
689 [[abcdefghijklmnopqrstuvw]]
690 [[xyz0123456789,./?><;'\|]]
691 [[]]
692 [[]]
693 [[":[]\}\{`~!@#$%^&*()]]
694 [[]]
695 ~~~~~~~~
696 create/write {} *ignored*
697 create/read {} *ignored*
698 rblock | create/write {} {} | {}
699 rblock | create/read {} {} | {}
700 vvvvvvvvvvvvv
701 rblock | query/maxRead {} -1 | {}
702 query/maxRead {} -1
703 read abcdefghijklmnopqrstu abcdefghijklmnopqrstu
704 query/maxRead {} -1
705 rblock | {read 23 21 :- 0} abcdefghijklmnopqrstu {} | abcdefghijklmnopqrstu
706 rblock | query/maxRead {} -1 | abcdefghijklmnopqrstu
707 query/maxRead {} -1
708 got: {[[]]}
709 =============
710 vvvvvvvvvvvvv
711 rblock | query/maxRead {} -1 | abcdefghijklmnopqrstu
712 query/maxRead {} -1
713 read vwxyz0123456789,./?>< vwxyz0123456789,./?><
714 query/maxRead {} -1
715 rblock | {read 23 42 :- 23} vwxyz0123456789,./?>< abcdefghijklmnopqrstuvw | xyz0123456789,./?><
716 rblock | query/maxRead {} -1 | xyz0123456789,./?><
717 query/maxRead {} -1
718 got: {[[]]} {[[abcdefghijklmnopqrstuvw]]}
719 =============
720 vvvvvvvvvvvvv
721 rblock | query/maxRead {} -1 | xyz0123456789,./?><
722 query/maxRead {} -1
723 read {;'\|":[]\}\{`~!@#$%^&} {;'\|":[]\}\{`~!@#$%^&}
724 query/maxRead {} -1
725 rblock | {read 23 40 :- 23} {;'\|":[]\}\{`~!@#$%^&} {xyz0123456789,./?><;'\|} | {":[]\}\{`~!@#$%^&}
726 rblock | query/maxRead {} -1 | {":[]\}\{`~!@#$%^&}
727 query/maxRead {} -1
728 got: {[[]]} {[[abcdefghijklmnopqrstuvw]]} {[[xyz0123456789,./?><;'\|]]}
729 =============
730 vvvvvvvvvvvvv
731 rblock | query/maxRead {} -1 | {":[]\}\{`~!@#$%^&}
732 query/maxRead {} -1
733 read *( *(
734 query/maxRead {} -1
735 rblock | {read 23 19 :- 0} *( {} | {":[]\}\{`~!@#$%^&*(}
736 rblock | query/maxRead {} -1 | {":[]\}\{`~!@#$%^&*(}
737 query/maxRead {} -1
738 got: {[[]]} {[[abcdefghijklmnopqrstuvw]]} {[[xyz0123456789,./?><;'\|]]} {[[]]}
739 =============
740 vvvvvvvvvvvvv
741 rblock | query/maxRead {} -1 | {":[]\}\{`~!@#$%^&*(}
742 query/maxRead {} -1
743 read ) )
744 query/maxRead {} -1
745 rblock | {read 23 20 :- 0} ) {} | {":[]\}\{`~!@#$%^&*()}
746 rblock | query/maxRead {} -1 | {":[]\}\{`~!@#$%^&*()}
747 query/maxRead {} -1
748 got: {[[]]} {[[abcdefghijklmnopqrstuvw]]} {[[xyz0123456789,./?><;'\|]]} {[[]]} {[[]]}
749 =============
750 vvvvvvvvvvvvv
751 rblock | query/maxRead {} -1 | {":[]\}\{`~!@#$%^&*()}
752 query/maxRead {} -1
753 flush/read {} {}
754 rblock | flush/read {} {":[]\}\{`~!@#$%^&*()} | {}
755 rblock | query/maxRead {} -1 | {}
756 query/maxRead {} -1
757 got: {[[]]} {[[abcdefghijklmnopqrstuvw]]} {[[xyz0123456789,./?><;'\|]]} {[[]]} {[[]]} {[[":[]\}\{`~!@#$%^&*()]]}
758 =============
759 vvvvvvvvvvvvv
760 rblock | query/maxRead {} -1 | {}
761 query/maxRead {} -1
762 got: {[[]]} {[[abcdefghijklmnopqrstuvw]]} {[[xyz0123456789,./?><;'\|]]} {[[]]} {[[]]} {[[":[]\}\{`~!@#$%^&*()]]} {[[]]}
763 xxxxxxxxxxxxx
764 rblock | flush/write {} {} | {}
765 rblock | delete/write {} {} | {}
766 rblock | delete/read {} {} | {}
767 flush/write {} {}
768 delete/write {} *ignored*
769 delete/read {} *ignored*}; # catch unescaped quote "
770
771 test iogt-5.0 {EOF simulation} -setup {
772 set fin [open $path(dummy) r]
773 set fout [open $path(dummyout) w]
774 set trail [list]
775 } -constraints {testchannel knownBug} -result {
776 audit_flow trail -attach $fin
777 stopafter_audit d trail 20 -attach $fin
778 audit_flow trail -attach $fout
779 fconfigure $fin -buffersize 20
780 fconfigure $fout -buffersize 10
781 fcopy $fin $fout
782 testchannel unstack $fin
783 # now copy the rest in the channel
784 lappend trail {**after unstack**}
785 fcopy $fin $fout
786 close $fin
787 close $fout
788 join $trail \n
789 } -result {create/read {} *ignored*
790 counter:create/read {} {}
791 create/write {} *ignored*
792 counter:query/maxRead {} 20
793 query/maxRead {} -1
794 read {abcdefghijklmnopqrstuvwxyz0123456789,./?><;'\|":[]\}\{`~!@#$%^&*()_+-=
795 } {abcdefghijklmnopqrstuvwxyz0123456789,./?><;'\|":[]\}\{`~!@#$%^&*()_+-=
796 }
797 query/maxRead {} -1
798 flush/read {} {}
799 counter:read abcdefghijklmnopqrst abcdefghijklmnopqrst
800 write abcdefghij abcdefghij
801 write klmnopqrst klmnopqrst
802 counter:query/maxRead {} 0
803 counter:flush/read {} {}
804 counter:delete/read {} {}
805 **after unstack**
806 query/maxRead {} -1
807 write uvwxyz0123 uvwxyz0123
808 write 456789,./? 456789,./?
809 write {><;'\|":[]} {><;'\|":[]}
810 write {\}\{`~!@#$} {\}\{`~!@#$}
811 write %^&*()_+-= %^&*()_+-=
812 write {
813 } {
814 }
815 query/maxRead {} -1
816 delete/read {} *ignored*
817 flush/write {} {}
818 delete/write {} *ignored*}
819
820 proc constX {op data} {
821 # replace anything coming in with a same-length string of x'es.
822 switch -- $op {
823 create/write - create/read - delete/write - delete/read - clear_read {
824 #ignore
825 }
826 flush/write - flush/read - write - read {
827 return [string repeat x [string length $data]]
828 }
829 query/maxRead {
830 return -1
831 }
832 }
833 }
834 proc constx {-attach channel} {
835 testchannel transform $channel -command [namespace code constX]
836 }
837
838 test iogt-6.0 {Push back} -constraints testchannel -body {
839 set f [open $path(dummy) r]
840 # contents of dummy = "abcdefghi..."
841 read $f 3; # skip behind "abc"
842 constx -attach $f
843 # expect to get "xxx" from the transform because of unread "def" input to
844 # transform which returns "xxx".
845 #
846 # Actually the IO layer preread the whole file and will read "def"
847 # directly from the buffer without bothering to consult the newly stacked
848 # transformation. This is wrong.
849 read $f 3
850 } -cleanup {
851 close $f
852 } -result {xxx}
853 test iogt-6.1 {Push back and up} -constraints {testchannel knownBug} -body {
854
855 # This test demonstrates the bug/misfeature in the stacked
856 # channel implementation that data can be discarded if it is
857 # read into the buffers of one channel in the stack, and then
858 # that channel is popped before anything above it reads.
859 #
860 # This bug can be worked around by always setting -buffersize
861 # to 1, but who wants to do that?
862
863 set f [open $path(dummy) r]
864 # contents of dummy = "abcdefghi..."
865 read $f 3; # skip behind "abc"
866 constx -attach $f
867 set res [read $f 3]
868 testchannel unstack $f
869 append res [read $f 3]
870 } -cleanup {
871 close $f
872 } -result {xxxghi}
873
874
875 # Driver for a base channel that emits several short "files"
876 # with each terminated by a fleeting EOF
877 proc driver {cmd args} {
878 variable buffer
879 variable index
880 set chan [lindex $args 0]
881 switch -- $cmd {
882 initialize {
883 set index($chan) 0
884 set buffer($chan) .....
885 return {initialize finalize watch read}
886 }
887 finalize {
888 if {![info exists index($chan)]} {return}
889 unset index($chan) buffer($chan)
890 return
891 }
892 watch {}
893 read {
894 set n [lindex $args 1]
895 if {![info exists index($chan)]} {
896 driver initialize $chan
897 }
898 set new [expr {$index($chan) + $n}]
899 set result [string range $buffer($chan) $index($chan) $new-1]
900 set index($chan) $new
901 if {[string length $result] == 0} {
902 driver finalize $chan
903 }
904 return $result
905 }
906 }
907 }
908
909 test iogt-7.0 {Handle fleeting EOF} -constraints {testchannel} -body {
910 set chan [chan create read [namespace which driver]]
911 identity -attach $chan
912 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
913 [read $chan] [eof $chan]
914 } -cleanup {
915 close $chan
916 } -result {0 ..... 1 {} 0 ..... 1}
917
918 proc delay {op data} {
919 variable store
920 switch -- $op {
921 create/write - create/read -
922 delete/write - delete/read -
923 flush/write - write -
924 clear_read {;#ignore}
925 flush/read -
926 read {
927 if {![info exists store]} {set store {}}
928 set reply $store
929 set store $data
930 return $reply
931 }
932 query/maxRead {return -1}
933 }
934 }
935
936 test iogt-7.1 {Handle fleeting EOF} -constraints {testchannel} -body {
937 set chan [chan create read [namespace which driver]]
938 testchannel transform $chan -command [namespace code delay]
939 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
940 [read $chan] [eof $chan]
941 } -cleanup {
942 close $chan
943 } -result {0 ..... 1 {} 0 ..... 1}
944
945 rename delay {}
946 rename driver {}
947
948 # cleanup
949 foreach file [list dummy dummyout __echo_srv__.tcl] {
950 removeFile $file
951 }
952 cleanupTests
953 }
954 namespace delete ::tcl::test::iogt
955 return