Browse Source

device, tun: rearrange code and fix device tests

Signed-off-by: Matt Layher <mdlayher@gmail.com>
Matt Layher 1 year ago
parent
commit
32912dc778
3 changed files with 86 additions and 103 deletions
  1. 30
    10
      device/device_test.go
  2. 56
    0
      device/tun_test.go
  3. 0
    93
      tun/helper_test.go

+ 30
- 10
device/device_test.go View File

@@ -9,21 +9,17 @@ package device
9 9
  * without network dependencies
10 10
  */
11 11
 
12
-import "testing"
12
+import (
13
+	"bytes"
14
+	"testing"
15
+)
13 16
 
14 17
 func TestDevice(t *testing.T) {
15 18
 
16 19
 	// prepare tun devices for generating traffic
17 20
 
18
-	tun1, err := CreateDummyTUN("tun1")
19
-	if err != nil {
20
-		t.Error("failed to create tun:", err.Error())
21
-	}
22
-
23
-	tun2, err := CreateDummyTUN("tun2")
24
-	if err != nil {
25
-		t.Error("failed to create tun:", err.Error())
26
-	}
21
+	tun1 := newDummyTUN("tun1")
22
+	tun2 := newDummyTUN("tun2")
27 23
 
28 24
 	_ = tun1
29 25
 	_ = tun2
@@ -46,3 +42,27 @@ func TestDevice(t *testing.T) {
46 42
 	// create binds
47 43
 
48 44
 }
45
+
46
+func randDevice(t *testing.T) *Device {
47
+	sk, err := newPrivateKey()
48
+	if err != nil {
49
+		t.Fatal(err)
50
+	}
51
+	tun := newDummyTUN("dummy")
52
+	logger := NewLogger(LogLevelError, "")
53
+	device := NewDevice(tun, logger)
54
+	device.SetPrivateKey(sk)
55
+	return device
56
+}
57
+
58
+func assertNil(t *testing.T, err error) {
59
+	if err != nil {
60
+		t.Fatal(err)
61
+	}
62
+}
63
+
64
+func assertEqual(t *testing.T, a []byte, b []byte) {
65
+	if bytes.Compare(a, b) != 0 {
66
+		t.Fatal(a, "!=", b)
67
+	}
68
+}

+ 56
- 0
device/tun_test.go View File

@@ -0,0 +1,56 @@
1
+/* SPDX-License-Identifier: MIT
2
+ *
3
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
4
+ */
5
+
6
+package device
7
+
8
+import (
9
+	"errors"
10
+	"os"
11
+
12
+	"golang.zx2c4.com/wireguard/tun"
13
+)
14
+
15
+// newDummyTUN creates a dummy TUN device with the specified name.
16
+func newDummyTUN(name string) tun.TUNDevice {
17
+	return &dummyTUN{
18
+		name:    name,
19
+		packets: make(chan []byte, 100),
20
+		events:  make(chan tun.TUNEvent, 10),
21
+	}
22
+}
23
+
24
+// A dummyTUN is a tun.TUNDevice which is used in unit tests.
25
+type dummyTUN struct {
26
+	name    string
27
+	mtu     int
28
+	packets chan []byte
29
+	events  chan tun.TUNEvent
30
+}
31
+
32
+func (d *dummyTUN) Events() chan tun.TUNEvent { return d.events }
33
+func (*dummyTUN) File() *os.File              { return nil }
34
+func (*dummyTUN) Flush() error                { return nil }
35
+func (d *dummyTUN) MTU() (int, error)         { return d.mtu, nil }
36
+func (d *dummyTUN) Name() (string, error)     { return d.name, nil }
37
+
38
+func (d *dummyTUN) Close() error {
39
+	close(d.events)
40
+	close(d.packets)
41
+	return nil
42
+}
43
+
44
+func (d *dummyTUN) Read(b []byte, offset int) (int, error) {
45
+	buf, ok := <-d.packets
46
+	if !ok {
47
+		return 0, errors.New("device closed")
48
+	}
49
+	copy(b[offset:], buf)
50
+	return len(buf), nil
51
+}
52
+
53
+func (d *dummyTUN) Write(b []byte, offset int) (int, error) {
54
+	d.packets <- b[offset:]
55
+	return len(b), nil
56
+}

+ 0
- 93
tun/helper_test.go View File

@@ -1,93 +0,0 @@
1
-/* SPDX-License-Identifier: MIT
2
- *
3
- * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
4
- */
5
-
6
-package tun
7
-
8
-import (
9
-	"bytes"
10
-	"errors"
11
-	"os"
12
-	"testing"
13
-
14
-	"golang.zx2c4.com/wireguard/tun"
15
-)
16
-
17
-/* Helpers for writing unit tests
18
- */
19
-
20
-type DummyTUN struct {
21
-	name    string
22
-	mtu     int
23
-	packets chan []byte
24
-	events  chan tun.TUNEvent
25
-}
26
-
27
-func (tun *DummyTUN) File() *os.File {
28
-	return nil
29
-}
30
-
31
-func (tun *DummyTUN) Name() (string, error) {
32
-	return tun.name, nil
33
-}
34
-
35
-func (tun *DummyTUN) MTU() (int, error) {
36
-	return tun.mtu, nil
37
-}
38
-
39
-func (tun *DummyTUN) Write(d []byte, offset int) (int, error) {
40
-	tun.packets <- d[offset:]
41
-	return len(d), nil
42
-}
43
-
44
-func (tun *DummyTUN) Close() error {
45
-	close(tun.events)
46
-	close(tun.packets)
47
-	return nil
48
-}
49
-
50
-func (tun *DummyTUN) Events() chan tun.TUNEvent {
51
-	return tun.events
52
-}
53
-
54
-func (tun *DummyTUN) Read(d []byte, offset int) (int, error) {
55
-	t, ok := <-tun.packets
56
-	if !ok {
57
-		return 0, errors.New("device closed")
58
-	}
59
-	copy(d[offset:], t)
60
-	return len(t), nil
61
-}
62
-
63
-func CreateDummyTUN(name string) (tun.TUNDevice, error) {
64
-	var dummy DummyTUN
65
-	dummy.mtu = 0
66
-	dummy.packets = make(chan []byte, 100)
67
-	dummy.events = make(chan tun.TUNEvent, 10)
68
-	return &dummy, nil
69
-}
70
-
71
-func assertNil(t *testing.T, err error) {
72
-	if err != nil {
73
-		t.Fatal(err)
74
-	}
75
-}
76
-
77
-func assertEqual(t *testing.T, a []byte, b []byte) {
78
-	if bytes.Compare(a, b) != 0 {
79
-		t.Fatal(a, "!=", b)
80
-	}
81
-}
82
-
83
-func randDevice(t *testing.T) *Device {
84
-	sk, err := newPrivateKey()
85
-	if err != nil {
86
-		t.Fatal(err)
87
-	}
88
-	tun, _ := CreateDummyTUN("dummy")
89
-	logger := NewLogger(LogLevelError, "")
90
-	device := NewDevice(tun, logger)
91
-	device.SetPrivateKey(sk)
92
-	return device
93
-}

Loading…
Cancel
Save