Skip to content

Commit b377a6e

Browse files
committed
make UpdatePassword() and UpdateLogin() work w/ tests
1 parent b634610 commit b377a6e

File tree

2 files changed

+169
-27
lines changed

2 files changed

+169
-27
lines changed

netrc/netrc.go

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,58 @@ func (n *Netrc) NewMachine(name, login, password, account string) *Machine {
8888
Account: account,
8989

9090
nametoken: &token{
91-
kind: tkMachine,
92-
rawkind: []byte("\nmachine"),
93-
value: name,
91+
kind: tkMachine,
92+
rawkind: []byte("\nmachine"),
93+
value: name,
94+
rawvalue: []byte(" " + name),
9495
},
9596
logintoken: &token{
96-
kind: tkLogin,
97-
rawkind: []byte("\n\tlogin"),
98-
value: login,
97+
kind: tkLogin,
98+
rawkind: []byte("\n\tlogin"),
99+
value: login,
100+
rawvalue: []byte(" " + login),
99101
},
100102
passtoken: &token{
101-
kind: tkPassword,
102-
rawkind: []byte("\n\tpassword"),
103-
value: password,
103+
kind: tkPassword,
104+
rawkind: []byte("\n\tpassword"),
105+
value: password,
106+
rawvalue: []byte(" " + password),
104107
},
105108
accounttoken: &token{
106-
kind: tkAccount,
107-
rawkind: []byte("\n\taccount"),
108-
value: account,
109+
kind: tkAccount,
110+
rawkind: []byte("\n\taccount"),
111+
value: account,
112+
rawvalue: []byte(" " + account),
109113
},
110114
}
115+
n.insertMachineTokensBeforeDefault(m)
111116
n.machines = append(n.machines, m)
112117
return m
113118
}
114119

