Debian Codegraph @

netrc/netrc_test.go

raw symbols SCIP
1 // Copyright © 2010 Fazlul Shahriar <[email protected]> and
2 // Copyright © 2014 Blake Gentry <[email protected]>.
3 // See LICENSE file for license details.
4
5 package netrc
6
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "io/ioutil"
12 "os"
13 "strings"
14 "testing"
15 )
16
17 var expectedMachines = []*Machine{
18 &Machine{Name: "mail.google.com", Login: "[email protected]", Password: "somethingSecret", Account: "justagmail"},
19 &Machine{Name: "ray", Login: "demo", Password: "mypassword", Account: ""},
20 &Machine{Name: "ray", Login: "demo1", Password: "demo1_password", Account: ""},
21 &Machine{Name: "weirdlogin", Login: "uname", Password: "pass#pass", Account: ""},
22 &Machine{Name: "google.com", Login: "[email protected]", Password: "secure"},
23 &Machine{Name: "", Login: "anonymous", Password: "[email protected]", Account: ""},
24 }
25 var expectedMacros = Macros{
26 "allput": "put src/*",
27 "allput2": " put src/*\nput src2/*",
28 }
29
30 func eqMachine(a *Machine, b *Machine) bool {
31 return a.Name == b.Name &&
32 a.Login == b.Login &&
33 a.Password == b.Password &&
34 a.Account == b.Account
35 }
36
37 func testExpected(n *Netrc, t *testing.T) {
38 if len(expectedMachines) != len(n.machines) {
39 t.Errorf("expected %d machines, got %d", len(expectedMachines), len(n.machines))
40 } else {
41 for i, e := range expectedMachines {
42 if !eqMachine(e, n.machines[i]) {
43 t.Errorf("bad machine; expected %v, got %v\n", e, n.machines[i])
44 }
45 }
46 }
47
48 if len(expectedMacros) != len(n.macros) {
49 t.Errorf("expected %d macros, got %d", len(expectedMacros), len(n.macros))
50 } else {
51 for k, v := range expectedMacros {
52 if v != n.macros[k] {
53 t.Errorf("bad macro for %s; expected %q, got %q\n", k, v, n.macros[k])
54 }
55 }
56 }
57 }
58
59 var newTokenTests = []struct {
60 rawkind string
61 tkind tkType
62 }{
63 {"machine", tkMachine},
64 {"\n\n\tmachine", tkMachine},
65 {"\n machine", tkMachine},
66 {"default", tkDefault},
67 {"login", tkLogin},
68 {"password", tkPassword},
69 {"account", tkAccount},
70 {"macdef", tkMacdef},
71 {"\n # comment stuff ", tkComment},
72 {"\n # I am another comment", tkComment},
73 {"\n\t\n ", tkWhitespace},
74 }
75
76 var newTokenInvalidTests = []string{
77 " junk",
78 "sdfdsf",
79 "account#unspaced comment",
80 }
81
82 func TestNewToken(t *testing.T) {
83 for _, tktest := range newTokenTests {
84 tok, err := newToken([]byte(tktest.rawkind))
85 if err != nil {
86 t.Fatal(err)
87 }
88 if tok.kind != tktest.tkind {
89 t.Errorf("expected tok.kind %d, got %d", tktest.tkind, tok.kind)
90 }
91 if string(tok.rawkind) != tktest.rawkind {
92 t.Errorf("expected tok.rawkind %q, got %q", tktest.rawkind, string(tok.rawkind))
93 }
94 }
95
96 for _, tktest := range newTokenInvalidTests {
97 _, err := newToken([]byte(tktest))
98 if err == nil {
99 t.Errorf("expected error with %q, got none", tktest)
100 }
101 }
102 }
103
104 func TestParse(t *testing.T) {
105 r := netrcReader("examples/good.netrc", t)
106 n, err := Parse(r)
107 if err != nil {
108 t.Fatal(err)
109 }
110 testExpected(n, t)
111 }
112
113 func TestParseFile(t *testing.T) {
114 n, err := ParseFile("examples/good.netrc")
115 if err != nil {
116 t.Fatal(err)
117 }
118 testExpected(n, t)
119
120 _, err = ParseFile("examples/bad_default_order.netrc")
121 if err == nil {
122 t.Error("expected an error parsing bad_default_order.netrc, got none")
123 } else if !err.(*Error).BadDefaultOrder() {
124 t.Error("expected BadDefaultOrder() to be true, got false")
125 }
126
127 _, err = ParseFile("examples/this_file_doesnt_exist.netrc")
128 if err == nil {
129 t.Error("expected an error loading this_file_doesnt_exist.netrc, got none")
130 } else if _, ok := err.(*os.PathError); !ok {
131 t.Errorf("expected *os.Error, got %v", err)
132 }
133 }
134
135 func TestFindMachine(t *testing.T) {
136 m, err := FindMachine("examples/good.netrc", "ray", "")
137 if err != nil {
138 t.Fatal(err)
139 }
140 if !eqMachine(m, expectedMachines[1]) {
141 t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[1], m)
142 }
143 if m.IsDefault() {
144 t.Errorf("expected m.IsDefault() to be false")
145 }
146
147 m, err = FindMachine("examples/good.netrc", "ray", "demo1")
148 if err != nil {
149 t.Fatal(err)
150 }
151 if !eqMachine(m, expectedMachines[2]) {
152 t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[2], m)
153 }
154 if m.IsDefault() {
155 t.Errorf("expected m.IsDefault() to be false")
156 }
157
158 m, err = FindMachine("examples/good.netrc", "ray", "non.existent")
159 if err != nil {
160 t.Fatal(err)
161 }
162 if !eqMachine(m, expectedMachines[5]) {
163 t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[5], m)
164 }
165 if !m.IsDefault() {
166 t.Errorf("expected m.IsDefault() to be true")
167 }
168
169 m, err = FindMachine("examples/good.netrc", "non.existent", "")
170 if err != nil {
171 t.Fatal(err)
172 }
173 if !eqMachine(m, expectedMachines[5]) {
174 t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[5], m)
175 }
176 if !m.IsDefault() {
177 t.Errorf("expected m.IsDefault() to be true")
178 }
179 }
180
181 func TestNetrcFindMachine(t *testing.T) {
182 n, err := ParseFile("examples/good.netrc")
183 if err != nil {
184 t.Fatal(err)
185 }
186
187 m := n.FindMachine("ray", "")
188 if !eqMachine(m, expectedMachines[1]) {
189 t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[1], m)
190 }
191 if m.IsDefault() {
192 t.Errorf("expected def to be false")
193 }
194
195 n = &Netrc{}
196 m = n.FindMachine("nonexistent", "")
197 if m != nil {
198 t.Errorf("expected nil, got %v", m)
199 }
200 }
201
202 func TestMarshalText(t *testing.T) {
203 // load up expected netrc Marshal output
204 expected, err := ioutil.ReadAll(netrcReader("examples/good.netrc", t))
205 if err != nil {
206 t.Fatal(err)
207 }
208
209 n, err := ParseFile("examples/good.netrc")
210 if err != nil {
211 t.Fatal(err)
212 }
213
214 result, err := n.MarshalText()
215 if err != nil {
216 t.Fatal(err)
217 }
218 if string(result) != string(expected) {
219 t.Errorf("expected:\n%q\ngot:\n%q", string(expected), string(result))
220 }
221
222 // make sure tokens w/ no value are not serialized
223 m := n.FindMachine("mail.google.com", "")
224 m.UpdatePassword("")
225 result, err = n.MarshalText()
226 if err != nil {
227 t.Fatal(err)
228 }
229 if strings.Contains(string(result), "\tpassword \n") {
230 fmt.Println(string(result))
231 t.Errorf("expected zero-value password token to not be serialzed")
232 }
233 }
234
235 var newMachineTests = []struct {
236 name string
237 login string
238 password string
239 account string
240 }{
241 {"heroku.com", "[email protected]", "octocatdodgeballchampions", "2011+2013"},
242 {"bgentry.io", "[email protected]", "noacct", ""},
243 {"github.io", "[email protected]", "", "acctwithnopass"},
244 {"someotherapi.com", "", "passonly", ""},
245 }
246
247 func TestNewMachine(t *testing.T) {
248 n, err := ParseFile("examples/good.netrc")
249 if err != nil {
250 t.Fatal(err)
251 }
252 testNewMachine(t, n)
253 n = &Netrc{}
254 testNewMachine(t, n)
255
256 // make sure that tokens without a value are not serialized at all
257 for _, test := range newMachineTests {
258 n = &Netrc{}
259 _ = n.NewMachine(test.name, test.login, test.password, test.account)
260
261 bodyb, _ := n.MarshalText()
262 body := string(bodyb)
263
264 // ensure desired values are present when they should be
265 if !strings.Contains(body, "machine") {
266 t.Errorf("NewMachine() %s missing keyword 'machine'", test.name)
267 }
268 if !strings.Contains(body, test.name) {
269 t.Errorf("NewMachine() %s missing value %q", test.name, test.name)
270 }
271 if test.login != "" && !strings.Contains(body, "login "+test.login) {
272 t.Errorf("NewMachine() %s missing value %q", test.name, "login "+test.login)
273 }
274 if test.password != "" && !strings.Contains(body, "password "+test.password) {
275 t.Errorf("NewMachine() %s missing value %q", test.name, "password "+test.password)
276 }
277 if test.account != "" && !strings.Contains(body, "account "+test.account) {
278 t.Errorf("NewMachine() %s missing value %q", test.name, "account "+test.account)
279 }
280
281 // ensure undesired values are not present when they shouldn't be
282 if test.login == "" && strings.Contains(body, "login") {
283 t.Errorf("NewMachine() %s contains unexpected value %q", test.name, "login")
284 }
285 if test.password == "" && strings.Contains(body, "password") {
286 t.Errorf("NewMachine() %s contains unexpected value %q", test.name, "password")
287 }
288 if test.account == "" && strings.Contains(body, "account") {
289 t.Errorf("NewMachine() %s contains unexpected value %q", test.name, "account")
290 }
291 }
292 }
293
294 func testNewMachine(t *testing.T, n *Netrc) {
295 for _, test := range newMachineTests {
296 mcount := len(n.machines)
297 // sanity check
298 bodyb, _ := n.MarshalText()
299 body := string(bodyb)
300 for _, value := range []string{test.name, test.login, test.password, test.account} {
301 if value != "" && strings.Contains(body, value) {
302 t.Errorf("MarshalText() before NewMachine() contained unexpected %q", value)
303 }
304 }
305
306 // test prefix for machine token
307 prefix := "\n"
308 if len(n.tokens) == 0 {
309 prefix = ""
310 }
311
312 m := n.NewMachine(test.name, test.login, test.password, test.account)
313 if m == nil {
314 t.Fatalf("NewMachine() returned nil")
315 }
316
317 if len(n.machines) != mcount+1 {
318 t.Errorf("n.machines count expected %d, got %d", mcount+1, len(n.machines))
319 }
320 // check values
321 if m.Name != test.name {
322 t.Errorf("m.Name expected %q, got %q", test.name, m.Name)
323 }
324 if m.Login != test.login {
325 t.Errorf("m.Login expected %q, got %q", test.login, m.Login)
326 }
327 if m.Password != test.password {
328 t.Errorf("m.Password expected %q, got %q", test.password, m.Password)
329 }
330 if m.Account != test.account {
331 t.Errorf("m.Account expected %q, got %q", test.account, m.Account)
332 }
333 // check tokens
334 checkToken(t, "nametoken", m.nametoken, tkMachine, prefix+"machine", test.name)
335 checkToken(t, "logintoken", m.logintoken, tkLogin, "\n\tlogin", test.login)
336 checkToken(t, "passtoken", m.passtoken, tkPassword, "\n\tpassword", test.password)
337 checkToken(t, "accounttoken", m.accounttoken, tkAccount, "\n\taccount", test.account)
338 // check marshal output
339 bodyb, _ = n.MarshalText()
340 body = string(bodyb)
341 for _, value := range []string{test.name, test.login, test.password, test.account} {
342 if !strings.Contains(body, value) {
343 t.Errorf("MarshalText() after NewMachine() did not include %q as expected", value)
344 }
345 }
346 }
347 }
348
349 func checkToken(t *testing.T, name string, tok *token, kind tkType, rawkind, value string) {
350 if tok == nil {
351 t.Errorf("%s not defined", name)
352 return
353 }
354 if tok.kind != kind {
355 t.Errorf("%s expected kind %d, got %d", name, kind, tok.kind)
356 }
357 if string(tok.rawkind) != rawkind {
358 t.Errorf("%s expected rawkind %q, got %q", name, rawkind, string(tok.rawkind))
359 }
360 if tok.value != value {
361 t.Errorf("%s expected value %q, got %q", name, value, tok.value)
362 }
363 if tok.value != value {
364 t.Errorf("%s expected value %q, got %q", name, value, tok.value)
365 }
366 }
367
368 func TestNewMachineGoesBeforeDefault(t *testing.T) {
369 n, err := ParseFile("examples/good.netrc")
370 if err != nil {
371 t.Fatal(err)
372 }
373 m := n.NewMachine("mymachine", "mylogin", "mypassword", "myaccount")
374 if m2 := n.machines[len(n.machines)-2]; m2 != m {
375 t.Errorf("expected machine %v, got %v", m, m2)
376 }
377 }
378
379 func TestRemoveMachine(t *testing.T) {
380 n, err := ParseFile("examples/good.netrc")
381 if err != nil {
382 t.Fatal(err)
383 }
384
385 tests := []struct {
386 name string
387 login string
388 }{
389 {"mail.google.com", ""},
390 {"weirdlogin", "uname"},
391 }
392
393 for _, test := range tests {
394 name := test.name
395 loginName := test.login
396 mcount := len(n.machines)
397 // sanity check
398 m := n.FindMachine(name, loginName)
399 if m == nil {
400 t.Fatalf("machine %q not found", name)
401 }
402 if m.IsDefault() {
403 t.Fatalf("expected machine %q, got default instead", name)
404 }
405 n.RemoveMachine(name, loginName)
406
407 if len(n.machines) != mcount-1 {
408 t.Errorf("n.machines count expected %d, got %d", mcount-1, len(n.machines))
409 }
410
411 // make sure Machine is no longer returned by FindMachine()
412 if m2 := n.FindMachine(name, loginName); m2 != nil && !m2.IsDefault() {
413 t.Errorf("Machine %q not removed from Machines list", name)
414 }
415
416 // make sure tokens are not present in tokens list
417 for _, token := range []*token{m.nametoken, m.logintoken, m.passtoken, m.accounttoken} {
418 if token != nil {
419 for _, tok2 := range n.tokens {
420 if tok2 == token {
421 t.Errorf("token not removed from tokens list: %v", token)
422 break
423 }
424 }
425 }
426 }
427
428 bodyb, _ := n.MarshalText()
429 body := string(bodyb)
430 for _, value := range []string{m.Name, m.Login, m.Password, m.Account} {
431 if value != "" && strings.Contains(body, value) {
432 t.Errorf("MarshalText() after RemoveMachine() contained unexpected %q", value)
433 }
434 }
435 }
436 }
437
438 func TestUpdateLogin(t *testing.T) {
439 n, err := ParseFile("examples/good.netrc")
440 if err != nil {
441 t.Fatal(err)
442 }
443
444 tests := []struct {
445 exists bool
446 name string
447 oldlogin string
448 newlogin string
449 }{
450 {true, "mail.google.com", "[email protected]", "[email protected]"},
451 {false, "heroku.com", "", "[email protected]"},
452 }
453
454 bodyb, _ := n.MarshalText()
455 body := string(bodyb)
456 for _, test := range tests {
457 if strings.Contains(body, test.newlogin) {
458 t.Errorf("MarshalText() before UpdateLogin() contained unexpected %q", test.newlogin)
459 }
460 }
461
462 for _, test := range tests {
463 m := n.FindMachine(test.name, "")
464 if m.IsDefault() == test.exists {
465 t.Errorf("expected machine %s to not exist, but it did", test.name)
466 } else {
467 if !test.exists {
468 m = n.NewMachine(test.name, test.newlogin, "", "")
469 }
470 if m == nil {
471 t.Errorf("machine %s was nil", test.name)
472 continue
473 }
474 m.UpdateLogin(test.newlogin)
475 m := n.FindMachine(test.name, "")
476 if m.Login != test.newlogin {
477 t.Errorf("expected new login %q, got %q", test.newlogin, m.Login)
478 }
479 if m.logintoken.value != test.newlogin {
480 t.Errorf("expected m.logintoken %q, got %q", test.newlogin, m.logintoken.value)
481 }
482 }
483 }
484
485 bodyb, _ = n.MarshalText()
486 body = string(bodyb)
487 for _, test := range tests {
488 if test.exists && strings.Contains(body, test.oldlogin) {
489 t.Errorf("MarshalText() after UpdateLogin() contained unexpected %q", test.oldlogin)
490 }
491 if !strings.Contains(body, test.newlogin) {
492 t.Errorf("MarshalText after UpdatePassword did not contain %q as expected", test.newlogin)
493 }
494 }
495 }
496
497 func TestUpdatePassword(t *testing.T) {
498 n, err := ParseFile("examples/good.netrc")
499 if err != nil {
500 t.Fatal(err)
501 }
502
503 tests := []struct {
504 exists bool
505 name string
506 oldpassword string
507 newpassword string
508 }{
509 {true, "ray", "mypassword", "supernewpass"},
510 {false, "heroku.com", "", "octocatdodgeballchampions"},
511 }
512
513 bodyb, _ := n.MarshalText()
514 body := string(bodyb)
515 for _, test := range tests {
516 if test.exists && !strings.Contains(body, test.oldpassword) {
517 t.Errorf("MarshalText() before UpdatePassword() did not include %q as expected", test.oldpassword)
518 }
519 if strings.Contains(body, test.newpassword) {
520 t.Errorf("MarshalText() before UpdatePassword() contained unexpected %q", test.newpassword)
521 }
522 }
523
524 for _, test := range tests {
525 m := n.FindMachine(test.name, "")
526 if m.IsDefault() == test.exists {
527 t.Errorf("expected machine %s to not exist, but it did", test.name)
528 } else {
529 if !test.exists {
530 m = n.NewMachine(test.name, "", test.newpassword, "")
531 }
532 if m == nil {
533 t.Errorf("machine %s was nil", test.name)
534 continue
535 }
536 m.UpdatePassword(test.newpassword)
537 m = n.FindMachine(test.name, "")
538 if m.Password != test.newpassword {
539 t.Errorf("expected new password %q, got %q", test.newpassword, m.Password)
540 }
541 if m.passtoken.value != test.newpassword {
542 t.Errorf("expected m.passtoken %q, got %q", test.newpassword, m.passtoken.value)
543 }
544 }
545 }
546
547 bodyb, _ = n.MarshalText()
548 body = string(bodyb)
549 for _, test := range tests {
550 if test.exists && strings.Contains(body, test.oldpassword) {
551 t.Errorf("MarshalText() after UpdatePassword() contained unexpected %q", test.oldpassword)
552 }
553 if !strings.Contains(body, test.newpassword) {
554 t.Errorf("MarshalText() after UpdatePassword() did not contain %q as expected", test.newpassword)
555 }
556 }
557 }
558
559 func TestNewFile(t *testing.T) {
560 var n Netrc
561
562 result, err := n.MarshalText()
563 if err != nil {
564 t.Fatal(err)
565 }
566 if string(result) != "" {
567 t.Errorf("expected empty result=\"\", got %q", string(result))
568 }
569
570 n.NewMachine("netrctest.heroku.com", "auser", "apassword", "")
571
572 result, err = n.MarshalText()
573 if err != nil {
574 t.Fatal(err)
575 }
576 expected := `machine netrctest.heroku.com
577 login auser
578 password apassword`
579
580 if string(result) != expected {
581 t.Errorf("expected result:\n%q\ngot:\n%q", expected, string(result))
582 }
583 }
584
585 func netrcReader(filename string, t *testing.T) io.Reader {
586 b, err := ioutil.ReadFile(filename)
587 if err != nil {
588 t.Fatal(err)
589 }
590 return bytes.NewReader(b)
591 }