OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/quic/core/crypto/crypto_secret_boxer.h" | 5 #include "net/quic/core/crypto/crypto_secret_boxer.h" |
6 | 6 |
7 #include "net/quic/core/crypto/quic_random.h" | 7 #include "net/quic/core/crypto/quic_random.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 using base::StringPiece; | |
11 using std::string; | 10 using std::string; |
12 | 11 |
13 namespace net { | 12 namespace net { |
14 namespace test { | 13 namespace test { |
15 | 14 |
16 TEST(CryptoSecretBoxerTest, BoxAndUnbox) { | 15 TEST(CryptoSecretBoxerTest, BoxAndUnbox) { |
17 StringPiece message("hello world"); | 16 QuicStringPiece message("hello world"); |
18 | 17 |
19 CryptoSecretBoxer boxer; | 18 CryptoSecretBoxer boxer; |
20 boxer.SetKeys({string(CryptoSecretBoxer::GetKeySize(), 0x11)}); | 19 boxer.SetKeys({string(CryptoSecretBoxer::GetKeySize(), 0x11)}); |
21 | 20 |
22 const string box = boxer.Box(QuicRandom::GetInstance(), message); | 21 const string box = boxer.Box(QuicRandom::GetInstance(), message); |
23 | 22 |
24 string storage; | 23 string storage; |
25 StringPiece result; | 24 QuicStringPiece result; |
26 EXPECT_TRUE(boxer.Unbox(box, &storage, &result)); | 25 EXPECT_TRUE(boxer.Unbox(box, &storage, &result)); |
27 EXPECT_EQ(result, message); | 26 EXPECT_EQ(result, message); |
28 | 27 |
29 EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box, &storage, &result)); | 28 EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box, &storage, &result)); |
30 EXPECT_FALSE(boxer.Unbox(box.substr(1, string::npos), &storage, &result)); | 29 EXPECT_FALSE(boxer.Unbox(box.substr(1, string::npos), &storage, &result)); |
31 EXPECT_FALSE(boxer.Unbox(string(), &storage, &result)); | 30 EXPECT_FALSE(boxer.Unbox(string(), &storage, &result)); |
32 EXPECT_FALSE( | 31 EXPECT_FALSE( |
33 boxer.Unbox(string(1, box[0] ^ 0x80) + box.substr(1, string::npos), | 32 boxer.Unbox(string(1, box[0] ^ 0x80) + box.substr(1, string::npos), |
34 &storage, &result)); | 33 &storage, &result)); |
35 } | 34 } |
36 | 35 |
37 // Helper function to test whether one boxer can decode the output of another. | 36 // Helper function to test whether one boxer can decode the output of another. |
38 static bool CanDecode(const CryptoSecretBoxer& decoder, | 37 static bool CanDecode(const CryptoSecretBoxer& decoder, |
39 const CryptoSecretBoxer& encoder) { | 38 const CryptoSecretBoxer& encoder) { |
40 StringPiece message("hello world"); | 39 QuicStringPiece message("hello world"); |
41 const string boxed = encoder.Box(QuicRandom::GetInstance(), message); | 40 const string boxed = encoder.Box(QuicRandom::GetInstance(), message); |
42 string storage; | 41 string storage; |
43 StringPiece result; | 42 QuicStringPiece result; |
44 bool ok = decoder.Unbox(boxed, &storage, &result); | 43 bool ok = decoder.Unbox(boxed, &storage, &result); |
45 if (ok) { | 44 if (ok) { |
46 EXPECT_EQ(result, message); | 45 EXPECT_EQ(result, message); |
47 } | 46 } |
48 return ok; | 47 return ok; |
49 } | 48 } |
50 | 49 |
51 TEST(CryptoSecretBoxerTest, MultipleKeys) { | 50 TEST(CryptoSecretBoxerTest, MultipleKeys) { |
52 string key_11(CryptoSecretBoxer::GetKeySize(), 0x11); | 51 string key_11(CryptoSecretBoxer::GetKeySize(), 0x11); |
53 string key_12(CryptoSecretBoxer::GetKeySize(), 0x12); | 52 string key_12(CryptoSecretBoxer::GetKeySize(), 0x12); |
(...skipping 16 matching lines...) Expand all Loading... |
70 EXPECT_TRUE(CanDecode(boxer, boxer_12)); | 69 EXPECT_TRUE(CanDecode(boxer, boxer_12)); |
71 | 70 |
72 // After we flush key_11 from |boxer|, it can no longer decode tokens from | 71 // After we flush key_11 from |boxer|, it can no longer decode tokens from |
73 // |boxer_11|. | 72 // |boxer_11|. |
74 boxer.SetKeys({key_12}); | 73 boxer.SetKeys({key_12}); |
75 EXPECT_FALSE(CanDecode(boxer, boxer_11)); | 74 EXPECT_FALSE(CanDecode(boxer, boxer_11)); |
76 } | 75 } |
77 | 76 |
78 } // namespace test | 77 } // namespace test |
79 } // namespace net | 78 } // namespace net |
OLD | NEW |