userid_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package packet
  5. import (
  6. "testing"
  7. )
  8. var userIdTests = []struct {
  9. id string
  10. name, comment, email string
  11. }{
  12. {"", "", "", ""},
  13. {"John Smith", "John Smith", "", ""},
  14. {"John Smith ()", "John Smith", "", ""},
  15. {"John Smith () <>", "John Smith", "", ""},
  16. {"(comment", "", "comment", ""},
  17. {"(comment)", "", "comment", ""},
  18. {"<email", "", "", "email"},
  19. {"<email> sdfk", "", "", "email"},
  20. {" John Smith ( Comment ) asdkflj < email > lksdfj", "John Smith", "Comment", "email"},
  21. {" John Smith < email > lksdfj", "John Smith", "", "email"},
  22. {"(<foo", "", "<foo", ""},
  23. {"René Descartes (العربي)", "René Descartes", "العربي", ""},
  24. }
  25. func TestParseUserId(t *testing.T) {
  26. for i, test := range userIdTests {
  27. name, comment, email := parseUserId(test.id)
  28. if name != test.name {
  29. t.Errorf("%d: name mismatch got:%s want:%s", i, name, test.name)
  30. }
  31. if comment != test.comment {
  32. t.Errorf("%d: comment mismatch got:%s want:%s", i, comment, test.comment)
  33. }
  34. if email != test.email {
  35. t.Errorf("%d: email mismatch got:%s want:%s", i, email, test.email)
  36. }
  37. }
  38. }
  39. var newUserIdTests = []struct {
  40. name, comment, email, id string
  41. }{
  42. {"foo", "", "", "foo"},
  43. {"", "bar", "", "(bar)"},
  44. {"", "", "baz", "<baz>"},
  45. {"foo", "bar", "", "foo (bar)"},
  46. {"foo", "", "baz", "foo <baz>"},
  47. {"", "bar", "baz", "(bar) <baz>"},
  48. {"foo", "bar", "baz", "foo (bar) <baz>"},
  49. }
  50. func TestNewUserId(t *testing.T) {
  51. for i, test := range newUserIdTests {
  52. uid := NewUserId(test.name, test.comment, test.email)
  53. if uid == nil {
  54. t.Errorf("#%d: returned nil", i)
  55. continue
  56. }
  57. if uid.Id != test.id {
  58. t.Errorf("#%d: got '%s', want '%s'", i, uid.Id, test.id)
  59. }
  60. }
  61. }
  62. var invalidNewUserIdTests = []struct {
  63. name, comment, email string
  64. }{
  65. {"foo(", "", ""},
  66. {"foo<", "", ""},
  67. {"", "bar)", ""},
  68. {"", "bar<", ""},
  69. {"", "", "baz>"},
  70. {"", "", "baz)"},
  71. {"", "", "baz\x00"},
  72. }
  73. func TestNewUserIdWithInvalidInput(t *testing.T) {
  74. for i, test := range invalidNewUserIdTests {
  75. if uid := NewUserId(test.name, test.comment, test.email); uid != nil {
  76. t.Errorf("#%d: returned non-nil value: %#v", i, uid)
  77. }
  78. }
  79. }