| 1 | package auth |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestBuildCanonicalizedHeader(t *testing.T) { |
| 10 | testData := []struct { |
| 11 | Input http.Header |
| 12 | Expected string |
| 13 | }{ |
| 14 | { |
| 15 | // no headers |
| 16 | Expected: "", |
| 17 | Input: map[string][]string{ |
| 18 | "": {""}, |
| 19 | }, |
| 20 | }, |
| 21 | { |
| 22 | // no x-ms headers |
| 23 | Expected: "", |
| 24 | Input: map[string][]string{ |
| 25 | "panda": {"pops"}, |
| 26 | }, |
| 27 | }, |
| 28 | { |
| 29 | // only a single x-ms header |
| 30 | Expected: "x-ms-panda:nom", |
| 31 | Input: map[string][]string{ |
| 32 | "x-ms-panda": {"nom"}, |
| 33 | }, |
| 34 | }, |
| 35 | { |
| 36 | // multiple x-ms headers |
| 37 | Expected: "x-ms-panda:nom\nx-ms-tiger:rawr", |
| 38 | Input: map[string][]string{ |
| 39 | "x-ms-panda": {"nom"}, |
| 40 | "x-ms-tiger": {"rawr"}, |
| 41 | }, |
| 42 | }, |
| 43 | { |
| 44 | // multiple x-ms headers, out of order |
| 45 | Expected: "x-ms-panda:nom\nx-ms-tiger:rawr", |
| 46 | Input: map[string][]string{ |
| 47 | "x-ms-tiger": {"rawr"}, |
| 48 | "x-ms-panda": {"nom"}, |
| 49 | }, |
| 50 | }, |
| 51 | { |
| 52 | // mixed headers (some ms, some non-ms) |
| 53 | Expected: "x-ms-panda:nom\nx-ms-tiger:rawr", |
| 54 | Input: map[string][]string{ |
| 55 | "x-ms-tiger": {"rawr"}, |
| 56 | "panda": {"pops"}, |
| 57 | "x-ms-panda": {"nom"}, |
| 58 | }, |
| 59 | }, |
| 60 | { |
| 61 | // casing |
| 62 | Expected: "x-ms-panda:nom\nx-ms-tiger:rawr", |
| 63 | Input: map[string][]string{ |
| 64 | "X-Ms-Tiger": {"rawr"}, |
| 65 | "X-Ms-Panda": {"nom"}, |
| 66 | }, |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | for _, v := range testData { |
| 71 | actual := buildCanonicalizedHeader(v.Input) |
| 72 | if actual != v.Expected { |
| 73 | t.Fatalf("Expected %q but got %q", v.Expected, actual) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestBuildCanonicalizedResource(t *testing.T) { |
| 79 | testData := []struct { |
| 80 | name string |
| 81 | accountName string |
| 82 | uri string |
| 83 | expected string |
| 84 | expectError bool |
| 85 | }{ |
| 86 | { |
| 87 | name: "invalid uri", |
| 88 | accountName: "example", |
| 89 | uri: "://example.com", |
| 90 | expected: "", |
| 91 | expectError: true, |
| 92 | }, |
| 93 | { |
| 94 | name: "storage emulator doesn't get prefix", |
| 95 | accountName: StorageEmulatorAccountName, |
| 96 | uri: "http://www.example.com/foo", |
| 97 | expected: "/foo", |
| 98 | }, |
| 99 | { |
| 100 | name: "non storage emulator gets prefix", |
| 101 | accountName: StorageEmulatorAccountName + "test", |
| 102 | uri: "http://www.example.com/foo", |
| 103 | expected: "/" + StorageEmulatorAccountName + "test/foo", |
| 104 | }, |
| 105 | { |
| 106 | name: "uri encoding", |
| 107 | accountName: "example", |
| 108 | uri: "<hello>", |
| 109 | expected: "/example%3Chello%3E", |
| 110 | }, |
| 111 | { |
| 112 | name: "comp-arg", |
| 113 | accountName: "example", |
| 114 | uri: "/endpoint?first=true&comp=bar&second=false&third=panda", |
| 115 | expected: "/example/endpoint?comp=bar", |
| 116 | }, |
| 117 | { |
| 118 | name: "arguments", |
| 119 | accountName: "example", |
| 120 | uri: "/endpoint?first=true&second=false&third=panda", |
| 121 | expected: "/example/endpoint", |
| 122 | }, |
| 123 | } |
| 124 | |
| 125 | for _, test := range testData { |
| 126 | t.Logf("Test %q", test.name) |
| 127 | actual, err := buildCanonicalizedResource(test.uri, test.accountName) |
| 128 | if err != nil { |
| 129 | if test.expectError { |
| 130 | continue |
| 131 | } |
| 132 | |
| 133 | t.Fatalf("Error: %s", err) |
| 134 | } |
| 135 | |
| 136 | if *actual != test.expected { |
| 137 | t.Fatalf("Expected %q but got %q", test.expected, *actual) |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func TestFormatSharedKeyLiteAuthorizationHeader(t *testing.T) { |
| 143 | testData := []struct { |
| 144 | name string |
| 145 | accountName string |
| 146 | accountKey string |
| 147 | expected string |
| 148 | }{ |
| 149 | { |
| 150 | name: "primary", |
| 151 | accountName: "account1", |
| 152 | accountKey: "examplekey", |
| 153 | expected: "SharedKeyLite account1:examplekey", |
| 154 | }, |
| 155 | { |
| 156 | name: "secondary", |
| 157 | accountName: "account1-secondary", |
| 158 | accountKey: "examplekey", |
| 159 | expected: "SharedKeyLite account1:examplekey", |
| 160 | }, |
| 161 | } |
| 162 | |
| 163 | for _, test := range testData { |
| 164 | t.Logf("Test: %q", test.name) |
| 165 | actual := formatSharedKeyLiteAuthorizationHeader(test.accountName, test.accountKey) |
| 166 | |
| 167 | if actual != test.expected { |
| 168 | t.Fatalf("Expected %q but got %q", test.expected, actual) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func TestHMAC(t *testing.T) { |
| 174 | testData := []struct { |
| 175 | Expected string |
| 176 | StorageAccountKey string |
| 177 | CanonicalizedString string |
| 178 | }{ |
| 179 | { |
| 180 | // When Storage Key isn't base-64 encoded |
| 181 | Expected: "", |
| 182 | StorageAccountKey: "bar", |
| 183 | CanonicalizedString: "foobarzoo", |
| 184 | }, |
| 185 | { |
| 186 | // Valid |
| 187 | Expected: "h5U0ATVX6SpbFX1H6GNuxIMeXXCILLoIvhflPtuQZ30=", |
| 188 | StorageAccountKey: base64.StdEncoding.EncodeToString([]byte("bar")), |
| 189 | CanonicalizedString: "foobarzoo", |
| 190 | }, |
| 191 | } |
| 192 | |
| 193 | for _, v := range testData { |
| 194 | actual := hmacValue(v.StorageAccountKey, v.CanonicalizedString) |
| 195 | if actual != v.Expected { |
| 196 | t.Fatalf("Expected %q but got %q", v.Expected, actual) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestTestPrepareHeadersForRequest(t *testing.T) { |
| 202 | request, err := http.NewRequest("GET", "http://example.com", nil) |
| 203 | if err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | |
| 207 | headers := []string{ |
| 208 | "Date", |
| 209 | "X-Ms-Date", |
| 210 | } |
| 211 | |
| 212 | for _, header := range headers { |
| 213 | existingVal := request.Header.Get(header) |
| 214 | if existingVal != "" { |
| 215 | t.Fatalf("%q had a value prior to being set: %q", header, existingVal) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | prepareHeadersForRequest(request) |
| 220 | |
| 221 | for _, header := range headers { |
| 222 | updatedVal := request.Header.Get(header) |
| 223 | if updatedVal == "" { |
| 224 | t.Fatalf("%q didn't have a value after being set: %q", header, updatedVal) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func TestPrepareHeadersForRequestWithNoneConfigured(t *testing.T) { |
| 230 | request, err := http.NewRequest("GET", "http://example.com", nil) |
| 231 | if err != nil { |
| 232 | t.Fatal(err) |
| 233 | } |
| 234 | |
| 235 | request.Header = nil |
| 236 | prepareHeadersForRequest(request) |
| 237 | |
| 238 | if request.Header == nil { |
| 239 | t.Fatalf("Expected `request.Header` to not be nil, but it was!") |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | func TestPrimaryStorageAccountName(t *testing.T) { |
| 244 | testData := []struct { |
| 245 | Expected string |
| 246 | Input string |
| 247 | }{ |
| 248 | { |
| 249 | // Empty |
| 250 | Expected: "", |
| 251 | Input: "", |
| 252 | }, |
| 253 | { |
| 254 | // Primary |
| 255 | Expected: "bar", |
| 256 | Input: "bar", |
| 257 | }, |
| 258 | { |
| 259 | // Secondary |
| 260 | Expected: "bar", |
| 261 | Input: "bar-secondary", |
| 262 | }, |
| 263 | } |
| 264 | |
| 265 | for _, v := range testData { |
| 266 | actual := primaryStorageAccountName(v.Input) |
| 267 | if actual != v.Expected { |
| 268 | t.Fatalf("Expected %q but got %q", v.Expected, actual) |
| 269 | } |
| 270 | } |
| 271 | } |