This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstate_test.go
178 lines (157 loc) · 3.49 KB
/
state_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
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
package zerotrace
import (
"net"
"testing"
"time"
)
var (
dummyAddr = net.ParseIP("1.2.3.4")
)
func failOnErr(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("Expected no error but got: %v", err)
}
}
func TestIsAnswered(t *testing.T) {
p := &tracePkt{}
if p.isAnswered() {
t.Fatal("Expected empty trace packet to be un-answered.")
}
now := time.Now().UTC()
p.sent = now
p.recvd = now
if !p.isAnswered() {
t.Fatal("Expected answered trace packet to be answered.")
}
}
func TestNewTrState(t *testing.T) {
s := newTrState(dummyAddr)
if s.tracePkts == nil {
t.Fatal("Map in trState struct uninitialized.")
}
}
func TestAddTracePkt(t *testing.T) {
s := newTrState(dummyAddr)
s.addTracePkt(&tracePkt{
ttl: 1,
ipID: 1,
sent: time.Now().UTC(),
})
expected := 1
if len(s.tracePkts) != expected {
t.Fatalf("Expected %d recorded packets but got %d.",
expected, len(s.tracePkts))
}
}
func TestIsFinished(t *testing.T) {
s := newTrState(dummyAddr)
now := time.Now().UTC()
p := &tracePkt{
ttl: 1,
ipID: 1,
sent: now,
}
s.addTracePkt(p)
if s.isFinished() {
t.Fatal("Expected traceroute to be unfinished.")
}
p.sent = now.Add(-reqTimeout)
if !s.isFinished() {
t.Fatal("Expected traceroute to be finished.")
}
p.recvd = now
if !s.isFinished() {
t.Fatal("Expected traceroute to be finished.")
}
}
func TestSummary(t *testing.T) {
s := newTrState(dummyAddr)
if len(s.summary()) == 0 {
t.Fatal("Expected string summary of traceroute.")
}
now := time.Now().UTC()
s.addTracePkt(&tracePkt{
ipID: 1,
ttl: 1,
sent: now,
recvd: now,
})
if len(s.summary()) == 0 {
t.Fatal("Expected string summary of traceroute.")
}
}
func TestCalcRTT(t *testing.T) {
var (
err error
rtt time.Duration
s = newTrState(dummyAddr)
now = time.Now().UTC()
)
expectedRTT := time.Second
s.addTracePkt(&tracePkt{
ttl: 1,
ipID: 1,
sent: now.Add(-expectedRTT),
recvd: now,
})
rtt, err = s.calcRTT()
failOnErr(t, err)
if rtt != expectedRTT {
t.Fatalf("Expected RTT to be %s but got %s.", expectedRTT, rtt)
}
// Add a trace packet with an identical TTL but a lower RTT.
expectedRTT = time.Millisecond * 500
s.addTracePkt(&tracePkt{
ttl: 1,
ipID: 2,
sent: now.Add(-expectedRTT),
recvd: now,
})
rtt, err = s.calcRTT()
failOnErr(t, err)
if rtt != expectedRTT {
t.Fatalf("Expected RTT to be %s but got %s.", expectedRTT, rtt)
}
// Add a trace packet with a higher TTL (i.e., it got closer to the
// target).
expectedRTT = time.Second * 2
s.addTracePkt(&tracePkt{
ttl: 2,
ipID: 2,
sent: now.Add(-expectedRTT),
recvd: now,
})
rtt, err = s.calcRTT()
failOnErr(t, err)
if rtt != expectedRTT {
t.Fatalf("Expected RTT to be %s but got %s.", expectedRTT, rtt)
}
// Add an unanswered packet and make sure that it doesn't affect the RTT.
s.addTracePkt(&tracePkt{
ttl: 3,
ipID: 3,
sent: now.Add(-time.Second * 10),
})
rtt, err = s.calcRTT()
failOnErr(t, err)
if rtt != expectedRTT {
t.Fatalf("Expected RTT to be %s but got %s.", expectedRTT, rtt)
}
// Add a packet whose TTL is lower than the existing ones but it got
// answered by the destination itself, so it should be used to calculate
// the RTT.
expectedRTT = time.Second * 3
s.addTracePkt(&tracePkt{
ttl: 1,
ipID: 4,
sent: now.Add(-expectedRTT),
recvd: now,
recvdFrom: dummyAddr,
})
rtt, err = s.calcRTT()
failOnErr(t, err)
if rtt != expectedRTT {
t.Fatalf("Expected RTT to be %s but got %s.", expectedRTT, rtt)
}
}