| 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/browser/experiments/api_key.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/test/simple_test_clock.h" |
| 9 #include "base/time/time.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char* kSampleAPIKey = |
| 17 "Signature|https://valid.example.com|Frobulate|1458766277"; |
| 18 |
| 19 const char* kExpectedAPIKeySignature = "Signature"; |
| 20 const char* kExpectedAPIKeyData = |
| 21 "https://valid.example.com|Frobulate|1458766277"; |
| 22 const char* kExpectedAPIName = "Frobulate"; |
| 23 const char* kExpectedOrigin = "https://valid.example.com"; |
| 24 const uint64_t kExpectedExpiry = 1458766277; |
| 25 |
| 26 double kValidTimestamp = 1458766276.0; |
| 27 double kInvalidTimestamp = 1458766278.0; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 class ApiKeyTest : public testing::Test {}; |
| 32 |
| 33 TEST_F(ApiKeyTest, ParseNullString) { |
| 34 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(std::string()); |
| 35 EXPECT_FALSE(empty_key); |
| 36 } |
| 37 |
| 38 TEST_F(ApiKeyTest, ParseEmptyString) { |
| 39 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(""); |
| 40 EXPECT_FALSE(empty_key); |
| 41 } |
| 42 |
| 43 TEST_F(ApiKeyTest, ParseInvalidString) { |
| 44 scoped_ptr<ApiKey> empty_key = ApiKey::Parse("abcdef"); |
| 45 EXPECT_FALSE(empty_key); |
| 46 } |
| 47 |
| 48 TEST_F(ApiKeyTest, ParseValidKeyString) { |
| 49 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); |
| 50 ASSERT_TRUE(key); |
| 51 EXPECT_EQ(kExpectedAPIName, key->api_name()); |
| 52 EXPECT_EQ(kExpectedAPIKeySignature, key->signature()); |
| 53 EXPECT_EQ(kExpectedAPIKeyData, key->data()); |
| 54 EXPECT_EQ(GURL(kExpectedOrigin), key->origin()); |
| 55 EXPECT_EQ(kExpectedExpiry, key->expiry_timestamp()); |
| 56 } |
| 57 |
| 58 TEST_F(ApiKeyTest, ValidateWhenNotExpired) { |
| 59 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); |
| 60 ASSERT_TRUE(key); |
| 61 EXPECT_TRUE(key->IsValidNow(base::Time::FromDoubleT(kValidTimestamp))); |
| 62 } |
| 63 |
| 64 TEST_F(ApiKeyTest, ValidateWhenExpired) { |
| 65 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); |
| 66 ASSERT_TRUE(key); |
| 67 EXPECT_FALSE(key->IsValidNow(base::Time::FromDoubleT(kInvalidTimestamp))); |
| 68 } |
| 69 |
| 70 } // namespace content |
| OLD | NEW |