1#!/bin/bash
2
3set -e
4
5if [ -x ./xl ] ; then
6    export LD_LIBRARY_PATH=.
7    XL=./xl
8else
9    XL=xl
10fi
11
12fprefix=tmp.check-xl-vif-parse
13
14expected () {
15    cat >$fprefix.expected
16}
17
18failures=0
19
20one () {
21    expected_rc=$1; shift
22    printf "test case %s...\n" "$*"
23    set +e
24    ${XL} -N network-attach 0 "$@" </dev/null >$fprefix.actual 2>/dev/null
25    actual_rc=$?
26    diff -u $fprefix.expected $fprefix.actual
27    diff_rc=$?
28    set -e
29    if [ $actual_rc != $expected_rc ] || [ $diff_rc != 0 ]; then
30        echo >&2 "test case \`$*' failed ($actual_rc $diff_rc)"
31        failures=$(( $failures + 1 ))
32    fi
33}
34
35complete () {
36    if [ "$failures" = 0 ]; then
37        echo all ok.; exit 0
38    else
39        echo "$failures tests failed."; exit 1
40    fi
41}
42
43e=255
44
45
46#---------- test data ----------
47
48# test invalid vif config
49expected </dev/null
50one 1 foo
51
52# test invalid rate units
53expected </dev/null
54one $e rate=foo
55one $e rate=foo
56one $e rate=10MB
57one $e rate=10MB/m
58one $e rate=10ZB
59one $e rate=10ZB/s
60one $e rate=10ZB/m
61
62# test b/s and B/s rate units
63expected <<END
64vif: {
65    "backend_domid": 0,
66    "devid": 0,
67    "mtu": 0,
68    "model": null,
69    "mac": "00:00:00:00:00:00",
70    "ip": null,
71    "bridge": null,
72    "ifname": null,
73    "script": null,
74    "nictype": null,
75    "rate_bytes_per_interval": 100000,
76    "rate_interval_usecs": 50000
77}
78
79END
80
81one 0 rate=16000000b/s
82one 0 rate=16000000b/s@50ms
83one 0 rate=2000000B/s
84one 0 rate=2000000B/s@50ms
85
86# test Kb/s and KB/s rate units
87expected <<END
88vif: {
89    "backend_domid": 0,
90    "devid": 0,
91    "mtu": 0,
92    "model": null,
93    "mac": "00:00:00:00:00:00",
94    "ip": null,
95    "bridge": null,
96    "ifname": null,
97    "script": null,
98    "nictype": null,
99    "rate_bytes_per_interval": 100,
100    "rate_interval_usecs": 50000
101}
102
103END
104one 0 rate=16Kb/s
105one 0 rate=16Kb/s@50ms
106one 0 rate=2KB/s
107one 0 rate=2KB/s@50ms
108
109# test Mb/s and MB/s rate units
110expected <<END
111vif: {
112    "backend_domid": 0,
113    "devid": 0,
114    "mtu": 0,
115    "model": null,
116    "mac": "00:00:00:00:00:00",
117    "ip": null,
118    "bridge": null,
119    "ifname": null,
120    "script": null,
121    "nictype": null,
122    "rate_bytes_per_interval": 100000,
123    "rate_interval_usecs": 50000
124}
125
126END
127one 0 rate=16Mb/s
128one 0 rate=16Mb/s@50ms
129one 0 rate=2MB/s
130one 0 rate=2MB/s@50ms
131
132# test Gb/s and GB/s rate units
133expected <<END
134vif: {
135    "backend_domid": 0,
136    "devid": 0,
137    "mtu": 0,
138    "model": null,
139    "mac": "00:00:00:00:00:00",
140    "ip": null,
141    "bridge": null,
142    "ifname": null,
143    "script": null,
144    "nictype": null,
145    "rate_bytes_per_interval": 50000000,
146    "rate_interval_usecs": 50000
147}
148
149END
150one 0 rate=8Gb/s
151one 0 rate=8Gb/s@50ms
152one 0 rate=1GB/s
153one 0 rate=1GB/s@50ms
154
155# test rate overflow
156expected </dev/null
157one $e rate=4294967296b/s
158one $e rate=4294967296Kb/s
159one $e rate=4294967296Mb/s
160one $e rate=4294967296Gb/s
161
162# test rate underflow
163expected </dev/null
164one $e rate=0B/s
165
166# test invalid replenishment interval
167expected </dev/null
168one $e rate=10Mb/s@foo
169one $e rate=10Mb/s@10h
170one $e rate=10MB/s@foo
171one $e rate=10MB/s@10h
172
173# test replenishment interval in seconds
174expected <<END
175vif: {
176    "backend_domid": 0,
177    "devid": 0,
178    "mtu": 0,
179    "model": null,
180    "mac": "00:00:00:00:00:00",
181    "ip": null,
182    "bridge": null,
183    "ifname": null,
184    "script": null,
185    "nictype": null,
186    "rate_bytes_per_interval": 10000000,
187    "rate_interval_usecs": 1000000
188}
189
190END
191one 0 rate=80Mb/s@1s
192one 0 rate=10MB/s@1s
193
194# test replenishment interval overflow
195expected </dev/null
196one $e rate=1B/s@4294967296us
197one $e rate=1B/s@4294968ms
198one $e rate=1B/s@4295s
199
200# test replenishment interval underflow
201expected </dev/null
202one $e rate=1B/s@0us
203
204# test rate limiting resulting in overflow
205expected </dev/null
206one $e rate=4294967295GB/s@5us
207one $e rate=4296MB/s@4294s
208
209# test include of single '@'
210expected </dev/null
211one $e rate=@
212
213complete
214