forked from aeden/traceroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceroute_test.go
51 lines (45 loc) · 1.07 KB
/
traceroute_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
package traceroute
import (
"fmt"
"testing"
)
func printHop(hop TracerouteHop) {
fmt.Printf("%-3d %v (%v) %v\n", hop.TTL, hop.HostOrAddressString(), hop.AddressString(), hop.ElapsedTime)
}
func TestTraceroute(t *testing.T) {
fmt.Println("Testing synchronous traceroute\n")
out, err := Traceroute("google.com", new(TracerouteOptions))
if err == nil {
if len(out.Hops) == 0 {
t.Errorf("TestTraceroute failed. Expected at least one hop")
}
} else {
t.Errorf("TestTraceroute failed due to an error: %v", err)
}
for _, hop := range out.Hops {
printHop(hop)
}
fmt.Println()
}
func TestTraceouteChannel(t *testing.T) {
fmt.Println("Testing asynchronous traceroute\n")
c := make(chan TracerouteHop, 0)
go func() {
for {
hop, ok := <-c
if !ok {
fmt.Println()
return
}
printHop(hop)
}
}()
out, err := Traceroute("google.com", new(TracerouteOptions), c)
if err == nil {
if len(out.Hops) == 0 {
t.Errorf("TestTracerouteChannel failed. Expected at least one hop")
}
} else {
t.Errorf("TestTraceroute failed due to an error: %v", err)
}
}