forked from PaloAltoNetworks/pango
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
231 lines (195 loc) · 3.72 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package pango
import (
"bytes"
"encoding/xml"
"log"
"os"
"strings"
"testing"
"github.com/PaloAltoNetworks/pango/commit"
"github.com/PaloAltoNetworks/pango/testdata"
)
func init() {
log.SetFlags(0)
}
// rl restores the logger output to the default log location.
func rl() {
log.SetOutput(os.Stderr)
}
func TestClientStringerHidesPasswords(t *testing.T) {
c := &Client{
Hostname: "foo",
Username: "admin",
Password: "secret",
}
if strings.Index(c.String(), "secret") != -1 {
t.Fail()
}
}
func TestClientStringerHidesApiKey(t *testing.T) {
c := &Client{
Hostname: "foo",
Username: "admin",
ApiKey: "secret",
}
if strings.Index(c.String(), "secret") != -1 {
t.Fail()
}
}
func TestLogActionDisabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogQuiet,
}
c.LogAction("no")
if buf.String() != "" {
t.Fail()
}
}
func TestLogActionEnabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogAction,
}
c.LogAction("yes")
s := buf.String()
if s != "yes\n" {
t.Fail()
}
}
func TestLogQueryDisabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogQuiet,
}
c.LogQuery("no")
if buf.String() != "" {
t.Fail()
}
}
func TestLogQueryEnabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogQuery,
}
c.LogQuery("yes")
s := buf.String()
if s != "yes\n" {
t.Fail()
}
}
func TestLogOpDisabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogQuiet,
}
c.LogOp("no")
if buf.String() != "" {
t.Fail()
}
}
func TestLogOpEnabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogOp,
}
c.LogOp("yes")
s := buf.String()
if s != "yes\n" {
t.Fail()
}
}
func TestLogUidDisabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogQuiet,
}
c.LogUid("no")
if buf.String() != "" {
t.Fail()
}
}
func TestLogUidEnabled(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer rl()
c := &Client{
Logging: LogUid,
}
c.LogUid("yes")
s := buf.String()
if s != "yes\n" {
t.Fail()
}
}
func TestRetrieveApiKey(t *testing.T) {
c := &Client{}
c.rb = [][]byte{
[]byte(testdata.ApiKeyXml),
}
if err := c.Initialize(); err != nil {
t.Errorf("Initialize failed: %s", err)
return
}
err := c.RetrieveApiKey()
if err != nil {
t.Errorf("Failed to retrieve api key: %s", err)
} else if c.ApiKey != "secret" {
t.Fail()
}
}
func TestAsStringWithString(t *testing.T) {
expected := "foobar"
val, err := asString(expected, true)
if err != nil {
t.Errorf("Failed asString: %s", err)
} else if val != expected {
t.Errorf("Expected:%s got:%s", expected, val)
}
}
type StringerCheck struct{}
func (o StringerCheck) String() string { return "hello" }
func TestAsStringWithStringer(t *testing.T) {
x := StringerCheck{}
expected := x.String()
val, err := asString(x, true)
if err != nil {
t.Errorf("Failed asString: %s", err)
} else if val != expected {
t.Errorf("Expected:%s got:%s", expected, val)
}
}
func TestAsStringWithElementer(t *testing.T) {
x := commit.FirewallCommit{
Description: "hello",
}
expected, err := xml.Marshal(x.Element())
if err != nil {
t.Errorf("Failed to marshal firewall commit: %s", err)
}
val, err := asString(x, true)
if err != nil {
t.Errorf("Failed asString: %s", err)
} else if val != string(expected) {
t.Errorf("Expected:%s got:%s", expected, val)
}
}
func TestAsStringWithNil(t *testing.T) {
if _, err := asString(nil, true); err == nil {
t.Errorf("asString() returned no error on nil input")
}
}