aboutsummaryrefslogtreecommitdiff
path: root/pkg/ipstack/ipstack_test.go
blob: ddb5453b16c6c4af107fa743240495fcdfa30d34 (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
package ipstack

import (
	"fmt"
	"testing"
	"time"
)

func TestInitialize(t *testing.T) {
	lnxFilePath := "../../doc-example/r2.lnx"
	err := Initialize(lnxFilePath)
	if err != nil {
		t.Error(err)
	}
	fmt.Printf("Interfaces:\n%s\n\n", SprintInterfaces())
	fmt.Printf("Neighbors:\n%s\n", SprintNeighbors())
	fmt.Printf("RoutingTable:\n%s\n", SprintRoutingTable())

	fmt.Println("TestInitialize successful")
	t.Cleanup(func() { CleanUp() })
}

func TestInterfaceUpThenDown(t *testing.T) {
	lnxFilePath := "../../doc-example/r2.lnx"
	err := Initialize(lnxFilePath)
	if err != nil {
		t.Error(err)
	}

	iface, err := GetInterfaceByName("if0")
	if err != nil {
		t.Error(err)
	}

	InterfaceUp(iface)
	if iface.State == false {
		t.Error("iface state should be true")
	}

	fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())

	time.Sleep(5 * time.Millisecond) // allow time to print

	InterfaceDown(iface)
	if iface.State == true {
		t.Error("iface state should be false")
	}

	time.Sleep(5 * time.Millisecond) // allow time to print

	fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())

	fmt.Println("TestInterfaceUpThenDown successful")
	t.Cleanup(func() { CleanUp() })
}

func TestInterfaceUpThenDownTwice(t *testing.T) {
	lnxFilePath := "../../doc-example/r2.lnx"
	err := Initialize(lnxFilePath)
	if err != nil {
		t.Error(err)
	}

	iface, err := GetInterfaceByName("if0")
	if err != nil {
		t.Error(err)
	}

	InterfaceUp(iface)
	if iface.State == false {
		t.Error("iface state should be true")
	}

	fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())

	time.Sleep(5 * time.Millisecond) // allow time to print

	fmt.Println("putting interface down")
	InterfaceDown(iface)
	if iface.State == true {
		t.Error("iface state should be false")
	}

	time.Sleep(3 * time.Millisecond)
	fmt.Println("putting interface back up")

	InterfaceUp(iface)
	if iface.State == false {
		t.Error("iface state should be true")
	}
	time.Sleep(3 * time.Millisecond) // allow time to print

	fmt.Println("putting interface down")
	InterfaceDown(iface)
	if iface.State == true {
		t.Error("iface state should be false")
	}

	time.Sleep(5 * time.Millisecond) // allow time to print

	fmt.Printf("Interfaces:\n%s\n", SprintInterfaces())

	fmt.Println("TestInterfaceUpThenDownTwice successful")
	t.Cleanup(func() { CleanUp() })
}