This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathupnp_test.go
97 lines (86 loc) · 1.8 KB
/
upnp_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
package upnp
import (
"context"
"sync"
"testing"
"time"
)
// TestConcurrentUPNP tests that several threads calling Discover() concurrently
// succeed.
func TestConcurrentUPNP(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// verify that a router exists
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, err := DiscoverCtx(ctx)
if err != nil {
t.Skip(err)
}
// now try to concurrently Discover() using 20 threads
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, err := DiscoverCtx(ctx)
if err != nil {
t.Error(err)
}
}()
}
wg.Wait()
}
func TestIGD(t *testing.T) {
// connect to router
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
d, err := DiscoverCtx(ctx)
if err != nil {
t.Skip(err)
}
// discover external IP
ip, err := d.ExternalIP()
if err != nil {
t.Fatal(err)
}
t.Log("Your external IP is:", ip)
// forward a port
err = d.Forward(9001, "upnp test")
if err != nil {
t.Fatal(err)
}
// check that port 9001 is now forwarded
forwarded, err := d.IsForwardedTCP(9001)
if err != nil {
t.Fatal(err)
} else if !forwarded {
t.Fatal("port 9001 was not reported as forwarded")
}
// un-forward a port
err = d.Clear(9001)
if err != nil {
t.Fatal(err)
}
// check that port 9001 is no longer forwarded
forwarded, err = d.IsForwardedTCP(9001)
if err != nil {
t.Fatal(err)
} else if forwarded {
t.Fatal("port 9001 should no longer be forwarded")
}
// record router's location
loc := d.Location()
if err != nil {
t.Fatal(err)
}
t.Log("Loc:", loc)
// connect to router directly
d, err = Load(loc)
if err != nil {
t.Fatal(err)
}
}