120+
func (n *Netrc) insertMachineTokensBeforeDefault(m *Machine) {
121+
newtokens := []*token{m.nametoken}
122+
if m.logintoken.value != "" {
123+
newtokens = append(newtokens, m.logintoken)
124+
}
125+
if m.passtoken.value != "" {
126+
newtokens = append(newtokens, m.passtoken)
127+
}
128+
if m.accounttoken.value != "" {
129+
newtokens = append(newtokens, m.accounttoken)
130+
}
131+
for i := range n.tokens {
132+
if n.tokens[i].kind == tkDefault {
133+
// found the default, now insert tokens before it
134+
n.tokens = append(n.tokens[:i], append(newtokens, n.tokens[i:]...)...)
135+
return
136+
}
137+
}
138+
// didn't find a default, just add the newtokens to the end
139+
n.tokens = append(n.tokens, newtokens...)
140+
return
141+
}
142+
115143
// Machine contains information about a remote machine.
116144
type Machine struct {
117145
Name string
@@ -127,21 +155,29 @@ type Machine struct {
127155

128156
// UpdatePassword sets the password for the Machine m.
129157
func (m *Machine) UpdatePassword(newpass string) error {
130-
if len(newpass) == 0 {
131-
return fmt.Errorf("newpass must be at least 1 letter")
132-
}
133158
m.Password = newpass
134-
oldpass := m.passtoken.value
135-
m.passtoken.value = newpass
136-
newraw := make([]byte, len(m.passtoken.rawvalue))
137-
copy(newraw, m.passtoken.rawvalue)
138-
m.passtoken.rawvalue = append(
139-
bytes.TrimSuffix(newraw, []byte(oldpass)),
140-
[]byte(newpass)...,
141-
)
159+
updateTokenValue(m.passtoken, newpass)
160+
return nil
161+
}
162+
163+
// UpdateLogin sets the login for the Machine m.
164+
func (m *Machine) UpdateLogin(newlogin string) error {
165+
m.Login = newlogin
166+
updateTokenValue(m.logintoken, newlogin)
142167
return nil
143168
}
144169

170+
func updateTokenValue(t *token, value string) {
171+
oldvalue := t.value
172+
t.value = value
173+
newraw := make([]byte, len(t.rawvalue))
174+
copy(newraw, t.rawvalue)
175+
t.rawvalue = append(
176+
bytes.TrimSuffix(newraw, []byte(oldvalue)),
177+
[]byte(value)...,
178+
)
179+
}
180+
145181
// Macros contains all the macro definitions in a netrc file.
146182
type Macros map[string]string
147183

netrc/netrc_test.go

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"io"
1010
"io/ioutil"
11+
"strings"
1112
"testing"
1213
)
1314

@@ -192,6 +193,16 @@ func TestNewMachine(t *testing.T) {
192193
loginVal := "dodging-samurai-42@heroku.com"
193194
passwordVal := "octocatdodgeballchampions"
194195
accountVal := "someacct"
196+
197+
// sanity check
198+
bodyb, _ := n.MarshalText()
199+
body := string(bodyb)
200+
for _, value := range []string{nameVal, loginVal, passwordVal, accountVal} {
201+
if strings.Contains(body, value) {
202+
t.Errorf("MarshalText() before NewMachine() contained unexpected %q", value)
203+
}
204+
}
205+
195206
m := n.NewMachine(nameVal, loginVal, passwordVal, accountVal)
196207
if m == nil {
197208
t.Fatalf("NewMachine() returned nil")
@@ -214,6 +225,14 @@ func TestNewMachine(t *testing.T) {
214225
checkToken(t, "logintoken", m.logintoken, tkLogin, "\n\tlogin", loginVal)
215226
checkToken(t, "passtoken", m.passtoken, tkPassword, "\n\tpassword", passwordVal)
216227
checkToken(t, "accounttoken", m.accounttoken, tkAccount, "\n\taccount", accountVal)
228+
// check marshal output
229+
bodyb, _ = n.MarshalText()
230+
body = string(bodyb)
231+
for _, value := range []string{nameVal, loginVal, passwordVal, accountVal} {
232+
if !strings.Contains(body, value) {
233+
t.Errorf("MarshalText() after NewMachine() did not include %q as expected", value)
234+
}
235+
}
217236
}
218237

219238
func checkToken(t *testing.T, name string, tok *token, kind tkType, rawkind, value string) {
@@ -243,6 +262,71 @@ type tokenss struct {
243262
rawvalue []byte
244263
}
245264

265+
func TestUpdateLogin(t *testing.T) {
266+
n, err := ParseFile("examples/good.netrc")
267+
if err != nil {
268+
t.Fatal(err)
269+
}
270+
271+
tests := []struct {
272+
exists bool
273+
name string
274+
oldlogin string
275+
newlogin string
276+
}{
277+
{true, "mail.google.com", "joe@gmail.com", "joe2@gmail.com"},
278+
{false, "heroku.com", "", "dodging-samurai-42@heroku.com"},
279+
}
280+
281+
bodyb, _ := n.MarshalText()
282+
body := string(bodyb)
283+
for _, test := range tests {
284+
if strings.Contains(body, test.newlogin) {
285+
t.Errorf("MarshalText() before UpdateLogin() contained unexpected %q", test.newlogin)
286+
}
287+
}
288+
289+
for _, test := range tests {
290+
m, def, err := n.FindMachine(test.name)
291+
if err != nil {
292+
t.Fatal(err)
293+
}
294+
if def == test.exists {
295+
t.Errorf("expected machine %s to not exist, but it did", test.name)
296+
} else {
297+
if !test.exists {
298+
m = n.NewMachine(test.name, test.newlogin, "", "")
299+
}
300+
if m == nil {
301+
t.Errorf("machine %s was nil", test.name)
302+
continue
303+
}
304+
m.UpdateLogin(test.newlogin)
305+
m, _, err := n.FindMachine(test.name)
306+
if err != nil {
307+
t.Fatal(err)
308+
}
309+
if m.Login != test.newlogin {
310+
t.Errorf("expected new login %q, got %q", test.newlogin, m.Login)
311+
}
312+
if m.logintoken.value != test.newlogin {
313+
t.Errorf("expected m.logintoken %q, got %q", test.newlogin, m.logintoken.value)
314+
}
315+
}
316+
}
317+
318+
bodyb, _ = n.MarshalText()
319+
body = string(bodyb)
320+
for _, test := range tests {
321+
if test.exists && strings.Contains(body, test.oldlogin) {
322+
t.Errorf("MarshalText() after UpdateLogin() contained unexpected %q", test.oldlogin)
323+
}
324+
if !strings.Contains(body, test.newlogin) {
325+
t.Errorf("MarshalText after UpdatePassword did not contain %q as expected", test.newlogin)
326+
}
327+
}
328+
}
329+
246330
func TestUpdatePassword(t *testing.T) {
247331
n, err := ParseFile("examples/good.netrc")
248332
if err != nil {
@@ -252,11 +336,22 @@ func TestUpdatePassword(t *testing.T) {
252336
tests := []struct {
253337
exists bool
254338
name string
255-
newlogin string
339+
oldpassword string
256340
newpassword string
257341
}{
258-
{true, "ray", "joe2@gmail.com", "supernewpass"},
259-
{false, "heroku.com", "dodging-samurai-42@heroku.com", "octocatdodgeballchampions"},
342+
{true, "ray", "mypassword", "supernewpass"},
343+
{false, "heroku.com", "", "octocatdodgeballchampions"},
344+
}
345+
346+
bodyb, _ := n.MarshalText()
347+
body := string(bodyb)
348+
for _, test := range tests {
349+
if test.exists && !strings.Contains(body, test.oldpassword) {
350+
t.Errorf("MarshalText() before UpdatePassword() did not include %q as expected", test.oldpassword)
351+
}
352+
if strings.Contains(body, test.newpassword) {
353+
t.Errorf("MarshalText() before UpdatePassword() contained unexpected %q", test.newpassword)
354+
}
260355
}
261356

262357
for _, test := range tests {
@@ -268,7 +363,7 @@ func TestUpdatePassword(t *testing.T) {
268363
t.Errorf("expected machine %s to not exist, but it did", test.name)
269364
} else {
270365
if !test.exists {
271-
m = n.NewMachine(test.name, test.newlogin, test.newpassword, "")
366+
m = n.NewMachine(test.name, "", test.newpassword, "")
272367
}
273368
if m == nil {
274369
t.Errorf("machine %s was nil", test.name)
@@ -287,6 +382,17 @@ func TestUpdatePassword(t *testing.T) {
287382
}
288383
}
289384
}
385+
386+
bodyb, _ = n.MarshalText()
387+
body = string(bodyb)
388+
for _, test := range tests {
389+
if test.exists && strings.Contains(body, test.oldpassword) {
390+
t.Errorf("MarshalText() after UpdatePassword() contained unexpected %q", test.oldpassword)
391+
}
392+
if !strings.Contains(body, test.newpassword) {
393+
t.Errorf("MarshalText() after UpdatePassword() did not contain %q as expected", test.newpassword)
394+
}
395+
}
290396
}
291397

292398
func netrcReader(filename string, t *testing.T) io.Reader {

0 commit comments

Comments
 (0)