2021-06-14 13:08:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_getPassword(t *testing.T) {
|
|
|
|
type args struct {
|
2022-03-25 13:38:24 +00:00
|
|
|
password string
|
|
|
|
pwdFile string
|
|
|
|
pwdStdin bool
|
2021-06-14 13:08:29 +00:00
|
|
|
reader io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
pass := "mySecretPass"
|
|
|
|
passwordFile := "testdata/my.pass"
|
|
|
|
reader := &bytes.Buffer{}
|
|
|
|
_, err := reader.WriteString(pass)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to write to buffer: %+v", err)
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2022-03-25 13:38:24 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
2021-06-14 13:08:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "password defined",
|
2022-03-25 13:38:24 +00:00
|
|
|
args: args{
|
|
|
|
password: pass,
|
|
|
|
},
|
2021-06-14 13:08:29 +00:00
|
|
|
want: pass,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pwdFile defined",
|
2022-03-25 13:38:24 +00:00
|
|
|
args: args{
|
|
|
|
pwdFile: passwordFile,
|
|
|
|
},
|
2021-06-14 13:08:29 +00:00
|
|
|
want: pass,
|
|
|
|
},
|
2022-03-25 13:38:24 +00:00
|
|
|
{
|
|
|
|
name: "pwdFile does not exist",
|
|
|
|
args: args{pwdFile: "iDontExist"},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-06-14 13:08:29 +00:00
|
|
|
{
|
|
|
|
name: "read pass from stdin defined",
|
|
|
|
args: args{
|
2022-03-25 13:38:24 +00:00
|
|
|
pwdStdin: true,
|
2021-06-14 13:08:29 +00:00
|
|
|
reader: reader,
|
|
|
|
},
|
|
|
|
want: pass,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-03-25 13:38:24 +00:00
|
|
|
got, err := getPassword(tt.args.password, tt.args.pwdFile, tt.args.pwdStdin, tt.args.reader)
|
|
|
|
if !tt.wantErr && err != nil {
|
|
|
|
t.Errorf("expected no error, but got %v", err)
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
2021-06-14 13:08:29 +00:00
|
|
|
t.Errorf("getPassword() = '%v', want '%v'", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|