| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/null_decrypter.h" | 5 #include "net/quic/core/crypto/null_decrypter.h" |
| 6 #include "net/quic/test_tools/quic_test_utils.h" | 6 #include "net/quic/test_tools/quic_test_utils.h" |
| 7 | 7 |
| 8 using base::StringPiece; | 8 using base::StringPiece; |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 namespace test { | 11 namespace test { |
| 12 | 12 |
| 13 class NullDecrypterTest : public ::testing::TestWithParam<bool> {}; | 13 class NullDecrypterTest : public ::testing::TestWithParam<bool> {}; |
| 14 | 14 |
| 15 TEST_F(NullDecrypterTest, DecryptClient) { | 15 TEST_F(NullDecrypterTest, DecryptClient) { |
| 16 unsigned char expected[] = { | 16 unsigned char expected[] = { |
| 17 // fnv hash | 17 // fnv hash |
| 18 0x97, 0xdc, 0x27, 0x2f, 0x18, 0xa8, 0x56, 0x73, 0xdf, 0x8d, 0x1d, 0xd0, | 18 0x97, 0xdc, 0x27, 0x2f, 0x18, 0xa8, 0x56, 0x73, 0xdf, 0x8d, 0x1d, 0xd0, |
| 19 // payload | 19 // payload |
| 20 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', | 20 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', |
| 21 }; | 21 }; |
| 22 const char* data = reinterpret_cast<const char*>(expected); | 22 const char* data = reinterpret_cast<const char*>(expected); |
| 23 size_t len = arraysize(expected); | 23 size_t len = arraysize(expected); |
| 24 NullDecrypter decrypter(Perspective::IS_SERVER); | 24 NullDecrypter decrypter(Perspective::IS_SERVER); |
| 25 char buffer[256]; | 25 char buffer[256]; |
| 26 size_t length = 0; | 26 size_t length = 0; |
| 27 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_37, kDefaultPathId, 0, | 27 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_37, 0, "hello world!", |
| 28 "hello world!", StringPiece(data, len), | 28 StringPiece(data, len), buffer, &length, |
| 29 buffer, &length, 256)); | 29 256)); |
| 30 EXPECT_LT(0u, length); | 30 EXPECT_LT(0u, length); |
| 31 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); | 31 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); |
| 32 } | 32 } |
| 33 | 33 |
| 34 TEST_F(NullDecrypterTest, DecryptServer) { | 34 TEST_F(NullDecrypterTest, DecryptServer) { |
| 35 unsigned char expected[] = { | 35 unsigned char expected[] = { |
| 36 // fnv hash | 36 // fnv hash |
| 37 0x63, 0x5e, 0x08, 0x03, 0x32, 0x80, 0x8f, 0x73, 0xdf, 0x8d, 0x1d, 0x1a, | 37 0x63, 0x5e, 0x08, 0x03, 0x32, 0x80, 0x8f, 0x73, 0xdf, 0x8d, 0x1d, 0x1a, |
| 38 // payload | 38 // payload |
| 39 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', | 39 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', |
| 40 }; | 40 }; |
| 41 const char* data = reinterpret_cast<const char*>(expected); | 41 const char* data = reinterpret_cast<const char*>(expected); |
| 42 size_t len = arraysize(expected); | 42 size_t len = arraysize(expected); |
| 43 NullDecrypter decrypter(Perspective::IS_CLIENT); | 43 NullDecrypter decrypter(Perspective::IS_CLIENT); |
| 44 char buffer[256]; | 44 char buffer[256]; |
| 45 size_t length = 0; | 45 size_t length = 0; |
| 46 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_37, kDefaultPathId, 0, | 46 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_37, 0, "hello world!", |
| 47 "hello world!", StringPiece(data, len), | 47 StringPiece(data, len), buffer, &length, |
| 48 buffer, &length, 256)); | 48 256)); |
| 49 EXPECT_LT(0u, length); | 49 EXPECT_LT(0u, length); |
| 50 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); | 50 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); |
| 51 } | 51 } |
| 52 | 52 |
| 53 TEST_F(NullDecrypterTest, DecryptClientPre37) { | 53 TEST_F(NullDecrypterTest, DecryptClientPre37) { |
| 54 unsigned char expected[] = { | 54 unsigned char expected[] = { |
| 55 // fnv hash | 55 // fnv hash |
| 56 0xa0, 0x6f, 0x44, 0x8a, 0x44, 0xf8, 0x18, 0x3b, 0x47, 0x91, 0xb2, 0x13, | 56 0xa0, 0x6f, 0x44, 0x8a, 0x44, 0xf8, 0x18, 0x3b, 0x47, 0x91, 0xb2, 0x13, |
| 57 // payload | 57 // payload |
| 58 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', | 58 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', |
| 59 }; | 59 }; |
| 60 const char* data = reinterpret_cast<const char*>(expected); | 60 const char* data = reinterpret_cast<const char*>(expected); |
| 61 size_t len = arraysize(expected); | 61 size_t len = arraysize(expected); |
| 62 NullDecrypter decrypter(Perspective::IS_CLIENT); | 62 NullDecrypter decrypter(Perspective::IS_CLIENT); |
| 63 char buffer[256]; | 63 char buffer[256]; |
| 64 size_t length = 0; | 64 size_t length = 0; |
| 65 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_36, kDefaultPathId, 0, | 65 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_36, 0, "hello world!", |
| 66 "hello world!", StringPiece(data, len), | 66 StringPiece(data, len), buffer, &length, |
| 67 buffer, &length, 256)); | 67 256)); |
| 68 EXPECT_LT(0u, length); | 68 EXPECT_LT(0u, length); |
| 69 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); | 69 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 TEST_F(NullDecrypterTest, DecryptServerPre37) { | 72 TEST_F(NullDecrypterTest, DecryptServerPre37) { |
| 73 unsigned char expected[] = { | 73 unsigned char expected[] = { |
| 74 // fnv hash | 74 // fnv hash |
| 75 0xa0, 0x6f, 0x44, 0x8a, 0x44, 0xf8, 0x18, 0x3b, 0x47, 0x91, 0xb2, 0x13, | 75 0xa0, 0x6f, 0x44, 0x8a, 0x44, 0xf8, 0x18, 0x3b, 0x47, 0x91, 0xb2, 0x13, |
| 76 // payload | 76 // payload |
| 77 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', | 77 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', |
| 78 }; | 78 }; |
| 79 const char* data = reinterpret_cast<const char*>(expected); | 79 const char* data = reinterpret_cast<const char*>(expected); |
| 80 size_t len = arraysize(expected); | 80 size_t len = arraysize(expected); |
| 81 NullDecrypter decrypter(Perspective::IS_SERVER); | 81 NullDecrypter decrypter(Perspective::IS_SERVER); |
| 82 char buffer[256]; | 82 char buffer[256]; |
| 83 size_t length = 0; | 83 size_t length = 0; |
| 84 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_36, kDefaultPathId, 0, | 84 ASSERT_TRUE(decrypter.DecryptPacket(QUIC_VERSION_36, 0, "hello world!", |
| 85 "hello world!", StringPiece(data, len), | 85 StringPiece(data, len), buffer, &length, |
| 86 buffer, &length, 256)); | 86 256)); |
| 87 EXPECT_LT(0u, length); | 87 EXPECT_LT(0u, length); |
| 88 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); | 88 EXPECT_EQ("goodbye!", StringPiece(buffer, length)); |
| 89 } | 89 } |
| 90 | 90 |
| 91 TEST_F(NullDecrypterTest, BadHash) { | 91 TEST_F(NullDecrypterTest, BadHash) { |
| 92 unsigned char expected[] = { | 92 unsigned char expected[] = { |
| 93 // fnv hash | 93 // fnv hash |
| 94 0x46, 0x11, 0xea, 0x5f, 0xcf, 0x1d, 0x66, 0x5b, 0xba, 0xf0, 0xbc, 0xfd, | 94 0x46, 0x11, 0xea, 0x5f, 0xcf, 0x1d, 0x66, 0x5b, 0xba, 0xf0, 0xbc, 0xfd, |
| 95 // payload | 95 // payload |
| 96 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', | 96 'g', 'o', 'o', 'd', 'b', 'y', 'e', '!', |
| 97 }; | 97 }; |
| 98 const char* data = reinterpret_cast<const char*>(expected); | 98 const char* data = reinterpret_cast<const char*>(expected); |
| 99 size_t len = arraysize(expected); | 99 size_t len = arraysize(expected); |
| 100 NullDecrypter decrypter(Perspective::IS_CLIENT); | 100 NullDecrypter decrypter(Perspective::IS_CLIENT); |
| 101 char buffer[256]; | 101 char buffer[256]; |
| 102 size_t length = 0; | 102 size_t length = 0; |
| 103 ASSERT_FALSE(decrypter.DecryptPacket(QUIC_VERSION_35, kDefaultPathId, 0, | 103 ASSERT_FALSE(decrypter.DecryptPacket(QUIC_VERSION_35, 0, "hello world!", |
| 104 "hello world!", StringPiece(data, len), | 104 StringPiece(data, len), buffer, &length, |
| 105 buffer, &length, 256)); | 105 256)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 TEST_F(NullDecrypterTest, ShortInput) { | 108 TEST_F(NullDecrypterTest, ShortInput) { |
| 109 unsigned char expected[] = { | 109 unsigned char expected[] = { |
| 110 // fnv hash (truncated) | 110 // fnv hash (truncated) |
| 111 0x46, 0x11, 0xea, 0x5f, 0xcf, 0x1d, 0x66, 0x5b, 0xba, 0xf0, 0xbc, | 111 0x46, 0x11, 0xea, 0x5f, 0xcf, 0x1d, 0x66, 0x5b, 0xba, 0xf0, 0xbc, |
| 112 }; | 112 }; |
| 113 const char* data = reinterpret_cast<const char*>(expected); | 113 const char* data = reinterpret_cast<const char*>(expected); |
| 114 size_t len = arraysize(expected); | 114 size_t len = arraysize(expected); |
| 115 NullDecrypter decrypter(Perspective::IS_CLIENT); | 115 NullDecrypter decrypter(Perspective::IS_CLIENT); |
| 116 char buffer[256]; | 116 char buffer[256]; |
| 117 size_t length = 0; | 117 size_t length = 0; |
| 118 ASSERT_FALSE(decrypter.DecryptPacket(QUIC_VERSION_35, kDefaultPathId, 0, | 118 ASSERT_FALSE(decrypter.DecryptPacket(QUIC_VERSION_35, 0, "hello world!", |
| 119 "hello world!", StringPiece(data, len), | 119 StringPiece(data, len), buffer, &length, |
| 120 buffer, &length, 256)); | 120 256)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 } // namespace test | 123 } // namespace test |
| 124 } // namespace net | 124 } // namespace net |
| OLD | NEW |