uri_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package uri
  2. import (
  3. "net/url"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestNew(t *testing.T) {
  8. type args struct {
  9. scheme string
  10. values url.Values
  11. }
  12. tests := []struct {
  13. name string
  14. args args
  15. want *URI
  16. }{
  17. {"ok", args{"yubikey", url.Values{"slot-id": []string{"9a"}}}, &URI{
  18. URL: &url.URL{Scheme: "yubikey", Opaque: "slot-id=9a"},
  19. Values: url.Values{"slot-id": []string{"9a"}},
  20. }},
  21. {"ok multiple", args{"yubikey", url.Values{"slot-id": []string{"9a"}, "foo": []string{"bar"}}}, &URI{
  22. URL: &url.URL{Scheme: "yubikey", Opaque: "foo=bar;slot-id=9a"},
  23. Values: url.Values{
  24. "slot-id": []string{"9a"},
  25. "foo": []string{"bar"},
  26. },
  27. }},
  28. }
  29. for _, tt := range tests {
  30. t.Run(tt.name, func(t *testing.T) {
  31. if got := New(tt.args.scheme, tt.args.values); !reflect.DeepEqual(got, tt.want) {
  32. t.Errorf("New() = %v, want %v", got, tt.want)
  33. }
  34. })
  35. }
  36. }
  37. func TestNewFile(t *testing.T) {
  38. type args struct {
  39. path string
  40. }
  41. tests := []struct {
  42. name string
  43. args args
  44. want *URI
  45. }{
  46. {"ok", args{"/tmp/ca.crt"}, &URI{
  47. URL: &url.URL{Scheme: "file", Path: "/tmp/ca.crt"},
  48. Values: url.Values(nil),
  49. }},
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. if got := NewFile(tt.args.path); !reflect.DeepEqual(got, tt.want) {
  54. t.Errorf("NewFile() = %v, want %v", got, tt.want)
  55. }
  56. })
  57. }
  58. }
  59. func TestHasScheme(t *testing.T) {
  60. type args struct {
  61. scheme string
  62. rawuri string
  63. }
  64. tests := []struct {
  65. name string
  66. args args
  67. want bool
  68. }{
  69. {"ok", args{"yubikey", "yubikey:slot-id=9a"}, true},
  70. {"ok empty", args{"yubikey", "yubikey:"}, true},
  71. {"ok letter case", args{"awsKMS", "AWSkms:key-id=abcdefg?foo=bar"}, true},
  72. {"fail", args{"yubikey", "awskms:key-id=abcdefg"}, false},
  73. {"fail parse", args{"yubikey", "yubi%key:slot-id=9a"}, false},
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. if got := HasScheme(tt.args.scheme, tt.args.rawuri); got != tt.want {
  78. t.Errorf("HasScheme() = %v, want %v", got, tt.want)
  79. }
  80. })
  81. }
  82. }
  83. func TestParse(t *testing.T) {
  84. type args struct {
  85. rawuri string
  86. }
  87. tests := []struct {
  88. name string
  89. args args
  90. want *URI
  91. wantErr bool
  92. }{
  93. {"ok", args{"yubikey:slot-id=9a"}, &URI{
  94. URL: &url.URL{Scheme: "yubikey", Opaque: "slot-id=9a"},
  95. Values: url.Values{"slot-id": []string{"9a"}},
  96. }, false},
  97. {"ok query", args{"yubikey:slot-id=9a;foo=bar?pin=123456&foo=bar"}, &URI{
  98. URL: &url.URL{Scheme: "yubikey", Opaque: "slot-id=9a;foo=bar", RawQuery: "pin=123456&foo=bar"},
  99. Values: url.Values{"slot-id": []string{"9a"}, "foo": []string{"bar"}},
  100. }, false},
  101. {"ok file", args{"file:///tmp/ca.cert"}, &URI{
  102. URL: &url.URL{Scheme: "file", Path: "/tmp/ca.cert"},
  103. Values: url.Values{},
  104. }, false},
  105. {"ok file simple", args{"file:/tmp/ca.cert"}, &URI{
  106. URL: &url.URL{Scheme: "file", Path: "/tmp/ca.cert"},
  107. Values: url.Values{},
  108. }, false},
  109. {"ok file host", args{"file://tmp/ca.cert"}, &URI{
  110. URL: &url.URL{Scheme: "file", Host: "tmp", Path: "/ca.cert"},
  111. Values: url.Values{},
  112. }, false},
  113. {"fail parse", args{"yubi%key:slot-id=9a"}, nil, true},
  114. {"fail scheme", args{"yubikey"}, nil, true},
  115. {"fail parse opaque", args{"yubikey:slot-id=%ZZ"}, nil, true},
  116. }
  117. for _, tt := range tests {
  118. t.Run(tt.name, func(t *testing.T) {
  119. got, err := Parse(tt.args.rawuri)
  120. if (err != nil) != tt.wantErr {
  121. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  122. return
  123. }
  124. if !reflect.DeepEqual(got, tt.want) {
  125. t.Errorf("Parse() = %#v, want %v", got.URL, tt.want)
  126. }
  127. })
  128. }
  129. }
  130. func TestParseWithScheme(t *testing.T) {
  131. type args struct {
  132. scheme string
  133. rawuri string
  134. }
  135. tests := []struct {
  136. name string
  137. args args
  138. want *URI
  139. wantErr bool
  140. }{
  141. {"ok", args{"yubikey", "yubikey:slot-id=9a"}, &URI{
  142. URL: &url.URL{Scheme: "yubikey", Opaque: "slot-id=9a"},
  143. Values: url.Values{"slot-id": []string{"9a"}},
  144. }, false},
  145. {"ok file", args{"file", "file:///tmp/ca.cert"}, &URI{
  146. URL: &url.URL{Scheme: "file", Path: "/tmp/ca.cert"},
  147. Values: url.Values{},
  148. }, false},
  149. {"fail parse", args{"yubikey", "yubikey"}, nil, true},
  150. {"fail scheme", args{"yubikey", "awskms:slot-id=9a"}, nil, true},
  151. }
  152. for _, tt := range tests {
  153. t.Run(tt.name, func(t *testing.T) {
  154. got, err := ParseWithScheme(tt.args.scheme, tt.args.rawuri)
  155. if (err != nil) != tt.wantErr {
  156. t.Errorf("ParseWithScheme() error = %v, wantErr %v", err, tt.wantErr)
  157. return
  158. }
  159. if !reflect.DeepEqual(got, tt.want) {
  160. t.Errorf("ParseWithScheme() = %v, want %v", got, tt.want)
  161. }
  162. })
  163. }
  164. }
  165. func TestURI_Get(t *testing.T) {
  166. mustParse := func(s string) *URI {
  167. u, err := Parse(s)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. return u
  172. }
  173. type args struct {
  174. key string
  175. }
  176. tests := []struct {
  177. name string
  178. uri *URI
  179. args args
  180. want string
  181. }{
  182. {"ok", mustParse("yubikey:slot-id=9a"), args{"slot-id"}, "9a"},
  183. {"ok first", mustParse("yubikey:slot-id=9a;slot-id=9b"), args{"slot-id"}, "9a"},
  184. {"ok multiple", mustParse("yubikey:slot-id=9a;foo=bar"), args{"foo"}, "bar"},
  185. {"fail missing", mustParse("yubikey:slot-id=9a"), args{"foo"}, ""},
  186. {"fail in query", mustParse("yubikey:slot-id=9a?foo=bar"), args{"foo"}, ""},
  187. }
  188. for _, tt := range tests {
  189. t.Run(tt.name, func(t *testing.T) {
  190. if got := tt.uri.Get(tt.args.key); got != tt.want {
  191. t.Errorf("URI.Get() = %v, want %v", got, tt.want)
  192. }
  193. })
  194. }
  195. }