Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(929)

Side by Side Diff: content/browser/experiments/api_key_signature_verifier_unittest.cc

Issue 1563903002: Add public key and signature verification to browser-side API keys Base URL: https://chromium.googlesource.com/chromium/src.git@keys
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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_signature_verifier.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 "content/common/experiments/api_key.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace content {
14
15 namespace {
16
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,
22 0x13, 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02,
23 0xe2, 0xba, 0xfd, 0xbb, 0xfa, 0xbc, 0x92, 0x76, 0x8a, 0x2c,
24 0x4b, 0xc7, 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9,
25 0x28, 0x70, 0xd2, 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b,
26 0xb7, 0xd5, 0xca, 0x1f, 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0,
27 0x56, 0x3a, 0x04, 0xd0
28 */
29 const uint8_t kTestPublicKey[] = {
30 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2,
31 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f,
32 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0,
33 };
34 const size_t kTestPublicKeyLength = sizeof(kTestPublicKey);
35
36 // This is a good key, signed with the above test private key.
37 const char* kSampleAPIKey =
38 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S"
39 "dUwk7Dyk/qbDw==|https://valid.example.com|Frobulate|1458766277";
40
41 // Well-formed API key with an invalid signature.
42 const char* kInvalidSignatureAPIKey =
43 "CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/hnx"
44 "mba765hz0mXMc==|https://valid.example.com|Frobulate|1458766277";
45
46 } // namespace
47
48 class ApiKeySignatureVerifierTest : public testing::Test {};
49
50 TEST_F(ApiKeySignatureVerifierTest, VerifyValidSignature) {
51 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
52 ASSERT_TRUE(key);
53 EXPECT_TRUE(ApiKeySignatureVerifier::VerifySignature(
54 key->signature(), key->data(), kTestPublicKey, kTestPublicKeyLength));
55 }
56
57 TEST_F(ApiKeySignatureVerifierTest, VerifyInvalidSignature) {
58 scoped_ptr<ApiKey> key = ApiKey::Parse(kInvalidSignatureAPIKey);
59 ASSERT_TRUE(key);
60 EXPECT_FALSE(ApiKeySignatureVerifier::VerifySignature(
61 key->signature(), key->data(), kTestPublicKey, kTestPublicKeyLength));
62 }
63
64 TEST_F(ApiKeySignatureVerifierTest, VerifySignatureOnWrongKey) {
65 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
66 ASSERT_TRUE(key);
67 // Signature will be invalid if tested against the real public key
68 EXPECT_FALSE(
69 ApiKeySignatureVerifier::VerifySignature(key->signature(), key->data()));
70 }
71
72 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/experiments/api_key_signature_verifier.cc ('k') | content/common/experiments/api_key.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698