...

Source file src/github.com/jackc/pgpassfile/pgpass_test.go

Documentation: github.com/jackc/pgpassfile

     1  package pgpassfile
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func unescape(s string) string {
    13  	s = strings.Replace(s, `\:`, `:`, -1)
    14  	s = strings.Replace(s, `\\`, `\`, -1)
    15  	return s
    16  }
    17  
    18  var passfile = [][]string{
    19  	{"test1", "5432", "larrydb", "larry", "whatstheidea"},
    20  	{"test1", "5432", "moedb", "moe", "imbecile"},
    21  	{"test1", "5432", "curlydb", "curly", "nyuknyuknyuk"},
    22  	{"test2", "5432", "*", "shemp", "heymoe"},
    23  	{"test2", "5432", "*", "*", `test\\ing\:`},
    24  	{"localhost", "*", "*", "*", "sesam"},
    25  	{"test3", "*", "", "", "swordfish"}, // user will be filled later
    26  }
    27  
    28  func TestParsePassFile(t *testing.T) {
    29  	buf := bytes.NewBufferString(`# A comment
    30  	test1:5432:larrydb:larry:whatstheidea
    31  	test1:5432:moedb:moe:imbecile
    32  	test1:5432:curlydb:curly:nyuknyuknyuk
    33  	test2:5432:*:shemp:heymoe
    34  	test2:5432:*:*:test\\ing\:
    35  	localhost:*:*:*:sesam
    36  		`)
    37  
    38  	passfile, err := ParsePassfile(buf)
    39  	require.Nil(t, err)
    40  
    41  	assert.Len(t, passfile.Entries, 6)
    42  
    43  	assert.Equal(t, "whatstheidea", passfile.FindPassword("test1", "5432", "larrydb", "larry"))
    44  	assert.Equal(t, "imbecile", passfile.FindPassword("test1", "5432", "moedb", "moe"))
    45  	assert.Equal(t, `test\ing:`, passfile.FindPassword("test2", "5432", "something", "else"))
    46  	assert.Equal(t, "sesam", passfile.FindPassword("localhost", "9999", "foo", "bare"))
    47  
    48  	assert.Equal(t, "", passfile.FindPassword("wrong", "5432", "larrydb", "larry"))
    49  	assert.Equal(t, "", passfile.FindPassword("test1", "wrong", "larrydb", "larry"))
    50  	assert.Equal(t, "", passfile.FindPassword("test1", "5432", "wrong", "larry"))
    51  	assert.Equal(t, "", passfile.FindPassword("test1", "5432", "larrydb", "wrong"))
    52  }
    53  

View as plain text