Debian Codegraph @

cmd/atomdns/handlers/drunk/setup_test.go

raw
1 package drunk
2
3 import (
4 "testing"
5
6 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver"
7 )
8
9 func TestSetup(t *testing.T) {
10 testcases := []struct {
11 input string
12 shouldErr bool
13 drop uint64
14 delay uint64
15 truncate uint64
16 }{
17 // oks
18 {`drunk`, false, 4, 0, 0},
19 {`drunk {
20 drop /2
21 delay /3 1ms
22
23 }`, false, 2, 3, 0},
24 {`drunk {
25 truncate /2
26 delay /3 1ms
27
28 }`, false, 0, 3, 2},
29 {`erraric {
30 drop /3
31 delay
32 }`, false, 3, 2, 0},
33 // fails
34 {`drunk {
35 drop -1
36 }`, true, 0, 0, 0},
37 {`drunk {
38 delay -1
39 }`, true, 0, 0, 0},
40 {`drunk {
41 delay 1 2 4
42 }`, true, 0, 0, 0},
43 {`drunk {
44 delay 15.a
45 }`, true, 0, 0, 0},
46 {`drunk {
47 drop 3
48 delay 3 bla
49 }`, true, 0, 0, 0},
50 {`drunk {
51 truncate 15.a
52 }`, true, 0, 0, 0},
53 {`drunk {
54 something-else
55 }`, true, 0, 0, 0},
56 }
57 for i, tc := range testcases {
58 co := dnsserver.NewTestController(tc.input)
59 d := new(Drunk)
60 err := d.Setup(co)
61 if tc.shouldErr && err == nil {
62 t.Errorf("test %d: expected error but found nil", i)
63 continue
64 } else if !tc.shouldErr && err != nil {
65 t.Errorf("test %d: expected no error but found error: %v", i, err)
66 continue
67 }
68
69 if tc.shouldErr {
70 continue
71 }
72
73 if tc.delay != d.delay {
74 t.Errorf("test %d: Expected delay %d but found: %d", i, tc.delay, d.delay)
75 }
76 if tc.drop != d.drop {
77 t.Errorf("test %d: Expected drop %d but found: %d", i, tc.drop, d.drop)
78 }
79 if tc.truncate != d.truncate {
80 t.Errorf("test %d: Expected truncate %d but found: %d", i, tc.truncate, d.truncate)
81 }
82 }
83 }