uri_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 schema", args{"cloudkms:"}, &URI{
  98. URL: &url.URL{Scheme: "cloudkms"},
  99. Values: url.Values{},
  100. }, false},
  101. {"ok query", args{"yubikey:slot-id=9a;foo=bar?pin=123456&foo=bar"}, &URI{
  102. URL: &url.URL{Scheme: "yubikey", Opaque: "slot-id=9a;foo=bar", RawQuery: "pin=123456&foo=bar"},
  103. Values: url.Values{"slot-id": []string{"9a"}, "foo": []string{"bar"}},
  104. }, false},
  105. {"ok file", 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 simple", args{"file:/tmp/ca.cert"}, &URI{
  110. URL: &url.URL{Scheme: "file", Path: "/tmp/ca.cert"},
  111. Values: url.Values{},
  112. }, false},
  113. {"ok file host", args{"file://tmp/ca.cert"}, &URI{
  114. URL: &url.URL{Scheme: "file", Host: "tmp", Path: "/ca.cert"},
  115. Values: url.Values{},
  116. }, false},
  117. {"fail schema", args{"cloudkms"}, nil, true},
  118. {"fail parse", args{"yubi%key:slot-id=9a"}, nil, true},
  119. {"fail scheme", args{"yubikey"}, nil, true},
  120. {"fail parse opaque", args{"yubikey:slot-id=%ZZ"}, nil, true},
  121. }
  122. for _, tt := range tests {
  123. t.Run(tt.name, func(t *testing.T) {
  124. got, err := Parse(tt.args.rawuri)
  125. if (err != nil) != tt.wantErr {
  126. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  127. return
  128. }
  129. if !reflect.DeepEqual(got, tt.want) {
  130. t.Errorf("Parse() = %#v, want %v", got.URL, tt.want)
  131. }
  132. })
  133. }
  134. }
  135. func TestParseWithScheme(t *testing.T) {
  136. type args struct {
  137. scheme string
  138. rawuri string
  139. }
  140. tests := []struct {
  141. name string
  142. args args
  143. want *URI
  144. wantErr bool
  145. }{
  146. {"ok", args{"yubikey", "yubikey:slot-id=9a"}, &URI{
  147. URL: &url.URL{Scheme: "yubikey", Opaque: "slot-id=9a"},
  148. Values: url.Values{"slot-id": []string{"9a"}},
  149. }, false},
  150. {"ok schema", args{"cloudkms", "cloudkms:"}, &URI{
  151. URL: &url.URL{Scheme: "cloudkms"},
  152. Values: url.Values{},
  153. }, false},
  154. {"ok file", args{"file", "file:///tmp/ca.cert"}, &URI{
  155. URL: &url.URL{Scheme: "file", Path: "/tmp/ca.cert"},
  156. Values: url.Values{},
  157. }, false},
  158. {"fail parse", args{"yubikey", "yubikey"}, nil, true},
  159. {"fail scheme", args{"yubikey", "awskms:slot-id=9a"}, nil, true},
  160. {"fail schema", args{"cloudkms", "cloudkms"}, nil, true},
  161. }
  162. for _, tt := range tests {
  163. t.Run(tt.name, func(t *testing.T) {
  164. got, err := ParseWithScheme(tt.args.scheme, tt.args.rawuri)
  165. if (err != nil) != tt.wantErr {
  166. t.Errorf("ParseWithScheme() error = %v, wantErr %v", err, tt.wantErr)
  167. return
  168. }
  169. if !reflect.DeepEqual(got, tt.want) {
  170. t.Errorf("ParseWithScheme() = %v, want %v", got, tt.want)
  171. }
  172. })
  173. }
  174. }
  175. func TestURI_Get(t *testing.T) {
  176. mustParse := func(s string) *URI {
  177. u, err := Parse(s)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. return u
  182. }
  183. type args struct {
  184. key string
  185. }
  186. tests := []struct {
  187. name string
  188. uri *URI
  189. args args
  190. want string
  191. }{
  192. {"ok", mustParse("yubikey:slot-id=9a"), args{"slot-id"}, "9a"},
  193. {"ok first", mustParse("yubikey:slot-id=9a;slot-id=9b"), args{"slot-id"}, "9a"},
  194. {"ok multiple", mustParse("yubikey:slot-id=9a;foo=bar"), args{"foo"}, "bar"},
  195. {"ok in query", mustParse("yubikey:slot-id=9a?foo=bar"), args{"foo"}, "bar"},
  196. {"fail missing", mustParse("yubikey:slot-id=9a"), args{"foo"}, ""},
  197. {"fail missing query", mustParse("yubikey:slot-id=9a?bar=zar"), args{"foo"}, ""},
  198. }
  199. for _, tt := range tests {
  200. t.Run(tt.name, func(t *testing.T) {
  201. if got := tt.uri.Get(tt.args.key); got != tt.want {
  202. t.Errorf("URI.Get() = %v, want %v", got, tt.want)
  203. }
  204. })
  205. }
  206. }
  207. func TestURI_GetEncoded(t *testing.T) {
  208. mustParse := func(s string) *URI {
  209. u, err := Parse(s)
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. return u
  214. }
  215. type args struct {
  216. key string
  217. }
  218. tests := []struct {
  219. name string
  220. uri *URI
  221. args args
  222. want []byte
  223. }{
  224. {"ok", mustParse("yubikey:slot-id=9a"), args{"slot-id"}, []byte{0x9a}},
  225. {"ok first", mustParse("yubikey:slot-id=9a9b;slot-id=9b"), args{"slot-id"}, []byte{0x9a, 0x9b}},
  226. {"ok percent", mustParse("yubikey:slot-id=9a;foo=%9a%9b%9c"), args{"foo"}, []byte{0x9a, 0x9b, 0x9c}},
  227. {"ok in query", mustParse("yubikey:slot-id=9a?foo=9a"), args{"foo"}, []byte{0x9a}},
  228. {"ok in query percent", mustParse("yubikey:slot-id=9a?foo=%9a"), args{"foo"}, []byte{0x9a}},
  229. {"ok missing", mustParse("yubikey:slot-id=9a"), args{"foo"}, nil},
  230. {"ok missing query", mustParse("yubikey:slot-id=9a?bar=zar"), args{"foo"}, nil},
  231. {"ok no hex", mustParse("yubikey:slot-id=09a?bar=zar"), args{"slot-id"}, []byte{'0', '9', 'a'}},
  232. }
  233. for _, tt := range tests {
  234. t.Run(tt.name, func(t *testing.T) {
  235. got := tt.uri.GetEncoded(tt.args.key)
  236. if !reflect.DeepEqual(got, tt.want) {
  237. t.Errorf("URI.GetEncoded() = %v, want %v", got, tt.want)
  238. }
  239. })
  240. }
  241. }
  242. func TestURI_Pin(t *testing.T) {
  243. mustParse := func(s string) *URI {
  244. u, err := Parse(s)
  245. if err != nil {
  246. t.Fatal(err)
  247. }
  248. return u
  249. }
  250. tests := []struct {
  251. name string
  252. uri *URI
  253. want string
  254. }{
  255. {"from value", mustParse("pkcs11:id=%72%73?pin-value=0123456789"), "0123456789"},
  256. {"from source", mustParse("pkcs11:id=%72%73?pin-source=testdata/pin.txt"), "trim-this-pin"},
  257. {"from missing", mustParse("pkcs11:id=%72%73"), ""},
  258. {"from source missing", mustParse("pkcs11:id=%72%73?pin-source=testdata/foo.txt"), ""},
  259. }
  260. for _, tt := range tests {
  261. t.Run(tt.name, func(t *testing.T) {
  262. if got := tt.uri.Pin(); got != tt.want {
  263. t.Errorf("URI.Pin() = %v, want %v", got, tt.want)
  264. }
  265. })
  266. }
  267. }