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/crypto/crypto_secret_boxer.h" | 5 #include "net/quic/crypto/crypto_secret_boxer.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "net/quic/crypto/quic_random.h" | 8 #include "net/quic/crypto/quic_random.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 using base::StringPiece; | 11 using base::StringPiece; |
12 using std::string; | 12 using std::string; |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 namespace test { | 15 namespace test { |
16 | 16 |
17 TEST(CryptoSecretBoxerTest, BoxAndUnbox) { | 17 TEST(CryptoSecretBoxerTest, BoxAndUnbox) { |
18 StringPiece message("hello world"); | 18 StringPiece message("hello world"); |
19 const size_t key_size = CryptoSecretBoxer::GetKeySize(); | 19 const size_t key_size = CryptoSecretBoxer::GetKeySize(); |
20 scoped_ptr<uint8[]> key(new uint8[key_size]); | 20 scoped_ptr<uint8_t[]> key(new uint8_t[key_size]); |
21 memset(key.get(), 0x11, key_size); | 21 memset(key.get(), 0x11, key_size); |
22 | 22 |
23 CryptoSecretBoxer boxer; | 23 CryptoSecretBoxer boxer; |
24 boxer.SetKey(StringPiece(reinterpret_cast<char*>(key.get()), key_size)); | 24 boxer.SetKey(StringPiece(reinterpret_cast<char*>(key.get()), key_size)); |
25 | 25 |
26 const string box = boxer.Box(QuicRandom::GetInstance(), message); | 26 const string box = boxer.Box(QuicRandom::GetInstance(), message); |
27 | 27 |
28 string storage; | 28 string storage; |
29 StringPiece result; | 29 StringPiece result; |
30 EXPECT_TRUE(boxer.Unbox(box, &storage, &result)); | 30 EXPECT_TRUE(boxer.Unbox(box, &storage, &result)); |
31 EXPECT_EQ(result, message); | 31 EXPECT_EQ(result, message); |
32 | 32 |
33 EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box, &storage, &result)); | 33 EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box, &storage, &result)); |
34 EXPECT_FALSE(boxer.Unbox(box.substr(1, string::npos), &storage, &result)); | 34 EXPECT_FALSE(boxer.Unbox(box.substr(1, string::npos), &storage, &result)); |
35 EXPECT_FALSE(boxer.Unbox(string(), &storage, &result)); | 35 EXPECT_FALSE(boxer.Unbox(string(), &storage, &result)); |
36 EXPECT_FALSE( | 36 EXPECT_FALSE( |
37 boxer.Unbox(string(1, box[0] ^ 0x80) + box.substr(1, string::npos), | 37 boxer.Unbox(string(1, box[0] ^ 0x80) + box.substr(1, string::npos), |
38 &storage, &result)); | 38 &storage, &result)); |
39 } | 39 } |
40 | 40 |
41 } // namespace test | 41 } // namespace test |
42 } // namespace net | 42 } // namespace net |
OLD | NEW |