| 1 | package packageurl |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // Verifies that qualifier values are properly percent-encoded. |
| 9 | func TestQualifierValueEncoding(t *testing.T) { |
| 10 | tests := []struct { |
| 11 | name string |
| 12 | given PackageURL |
| 13 | want string |
| 14 | }{ |
| 15 | { |
| 16 | name: "must percent-encode a qualifier value", |
| 17 | given: PackageURL{ |
| 18 | Type: "generic", |
| 19 | Name: "openssl", |
| 20 | Version: "1.2.3", |
| 21 | Qualifiers: Qualifiers{ |
| 22 | {Key: "download_url", Value: "dl.site.org/openssl"}, |
| 23 | }, |
| 24 | }, |
| 25 | want: "pkg:generic/[email protected]?download_url=dl.site.org%2Fopenssl", |
| 26 | }, |
| 27 | |
| 28 | { |
| 29 | name: "must not percent-encode colons in qualifier values", |
| 30 | given: PackageURL{ |
| 31 | Type: "generic", |
| 32 | Name: "openssl", |
| 33 | Version: "1.2.3", |
| 34 | Qualifiers: Qualifiers{ |
| 35 | {Key: "download_url", Value: "dl.site.org:443/openssl"}, |
| 36 | }, |
| 37 | }, |
| 38 | want: "pkg:generic/[email protected]?download_url=dl.site.org:443%2Fopenssl", |
| 39 | }, |
| 40 | |
| 41 | // Note: checks that the use of [url.QueryEscape] does not lead to a space being |
| 42 | // encoded as "+". |
| 43 | { |
| 44 | name: "must properly percent-encode a plus", |
| 45 | given: PackageURL{ |
| 46 | Type: "generic", |
| 47 | Name: "openssl", |
| 48 | Version: "1.2.3", |
| 49 | Qualifiers: Qualifiers{ |
| 50 | {Key: "download_url", Value: "dl.site.org:443/openssl secure"}, |
| 51 | }, |
| 52 | }, |
| 53 | want: "pkg:generic/[email protected]?download_url=dl.site.org:443%2Fopenssl%20secure", |
| 54 | }, |
| 55 | |
| 56 | { |
| 57 | name: "must order qualifier values lexicographically by key", |
| 58 | given: PackageURL{ |
| 59 | Type: "generic", |
| 60 | Name: "openssl", |
| 61 | Version: "1.2.3", |
| 62 | Qualifiers: Qualifiers{ |
| 63 | {Key: "download_url", Value: "dl.site.org"}, |
| 64 | {Key: "checksum", Value: "sha256:abc123"}, |
| 65 | }, |
| 66 | }, |
| 67 | want: "pkg:generic/[email protected]?checksum=sha256:abc123&download_url=dl.site.org", |
| 68 | }, |
| 69 | |
| 70 | { |
| 71 | name: "must percent-encode separators in qualifier value", |
| 72 | given: PackageURL{ |
| 73 | Type: "generic", |
| 74 | Name: "openssl", |
| 75 | Version: "1.2.3", |
| 76 | Qualifiers: Qualifiers{ |
| 77 | {Key: "download_url", Value: "https://dl.site.org:443/openssl+secure?type=zip&fast=yes"}, |
| 78 | {Key: "checksum", Value: "sha256:abc123"}, |
| 79 | }, |
| 80 | }, |
| 81 | want: "pkg:generic/[email protected]?checksum=sha256:abc123&download_url=https:%2F%2Fdl.site.org:443%2Fopenssl%2Bsecure%3Ftype%3Dzip%26fast%3Dyes", |
| 82 | }, |
| 83 | |
| 84 | { |
| 85 | name: "must escape a qualifier value", |
| 86 | given: PackageURL{ |
| 87 | Type: "generic", |
| 88 | Name: "openssl", |
| 89 | Version: "1.2.3", |
| 90 | Qualifiers: Qualifiers{ |
| 91 | {Key: "download_url", Value: "dl.site.org/openssl"}, |
| 92 | }, |
| 93 | }, |
| 94 | want: "pkg:generic/[email protected]?download_url=dl.site.org%2Fopenssl", |
| 95 | }, |
| 96 | } |
| 97 | |
| 98 | for _, tc := range tests { |
| 99 | t.Run(tc.name, func(t *testing.T) { |
| 100 | got := tc.given.String() |
| 101 | if tc.want != got { |
| 102 | t.Logf("'%s' test failed: wanted: '%s', got '%s'", tc.name, tc.want, got) |
| 103 | t.Fail() |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Exercise the [escapeQualifier] function. |
| 110 | func TestPercentEncode(t *testing.T) { |
| 111 | tests := []struct { |
| 112 | name string |
| 113 | givenValue string |
| 114 | want string |
| 115 | }{ |
| 116 | { |
| 117 | name: "must not percent-encode alphanumeric characters", |
| 118 | givenValue: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", |
| 119 | want: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", |
| 120 | }, |
| 121 | |
| 122 | { |
| 123 | name: "must not percent-encode punctuation characters", |
| 124 | givenValue: ".-_~", |
| 125 | want: ".-_~", |
| 126 | }, |
| 127 | |
| 128 | { |
| 129 | name: "must not percent-encode colon", |
| 130 | givenValue: ":", |
| 131 | want: ":", |
| 132 | }, |
| 133 | |
| 134 | { |
| 135 | name: "must percent-encode separator characters other than colon", |
| 136 | givenValue: "/@?=&#", |
| 137 | // slash: %2F, at: %40, question mark: %3F, equal: %3D, ampersand: %26, |
| 138 | // pound-sign: %23 |
| 139 | want: "%2F%40%3F%3D%26%23", |
| 140 | }, |
| 141 | |
| 142 | { |
| 143 | name: "must percent-encode a plus", |
| 144 | givenValue: "+", |
| 145 | want: "%2B", |
| 146 | }, |
| 147 | |
| 148 | { |
| 149 | name: "must percent-encode a whitespace", |
| 150 | givenValue: " ", |
| 151 | want: "%20", |
| 152 | }, |
| 153 | } |
| 154 | |
| 155 | for _, tc := range tests { |
| 156 | t.Run(tc.name, func(t *testing.T) { |
| 157 | var b strings.Builder |
| 158 | escapeQualifier(&b, tc.givenValue) |
| 159 | got := b.String() |
| 160 | if tc.want != got { |
| 161 | t.Logf("'%s' test failed: wanted: '%s', got '%s'", tc.name, tc.want, got) |
| 162 | t.Fail() |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | } |