options_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. package policy
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_normalizeAndValidateCommonName(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. constraint string
  12. want string
  13. wantErr bool
  14. }{
  15. {
  16. name: "fail/empty-constraint",
  17. constraint: "",
  18. want: "",
  19. wantErr: true,
  20. },
  21. {
  22. name: "fail/wildcard",
  23. constraint: "*",
  24. want: "",
  25. wantErr: true,
  26. },
  27. {
  28. name: "ok",
  29. constraint: "step",
  30. want: "step",
  31. wantErr: false,
  32. },
  33. }
  34. for _, tt := range tests {
  35. t.Run(tt.name, func(t *testing.T) {
  36. got, err := normalizeAndValidateCommonName(tt.constraint)
  37. if (err != nil) != tt.wantErr {
  38. t.Errorf("normalizeAndValidateCommonName() error = %v, wantErr %v", err, tt.wantErr)
  39. return
  40. }
  41. if got != tt.want {
  42. t.Errorf("normalizeAndValidateCommonName() = %v, want %v", got, tt.want)
  43. }
  44. })
  45. }
  46. }
  47. func Test_normalizeAndValidateDNSDomainConstraint(t *testing.T) {
  48. tests := []struct {
  49. name string
  50. constraint string
  51. want string
  52. wantErr bool
  53. }{
  54. {
  55. name: "fail/empty-constraint",
  56. constraint: "",
  57. want: "",
  58. wantErr: true,
  59. },
  60. {
  61. name: "fail/wildcard-partial-label",
  62. constraint: "*xxxx.local",
  63. want: "",
  64. wantErr: true,
  65. },
  66. {
  67. name: "fail/wildcard-in-the-middle",
  68. constraint: "x.*.local",
  69. want: "",
  70. wantErr: true,
  71. },
  72. {
  73. name: "fail/empty-label",
  74. constraint: "..local",
  75. want: "",
  76. wantErr: true,
  77. },
  78. {
  79. name: "fail/empty-reverse",
  80. constraint: ".",
  81. want: "",
  82. wantErr: true,
  83. },
  84. {
  85. name: "fail/no-asterisk",
  86. constraint: ".example.com",
  87. want: "",
  88. wantErr: true,
  89. },
  90. {
  91. name: "fail/idna-internationalized-domain-name-lookup",
  92. constraint: `\00.local`, // invalid IDNA ASCII character
  93. want: "",
  94. wantErr: true,
  95. },
  96. {
  97. name: "ok/wildcard",
  98. constraint: "*.local",
  99. want: ".local",
  100. wantErr: false,
  101. },
  102. {
  103. name: "ok/specific-domain",
  104. constraint: "example.local",
  105. want: "example.local",
  106. wantErr: false,
  107. },
  108. {
  109. name: "ok/idna-internationalized-domain-name-punycode",
  110. constraint: "*.xn--fsq.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  111. want: ".xn--fsq.jp",
  112. wantErr: false,
  113. },
  114. {
  115. name: "ok/idna-internationalized-domain-name-lookup-transformed",
  116. constraint: "*.例.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  117. want: ".xn--fsq.jp",
  118. wantErr: false,
  119. },
  120. }
  121. for _, tt := range tests {
  122. t.Run(tt.name, func(t *testing.T) {
  123. got, err := normalizeAndValidateDNSDomainConstraint(tt.constraint)
  124. if (err != nil) != tt.wantErr {
  125. t.Errorf("normalizeAndValidateDNSDomainConstraint() error = %v, wantErr %v", err, tt.wantErr)
  126. }
  127. if got != tt.want {
  128. t.Errorf("normalizeAndValidateDNSDomainConstraint() = %v, want %v", got, tt.want)
  129. }
  130. })
  131. }
  132. }
  133. func Test_normalizeAndValidateEmailConstraint(t *testing.T) {
  134. tests := []struct {
  135. name string
  136. constraint string
  137. want string
  138. wantErr bool
  139. }{
  140. {
  141. name: "fail/empty-constraint",
  142. constraint: "",
  143. want: "",
  144. wantErr: true,
  145. },
  146. {
  147. name: "fail/asterisk",
  148. constraint: "*.local",
  149. want: "",
  150. wantErr: true,
  151. },
  152. {
  153. name: "fail/period",
  154. constraint: ".local",
  155. want: "",
  156. wantErr: true,
  157. },
  158. {
  159. name: "fail/@period",
  160. constraint: "@.local",
  161. want: "",
  162. wantErr: true,
  163. },
  164. {
  165. name: "fail/too-many-@s",
  166. constraint: "@local@example.com",
  167. want: "",
  168. wantErr: true,
  169. },
  170. {
  171. name: "fail/parse-mailbox",
  172. constraint: "mail@example.com" + string(byte(0)),
  173. want: "",
  174. wantErr: true,
  175. },
  176. {
  177. name: "fail/idna-internationalized-domain",
  178. constraint: "mail@xn--bla.local",
  179. want: "",
  180. wantErr: true,
  181. },
  182. {
  183. name: "fail/idna-internationalized-domain-name-lookup",
  184. constraint: `\00local`,
  185. want: "",
  186. wantErr: true,
  187. },
  188. {
  189. name: "fail/parse-domain",
  190. constraint: "x..example.com",
  191. want: "",
  192. wantErr: true,
  193. },
  194. {
  195. name: "ok/wildcard",
  196. constraint: "@local",
  197. want: "local",
  198. wantErr: false,
  199. },
  200. {
  201. name: "ok/specific-mail",
  202. constraint: "mail@local",
  203. want: "mail@local",
  204. wantErr: false,
  205. },
  206. // TODO(hs): fix the below; doesn't get past parseRFC2821Mailbox; I think it should be allowed.
  207. // {
  208. // name: "ok/idna-internationalized-local",
  209. // constraint: `bücher@local`,
  210. // want: "bücher@local",
  211. // wantErr: false,
  212. // },
  213. }
  214. for _, tt := range tests {
  215. t.Run(tt.name, func(t *testing.T) {
  216. got, err := normalizeAndValidateEmailConstraint(tt.constraint)
  217. if (err != nil) != tt.wantErr {
  218. t.Errorf("normalizeAndValidateEmailConstraint() error = %v, wantErr %v", err, tt.wantErr)
  219. }
  220. if got != tt.want {
  221. t.Errorf("normalizeAndValidateEmailConstraint() = %v, want %v", got, tt.want)
  222. }
  223. })
  224. }
  225. }
  226. func TestNew(t *testing.T) {
  227. type test struct {
  228. options []NamePolicyOption
  229. want *NamePolicyEngine
  230. wantErr bool
  231. }
  232. var tests = map[string]func(t *testing.T) test{
  233. "fail/with-permitted-common-name": func(t *testing.T) test {
  234. return test{
  235. options: []NamePolicyOption{
  236. WithPermittedCommonNames("*"),
  237. },
  238. want: nil,
  239. wantErr: true,
  240. }
  241. },
  242. "fail/with-excluded-common-name": func(t *testing.T) test {
  243. return test{
  244. options: []NamePolicyOption{
  245. WithExcludedCommonNames(""),
  246. },
  247. want: nil,
  248. wantErr: true,
  249. }
  250. },
  251. "fail/with-permitted-dns-domains": func(t *testing.T) test {
  252. return test{
  253. options: []NamePolicyOption{
  254. WithPermittedDNSDomains("**.local"),
  255. },
  256. want: nil,
  257. wantErr: true,
  258. }
  259. },
  260. "fail/with-excluded-dns-domains": func(t *testing.T) test {
  261. return test{
  262. options: []NamePolicyOption{
  263. WithExcludedDNSDomains("**.local"),
  264. },
  265. want: nil,
  266. wantErr: true,
  267. }
  268. },
  269. "fail/with-permitted-cidrs": func(t *testing.T) test {
  270. return test{
  271. options: []NamePolicyOption{
  272. WithPermittedCIDRs("127.0.0.1//24"),
  273. },
  274. want: nil,
  275. wantErr: true,
  276. }
  277. },
  278. "fail/with-excluded-cidrs": func(t *testing.T) test {
  279. return test{
  280. options: []NamePolicyOption{
  281. WithExcludedCIDRs("127.0.0.1//24"),
  282. },
  283. want: nil,
  284. wantErr: true,
  285. }
  286. },
  287. "fail/with-permitted-ipsOrCIDRs-cidr": func(t *testing.T) test {
  288. return test{
  289. options: []NamePolicyOption{
  290. WithPermittedIPsOrCIDRs("127.0.0.1//24"),
  291. },
  292. want: nil,
  293. wantErr: true,
  294. }
  295. },
  296. "fail/with-permitted-ipsOrCIDRs-ip": func(t *testing.T) test {
  297. return test{
  298. options: []NamePolicyOption{
  299. WithPermittedIPsOrCIDRs("127.0.0:1"),
  300. },
  301. want: nil,
  302. wantErr: true,
  303. }
  304. },
  305. "fail/with-excluded-ipsOrCIDRs-cidr": func(t *testing.T) test {
  306. return test{
  307. options: []NamePolicyOption{
  308. WithExcludedIPsOrCIDRs("127.0.0.1//24"),
  309. },
  310. want: nil,
  311. wantErr: true,
  312. }
  313. },
  314. "fail/with-excluded-ipsOrCIDRs-ip": func(t *testing.T) test {
  315. return test{
  316. options: []NamePolicyOption{
  317. WithExcludedIPsOrCIDRs("127.0.0:1"),
  318. },
  319. want: nil,
  320. wantErr: true,
  321. }
  322. },
  323. "fail/with-permitted-emails": func(t *testing.T) test {
  324. return test{
  325. options: []NamePolicyOption{
  326. WithPermittedEmailAddresses("*.local"),
  327. },
  328. want: nil,
  329. wantErr: true,
  330. }
  331. },
  332. "fail/with-excluded-emails": func(t *testing.T) test {
  333. return test{
  334. options: []NamePolicyOption{
  335. WithExcludedEmailAddresses("*.local"),
  336. },
  337. want: nil,
  338. wantErr: true,
  339. }
  340. },
  341. "fail/with-permitted-uris": func(t *testing.T) test {
  342. return test{
  343. options: []NamePolicyOption{
  344. WithPermittedURIDomains("**.local"),
  345. },
  346. want: nil,
  347. wantErr: true,
  348. }
  349. },
  350. "fail/with-excluded-uris": func(t *testing.T) test {
  351. return test{
  352. options: []NamePolicyOption{
  353. WithExcludedURIDomains("**.local"),
  354. },
  355. want: nil,
  356. wantErr: true,
  357. }
  358. },
  359. "ok/default": func(t *testing.T) test {
  360. return test{
  361. options: []NamePolicyOption{},
  362. want: &NamePolicyEngine{},
  363. wantErr: false,
  364. }
  365. },
  366. "ok/subject-verification": func(t *testing.T) test {
  367. options := []NamePolicyOption{
  368. WithSubjectCommonNameVerification(),
  369. }
  370. return test{
  371. options: options,
  372. want: &NamePolicyEngine{
  373. verifySubjectCommonName: true,
  374. },
  375. wantErr: false,
  376. }
  377. },
  378. "ok/literal-wildcards": func(t *testing.T) test {
  379. options := []NamePolicyOption{
  380. WithAllowLiteralWildcardNames(),
  381. }
  382. return test{
  383. options: options,
  384. want: &NamePolicyEngine{
  385. allowLiteralWildcardNames: true,
  386. },
  387. wantErr: false,
  388. }
  389. },
  390. "ok/with-permitted-dns-wildcard-domains": func(t *testing.T) test {
  391. options := []NamePolicyOption{
  392. WithPermittedDNSDomains("*.local", "*.example.com"),
  393. }
  394. return test{
  395. options: options,
  396. want: &NamePolicyEngine{
  397. permittedDNSDomains: []string{".local", ".example.com"},
  398. numberOfDNSDomainConstraints: 2,
  399. totalNumberOfPermittedConstraints: 2,
  400. totalNumberOfConstraints: 2,
  401. },
  402. wantErr: false,
  403. }
  404. },
  405. "ok/with-excluded-dns-domains": func(t *testing.T) test {
  406. options := []NamePolicyOption{
  407. WithExcludedDNSDomains("*.local", "*.example.com"),
  408. }
  409. return test{
  410. options: options,
  411. want: &NamePolicyEngine{
  412. excludedDNSDomains: []string{".local", ".example.com"},
  413. numberOfDNSDomainConstraints: 2,
  414. totalNumberOfExcludedConstraints: 2,
  415. totalNumberOfConstraints: 2,
  416. },
  417. wantErr: false,
  418. }
  419. },
  420. "ok/with-permitted-ip-ranges": func(t *testing.T) test {
  421. _, nw1, err := net.ParseCIDR("127.0.0.1/24")
  422. assert.NoError(t, err)
  423. _, nw2, err := net.ParseCIDR("192.168.0.1/24")
  424. assert.NoError(t, err)
  425. options := []NamePolicyOption{
  426. WithPermittedIPRanges(nw1, nw2),
  427. }
  428. return test{
  429. options: options,
  430. want: &NamePolicyEngine{
  431. permittedIPRanges: []*net.IPNet{
  432. nw1, nw2,
  433. },
  434. numberOfIPRangeConstraints: 2,
  435. totalNumberOfPermittedConstraints: 2,
  436. totalNumberOfConstraints: 2,
  437. },
  438. wantErr: false,
  439. }
  440. },
  441. "ok/with-excluded-ip-ranges": func(t *testing.T) test {
  442. _, nw1, err := net.ParseCIDR("127.0.0.1/24")
  443. assert.NoError(t, err)
  444. _, nw2, err := net.ParseCIDR("192.168.0.1/24")
  445. assert.NoError(t, err)
  446. options := []NamePolicyOption{
  447. WithExcludedIPRanges(nw1, nw2),
  448. }
  449. return test{
  450. options: options,
  451. want: &NamePolicyEngine{
  452. excludedIPRanges: []*net.IPNet{
  453. nw1, nw2,
  454. },
  455. numberOfIPRangeConstraints: 2,
  456. totalNumberOfExcludedConstraints: 2,
  457. totalNumberOfConstraints: 2,
  458. },
  459. wantErr: false,
  460. }
  461. },
  462. "ok/with-permitted-cidrs": func(t *testing.T) test {
  463. _, nw1, err := net.ParseCIDR("127.0.0.1/24")
  464. assert.NoError(t, err)
  465. _, nw2, err := net.ParseCIDR("192.168.0.1/24")
  466. assert.NoError(t, err)
  467. options := []NamePolicyOption{
  468. WithPermittedCIDRs("127.0.0.1/24", "192.168.0.1/24"),
  469. }
  470. return test{
  471. options: options,
  472. want: &NamePolicyEngine{
  473. permittedIPRanges: []*net.IPNet{
  474. nw1, nw2,
  475. },
  476. numberOfIPRangeConstraints: 2,
  477. totalNumberOfPermittedConstraints: 2,
  478. totalNumberOfConstraints: 2,
  479. },
  480. wantErr: false,
  481. }
  482. },
  483. "ok/with-excluded-cidrs": func(t *testing.T) test {
  484. _, nw1, err := net.ParseCIDR("127.0.0.1/24")
  485. assert.NoError(t, err)
  486. _, nw2, err := net.ParseCIDR("192.168.0.1/24")
  487. assert.NoError(t, err)
  488. options := []NamePolicyOption{
  489. WithExcludedCIDRs("127.0.0.1/24", "192.168.0.1/24"),
  490. }
  491. return test{
  492. options: options,
  493. want: &NamePolicyEngine{
  494. excludedIPRanges: []*net.IPNet{
  495. nw1, nw2,
  496. },
  497. numberOfIPRangeConstraints: 2,
  498. totalNumberOfExcludedConstraints: 2,
  499. totalNumberOfConstraints: 2,
  500. },
  501. wantErr: false,
  502. }
  503. },
  504. "ok/with-permitted-ipsOrCIDRs-cidr": func(t *testing.T) test {
  505. _, nw1, err := net.ParseCIDR("127.0.0.1/24")
  506. assert.NoError(t, err)
  507. _, nw2, err := net.ParseCIDR("192.168.0.31/32")
  508. assert.NoError(t, err)
  509. _, nw3, err := net.ParseCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/128")
  510. assert.NoError(t, err)
  511. options := []NamePolicyOption{
  512. WithPermittedIPsOrCIDRs("127.0.0.1/24", "192.168.0.31", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  513. }
  514. return test{
  515. options: options,
  516. want: &NamePolicyEngine{
  517. permittedIPRanges: []*net.IPNet{
  518. nw1, nw2, nw3,
  519. },
  520. numberOfIPRangeConstraints: 3,
  521. totalNumberOfPermittedConstraints: 3,
  522. totalNumberOfConstraints: 3,
  523. },
  524. wantErr: false,
  525. }
  526. },
  527. "ok/with-excluded-ipsOrCIDRs-cidr": func(t *testing.T) test {
  528. _, nw1, err := net.ParseCIDR("127.0.0.1/24")
  529. assert.NoError(t, err)
  530. _, nw2, err := net.ParseCIDR("192.168.0.31/32")
  531. assert.NoError(t, err)
  532. _, nw3, err := net.ParseCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/128")
  533. assert.NoError(t, err)
  534. options := []NamePolicyOption{
  535. WithExcludedIPsOrCIDRs("127.0.0.1/24", "192.168.0.31", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  536. }
  537. return test{
  538. options: options,
  539. want: &NamePolicyEngine{
  540. excludedIPRanges: []*net.IPNet{
  541. nw1, nw2, nw3,
  542. },
  543. numberOfIPRangeConstraints: 3,
  544. totalNumberOfExcludedConstraints: 3,
  545. totalNumberOfConstraints: 3,
  546. },
  547. wantErr: false,
  548. }
  549. },
  550. "ok/with-permitted-emails": func(t *testing.T) test {
  551. options := []NamePolicyOption{
  552. WithPermittedEmailAddresses("mail@local", "@example.com"),
  553. }
  554. return test{
  555. options: options,
  556. want: &NamePolicyEngine{
  557. permittedEmailAddresses: []string{"mail@local", "example.com"},
  558. numberOfEmailAddressConstraints: 2,
  559. totalNumberOfPermittedConstraints: 2,
  560. totalNumberOfConstraints: 2,
  561. },
  562. wantErr: false,
  563. }
  564. },
  565. "ok/with-excluded-emails": func(t *testing.T) test {
  566. options := []NamePolicyOption{
  567. WithExcludedEmailAddresses("mail@local", "@example.com"),
  568. }
  569. return test{
  570. options: options,
  571. want: &NamePolicyEngine{
  572. excludedEmailAddresses: []string{"mail@local", "example.com"},
  573. numberOfEmailAddressConstraints: 2,
  574. totalNumberOfExcludedConstraints: 2,
  575. totalNumberOfConstraints: 2,
  576. },
  577. wantErr: false,
  578. }
  579. },
  580. "ok/with-permitted-uris": func(t *testing.T) test {
  581. options := []NamePolicyOption{
  582. WithPermittedURIDomains("host.local", "*.example.com"),
  583. }
  584. return test{
  585. options: options,
  586. want: &NamePolicyEngine{
  587. permittedURIDomains: []string{"host.local", ".example.com"},
  588. numberOfURIDomainConstraints: 2,
  589. totalNumberOfPermittedConstraints: 2,
  590. totalNumberOfConstraints: 2,
  591. },
  592. wantErr: false,
  593. }
  594. },
  595. "ok/with-excluded-uris": func(t *testing.T) test {
  596. options := []NamePolicyOption{
  597. WithExcludedURIDomains("host.local", "*.example.com"),
  598. }
  599. return test{
  600. options: options,
  601. want: &NamePolicyEngine{
  602. excludedURIDomains: []string{"host.local", ".example.com"},
  603. numberOfURIDomainConstraints: 2,
  604. totalNumberOfExcludedConstraints: 2,
  605. totalNumberOfConstraints: 2,
  606. },
  607. wantErr: false,
  608. }
  609. },
  610. "ok/with-permitted-principals": func(t *testing.T) test {
  611. options := []NamePolicyOption{
  612. WithPermittedPrincipals("root", "ops"),
  613. }
  614. return test{
  615. options: options,
  616. want: &NamePolicyEngine{
  617. permittedPrincipals: []string{"root", "ops"},
  618. numberOfPrincipalConstraints: 2,
  619. totalNumberOfPermittedConstraints: 2,
  620. totalNumberOfConstraints: 2,
  621. },
  622. wantErr: false,
  623. }
  624. },
  625. "ok/with-excluded-principals": func(t *testing.T) test {
  626. options := []NamePolicyOption{
  627. WithExcludedPrincipals("root", "ops"),
  628. }
  629. return test{
  630. options: options,
  631. want: &NamePolicyEngine{
  632. excludedPrincipals: []string{"root", "ops"},
  633. numberOfPrincipalConstraints: 2,
  634. totalNumberOfExcludedConstraints: 2,
  635. totalNumberOfConstraints: 2,
  636. },
  637. wantErr: false,
  638. }
  639. },
  640. }
  641. for name, prep := range tests {
  642. tc := prep(t)
  643. t.Run(name, func(t *testing.T) {
  644. got, err := New(tc.options...)
  645. if (err != nil) != tc.wantErr {
  646. t.Errorf("New() error = %v, wantErr %v", err, tc.wantErr)
  647. return
  648. }
  649. if !cmp.Equal(tc.want, got, cmp.AllowUnexported(NamePolicyEngine{})) {
  650. t.Errorf("New() diff =\n %s", cmp.Diff(tc.want, got, cmp.AllowUnexported(NamePolicyEngine{})))
  651. }
  652. })
  653. }
  654. }
  655. func Test_normalizeAndValidateURIDomainConstraint(t *testing.T) {
  656. tests := []struct {
  657. name string
  658. constraint string
  659. want string
  660. wantErr bool
  661. }{
  662. {
  663. name: "fail/empty-constraint",
  664. constraint: "",
  665. want: "",
  666. wantErr: true,
  667. },
  668. {
  669. name: "fail/scheme-https",
  670. constraint: `https://*.local`,
  671. want: "",
  672. wantErr: true,
  673. },
  674. {
  675. name: "fail/too-many-asterisks",
  676. constraint: "**.local",
  677. want: "",
  678. wantErr: true,
  679. },
  680. {
  681. name: "fail/empty-label",
  682. constraint: "..local",
  683. want: "",
  684. wantErr: true,
  685. },
  686. {
  687. name: "fail/empty-reverse",
  688. constraint: ".",
  689. want: "",
  690. wantErr: true,
  691. },
  692. {
  693. name: "fail/domain-with-port",
  694. constraint: "host.local:8443",
  695. want: "",
  696. wantErr: true,
  697. },
  698. {
  699. name: "fail/no-asterisk",
  700. constraint: ".example.com",
  701. want: "",
  702. wantErr: true,
  703. },
  704. {
  705. name: "fail/ipv4",
  706. constraint: "127.0.0.1",
  707. want: "",
  708. wantErr: true,
  709. },
  710. {
  711. name: "fail/ipv6-brackets",
  712. constraint: "[::1]",
  713. want: "",
  714. wantErr: true,
  715. },
  716. {
  717. name: "fail/ipv6-no-brackets",
  718. constraint: "::1",
  719. want: "",
  720. wantErr: true,
  721. },
  722. {
  723. name: "fail/ipv6-no-brackets",
  724. constraint: "[::1",
  725. want: "",
  726. wantErr: true,
  727. },
  728. {
  729. name: "fail/idna-internationalized-domain-name-lookup",
  730. constraint: `\00local`,
  731. want: "",
  732. wantErr: true,
  733. },
  734. {
  735. name: "ok/wildcard",
  736. constraint: "*.local",
  737. want: ".local",
  738. wantErr: false,
  739. },
  740. {
  741. name: "ok/specific-domain",
  742. constraint: "example.local",
  743. want: "example.local",
  744. wantErr: false,
  745. },
  746. {
  747. name: "ok/idna-internationalized-domain-name-lookup",
  748. constraint: `*.bücher.example.com`,
  749. want: ".xn--bcher-kva.example.com",
  750. wantErr: false,
  751. },
  752. {
  753. // IDNA2003 vs. 2008 deviation: https://unicode.org/reports/tr46/#Deviations results
  754. // in a difference between Go 1.18 and lower versions. Go 1.18 expects ".xn--fa-hia.de"; not .fass.de.
  755. name: "ok/idna-internationalized-domain-name-lookup-deviation",
  756. constraint: `*.faß.de`,
  757. want: ".xn--fa-hia.de",
  758. wantErr: false,
  759. },
  760. }
  761. for _, tt := range tests {
  762. t.Run(tt.name, func(t *testing.T) {
  763. got, err := normalizeAndValidateURIDomainConstraint(tt.constraint)
  764. if (err != nil) != tt.wantErr {
  765. t.Errorf("normalizeAndValidateURIDomainConstraint() error = %v, wantErr %v", err, tt.wantErr)
  766. }
  767. if got != tt.want {
  768. t.Errorf("normalizeAndValidateURIDomainConstraint() = %v, want %v", got, tt.want)
  769. }
  770. })
  771. }
  772. }