-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnic_test.go
121 lines (100 loc) · 2.22 KB
/
nic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package netem
import (
"errors"
"testing"
)
func TestMockableNIC(t *testing.T) {
t.Run("MockFrameAvailable", func(t *testing.T) {
ch := make(chan any, 1)
ch <- true
nic := &MockableNIC{
MockFrameAvailable: func() <-chan any {
return ch
},
}
value := <-nic.FrameAvailable()
if flag, good := value.(bool); !good || !flag {
t.Fatal("FrameAvailable is broken")
}
})
t.Run("MockReadFrameNonblocking", func(t *testing.T) {
expect := errors.New("mocked error")
nic := &MockableNIC{
MockReadFrameNonblocking: func() (*Frame, error) {
return nil, expect
},
}
frame, err := nic.ReadFrameNonblocking()
if !errors.Is(err, expect) {
t.Fatal("unexpected error", err)
}
if frame != nil {
t.Fatal("expected nil frame")
}
})
t.Run("MockStackClosed", func(t *testing.T) {
ch := make(chan any, 1)
ch <- true
nic := &MockableNIC{
MockStackClosed: func() <-chan any {
return ch
},
}
value := <-nic.StackClosed()
if flag, good := value.(bool); !good || !flag {
t.Fatal("FrameAvailable is broken")
}
})
t.Run("MockClose", func(t *testing.T) {
expect := errors.New("mocked error")
nic := &MockableNIC{
MockClose: func() error {
return expect
},
}
err := nic.Close()
if !errors.Is(err, expect) {
t.Fatal("unexpected error", err)
}
})
t.Run("MockIPAddress", func(t *testing.T) {
expect := "1.1.1.1"
nic := &MockableNIC{
MockIPAddress: func() string {
return expect
},
}
if v := nic.IPAddress(); v != expect {
t.Fatal("unexpected value", v)
}
})
t.Run("MockInterfaceName", func(t *testing.T) {
expect := "eth0"
nic := &MockableNIC{
MockInterfaceName: func() string {
return expect
},
}
if v := nic.InterfaceName(); v != expect {
t.Fatal("unexpected value", v)
}
})
t.Run("MockWriteFrame", func(t *testing.T) {
expectedErr := errors.New("mocked error")
expectedFrame := NewFrame(nil)
var got *Frame
nic := &MockableNIC{
MockWriteFrame: func(frame *Frame) error {
got = frame
return expectedErr
},
}
err := nic.WriteFrame(expectedFrame)
if !errors.Is(err, expectedErr) {
t.Fatal("unexpected error", err)
}
if got != expectedFrame {
t.Fatal("got unexpected frame")
}
})
}