| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/common/experiments/api_key.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/test/simple_test_clock.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 namespace { | |
| 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. | |
| 34 const char* kSampleAPIKey = | |
| 35 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" | |
| 36 "dUwk7Dyk/qbDw==|https://valid.example.com|Frobulate|1458766277"; | |
| 37 const char* kExpectedAPIKeySignature = | |
| 38 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" | |
| 39 "dUwk7Dyk/qbDw=="; | |
| 40 const char* kExpectedAPIKeyData = | |
| 41 "https://valid.example.com|Frobulate|1458766277"; | |
| 42 const char* kExpectedAPIName = "Frobulate"; | |
| 43 const char* kExpectedOrigin = "https://valid.example.com"; | |
| 44 const uint64_t kExpectedExpiry = 1458766277; | |
| 45 | |
| 46 // The key should not be valid for this origin, or for this API. | |
| 47 const char* kInvalidOrigin = "https://invalid.example.com"; | |
| 48 const char* kInsecureOrigin = "http://valid.example.com"; | |
| 49 const char* kInvalidAPIName = "Grokalyze"; | |
| 50 | |
| 51 // The key should be valid if the current time is kValidTimestamp or earlier. | |
| 52 double kValidTimestamp = 1458766276.0; | |
| 53 | |
| 54 // The key should be invalid if the current time is kInvalidTimestamp or later. | |
| 55 double kInvalidTimestamp = 1458766278.0; | |
| 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 | |
| 62 // Various ill-formed API keys. These should all fail to parse. | |
| 63 const char* kInvalidAPIKeys[] = { | |
| 64 // Invalid - only one part | |
| 65 "abcde", | |
| 66 // Not enough parts | |
| 67 "https://valid.example.com|APIName|1458766277", | |
| 68 // Delimiter in API Name | |
| 69 "Signature|https://valid.example.com|API|Name|1458766277", | |
| 70 // Extra string field | |
| 71 "Signature|https://valid.example.com|APIName|1458766277|SomethingElse", | |
| 72 // Extra numeric field | |
| 73 "Signature|https://valid.example.com|APIName|1458766277|1458766277", | |
| 74 // Non-numeric expiry timestamp | |
| 75 "Signature|https://valid.example.com|APIName|abcdefghij", | |
| 76 "Signature|https://valid.example.com|APIName|1458766277x", | |
| 77 // Negative expiry timestamp | |
| 78 "Signature|https://valid.example.com|APIName|-1458766277", | |
| 79 // Origin not a proper origin URL | |
| 80 "Signature|abcdef|APIName|1458766277", | |
| 81 "Signature|data:text/plain,abcdef|APIName|1458766277", | |
| 82 "Signature|javascript:alert(1)|APIName|1458766277"}; | |
| 83 const size_t kNumInvalidAPIKeys = arraysize(kInvalidAPIKeys); | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 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 | |
| 94 protected: | |
| 95 bool ValidateOrigin(ApiKey* api_key, const char* origin) { | |
| 96 return api_key->ValidateOrigin(origin); | |
| 97 } | |
| 98 | |
| 99 bool ValidateApiName(ApiKey* api_key, const char* api_name) { | |
| 100 return api_key->ValidateApiName(api_name); | |
| 101 } | |
| 102 | |
| 103 bool ValidateDate(ApiKey* api_key, const base::Time& now) { | |
| 104 return api_key->ValidateDate(now); | |
| 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_; | |
| 115 }; | |
| 116 | |
| 117 TEST_F(ApiKeyTest, ParseEmptyString) { | |
| 118 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(""); | |
| 119 EXPECT_FALSE(empty_key); | |
| 120 } | |
| 121 | |
| 122 TEST_F(ApiKeyTest, ParseInvalidStrings) { | |
| 123 for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) { | |
| 124 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(kInvalidAPIKeys[i]); | |
| 125 EXPECT_FALSE(empty_key) << "Invalid API Key should not parse: " | |
| 126 << kInvalidAPIKeys[i]; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 TEST_F(ApiKeyTest, ParseValidKeyString) { | |
| 131 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); | |
| 132 ASSERT_TRUE(key); | |
| 133 EXPECT_EQ(kExpectedAPIName, key->api_name()); | |
| 134 EXPECT_EQ(kExpectedAPIKeySignature, key->signature()); | |
| 135 EXPECT_EQ(kExpectedAPIKeyData, key->data()); | |
| 136 EXPECT_EQ(GURL(kExpectedOrigin), key->origin()); | |
| 137 EXPECT_EQ(kExpectedExpiry, key->expiry_timestamp()); | |
| 138 } | |
| 139 | |
| 140 TEST_F(ApiKeyTest, ValidateValidKey) { | |
| 141 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); | |
| 142 ASSERT_TRUE(key); | |
| 143 EXPECT_TRUE(ValidateOrigin(key.get(), kExpectedOrigin)); | |
| 144 EXPECT_FALSE(ValidateOrigin(key.get(), kInvalidOrigin)); | |
| 145 EXPECT_FALSE(ValidateOrigin(key.get(), kInsecureOrigin)); | |
| 146 EXPECT_TRUE(ValidateApiName(key.get(), kExpectedAPIName)); | |
| 147 EXPECT_FALSE(ValidateApiName(key.get(), kInvalidAPIName)); | |
| 148 EXPECT_TRUE( | |
| 149 ValidateDate(key.get(), base::Time::FromDoubleT(kValidTimestamp))); | |
| 150 EXPECT_FALSE( | |
| 151 ValidateDate(key.get(), base::Time::FromDoubleT(kInvalidTimestamp))); | |
| 152 } | |
| 153 | |
| 154 TEST_F(ApiKeyTest, KeyIsAppropriateForOriginAndAPI) { | |
| 155 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); | |
| 156 ASSERT_TRUE(key); | |
| 157 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, kExpectedAPIName)); | |
| 158 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, | |
| 159 base::ToUpperASCII(kExpectedAPIName))); | |
| 160 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, | |
| 161 base::ToLowerASCII(kExpectedAPIName))); | |
| 162 EXPECT_FALSE(key->IsAppropriate(kInvalidOrigin, kExpectedAPIName)); | |
| 163 EXPECT_FALSE(key->IsAppropriate(kInsecureOrigin, kExpectedAPIName)); | |
| 164 EXPECT_FALSE(key->IsAppropriate(kExpectedOrigin, kInvalidAPIName)); | |
| 165 } | |
| 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 | |
| 191 } // namespace content | |
| OLD | NEW |