Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_auth_ica_unittest.cc
blob8e1b6a51ac449f2df14df2b439fcc9402a90b366
1 // Copyright 2014 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.
5 #include "extensions/browser/api/cast_channel/cast_auth_ica.h"
7 #include <string>
9 #include "base/base64.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions {
14 namespace core_api {
16 namespace cast_channel {
18 namespace {
20 static const net::SHA256HashValue kFingerprintValid1 = {{
21 0x52, 0x9D, 0x9C, 0xD6, 0x7F, 0xE5, 0xEB, 0x69, 0x8E, 0x70, 0xDD, 0x26,
22 0xD7, 0xD8, 0xF1, 0x26, 0x59, 0xF1, 0xE6, 0xE5, 0x23, 0x48, 0xBF, 0x6A,
23 0x5C, 0xF7, 0x16, 0xE1, 0x3F, 0x41, 0x0E, 0x73
24 }};
26 static const net::SHA256HashValue kFingerprintValid2 = {{
27 0xA2, 0x48, 0xC2, 0xE8, 0x54, 0xE6, 0x56, 0xA5, 0x6D, 0xE8, 0x23, 0x1F,
28 0x1E, 0xE1, 0x75, 0x6F, 0xDB, 0xE4, 0x07, 0xF9, 0xFE, 0xD4, 0x65, 0x0D,
29 0x60, 0xCC, 0x5A, 0xCB, 0x65, 0x11, 0xC7, 0x20
30 }};
32 static const net::SHA256HashValue kFingerprintInvalid = {{
33 0x00, 0x9D, 0x9C, 0xD6, 0x7F, 0xE5, 0xEB, 0x69, 0x8E, 0x70, 0xDD, 0x26,
34 0xD7, 0xD8, 0xF1, 0x26, 0x59, 0xF1, 0xE6, 0xE5, 0x23, 0x48, 0xBF, 0x6A,
35 0x5C, 0xF7, 0x16, 0xE1, 0x3F, 0x41, 0x0E, 0x73
36 }};
38 } // namespace
40 class CastChannelAuthorityKeysTest : public testing::Test {
41 public:
42 CastChannelAuthorityKeysTest() {}
43 ~CastChannelAuthorityKeysTest() override {}
45 protected:
46 void ExpectKeysLoaded();
47 AuthorityKeyStore authority_keys_store_;
50 void CastChannelAuthorityKeysTest::ExpectKeysLoaded() {
51 base::StringPiece key = authority_keys_store_.GetDefaultICAPublicKey();
52 EXPECT_FALSE(key.empty());
54 key =
55 authority_keys_store_.GetICAPublicKeyFromFingerprint(kFingerprintValid1);
56 EXPECT_FALSE(key.empty());
58 key =
59 authority_keys_store_.GetICAPublicKeyFromFingerprint(kFingerprintInvalid);
60 EXPECT_TRUE(key.empty());
63 TEST_F(CastChannelAuthorityKeysTest, TestDefaultKeys) {
64 ExpectKeysLoaded();
67 TEST_F(CastChannelAuthorityKeysTest, TestInvalidProtobuf) {
68 std::string keys = "test";
69 EXPECT_EQ(authority_keys_store_.Load(keys), false);
71 base::StringPiece key = authority_keys_store_.GetDefaultICAPublicKey();
72 EXPECT_TRUE(key.empty());
75 TEST_F(CastChannelAuthorityKeysTest, TestValidProtobuf) {
76 std::string keys =
77 "CrMCCiBSnZzWf+XraY5w3SbX2PEmWfHm5SNIv2pc9xbhP0EOcxKOAjCCAQoCggEBALwigL"
78 "2A9johADuudl41fz3DZFxVlIY0LwWHKM33aYwXs1CnuIL638dDLdZ+q6BvtxNygKRHFcEg"
79 "mVDN7BRiCVukmM3SQbY2Tv/oLjIwSoGoQqNsmzNuyrL1U2bgJ1OGGoUepzk/SneO+1RmZv"
80 "tYVMBeOcf1UAYL4IrUzuFqVR+LFwDmaaMn5gglaTwSnY0FLNYuojHetFJQ1iBJ3nGg+a0g"
81 "QBLx3SXr1ea4NvTWj3/KQ9zXEFvmP1GKhbPz//YDLcsjT5ytGOeTBYysUpr3TOmZer5ufk"
82 "0K48YcqZP6OqWRXRy9ZuvMYNyGdMrP+JIcmH1X+mFHnquAt+RIgCqSxRsCAwEAAQqzAgog"
83 "okjC6FTmVqVt6CMfHuF1b9vkB/n+1GUNYMxay2URxyASjgIwggEKAoIBAQCwDl4HOt+kX2"
84 "j3Icdk27Z27+6Lk/j2G4jhk7cX8BUeflJVdzwCjXtKbNO91sGccsizFc8RwfVGxNUgR/sw"
85 "9ORhDGjwXqs3jpvhvIHDcIp41oM0MpwZYuvknO3jZGxBHZzSi0hMI5CVs+dS6gVXzGCzuh"
86 "TkugA55EZVdM5ajnpnI9poCvrEhB60xaGianMfbsguL5qeqLEO/Yemj009SwXVNVp0TbyO"
87 "gkSW9LWVYE6l3yc9QVwHo7Q1WrOe8gUkys0xWg0mTNTT/VDhNOlMgVgwssd63YGJptQ6OI"
88 "QDtzSedz//eAdbmcGyHzVWbjo8DCXhV/aKfknAzIMRNeeRbS5lAgMBAAE=";
90 std::string decoded_keys;
91 EXPECT_EQ(base::Base64Decode(keys, &decoded_keys), true);
92 EXPECT_EQ(authority_keys_store_.Load(decoded_keys), true);
94 ExpectKeysLoaded();
96 base::StringPiece key =
97 authority_keys_store_.GetICAPublicKeyFromFingerprint(kFingerprintValid2);
98 EXPECT_FALSE(key.empty());
101 TEST_F(CastChannelAuthorityKeysTest, TestSetTrustedCertificateAuthorities) {
102 std::string keys =
103 "CrMCCiBSnZzWf+XraY5w3SbX2PEmWfHm5SNIv2pc9xbhP0EOcxKOAjCCAQoCggEBALwigL"
104 "2A9johADuudl41fz3DZFxVlIY0LwWHKM33aYwXs1CnuIL638dDLdZ+q6BvtxNygKRHFcEg"
105 "mVDN7BRiCVukmM3SQbY2Tv/oLjIwSoGoQqNsmzNuyrL1U2bgJ1OGGoUepzk/SneO+1RmZv"
106 "tYVMBeOcf1UAYL4IrUzuFqVR+LFwDmaaMn5gglaTwSnY0FLNYuojHetFJQ1iBJ3nGg+a0g"
107 "QBLx3SXr1ea4NvTWj3/KQ9zXEFvmP1GKhbPz//YDLcsjT5ytGOeTBYysUpr3TOmZer5ufk"
108 "0K48YcqZP6OqWRXRy9ZuvMYNyGdMrP+JIcmH1X+mFHnquAt+RIgCqSxRsCAwEAAQ==";
109 std::string signature =
110 "chCUHZKkykcwU8HzU+hm027fUTBL0dqPMtrzppwExQwK9+"
111 "XlmCjJswfce2sUUfhR1OL1tyW4hWFwu4JnuQCJ+CvmSmAh2bzRpnuSKzBfgvIDjNOAGUs7"
112 "ADaNSSWPLxp+6ko++2Dn4S9HpOt8N1v6gMWqj3Ru5IqFSQPZSvGH2ois6uE50CFayPcjQE"
113 "OVZt41noQdFd15RmKTvocoCC5tHNlaikeQ52yi0IScOlad1B1lMhoplW3rWophQaqxMumr"
114 "OcHIZ+Y+p858x5f8Pny/kuqUClmFh9B/vF07NsUHwoSL9tA5t5jCY3L5iUc/v7o3oFcW/T"
115 "gojKkX2Kg7KQ86QA==";
116 EXPECT_FALSE(SetTrustedCertificateAuthorities(keys, "signature"));
117 EXPECT_FALSE(SetTrustedCertificateAuthorities("keys", signature));
118 EXPECT_FALSE(SetTrustedCertificateAuthorities(keys, signature));
119 EXPECT_FALSE(SetTrustedCertificateAuthorities(std::string(), std::string()));
121 keys =
122 "CrMCCiBSnZzWf+XraY5w3SbX2PEmWfHm5SNIv2pc9xbhP0EOcxKOAjCCAQoCggEBALwigL"
123 "2A9johADuudl41fz3DZFxVlIY0LwWHKM33aYwXs1CnuIL638dDLdZ+q6BvtxNygKRHFcEg"
124 "mVDN7BRiCVukmM3SQbY2Tv/oLjIwSoGoQqNsmzNuyrL1U2bgJ1OGGoUepzk/SneO+1RmZv"
125 "tYVMBeOcf1UAYL4IrUzuFqVR+LFwDmaaMn5gglaTwSnY0FLNYuojHetFJQ1iBJ3nGg+a0g"
126 "QBLx3SXr1ea4NvTWj3/KQ9zXEFvmP1GKhbPz//YDLcsjT5ytGOeTBYysUpr3TOmZer5ufk"
127 "0K48YcqZP6OqWRXRy9ZuvMYNyGdMrP+JIcmH1X+mFHnquAt+RIgCqSxRsCAwEAAQqzAgog"
128 "okjC6FTmVqVt6CMfHuF1b9vkB/n+1GUNYMxay2URxyASjgIwggEKAoIBAQCwDl4HOt+kX2"
129 "j3Icdk27Z27+6Lk/j2G4jhk7cX8BUeflJVdzwCjXtKbNO91sGccsizFc8RwfVGxNUgR/sw"
130 "9ORhDGjwXqs3jpvhvIHDcIp41oM0MpwZYuvknO3jZGxBHZzSi0hMI5CVs+dS6gVXzGCzuh"
131 "TkugA55EZVdM5ajnpnI9poCvrEhB60xaGianMfbsguL5qeqLEO/Yemj009SwXVNVp0TbyO"
132 "gkSW9LWVYE6l3yc9QVwHo7Q1WrOe8gUkys0xWg0mTNTT/VDhNOlMgVgwssd63YGJptQ6OI"
133 "QDtzSedz//eAdbmcGyHzVWbjo8DCXhV/aKfknAzIMRNeeRbS5lAgMBAAE=";
134 signature =
135 "o83oku3jP+xjTysNBalqp/ZfJRPLt8R+IUhZMepbARFSRVizLoeFW5XyUwe6lQaC+PFFQH"
136 "SZeGZyeeGRpwCJ/lef0xh6SWJlVMWNTk5+z0U84GQdizJP/CTCeHpIwMobN+kyDajgOyfD"
137 "DLhktc6LHmSlFGG6J7B8W67oziS8ZFEdrcT9WSXFrjLVyURHjvidZD5iFtuImI6k9R9OoX"
138 "LR6SyAwpjdrL+vlHMk3Gol6KQ98YpF0ghHnN3/FFW4ibvIwjmRbp+tUV3h8TRcCOjlXVGp"
139 "bzPtNRRlTqfv7Rxm5YXkZMLmJJMZiTs5+o8FMRMTQZT4hRR3DQ+A/jofViyTGA==";
140 EXPECT_TRUE(SetTrustedCertificateAuthorities(keys, signature));
142 base::StringPiece result = GetDefaultTrustedICAPublicKey();
143 EXPECT_FALSE(result.empty());
146 } // namespace cast_channel
148 } // namespace core_api
150 } // namespace extensions