-
Notifications
You must be signed in to change notification settings - Fork 0
/
fritzbox_test.go
98 lines (80 loc) · 2.13 KB
/
fritzbox_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
package fritzbox
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"testing"
"gopkg.in/xmlpath.v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testfile(name string) *xmlpath.Node {
file, err := os.Open(fmt.Sprintf("testdata/%s.xml", name))
if err != nil {
panic(err)
}
root, _ := xmlpath.Parse(file)
return root
}
func TestIsFault(t *testing.T) {
assert.EqualError(t, checkFault(testfile("error")), "UPNP failed: Invalid Action")
}
func TestNonFault(t *testing.T) {
assert.Nil(t, checkFault(testfile("address/valid")))
}
func checkFault(root *xmlpath.Node) error {
if faultPath.Exists(root) {
if s, ok := faultDescription.String(root); ok {
return errors.New("UPNP failed: " + s)
}
return errors.New("UPNP failed with unknown error")
}
return nil
}
func TestClient(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(err)
defer listener.Close()
mux := http.NewServeMux()
mux.HandleFunc("/igdupnp/control/WANCommonIFC1", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/linkspeed/valid.xml")
})
srv := &http.Server{Handler: mux}
go func() {
if e := srv.Serve(listener); !errors.Is(e, http.ErrServerClosed) {
log.Printf("unexpected HTTP server shutdown: %v", e)
}
}()
defer srv.Close()
client := Client{
Endpoint: listener.Addr().String(),
Context: context.Background(),
}
// should succeed
linkSpeed, err := client.GetLinkspeed()
assert.NoError(err)
if assert.NotNil(linkSpeed) {
assert.Equal(51392000, linkSpeed.DownlinkBitRate)
}
// should fail (no handler)
_, err = client.GetPublicAddress()
if assert.Error(err) {
assert.Contains(err.Error(), "unexpected status code 404 for http://127")
}
}
func TestClientInvalidAddress(t *testing.T) {
assert := assert.New(t)
client := NewClientFromIP(net.IP{127, 0, 0, 2})
assert.Equal("127.0.0.2:49000", client.Endpoint)
// should succeed
_, err := client.GetLinkspeed()
if assert.Error(err) {
assert.Contains(err.Error(), "dial tcp 127.0.0.2:49000: connect: connection refused")
}
}