forked from go-routeros/routeros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
123 lines (110 loc) · 2.77 KB
/
client_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
package routeros
import (
"flag"
"testing"
)
var (
routerosAddress = flag.String("routeros.address", "", "RouterOS address:port")
routerosUsername = flag.String("routeros.username", "admin", "RouterOS user name")
routerosPassword = flag.String("routeros.password", "admin", "RouterOS password")
)
type liveTest struct {
*testing.T
c *Client
}
func newLiveTest(t *testing.T) *liveTest {
tt := &liveTest{T: t}
tt.connect()
return tt
}
func (t *liveTest) connect() {
if *routerosAddress == "" {
t.Skip("Flag -routeros.address not set")
}
var err error
t.c, err = Dial(*routerosAddress, *routerosUsername, *routerosPassword)
if err != nil {
t.Fatal(err)
}
}
func (t *liveTest) run(sentence ...string) *Reply {
t.Logf("Run: %#q", sentence)
r, err := t.c.RunArgs(sentence)
if err != nil {
t.Fatal(err)
}
t.Logf("Reply: %s", r)
return r
}
func (t *liveTest) getUptime() {
r := t.run("/system/resource/print")
if len(r.Re) != 1 {
t.Fatalf("len(!re)=%d; want 1", len(r.Re))
}
_, ok := r.Re[0].Map["uptime"]
if !ok {
t.Fatal("Missing uptime")
}
}
func TestRunSync(tt *testing.T) {
t := newLiveTest(tt)
defer t.c.Close()
t.getUptime()
}
func TestRunAsync(tt *testing.T) {
t := newLiveTest(tt)
defer t.c.Close()
t.c.Async()
t.getUptime()
}
func TestRunError(tt *testing.T) {
t := newLiveTest(tt)
defer t.c.Close()
for i, sentence := range [][]string{
{"/xxx"},
{"/ip/address/add", "=address=127.0.0.2/32", "=interface=xxx"},
} {
t.Logf("#%d: Run: %#q", i, sentence)
_, err := t.c.RunArgs(sentence)
if err == nil {
t.Error("Success; want error from RouterOS device trying to run an invalid command")
}
}
}
func TestDialInvalidPort(t *testing.T) {
c, err := Dial("127.0.0.1:xxx", "x", "x")
if err == nil {
c.Close()
t.Fatalf("Dial succeeded; want error")
}
if err.Error() != "dial tcp: lookup tcp/xxx: getaddrinfow: The specified class was not found." &&
err.Error() != "dial tcp: lookup tcp/xxx: Servname not supported for ai_socktype" {
t.Fatal(err)
}
}
func TestDialTLSInvalidPort(t *testing.T) {
c, err := DialTLS("127.0.0.1:xxx", "x", "x", nil)
if err == nil {
c.Close()
t.Fatalf("Dial succeeded; want error")
}
if err.Error() != "dial tcp: lookup tcp/xxx: getaddrinfow: The specified class was not found." &&
err.Error() != "dial tcp: lookup tcp/xxx: Servname not supported for ai_socktype" {
t.Fatal(err)
}
}
func TestInvalidLogin(t *testing.T) {
if *routerosAddress == "" {
t.Skip("Flag -routeros.address not set")
}
var err error
c, err := Dial(*routerosAddress, "xxx", "APasswordThatWillNeverExistir")
if err == nil {
c.Close()
t.Fatalf("Dial succeeded; want error")
}
if err.Error() != "from RouterOS device: cannot log in" &&
err.Error() != "from RouterOS device: invalid user name or password (6)" {
t.Fatal(err)
}
}