summaryrefslogtreecommitdiff
path: root/src/pkg/net/interface_unix_test.go
blob: 01f609f15e471a24c52be5c1129ab6f74f9f30ce (plain)
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
// Copyright 2013 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux netbsd openbsd

package net

import (
	"os"
	"os/exec"
	"runtime"
	"testing"
	"time"
)

type testInterface struct {
	name         string
	local        string
	remote       string
	setupCmds    []*exec.Cmd
	teardownCmds []*exec.Cmd
}

func (ti *testInterface) setup() error {
	for _, cmd := range ti.setupCmds {
		if err := cmd.Run(); err != nil {
			return err
		}
	}
	return nil
}

func (ti *testInterface) teardown() error {
	for _, cmd := range ti.teardownCmds {
		if err := cmd.Run(); err != nil {
			return err
		}
	}
	return nil
}

func TestPointToPointInterface(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode")
	}
	switch {
	case runtime.GOOS == "darwin":
		t.Skipf("skipping read test on %q", runtime.GOOS)
	}
	if os.Getuid() != 0 {
		t.Skip("skipping test; must be root")
	}

	local, remote := "169.254.0.1", "169.254.0.254"
	ip := ParseIP(remote)
	for i := 0; i < 3; i++ {
		ti := &testInterface{}
		if err := ti.setPointToPoint(5963+i, local, remote); err != nil {
			t.Skipf("test requries external command: %v", err)
		}
		if err := ti.setup(); err != nil {
			t.Fatalf("testInterface.setup failed: %v", err)
		} else {
			time.Sleep(3 * time.Millisecond)
		}
		ift, err := Interfaces()
		if err != nil {
			ti.teardown()
			t.Fatalf("Interfaces failed: %v", err)
		}
		for _, ifi := range ift {
			if ti.name == ifi.Name {
				ifat, err := ifi.Addrs()
				if err != nil {
					ti.teardown()
					t.Fatalf("Interface.Addrs failed: %v", err)
				}
				for _, ifa := range ifat {
					if ip.Equal(ifa.(*IPNet).IP) {
						ti.teardown()
						t.Fatalf("got %v; want %v", ip, local)
					}
				}
			}
		}
		if err := ti.teardown(); err != nil {
			t.Fatalf("testInterface.teardown failed: %v", err)
		} else {
			time.Sleep(3 * time.Millisecond)
		}
	}
}

func TestInterfaceArrivalAndDeparture(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode")
	}
	if os.Getuid() != 0 {
		t.Skip("skipping test; must be root")
	}

	for i := 0; i < 3; i++ {
		ift1, err := Interfaces()
		if err != nil {
			t.Fatalf("Interfaces failed: %v", err)
		}
		ti := &testInterface{}
		if err := ti.setBroadcast(5682 + i); err != nil {
			t.Skipf("test requires external command: %v", err)
		}
		if err := ti.setup(); err != nil {
			t.Fatalf("testInterface.setup failed: %v", err)
		} else {
			time.Sleep(3 * time.Millisecond)
		}
		ift2, err := Interfaces()
		if err != nil {
			ti.teardown()
			t.Fatalf("Interfaces failed: %v", err)
		}
		if len(ift2) <= len(ift1) {
			for _, ifi := range ift1 {
				t.Logf("before: %v", ifi)
			}
			for _, ifi := range ift2 {
				t.Logf("after: %v", ifi)
			}
			ti.teardown()
			t.Fatalf("got %v; want gt %v", len(ift2), len(ift1))
		}
		if err := ti.teardown(); err != nil {
			t.Fatalf("testInterface.teardown failed: %v", err)
		} else {
			time.Sleep(3 * time.Millisecond)
		}
		ift3, err := Interfaces()
		if err != nil {
			t.Fatalf("Interfaces failed: %v", err)
		}
		if len(ift3) >= len(ift2) {
			for _, ifi := range ift2 {
				t.Logf("before: %v", ifi)
			}
			for _, ifi := range ift3 {
				t.Logf("after: %v", ifi)
			}
			t.Fatalf("got %v; want lt %v", len(ift3), len(ift2))
		}
	}
}