| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/quic/crypto/crypto_secret_boxer.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "net/quic/crypto/quic_random.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 using base::StringPiece; | |
| 13 using std::string; | |
| 14 | |
| 15 namespace net { | |
| 16 namespace test { | |
| 17 | |
| 18 TEST(CryptoSecretBoxerTest, BoxAndUnbox) { | |
| 19 StringPiece message("hello world"); | |
| 20 | |
| 21 CryptoSecretBoxer boxer; | |
| 22 boxer.SetKeys({string(CryptoSecretBoxer::GetKeySize(), 0x11)}); | |
| 23 | |
| 24 const string box = boxer.Box(QuicRandom::GetInstance(), message); | |
| 25 | |
| 26 string storage; | |
| 27 StringPiece result; | |
| 28 EXPECT_TRUE(boxer.Unbox(box, &storage, &result)); | |
| 29 EXPECT_EQ(result, message); | |
| 30 | |
| 31 EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box, &storage, &result)); | |
| 32 EXPECT_FALSE(boxer.Unbox(box.substr(1, string::npos), &storage, &result)); | |
| 33 EXPECT_FALSE(boxer.Unbox(string(), &storage, &result)); | |
| 34 EXPECT_FALSE( | |
| 35 boxer.Unbox(string(1, box[0] ^ 0x80) + box.substr(1, string::npos), | |
| 36 &storage, &result)); | |
| 37 } | |
| 38 | |
| 39 // Helper function to test whether one boxer can decode the output of another. | |
| 40 static bool CanDecode(const CryptoSecretBoxer& decoder, | |
| 41 const CryptoSecretBoxer& encoder) { | |
| 42 StringPiece message("hello world"); | |
| 43 const string boxed = encoder.Box(QuicRandom::GetInstance(), message); | |
| 44 string storage; | |
| 45 StringPiece result; | |
| 46 bool ok = decoder.Unbox(boxed, &storage, &result); | |
| 47 if (ok) { | |
| 48 EXPECT_EQ(result, message); | |
| 49 } | |
| 50 return ok; | |
| 51 } | |
| 52 | |
| 53 TEST(CryptoSecretBoxerTest, MultipleKeys) { | |
| 54 string key_11(CryptoSecretBoxer::GetKeySize(), 0x11); | |
| 55 string key_12(CryptoSecretBoxer::GetKeySize(), 0x12); | |
| 56 | |
| 57 CryptoSecretBoxer boxer_11, boxer_12, boxer; | |
| 58 boxer_11.SetKeys({key_11}); | |
| 59 boxer_12.SetKeys({key_12}); | |
| 60 boxer.SetKeys({key_12, key_11}); | |
| 61 | |
| 62 // Neither single-key boxer can decode the other's tokens. | |
| 63 EXPECT_FALSE(CanDecode(boxer_11, boxer_12)); | |
| 64 EXPECT_FALSE(CanDecode(boxer_12, boxer_11)); | |
| 65 | |
| 66 // |boxer| encodes with the first key, which is key_12. | |
| 67 EXPECT_TRUE(CanDecode(boxer_12, boxer)); | |
| 68 EXPECT_FALSE(CanDecode(boxer_11, boxer)); | |
| 69 | |
| 70 // The boxer with both keys can decode tokens from either single-key boxer. | |
| 71 EXPECT_TRUE(CanDecode(boxer, boxer_11)); | |
| 72 EXPECT_TRUE(CanDecode(boxer, boxer_12)); | |
| 73 | |
| 74 // After we flush key_11 from |boxer|, it can no longer decode tokens from | |
| 75 // |boxer_11|. | |
| 76 boxer.SetKeys({key_12}); | |
| 77 EXPECT_FALSE(CanDecode(boxer, boxer_11)); | |
| 78 } | |
| 79 | |
| 80 } // namespace test | |
| 81 } // namespace net | |
| OLD | NEW |