| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/experiments/api_key.h" | 5 #include "content/common/experiments/api_key.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // This is a sample public key for testing the API. The corresponding private |
| 19 // key (use this to generate new samples for this test file) is: |
| 20 // |
| 21 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, |
| 22 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba, |
| 23 // 0xfd, 0xbb, 0xfa, 0xbc, 0x92, 0x76, 0x8a, 0x2c, 0x4b, 0xc7, 0x75, |
| 24 // 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, 0x9a, |
| 25 // 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, 0x64, |
| 26 // 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0 |
| 27 const uint8_t kTestPublicKey[] = { |
| 28 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, |
| 29 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, |
| 30 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0, |
| 31 }; |
| 32 |
| 33 // This is a good key, signed with the above test private key. |
| 18 const char* kSampleAPIKey = | 34 const char* kSampleAPIKey = |
| 19 "Signature|https://valid.example.com|Frobulate|1458766277"; | 35 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" |
| 20 | 36 "dUwk7Dyk/qbDw==|https://valid.example.com|Frobulate|1458766277"; |
| 21 const char* kExpectedAPIKeySignature = "Signature"; | 37 const char* kExpectedAPIKeySignature = |
| 38 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" |
| 39 "dUwk7Dyk/qbDw=="; |
| 22 const char* kExpectedAPIKeyData = | 40 const char* kExpectedAPIKeyData = |
| 23 "https://valid.example.com|Frobulate|1458766277"; | 41 "https://valid.example.com|Frobulate|1458766277"; |
| 24 const char* kExpectedAPIName = "Frobulate"; | 42 const char* kExpectedAPIName = "Frobulate"; |
| 25 const char* kExpectedOrigin = "https://valid.example.com"; | 43 const char* kExpectedOrigin = "https://valid.example.com"; |
| 26 const uint64_t kExpectedExpiry = 1458766277; | 44 const uint64_t kExpectedExpiry = 1458766277; |
| 27 | 45 |
| 28 // The key should not be valid for this origin, or for this API. | 46 // The key should not be valid for this origin, or for this API. |
| 29 const char* kInvalidOrigin = "https://invalid.example.com"; | 47 const char* kInvalidOrigin = "https://invalid.example.com"; |
| 30 const char* kInsecureOrigin = "http://valid.example.com"; | 48 const char* kInsecureOrigin = "http://valid.example.com"; |
| 31 const char* kInvalidAPIName = "Grokalyze"; | 49 const char* kInvalidAPIName = "Grokalyze"; |
| 32 | 50 |
| 33 // The key should be valid if the current time is kValidTimestamp or earlier. | 51 // The key should be valid if the current time is kValidTimestamp or earlier. |
| 34 double kValidTimestamp = 1458766276.0; | 52 double kValidTimestamp = 1458766276.0; |
| 35 | 53 |
| 36 // The key should be invalid if the current time is kInvalidTimestamp or later. | 54 // The key should be invalid if the current time is kInvalidTimestamp or later. |
| 37 double kInvalidTimestamp = 1458766278.0; | 55 double kInvalidTimestamp = 1458766278.0; |
| 38 | 56 |
| 57 // Well-formed API key with an invalid signature. |
| 58 const char* kInvalidSignatureAPIKey = |
| 59 "CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/hnx" |
| 60 "mba765hz0mXMc==|https://valid.example.com|Frobulate|1458766277"; |
| 61 |
| 39 // Various ill-formed API keys. These should all fail to parse. | 62 // Various ill-formed API keys. These should all fail to parse. |
| 40 const char* kInvalidAPIKeys[] = { | 63 const char* kInvalidAPIKeys[] = { |
| 41 // Invalid - only one part | 64 // Invalid - only one part |
| 42 "abcde", | 65 "abcde", |
| 43 // Not enough parts | 66 // Not enough parts |
| 44 "https://valid.example.com|APIName|1458766277", | 67 "https://valid.example.com|APIName|1458766277", |
| 45 // Delimiter in API Name | 68 // Delimiter in API Name |
| 46 "Signature|https://valid.example.com|API|Name|1458766277", | 69 "Signature|https://valid.example.com|API|Name|1458766277", |
| 47 // Extra string field | 70 // Extra string field |
| 48 "Signature|https://valid.example.com|APIName|1458766277|SomethingElse", | 71 "Signature|https://valid.example.com|APIName|1458766277|SomethingElse", |
| 49 // Extra numeric field | 72 // Extra numeric field |
| 50 "Signature|https://valid.example.com|APIName|1458766277|1458766277", | 73 "Signature|https://valid.example.com|APIName|1458766277|1458766277", |
| 51 // Non-numeric expiry timestamp | 74 // Non-numeric expiry timestamp |
| 52 "Signature|https://valid.example.com|APIName|abcdefghij", | 75 "Signature|https://valid.example.com|APIName|abcdefghij", |
| 53 "Signature|https://valid.example.com|APIName|1458766277x", | 76 "Signature|https://valid.example.com|APIName|1458766277x", |
| 54 // Negative expiry timestamp | 77 // Negative expiry timestamp |
| 55 "Signature|https://valid.example.com|APIName|-1458766277", | 78 "Signature|https://valid.example.com|APIName|-1458766277", |
| 56 // Origin not a proper origin URL | 79 // Origin not a proper origin URL |
| 57 "Signature|abcdef|APIName|1458766277", | 80 "Signature|abcdef|APIName|1458766277", |
| 58 "Signature|data:text/plain,abcdef|APIName|1458766277", | 81 "Signature|data:text/plain,abcdef|APIName|1458766277", |
| 59 "Signature|javascript:alert(1)|APIName|1458766277"}; | 82 "Signature|javascript:alert(1)|APIName|1458766277"}; |
| 60 const size_t kNumInvalidAPIKeys = arraysize(kInvalidAPIKeys); | 83 const size_t kNumInvalidAPIKeys = arraysize(kInvalidAPIKeys); |
| 61 | 84 |
| 62 } // namespace | 85 } // namespace |
| 63 | 86 |
| 64 class ApiKeyTest : public testing::Test { | 87 class ApiKeyTest : public testing::Test { |
| 88 public: |
| 89 ApiKeyTest() |
| 90 : public_key_( |
| 91 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), |
| 92 arraysize(kTestPublicKey))) {} |
| 93 |
| 65 protected: | 94 protected: |
| 66 bool ValidateOrigin(ApiKey* api_key, const char* origin) { | 95 bool ValidateOrigin(ApiKey* api_key, const char* origin) { |
| 67 return api_key->ValidateOrigin(origin); | 96 return api_key->ValidateOrigin(origin); |
| 68 } | 97 } |
| 69 | 98 |
| 70 bool ValidateApiName(ApiKey* api_key, const char* api_name) { | 99 bool ValidateApiName(ApiKey* api_key, const char* api_name) { |
| 71 return api_key->ValidateApiName(api_name); | 100 return api_key->ValidateApiName(api_name); |
| 72 } | 101 } |
| 73 | 102 |
| 74 bool ValidateDate(ApiKey* api_key, const base::Time& now) { | 103 bool ValidateDate(ApiKey* api_key, const base::Time& now) { |
| 75 return api_key->ValidateDate(now); | 104 return api_key->ValidateDate(now); |
| 76 } | 105 } |
| 106 |
| 107 bool ValidateSignature(ApiKey* api_key, const base::StringPiece& public_key) { |
| 108 return api_key->ValidateSignature(public_key); |
| 109 } |
| 110 |
| 111 const base::StringPiece& public_key() { return public_key_; }; |
| 112 |
| 113 private: |
| 114 base::StringPiece public_key_; |
| 77 }; | 115 }; |
| 78 | 116 |
| 79 TEST_F(ApiKeyTest, ParseEmptyString) { | 117 TEST_F(ApiKeyTest, ParseEmptyString) { |
| 80 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(""); | 118 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(""); |
| 81 EXPECT_FALSE(empty_key); | 119 EXPECT_FALSE(empty_key); |
| 82 } | 120 } |
| 83 | 121 |
| 84 TEST_F(ApiKeyTest, ParseInvalidStrings) { | 122 TEST_F(ApiKeyTest, ParseInvalidStrings) { |
| 85 for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) { | 123 for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) { |
| 86 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(kInvalidAPIKeys[i]); | 124 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(kInvalidAPIKeys[i]); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, kExpectedAPIName)); | 157 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, kExpectedAPIName)); |
| 120 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, | 158 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, |
| 121 base::ToUpperASCII(kExpectedAPIName))); | 159 base::ToUpperASCII(kExpectedAPIName))); |
| 122 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, | 160 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, |
| 123 base::ToLowerASCII(kExpectedAPIName))); | 161 base::ToLowerASCII(kExpectedAPIName))); |
| 124 EXPECT_FALSE(key->IsAppropriate(kInvalidOrigin, kExpectedAPIName)); | 162 EXPECT_FALSE(key->IsAppropriate(kInvalidOrigin, kExpectedAPIName)); |
| 125 EXPECT_FALSE(key->IsAppropriate(kInsecureOrigin, kExpectedAPIName)); | 163 EXPECT_FALSE(key->IsAppropriate(kInsecureOrigin, kExpectedAPIName)); |
| 126 EXPECT_FALSE(key->IsAppropriate(kExpectedOrigin, kInvalidAPIName)); | 164 EXPECT_FALSE(key->IsAppropriate(kExpectedOrigin, kInvalidAPIName)); |
| 127 } | 165 } |
| 128 | 166 |
| 167 TEST_F(ApiKeyTest, ValidateValidSignature) { |
| 168 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); |
| 169 ASSERT_TRUE(key); |
| 170 EXPECT_TRUE(ValidateSignature(key.get(), public_key())); |
| 171 } |
| 172 |
| 173 TEST_F(ApiKeyTest, ValidateInvalidSignature) { |
| 174 scoped_ptr<ApiKey> key = ApiKey::Parse(kInvalidSignatureAPIKey); |
| 175 ASSERT_TRUE(key); |
| 176 EXPECT_FALSE(ValidateSignature(key.get(), public_key())); |
| 177 } |
| 178 |
| 179 TEST_F(ApiKeyTest, ValidateSignatureOnWrongKey) { |
| 180 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); |
| 181 ASSERT_TRUE(key); |
| 182 // Signature will be invalid if tested against the real public key |
| 183 EXPECT_FALSE(key->IsValid(base::Time::FromDoubleT(kValidTimestamp))); |
| 184 } |
| 185 |
| 186 TEST_F(ApiKeyTest, ValidateWhenNotExpired) { |
| 187 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); |
| 188 ASSERT_TRUE(key); |
| 189 } |
| 190 |
| 129 } // namespace content | 191 } // namespace content |
| OLD | NEW |