| 11 | ) |
| 12 | |
| 13 | func TestDecodeAuthConfig(t *testing.T) { |
| 14 | tests := []struct { |
| 15 | doc string |
| 16 | input string |
| 17 | inputBase64 string |
| 18 | expected registry.AuthConfig |
| 19 | expectedErr string |
| 20 | }{ |
| 21 | { |
| 22 | doc: "empty", |
| 23 | input: ``, |
| 24 | inputBase64: ``, |
| 25 | expected: registry.AuthConfig{}, |
| 26 | }, |
| 27 | { |
| 28 | doc: "empty JSON", |
| 29 | input: `{}`, |
| 30 | inputBase64: `e30=`, |
| 31 | expected: registry.AuthConfig{}, |
| 32 | }, |
| 33 | { |
| 34 | doc: "malformed JSON", |
| 35 | input: `{`, |
| 36 | inputBase64: `ew==`, |
| 37 | expected: registry.AuthConfig{}, |
| 38 | expectedErr: `invalid X-Registry-Auth header: invalid JSON: unexpected EOF`, |
| 39 | }, |
| 40 | { |
| 41 | doc: "test authConfig", |
| 42 | input: `{"username":"testuser","password":"testpassword","serveraddress":"example.com"}`, |
| 43 | inputBase64: `eyJ1c2VybmFtZSI6InRlc3R1c2VyIiwicGFzc3dvcmQiOiJ0ZXN0cGFzc3dvcmQiLCJzZXJ2ZXJhZGRyZXNzIjoiZXhhbXBsZS5jb20ifQ==`, |
| 44 | expected: registry.AuthConfig{ |
| 45 | Username: "testuser", |
| 46 | Password: "testpassword", |
| 47 | ServerAddress: "example.com", |
| 48 | }, |
| 49 | }, |
| 50 | { |
| 51 | doc: "multiple authConfig (should be rejected)", |
| 52 | input: `{"username":"testuser","password":"testpassword","serveraddress":"example.com"}{"username":"testuser2","password":"testpassword2","serveraddress":"example.org"}`, |
| 53 | inputBase64: `eyJ1c2VybmFtZSI6InRlc3R1c2VyIiwicGFzc3dvcmQiOiJ0ZXN0cGFzc3dvcmQiLCJzZXJ2ZXJhZGRyZXNzIjoiZXhhbXBsZS5jb20ifXsidXNlcm5hbWUiOiJ0ZXN0dXNlcjIiLCJwYXNzd29yZCI6InRlc3RwYXNzd29yZDIiLCJzZXJ2ZXJhZGRyZXNzIjoiZXhhbXBsZS5vcmcifQ==`, |
| 54 | expected: registry.AuthConfig{}, |
| 55 | expectedErr: `invalid X-Registry-Auth header: multiple JSON documents not allowed`, |
| 56 | }, |
| 57 | // We currently only support base64url encoding with padding, so |
| 58 | // un-padded should produce an error. |
| 59 | // |
| 60 | // RFC4648, section 5: https://tools.ietf.org/html/rfc4648#section-5 |
| 61 | // RFC4648, section 3.2: https://tools.ietf.org/html/rfc4648#section-3.2 |
| 62 | { |
| 63 | doc: "empty JSON no padding", |
| 64 | input: `{}`, |
| 65 | inputBase64: `e30`, |
| 66 | expected: registry.AuthConfig{}, |
| 67 | expectedErr: `invalid X-Registry-Auth header: must be a valid base64url-encoded string`, |
| 68 | }, |
| 69 | { |
| 70 | doc: "test authConfig", |