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

Side by Side Diff: content/renderer/origin_trials/trial_token_unittest.cc

Issue 1752463002: Cleanup TrialToken in preparation of moving it to content/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 9 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
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/renderer/origin_trials/trial_token.h" 5 #include "content/renderer/origin_trials/trial_token.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_piece.h" 9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/test/simple_test_clock.h" 11 #include "base/test/simple_test_clock.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "url/gurl.h"
14 15
15 namespace content { 16 namespace content {
16 17
17 namespace { 18 namespace {
18 19
19 // This is a sample public key for testing the API. The corresponding private 20 // This is a sample public key for testing the API. The corresponding private
20 // key (use this to generate new samples for this test file) is: 21 // key (use this to generate new samples for this test file) is:
21 // 22 //
22 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, 23 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13,
23 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba, 24 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 "1|Signature|abcdef|FeatureName|1458766277", 107 "1|Signature|abcdef|FeatureName|1458766277",
107 "1|Signature|data:text/plain,abcdef|FeatureName|1458766277", 108 "1|Signature|data:text/plain,abcdef|FeatureName|1458766277",
108 "1|Signature|javascript:alert(1)|FeatureName|1458766277"}; 109 "1|Signature|javascript:alert(1)|FeatureName|1458766277"};
109 const size_t kNumInvalidTokens = arraysize(kInvalidTokens); 110 const size_t kNumInvalidTokens = arraysize(kInvalidTokens);
110 111
111 } // namespace 112 } // namespace
112 113
113 class TrialTokenTest : public testing::Test { 114 class TrialTokenTest : public testing::Test {
114 public: 115 public:
115 TrialTokenTest() 116 TrialTokenTest()
116 : correct_public_key_( 117 : expected_origin_(GURL(kExpectedOrigin)),
118 invalid_origin_(GURL(kInvalidOrigin)),
119 insecure_origin_(GURL(kInsecureOrigin)),
120 correct_public_key_(
117 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), 121 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey),
118 arraysize(kTestPublicKey))), 122 arraysize(kTestPublicKey))),
119 incorrect_public_key_( 123 incorrect_public_key_(
120 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2), 124 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2),
121 arraysize(kTestPublicKey2))) {} 125 arraysize(kTestPublicKey2))) {}
122 126
123 protected: 127 protected:
124 bool ValidateOrigin(TrialToken* token, const char* origin) { 128 bool ValidateOrigin(TrialToken* token, const url::Origin origin) {
125 return token->ValidateOrigin(origin); 129 return token->ValidateOrigin(origin);
126 } 130 }
127 131
128 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { 132 bool ValidateFeatureName(TrialToken* token, const char* feature_name) {
129 return token->ValidateFeatureName(feature_name); 133 return token->ValidateFeatureName(feature_name);
130 } 134 }
131 135
132 bool ValidateDate(TrialToken* token, const base::Time& now) { 136 bool ValidateDate(TrialToken* token, const base::Time& now) {
133 return token->ValidateDate(now); 137 return token->ValidateDate(now);
134 } 138 }
135 139
136 bool ValidateSignature(TrialToken* token, 140 bool ValidateSignature(TrialToken* token,
137 const base::StringPiece& public_key) { 141 const base::StringPiece& public_key) {
138 return token->ValidateSignature(public_key); 142 return token->ValidateSignature(public_key);
139 } 143 }
140 144
141 const base::StringPiece& correct_public_key() { return correct_public_key_; } 145 base::StringPiece correct_public_key() { return correct_public_key_; }
142 const base::StringPiece& incorrect_public_key() { 146 base::StringPiece incorrect_public_key() { return incorrect_public_key_; }
143 return incorrect_public_key_; 147
144 } 148 const url::Origin expected_origin_;
149 const url::Origin invalid_origin_;
150 const url::Origin insecure_origin_;
145 151
146 private: 152 private:
147 base::StringPiece correct_public_key_; 153 base::StringPiece correct_public_key_;
148 base::StringPiece incorrect_public_key_; 154 base::StringPiece incorrect_public_key_;
149 }; 155 };
150 156
151 TEST_F(TrialTokenTest, ParseEmptyString) { 157 TEST_F(TrialTokenTest, ParseEmptyString) {
152 scoped_ptr<TrialToken> empty_token = TrialToken::Parse(""); 158 scoped_ptr<TrialToken> empty_token = TrialToken::Parse("");
153 EXPECT_FALSE(empty_token); 159 EXPECT_FALSE(empty_token);
154 } 160 }
155 161
156 TEST_F(TrialTokenTest, ParseInvalidStrings) { 162 TEST_F(TrialTokenTest, ParseInvalidStrings) {
157 for (size_t i = 0; i < kNumInvalidTokens; ++i) { 163 for (size_t i = 0; i < kNumInvalidTokens; ++i) {
158 scoped_ptr<TrialToken> empty_token = TrialToken::Parse(kInvalidTokens[i]); 164 scoped_ptr<TrialToken> empty_token = TrialToken::Parse(kInvalidTokens[i]);
159 EXPECT_FALSE(empty_token) << "Invalid trial token should not parse: " 165 EXPECT_FALSE(empty_token) << "Invalid trial token should not parse: "
160 << kInvalidTokens[i]; 166 << kInvalidTokens[i];
161 } 167 }
162 } 168 }
163 169
164 TEST_F(TrialTokenTest, ParseValidToken) { 170 TEST_F(TrialTokenTest, ParseValidToken) {
165 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 171 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
166 ASSERT_TRUE(token); 172 ASSERT_TRUE(token);
167 EXPECT_EQ(kExpectedVersion, token->version()); 173 EXPECT_EQ(kExpectedVersion, token->version());
168 EXPECT_EQ(kExpectedFeatureName, token->feature_name()); 174 EXPECT_EQ(kExpectedFeatureName, token->feature_name());
169 EXPECT_EQ(kExpectedSignature, token->signature()); 175 EXPECT_EQ(kExpectedSignature, token->signature());
170 EXPECT_EQ(kExpectedData, token->data()); 176 EXPECT_EQ(kExpectedData, token->data());
171 EXPECT_EQ(GURL(kExpectedOrigin), token->origin()); 177 EXPECT_EQ(expected_origin_, token->origin());
172 EXPECT_EQ(kExpectedExpiry, token->expiry_timestamp()); 178 EXPECT_EQ(base::Time::FromDoubleT(kExpectedExpiry), token->expiry_time());
173 } 179 }
174 180
175 TEST_F(TrialTokenTest, ValidateValidToken) { 181 TEST_F(TrialTokenTest, ValidateValidToken) {
176 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 182 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
177 ASSERT_TRUE(token); 183 ASSERT_TRUE(token);
178 EXPECT_TRUE(ValidateOrigin(token.get(), kExpectedOrigin)); 184 EXPECT_TRUE(ValidateOrigin(token.get(), expected_origin_));
179 EXPECT_FALSE(ValidateOrigin(token.get(), kInvalidOrigin)); 185 EXPECT_FALSE(ValidateOrigin(token.get(), invalid_origin_));
180 EXPECT_FALSE(ValidateOrigin(token.get(), kInsecureOrigin)); 186 EXPECT_FALSE(ValidateOrigin(token.get(), insecure_origin_));
181 EXPECT_TRUE(ValidateFeatureName(token.get(), kExpectedFeatureName)); 187 EXPECT_TRUE(ValidateFeatureName(token.get(), kExpectedFeatureName));
182 EXPECT_FALSE(ValidateFeatureName(token.get(), kInvalidFeatureName)); 188 EXPECT_FALSE(ValidateFeatureName(token.get(), kInvalidFeatureName));
183 EXPECT_FALSE(ValidateFeatureName( 189 EXPECT_FALSE(ValidateFeatureName(
184 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str())); 190 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str()));
185 EXPECT_FALSE(ValidateFeatureName( 191 EXPECT_FALSE(ValidateFeatureName(
186 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str())); 192 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str()));
187 EXPECT_TRUE( 193 EXPECT_TRUE(
188 ValidateDate(token.get(), base::Time::FromDoubleT(kValidTimestamp))); 194 ValidateDate(token.get(), base::Time::FromDoubleT(kValidTimestamp)));
189 EXPECT_FALSE( 195 EXPECT_FALSE(
190 ValidateDate(token.get(), base::Time::FromDoubleT(kInvalidTimestamp))); 196 ValidateDate(token.get(), base::Time::FromDoubleT(kInvalidTimestamp)));
191 } 197 }
192 198
193 TEST_F(TrialTokenTest, TokenIsAppropriateForOriginAndFeature) { 199 TEST_F(TrialTokenTest, TokenIsAppropriateForOriginAndFeature) {
194 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 200 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
195 ASSERT_TRUE(token); 201 ASSERT_TRUE(token);
196 EXPECT_TRUE(token->IsAppropriate(kExpectedOrigin, kExpectedFeatureName)); 202 EXPECT_TRUE(token->IsAppropriate(expected_origin_, kExpectedFeatureName));
197 EXPECT_FALSE(token->IsAppropriate(kExpectedOrigin, 203 EXPECT_FALSE(token->IsAppropriate(expected_origin_,
198 base::ToUpperASCII(kExpectedFeatureName))); 204 base::ToUpperASCII(kExpectedFeatureName)));
199 EXPECT_FALSE(token->IsAppropriate(kExpectedOrigin, 205 EXPECT_FALSE(token->IsAppropriate(expected_origin_,
200 base::ToLowerASCII(kExpectedFeatureName))); 206 base::ToLowerASCII(kExpectedFeatureName)));
201 EXPECT_FALSE(token->IsAppropriate(kInvalidOrigin, kExpectedFeatureName)); 207 EXPECT_FALSE(token->IsAppropriate(invalid_origin_, kExpectedFeatureName));
202 EXPECT_FALSE(token->IsAppropriate(kInsecureOrigin, kExpectedFeatureName)); 208 EXPECT_FALSE(token->IsAppropriate(insecure_origin_, kExpectedFeatureName));
203 EXPECT_FALSE(token->IsAppropriate(kExpectedOrigin, kInvalidFeatureName)); 209 EXPECT_FALSE(token->IsAppropriate(expected_origin_, kInvalidFeatureName));
204 } 210 }
205 211
206 TEST_F(TrialTokenTest, ValidateValidSignature) { 212 TEST_F(TrialTokenTest, ValidateValidSignature) {
207 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 213 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
208 ASSERT_TRUE(token); 214 ASSERT_TRUE(token);
209 EXPECT_TRUE(ValidateSignature(token.get(), correct_public_key())); 215 EXPECT_TRUE(ValidateSignature(token.get(), correct_public_key()));
210 } 216 }
211 217
212 TEST_F(TrialTokenTest, ValidateInvalidSignature) { 218 TEST_F(TrialTokenTest, ValidateInvalidSignature) {
213 scoped_ptr<TrialToken> token = TrialToken::Parse(kInvalidSignatureToken); 219 scoped_ptr<TrialToken> token = TrialToken::Parse(kInvalidSignatureToken);
(...skipping 14 matching lines...) Expand all
228 EXPECT_FALSE(token->IsValid(base::Time::FromDoubleT(kValidTimestamp), 234 EXPECT_FALSE(token->IsValid(base::Time::FromDoubleT(kValidTimestamp),
229 incorrect_public_key())); 235 incorrect_public_key()));
230 } 236 }
231 237
232 TEST_F(TrialTokenTest, ValidateWhenNotExpired) { 238 TEST_F(TrialTokenTest, ValidateWhenNotExpired) {
233 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 239 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
234 ASSERT_TRUE(token); 240 ASSERT_TRUE(token);
235 } 241 }
236 242
237 } // namespace content 243 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698