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
|
package pgpassfile
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func unescape(s string) string {
s = strings.Replace(s, `\:`, `:`, -1)
s = strings.Replace(s, `\\`, `\`, -1)
return s
}
var passfile = [][]string{
{"test1", "5432", "larrydb", "larry", "whatstheidea"},
{"test1", "5432", "moedb", "moe", "imbecile"},
{"test1", "5432", "curlydb", "curly", "nyuknyuknyuk"},
{"test2", "5432", "*", "shemp", "heymoe"},
{"test2", "5432", "*", "*", `test\\ing\:`},
{"localhost", "*", "*", "*", "sesam"},
{"test3", "*", "", "", "swordfish"}, // user will be filled later
}
func TestParsePassFile(t *testing.T) {
buf := bytes.NewBufferString(`# A comment
test1:5432:larrydb:larry:whatstheidea
test1:5432:moedb:moe:imbecile
test1:5432:curlydb:curly:nyuknyuknyuk
test2:5432:*:shemp:heymoe
test2:5432:*:*:test\\ing\:
localhost:*:*:*:sesam
`)
passfile, err := ParsePassfile(buf)
require.Nil(t, err)
assert.Len(t, passfile.Entries, 6)
assert.Equal(t, "whatstheidea", passfile.FindPassword("test1", "5432", "larrydb", "larry"))
assert.Equal(t, "imbecile", passfile.FindPassword("test1", "5432", "moedb", "moe"))
assert.Equal(t, `test\ing:`, passfile.FindPassword("test2", "5432", "something", "else"))
assert.Equal(t, "sesam", passfile.FindPassword("localhost", "9999", "foo", "bare"))
assert.Equal(t, "", passfile.FindPassword("wrong", "5432", "larrydb", "larry"))
assert.Equal(t, "", passfile.FindPassword("test1", "wrong", "larrydb", "larry"))
assert.Equal(t, "", passfile.FindPassword("test1", "5432", "wrong", "larry"))
assert.Equal(t, "", passfile.FindPassword("test1", "5432", "larrydb", "wrong"))
}
|