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