chiark / gitweb /
stest: Add debug logs
[secnet.git] / stest / common.tcl
1 source test-common.tcl
2
3 package require Tclx
4
5 load chiark_tcl_hbytes-1.so
6 load chiark_tcl_dgram-1.so
7
8 set netlink(inside) {
9     local-address "172.18.232.9";
10     secnet-address "172.18.232.10";
11     remote-networks "172.18.232.0/28";
12 }
13 set netlink(outside) {
14     local-address "172.18.232.1";
15     secnet-address "172.18.232.2";
16     remote-networks "172.18.232.0/28";
17 }
18
19 set ports(inside) {16913 16910}
20 set ports(outside) 16900
21
22 set extra(inside) {
23     local-mobile True;
24     mtu-target 1260;
25 }
26 set extra(outside) {}
27
28 proc mkconf {location site} {
29     global tmp
30     global builddir
31     global netlink
32     global ports
33     global extra
34     global netlinkfh
35     set pipefp $tmp/$site.netlink
36     foreach tr {t r} {
37         file delete $pipefp.$tr
38         exec mkfifo -m600 $pipefp.$tr
39         set netlinkfh($site.$tr) [set fh [open $pipefp.$tr r+]]
40         fconfigure $fh -blocking 0 -buffering none -translation binary
41     }
42     fileevent $netlinkfh($site.r) readable \
43         [list netlink-readable $location $site]
44     set fakeuf $tmp/$site.fake-userv
45     set fakeuh [open $fakeuf w 0755]
46     puts $fakeuh "#!/bin/sh
47 set -e
48 exec 3<&0
49 cat <&3 3<&- >$pipefp.r &
50 exec 3<>$pipefp.t
51 exec <$pipefp.t
52 exec 3<&-
53 exec cat
54 "
55     close $fakeuh
56     set cfg "
57         netlink userv-ipif {
58             name \"netlink\";
59             userv-path \"$fakeuf\";
60         $netlink($site)
61             mtu 1400;
62             buffer sysbuffer(2048);
63             interface \"secnet-test-[string range $site 0 0]\";
64         };
65         comm
66 "
67     set delim {}
68     foreach port $ports($site) {
69         append cfg "$delim
70             udp {
71                 port $port;
72                 address \"::1\", \"127.0.0.1\";
73                 buffer sysbuffer(4096);
74             }
75         "
76         set delim ,
77     }
78     append cfg ";
79         local-name \"test-example/$location/$site\";
80         local-key rsa-private(\"$builddir/test-example/$site.key\");
81 "
82     append cfg $extra($site)
83     append cfg "
84         log logfile {
85             prefix \"$site\";
86             class \"debug\",\"info\",\"notice\",\"warning\",\"error\",\"security\",\"fatal\";
87         };
88     "
89     append cfg {
90         system {
91         };
92         resolver adns {
93         };
94         log-events "all";
95         random randomfile("/dev/urandom",no);
96         transform eax-serpent { }, serpent256-cbc { };
97     }
98
99     set f [open $builddir/test-example/sites.conf r]
100     set sites [read $f]
101     close $f
102     append cfg $sites
103     append cfg {
104         sites map(site,all-sites);
105     }
106     return $cfg
107 }
108
109 proc spawn-secnet {location site} {
110     global tmp
111     global builddir
112     global netlinkfh
113     upvar #0 pids($site) pid
114     set cf $tmp/$site.conf
115     set ch [open $cf w]
116     puts $ch [mkconf $location $site]
117     close $ch
118     set argl [list $builddir/secnet -dvnc $cf]
119     set pid [fork]
120     if {!$pid} {
121         execl [lindex $argl 0] [lrange $argl 1 end]
122     }
123     puts -nonewline $netlinkfh($site.t) [hbytes h2raw c0]
124 }
125
126 proc netlink-readable {location site} {
127     global ok
128     upvar #0 netlinkfh($site.r) fh
129     read $fh; # empty the buffer
130     switch -exact $site {
131         inside {
132             puts OK
133             set ok 1; # what a bodge
134             return
135         }
136         outside {
137             error "inside rx'd!"
138         }
139     }
140 }
141
142 proc bgerror {message} {
143     global errorInfo errorCode
144     catch {
145         puts stderr "
146 ----------------------------------------
147 $errorInfo
148
149 $errorCode
150 $message
151 ----------------------------------------
152     "
153     }
154     exit 1
155 }
156
157 proc sendpkt {} {
158     global netlinkfh
159     set p {
160         4500 0054 ed9d 4000 4001 24da ac12 e809
161         ac12 e802 0800 1de4 2d96 0001 f1d4 a05d
162         0000 0000 507f 0b00 0000 0000 1011 1213
163         1415 1617 1819 1a1b 1c1d 1e1f 2021 2223
164         2425 2627 2829 2a2b 2c2d 2e2f 3031 3233
165         3435 3637
166     }
167     puts -nonewline $netlinkfh(inside.t) \
168         [hbytes h2raw c0[join $p ""]c0]
169 }
170
171 set socktmp $tmp/s
172 exec mkdir -p -m700 $socktmp
173 regsub {^(?!/)} $socktmp {./} socktmp ;# dgram-socket wants ./ or /
174
175 proc prefix_preload {lib} { prefix_some_path LD_PRELOAD $lib }
176
177 set env(UDP_PRELOAD_DIR) $socktmp
178 prefix_preload $builddir/stest/udp-preload.so
179
180 proc udp-proxy {} {
181     global socktmp udpsock
182     set u $socktmp/udp
183     file delete $u
184     regsub {^(?!/)} $u {./} u
185     set udpsock [dgram-socket create $u]
186     dgram-socket on-receive $udpsock udp-relay
187 }
188
189 proc udp-relay {data src sock args} {
190     global udpsock socktmp
191     set headerlen [expr {52+1}]
192     set orgsrc $src
193
194     set dst [hbytes range $data 0 $headerlen]
195     regsub {(?:00)*$} $dst {} dst
196     set dst [hbytes h2raw $dst]
197
198     hbytes overwrite data 0 [hbytes zeroes $headerlen]
199     regsub {.*/} $src {} src
200     set srch [hbytes raw2h $src]
201     hbytes append srch 00
202     if {[catch {
203         if {[regexp {[^.,:0-9a-f]} $dst c]} { error "bad dst" }
204         if {[hbytes length $srch] > $headerlen} { error "src addr too long" }
205         hbytes overwrite data 0 $srch
206         dgram-socket transmit $udpsock $data $socktmp/$dst
207     } emsg]} {
208         puts stderr "$orgsrc -> $dst: $emsg"
209     }
210 }
211
212 proc test-kex {} {
213     udp-proxy
214     spawn-secnet in inside
215     spawn-secnet out outside
216
217     after 500 sendpkt
218     after 1000 sendpkt
219     after 5000 timed-out
220
221     vwait ok
222 }