uri_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_GetBool(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 bool
  223. }{
  224. {"true", mustParse("azurekms:name=foo;vault=bar;hsm=true"), args{"hsm"}, true},
  225. {"TRUE", mustParse("azurekms:name=foo;vault=bar;hsm=TRUE"), args{"hsm"}, true},
  226. {"tRUe query", mustParse("azurekms:name=foo;vault=bar?hsm=tRUe"), args{"hsm"}, true},
  227. {"false", mustParse("azurekms:name=foo;vault=bar;hsm=false"), args{"hsm"}, false},
  228. {"false query", mustParse("azurekms:name=foo;vault=bar?hsm=false"), args{"hsm"}, false},
  229. {"empty", mustParse("azurekms:name=foo;vault=bar;hsm=?bar=true"), args{"hsm"}, false},
  230. {"missing", mustParse("azurekms:name=foo;vault=bar"), args{"hsm"}, false},
  231. }
  232. for _, tt := range tests {
  233. t.Run(tt.name, func(t *testing.T) {
  234. if got := tt.uri.GetBool(tt.args.key); got != tt.want {
  235. t.Errorf("URI.GetBool() = %v, want %v", got, tt.want)
  236. }
  237. })
  238. }
  239. }
  240. func TestURI_GetEncoded(t *testing.T) {
  241. mustParse := func(s string) *URI {
  242. u, err := Parse(s)
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. return u
  247. }
  248. type args struct {
  249. key string
  250. }
  251. tests := []struct {
  252. name string
  253. uri *URI
  254. args args
  255. want []byte
  256. }{
  257. {"ok", mustParse("yubikey:slot-id=9a"), args{"slot-id"}, []byte{0x9a}},
  258. {"ok first", mustParse("yubikey:slot-id=9a9b;slot-id=9b"), args{"slot-id"}, []byte{0x9a, 0x9b}},
  259. {"ok percent", mustParse("yubikey:slot-id=9a;foo=%9a%9b%9c"), args{"foo"}, []byte{0x9a, 0x9b, 0x9c}},
  260. {"ok in query", mustParse("yubikey:slot-id=9a?foo=9a"), args{"foo"}, []byte{0x9a}},
  261. {"ok in query percent", mustParse("yubikey:slot-id=9a?foo=%9a"), args{"foo"}, []byte{0x9a}},
  262. {"ok missing", mustParse("yubikey:slot-id=9a"), args{"foo"}, nil},
  263. {"ok missing query", mustParse("yubikey:slot-id=9a?bar=zar"), args{"foo"}, nil},
  264. {"ok no hex", mustParse("yubikey:slot-id=09a?bar=zar"), args{"slot-id"}, []byte{'0', '9', 'a'}},
  265. }
  266. for _, tt := range tests {
  267. t.Run(tt.name, func(t *testing.T) {
  268. got := tt.uri.GetEncoded(tt.args.key)
  269. if !reflect.DeepEqual(got, tt.want) {
  270. t.Errorf("URI.GetEncoded() = %v, want %v", got, tt.want)
  271. }
  272. })
  273. }
  274. }
  275. func TestURI_Pin(t *testing.T) {
  276. mustParse := func(s string) *URI {
  277. u, err := Parse(s)
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. return u
  282. }
  283. tests := []struct {
  284. name string
  285. uri *URI
  286. want string
  287. }{
  288. {"from value", mustParse("pkcs11:id=%72%73?pin-value=0123456789"), "0123456789"},
  289. {"from source", mustParse("pkcs11:id=%72%73?pin-source=testdata/pin.txt"), "trim-this-pin"},
  290. {"from missing", mustParse("pkcs11:id=%72%73"), ""},
  291. {"from source missing", mustParse("pkcs11:id=%72%73?pin-source=testdata/foo.txt"), ""},
  292. }
  293. for _, tt := range tests {
  294. t.Run(tt.name, func(t *testing.T) {
  295. if got := tt.uri.Pin(); got != tt.want {
  296. t.Errorf("URI.Pin() = %v, want %v", got, tt.want)
  297. }
  298. })
  299. }
  300. }
  301. func TestURI_String(t *testing.T) {
  302. mustParse := func(s string) *URI {
  303. u, err := Parse(s)
  304. if err != nil {
  305. t.Fatal(err)
  306. }
  307. return u
  308. }
  309. tests := []struct {
  310. name string
  311. uri *URI
  312. want string
  313. }{
  314. {"ok new", New("yubikey", url.Values{"slot-id": []string{"9a"}, "foo": []string{"bar"}}), "yubikey:foo=bar;slot-id=9a"},
  315. {"ok parse", mustParse("yubikey:slot-id=9a;foo=bar?bar=zar"), "yubikey:slot-id=9a;foo=bar?bar=zar"},
  316. }
  317. for _, tt := range tests {
  318. t.Run(tt.name, func(t *testing.T) {
  319. if got := tt.uri.String(); got != tt.want {
  320. t.Errorf("URI.String() = %v, want %v", got, tt.want)
  321. }
  322. })
  323. }
  324. }