package papod_test import ( "bufio" "bytes" "crypto/sha1" "crypto/sha256" "database/sql" "encoding/base64" "encoding/json" "errors" "fmt" "hash" "log" "log/slog" "reflect" "strings" "testing" "euandre.org/papod/src" ) func errorIf(t *testing.T, err error) { if err != nil { t.Errorf("Unexpected error: %#v\n", err) } } func errorIfNotI(t *testing.T, i int, err error) { if err == nil { t.Errorf("Expected error, got nil (i = %d)\n", i) } } func assertEqualI(t *testing.T, i int, given any, expected any) { if !reflect.DeepEqual(given, expected) { t.Errorf("given != expected (i = %d)\n", i) t.Errorf("given: %#v\n", given) t.Errorf("expected: %#v\n", expected) } } func assertEqual(t *testing.T, given any, expected any) { if !reflect.DeepEqual(given, expected) { t.Errorf("given != expected") t.Errorf("given: %#v\n", given) t.Errorf("expected: %#v\n", expected) } } func TestSetLoggerOutput(t *testing.T) { return type entry struct { msg string `json:"msg"` aKey string `json:"a-key"` } var e entry var buf bytes.Buffer slog.Error("the message", "a-key", "a-value") s := buf.String() // fmt.Println(s) // fmt.Println(e) err := json.Unmarshal([]byte(s), &e) if err != nil { t.Fail() } if e.msg != "the message" { t.Fail() } fmt.Println(1) // fmt.Println(e) } type pbkdfTestVector struct { password string salt string iter int output []byte } // Test vectors from RFC 6070, http://tools.ietf.org/html/rfc6070 var sha1TestVectors = []pbkdfTestVector { { "password", "salt", 1, []byte { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6, }, }, { "password", "salt", 2, []byte { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57, }, }, { "password", "salt", 4096, []byte { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, 0x65, 0xa4, 0x29, 0xc1, }, }, /* // This one takes too long { "password", "salt", 16777216, []byte { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4, 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c, 0x26, 0x34, 0xe9, 0x84, }, }, */ { "passwordPASSWORDpassword", "saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096, []byte { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, 0x38, }, }, { "pass\000word", "sa\000lt", 4096, []byte { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3, }, }, } // Test vectors from // http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors var sha256TestVectors = []pbkdfTestVector { { "password", "salt", 1, []byte { 0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c, 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37, 0xa8, 0x65, 0x48, 0xc9, }, }, { "password", "salt", 2, []byte { 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0, 0x2a, 0x30, 0x3f, 0x8e, }, }, { "password", "salt", 4096, []byte { 0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41, 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d, 0x96, 0x28, 0x93, 0xa0, }, }, { "passwordPASSWORDpassword", "saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096, []byte { 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f, 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf, 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, 0x1c, }, }, { "pass\000word", "sa\000lt", 4096, []byte { 0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89, 0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87, }, }, } func testHash( t *testing.T, h func() hash.Hash, hashName string, vectors []pbkdfTestVector, ) { for i, v := range vectors { o := papod.PBKDF2Key( []byte(v.password), []byte(v.salt), v.iter, len(v.output), h, ) assertEqualI(t, i, v.output, o) } } func TestWithHMACSHA1(t *testing.T) { testHash(t, sha1.New, "SHA1", sha1TestVectors) } func TestWithHMACSHA256(t *testing.T) { testHash(t, sha256.New, "SHA256", sha256TestVectors) } type scryptTestVector struct { password string salt string N int r int p int output []byte } var good = []scryptTestVector { { "password", "salt", 2, 10, 10, []byte { 0x48, 0x2c, 0x85, 0x8e, 0x22, 0x90, 0x55, 0xe6, 0x2f, 0x41, 0xe0, 0xec, 0x81, 0x9a, 0x5e, 0xe1, 0x8b, 0xdb, 0x87, 0x25, 0x1a, 0x53, 0x4f, 0x75, 0xac, 0xd9, 0x5a, 0xc5, 0xe5, 0xa, 0xa1, 0x5f, }, }, { "password", "salt", 16, 100, 100, []byte { 0x88, 0xbd, 0x5e, 0xdb, 0x52, 0xd1, 0xdd, 0x0, 0x18, 0x87, 0x72, 0xad, 0x36, 0x17, 0x12, 0x90, 0x22, 0x4e, 0x74, 0x82, 0x95, 0x25, 0xb1, 0x8d, 0x73, 0x23, 0xa5, 0x7f, 0x91, 0x96, 0x3c, 0x37, }, }, { "this is a long \000 password", "and this is a long \000 salt", 16384, 8, 1, []byte { 0xc3, 0xf1, 0x82, 0xee, 0x2d, 0xec, 0x84, 0x6e, 0x70, 0xa6, 0x94, 0x2f, 0xb5, 0x29, 0x98, 0x5a, 0x3a, 0x09, 0x76, 0x5e, 0xf0, 0x4c, 0x61, 0x29, 0x23, 0xb1, 0x7f, 0x18, 0x55, 0x5a, 0x37, 0x07, 0x6d, 0xeb, 0x2b, 0x98, 0x30, 0xd6, 0x9d, 0xe5, 0x49, 0x26, 0x51, 0xe4, 0x50, 0x6a, 0xe5, 0x77, 0x6d, 0x96, 0xd4, 0x0f, 0x67, 0xaa, 0xee, 0x37, 0xe1, 0x77, 0x7b, 0x8a, 0xd5, 0xc3, 0x11, 0x14, 0x32, 0xbb, 0x3b, 0x6f, 0x7e, 0x12, 0x64, 0x40, 0x18, 0x79, 0xe6, 0x41, 0xae, }, }, { "p", "s", 2, 1, 1, []byte { 0x48, 0xb0, 0xd2, 0xa8, 0xa3, 0x27, 0x26, 0x11, 0x98, 0x4c, 0x50, 0xeb, 0xd6, 0x30, 0xaf, 0x52, }, }, { "", "", 16, 1, 1, []byte { 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20, 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97, 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8, 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42, 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8, 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17, 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28, 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06, }, }, { "password", "NaCl", 1024, 8, 16, []byte { 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00, 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe, 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30, 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62, 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88, 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda, 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d, 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40, }, }, { "pleaseletmein", "SodiumChloride", 16384, 8, 1, []byte { 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48, 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb, 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e, 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2, 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf, 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9, 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40, 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87, }, }, /* // Disabled: needs 1 GiB RAM and takes too long for a simple test. { "pleaseletmein", "SodiumChloride", 1048576, 8, 1, []byte { 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae, 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81, 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d, 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47, 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f, 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3, 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb, 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4, }, }, */ } const maxInt = int(^uint(0) >> 1) var bad = []scryptTestVector { {"p", "s", 0, 1, 1, nil}, // N == 0 {"p", "s", 1, 1, 1, nil}, // N == 1 {"p", "s", 7, 8, 1, nil}, // N is not power of 2 {"p", "s", 16, maxInt / 2, maxInt / 2, nil}, // p * r too large } func TestKey(t *testing.T) { for i, v := range good { k, err := papod.Scrypt( []byte(v.password), []byte(v.salt), v.N, v.r, v.p, len(v.output), ) errorIf(t, err) assertEqualI(t, i, v.output, k) } for i, v := range bad { _, err := papod.Scrypt( []byte(v.password), []byte(v.salt), v.N, v.r, v.p, 32, ) errorIfNotI(t, i, err) } } func TestExample(t *testing.T) { const expected = "lGnMz8io0AUkfzn6Pls1qX20Vs7PGN6sbYQ2TQgY12M=" // DO NOT use this salt value; generate your own random salt. 8 bytes is // a good length. salt := []byte {0xc8, 0x28, 0xf2, 0x58, 0xa7, 0x6a, 0xad, 0x7b} dk, err := papod.Scrypt([]byte("some password"), salt, 1<<15, 8, 1, 32) if err != nil { log.Fatal(err) } given := base64.StdEncoding.EncodeToString(dk) assertEqual(t, given, expected) } func TestSplitOnCRLF(t *testing.T) { type tableT struct { input string expected []string } table := []tableT { { "", nil, }, { "\r\n", []string { "" }, }, { "abc\r\n", []string { "abc" }, }, { "abc\r\n ", []string { "abc" }, }, { "abc\r\n \r\n", []string { "abc", " " }, }, { " \r\n \r\n", []string { " ", " " }, }, { "aaa\r\nbbb\r\nccc\r\n", []string { "aaa", "bbb", "ccc" }, }, { "\r\nsplit \r \n CRLF\r\n\r\n", []string { "", "split \r \n CRLF", "" }, }, } for i, entry := range table { var given []string scanner := bufio.NewScanner(strings.NewReader(entry.input)) scanner.Split(papod.SplitOnCRLF) for scanner.Scan() { given = append(given, scanner.Text()) } err := scanner.Err() errorIf(t, err) assertEqualI(t, i, given, entry.expected) } } func TestSplitOnRawMessage(t *testing.T) { type tableT struct { input string expected []string } table := []tableT { { "first message\r\nsecond message\r\n", []string { "first message", "second message" }, }, { "message 1\r\n\r\nmessage 2\r\n\r\nignored", []string { "message 1", "message 2" }, }, } for i, entry := range table { var given []string scanner := bufio.NewScanner(strings.NewReader(entry.input)) scanner.Split(papod.SplitOnRawMessage) for scanner.Scan() { given = append(given, scanner.Text()) } err := scanner.Err() errorIf(t, err) assertEqualI(t, i, given, entry.expected) } } func TestParseMessageParams(t *testing.T) { type tableT struct { input string expected papod.MessageParams } table := []tableT { { "", papod.MessageParams { Middle: []string { }, Trailing: "", }, }, { " ", papod.MessageParams { Middle: []string { }, Trailing: "", }, }, { " :", papod.MessageParams { Middle: []string { }, Trailing: "", }, }, { " : ", papod.MessageParams { Middle: []string { }, Trailing: " ", }, }, { ": ", papod.MessageParams { Middle: []string { ":" }, Trailing: "", }, }, { ": ", papod.MessageParams { Middle: []string { ":" }, Trailing: "", }, }, { " : ", papod.MessageParams { Middle: []string { }, Trailing: " ", }, }, { " :", papod.MessageParams { Middle: []string { }, Trailing: "", }, }, { " :", papod.MessageParams { Middle: []string { }, Trailing: "", }, }, { "a", papod.MessageParams { Middle: []string { "a" }, Trailing: "", }, }, { "ab", papod.MessageParams { Middle: []string { "ab" }, Trailing: "", }, }, { "a b", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: "", }, }, { "a b c", papod.MessageParams { Middle: []string { "a", "b", "c" }, Trailing: "", }, }, { "a b:c", papod.MessageParams { Middle: []string { "a", "b:c" }, Trailing: "", }, }, { "a b:c:", papod.MessageParams { Middle: []string { "a", "b:c:" }, Trailing: "", }, }, { "a b :c", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: "c", }, }, { "a b :c:", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: "c:", }, }, { "a b :c ", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: "c ", }, }, { "a b : c", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: " c", }, }, { "a b : c ", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: " c ", }, }, { "a b : c :", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: " c :", }, }, { "a b : c : ", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: " c : ", }, }, { "a b : c :d", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: " c :d", }, }, { "a b : c :d ", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: " c :d ", }, }, { "a b : c : d ", papod.MessageParams { Middle: []string { "a", "b" }, Trailing: " c : d ", }, }, } for i, entry := range table { given := papod.ParseMessageParams(entry.input) assertEqualI(t, i, given, entry.expected) } } func TestParseMessage(t *testing.T) { type tableTOK struct { input string expected papod.Message } tableOK := []tableTOK {{ "NICK joebloe ", papod.Message { Prefix: "", Command: "NICK", Params: papod.MessageParams { Middle: []string { "joebloe" }, Trailing: "", }, Raw: "NICK joebloe ", }, }, { "USER joebloe 0.0.0.0 joe :Joe Bloe", papod.Message { Prefix: "", Command: "USER", Params: papod.MessageParams { Middle: []string { "joebloe", "0.0.0.0", "joe", }, Trailing: "Joe Bloe", }, Raw: "USER joebloe 0.0.0.0 joe :Joe Bloe", }, }, { ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { "joebloe", "0.0.0.0", "joe", }, Trailing: "Joe Bloe", }, Raw: ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", }, }, { ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { "joebloe", "0.0.0.0", "joe", }, Trailing: " Joe Bloe ", }, Raw: ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", }, }, { ":pre USER joebloe: 0:0:0:1 joe::a: : Joe Bloe ", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { "joebloe:", "0:0:0:1", "joe::a:", }, Trailing: " Joe Bloe ", }, Raw: ":pre USER joebloe: 0:0:0:1 joe::a: : Joe Bloe ", }, }, { ":pre USER :Joe Bloe", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { }, Trailing: "Joe Bloe", }, Raw: ":pre USER :Joe Bloe", }, }, { ":pre USER : Joe Bloe", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { }, Trailing: " Joe Bloe", }, Raw: ":pre USER : Joe Bloe", }, }, { ":pre USER : Joe Bloe", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { }, Trailing: " Joe Bloe", }, Raw: ":pre USER : Joe Bloe", }, }, { ":pre USER : ", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { }, Trailing: " ", }, Raw: ":pre USER : ", }, }, { ":pre USER :", papod.Message { Prefix: "pre", Command: "USER", Params: papod.MessageParams { Middle: []string { }, Trailing: "", }, Raw: ":pre USER :", }, }} for i, entry := range tableOK { given, err := papod.ParseMessage(entry.input) errorIf(t, err) assertEqualI(t, i, given, entry.expected) } type tableErrorT struct { input string expected error } parseErr := errors.New("Can't parse message") tableError := []tableErrorT { { ":pre", parseErr, }, { ": pre", parseErr, }, { ":pre N1CK", parseErr, }, } for i, entry := range tableError { _, given := papod.ParseMessage(entry.input) assertEqualI(t, i, given, entry.expected) } } func TestInitMigrations(t *testing.T) { const query = `SELECT filename FROM migrations;` db, err := sql.Open("sqlite3", ":memory:") papod.FatalIf(err) _, err = db.Query(query) assertEqual(t, err.Error(), "no such table: migrations") for i := 0; i < 5; i++ { papod.InitMigrations(db) rows, err := db.Query(query) papod.FatalIf(err) assertEqual(t, rows.Next(), false) papod.FatalIf(rows.Err()) } } func TestPendingMigrations(t *testing.T) { db, err := sql.Open("sqlite3", ":memory:") papod.FatalIf(err) papod.InitMigrations(db) pending1 := papod.PendingMigrations(db) pending2 := papod.PendingMigrations(db) assertEqual(t, pending1, pending2) } func TestRunMigrations(t *testing.T) { db, err := sql.Open("sqlite3", ":memory:") papod.FatalIf(err) for i := 0; i < 5; i++ { papod.RunMigrations(db) } }