auth_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "testing"
  4. )
  5. func stringsEqual(a, b []string) bool {
  6. if len(a) != len(b) {
  7. return false
  8. }
  9. for i := range a {
  10. if a[i] != b[i] {
  11. return false
  12. }
  13. }
  14. return true
  15. }
  16. func TestParseLine(t *testing.T) {
  17. var tests = []struct {
  18. name string
  19. expectFail bool
  20. line string
  21. username string
  22. addrs []string
  23. }{
  24. {
  25. name: "Empty line",
  26. expectFail: true,
  27. line: "",
  28. },
  29. {
  30. name: "Too few fields",
  31. expectFail: true,
  32. line: "joe",
  33. },
  34. {
  35. name: "Too many fields",
  36. expectFail: true,
  37. line: "joe xxx [email protected] whatsthis",
  38. },
  39. {
  40. name: "Normal case",
  41. line: "joe xxx [email protected]",
  42. username: "joe",
  43. addrs: []string{"[email protected]"},
  44. },
  45. {
  46. name: "No allowed addrs given",
  47. line: "joe xxx",
  48. username: "joe",
  49. addrs: []string{},
  50. },
  51. {
  52. name: "Trailing comma",
  53. line: "joe xxx [email protected],",
  54. username: "joe",
  55. addrs: []string{"[email protected]"},
  56. },
  57. {
  58. name: "Multiple allowed addrs",
  59. line: "joe xxx [email protected],@foo.example.com",
  60. username: "joe",
  61. addrs: []string{"[email protected]", "@foo.example.com"},
  62. },
  63. }
  64. for i, test := range tests {
  65. t.Run(test.name, func(t *testing.T) {
  66. user := parseLine(test.line)
  67. if user == nil {
  68. if !test.expectFail {
  69. t.Errorf("parseLine() returned nil unexpectedly")
  70. }
  71. return
  72. }
  73. if user.username != test.username {
  74. t.Errorf("Testcase %d: Incorrect username: expected %v, got %v",
  75. i, test.username, user.username)
  76. }
  77. if !stringsEqual(user.allowedAddresses, test.addrs) {
  78. t.Errorf("Testcase %d: Incorrect addresses: expected %v, got %v",
  79. i, test.addrs, user.allowedAddresses)
  80. }
  81. })
  82. }
  83. }