-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendtoend_test.go
More file actions
225 lines (197 loc) · 4.84 KB
/
endtoend_test.go
File metadata and controls
225 lines (197 loc) · 4.84 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package l2
import (
"bytes"
"errors"
"fmt"
"log"
"math"
"strings"
"testing"
"time"
)
var (
dev_name = "test"
dev_mac = "00:11:22:33:44:55"
dest_mac = "ff:11:22:33:44:55"
)
// Waits for a frame that matches a desired one.
// Args:
// target: The frame we are looking for.
// dev: The dvice we are waiting on.
// Returns:
// The number of bytes it read total, error.
func waitForFrame(target []byte, dev FrameReader) (int, error) {
timeout := time.After(time.Second)
bytes_read := 0
for {
// Check if we're out of time
select {
case <-timeout:
return 0, errors.New("waitForFrame timed out.")
default:
}
frame, err := dev.ReadFrame()
if err != nil {
log.Print(err)
return 0, err
}
bytes_read += len(frame)
if bytes.Equal(target, frame) {
return bytes_read, nil
}
PrintFrame("desired", target)
PrintFrame("found", frame)
}
}
// Creates a new connected tap/eth pair.
// Returns:
// The tap device and the ethernet device, error.
func NewDevices() (FrameReadWriteCloser, FrameReadWriter, error) {
tap, err := NewTapDevice(dev_mac, dev_name)
if err != nil {
return nil, nil, err
}
return makeEth(tap)
}
// Creates a new connected tap/eth pair with bandwidth restrictions.
// Args:
// send_bandwidth: Max bandwidth for outgoing data. (b/s)
// receive_bandwidth: Max bandwidth for incoming data. (b/s)
// Returns:
// The tap device and the ethernet device, error.
func NewDevicesWithBandwidth(send_bandwidth,
receive_bandwidth int) (FrameReadWriteCloser,
FrameReadWriter, error) {
tap, err := NewTapDevice(dev_mac, dev_name)
if err != nil {
return nil, nil, err
}
latency := NewDeviceWithLatency(tap, send_bandwidth,
receive_bandwidth)
return makeEth(latency)
}
// Makes an eth device and connects an existing tap device to it.
// Args:
// tap: The tap device.
// Returns:
// The tap device and the ethernet device, error.
func makeEth(tap FrameReadWriteCloser) (FrameReadWriteCloser,
FrameReadWriter, error) {
eth, err := ConnectExistingDevice(dev_name)
if err != nil {
tap.Close()
return nil, nil, err
}
return tap, eth, nil
}
func NewFrame(dest, src string) EthFrame {
data := make([]byte, 100)
return NewEthFrame(macToBytesOrDie(dest), macToBytesOrDie(src), 1, data)
}
func TestEthToTap(t *testing.T) {
tap, eth, err := NewDevices()
if err != nil {
t.Fatal(err)
}
defer tap.Close()
frame := NewFrame(dest_mac, dev_mac)
err = eth.WriteFrame(frame)
if err != nil {
t.Fatal(err)
}
_, err = waitForFrame(frame, tap)
if err != nil {
t.Fatal(err)
}
}
// Test sending packets with restricted bandwidth.
func TestSendingBandwidth(t *testing.T) {
tap, _, err := NewDevicesWithBandwidth(10000, 0)
if err != nil {
t.Fatal(err)
}
defer tap.Close()
frame := NewFrame(dest_mac, dev_mac)
start_time := time.Now()
bytes_written := 0
// Write the frame a lot of times so we get a more accurate idea of bandwidth.
for i := 0; i < 100; i++ {
err = tap.WriteFrame(frame)
if err != nil {
t.Fatal(err)
}
bytes_written += len(frame)
}
end_time := time.Now()
// Check outgoing bandwidth.
elapsed := float64(end_time.Sub(start_time)) / float64(time.Second)
bandwidth := float64(bytes_written) / elapsed
if math.Abs(10000-bandwidth) > 500 {
t.Fatalf("Got %f b/s of bandwidth, expected 10000.", bandwidth)
}
}
// Test receiving packets with restricted bandwidth.
func TestReceivingBandwidth(t *testing.T) {
tap, eth, err := NewDevicesWithBandwidth(0, 10000)
if err != nil {
t.Fatal(err)
}
defer tap.Close()
frame := NewFrame(dest_mac, dev_mac)
// Write the frame a lot of times so we get a more accurate idea of bandwidth.
for i := 0; i < 100; i++ {
err = eth.WriteFrame(frame)
if err != nil {
t.Fatal(err)
}
}
start_time := time.Now()
bytes_read := 0
// Now read all those frames back.
for i := 0; i < 100; i++ {
read_this_cycle, err := waitForFrame(frame, tap)
if err != nil {
t.Fatal(err)
}
if read_this_cycle < 0 {
t.Fatal("Reading frame failed.")
}
bytes_read += read_this_cycle
}
end_time := time.Now()
// Check incoming bandwidth.
elapsed := float64(end_time.Sub(start_time)) / float64(time.Second)
bandwidth := float64(bytes_read) / elapsed
if math.Abs(10000-bandwidth) > 500 {
t.Fatalf("Got %f b/s of bandwidth, expected 10000.", bandwidth)
}
}
func TestTapToEth(t *testing.T) {
tap, eth, err := NewDevices()
if err != nil {
t.Fatal(err)
}
defer tap.Close()
frame := NewFrame(dest_mac, dev_mac)
err = tap.WriteFrame(frame)
if err != nil {
t.Fatal(err)
}
_, err = waitForFrame(frame, eth)
if err != nil {
t.Fatal(err)
}
}
func TestPrinting(t *testing.T) {
tap, eth, err := NewDevices()
if err != nil {
t.Fatal(err)
}
defer tap.Close()
if !strings.Contains(fmt.Sprint(tap), "TapDevice") {
t.Fatal("Missing TapDevice from", tap)
}
if !strings.Contains(fmt.Sprint(eth), "existingDevice") {
t.Fatal("Missing existingDevice from", eth)
}
}