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
3 // found in the LICENSE file.
4
5 #include "quiche/quic/core/crypto/null_decrypter.h"
6
7 #include "absl/base/macros.h"
8 #include "quiche/quic/platform/api/quic_test.h"
9 #include "quiche/quic/test_tools/quic_test_utils.h"
10
11 namespace quic {
12 namespace test {
13
14 class NullDecrypterTest : public QuicTestWithParam<bool> {};
15
TEST_F(NullDecrypterTest,DecryptClient)16 TEST_F(NullDecrypterTest, DecryptClient) {
17 unsigned char expected[] = {
18 // fnv hash
19 0x97,
20 0xdc,
21 0x27,
22 0x2f,
23 0x18,
24 0xa8,
25 0x56,
26 0x73,
27 0xdf,
28 0x8d,
29 0x1d,
30 0xd0,
31 // payload
32 'g',
33 'o',
34 'o',
35 'd',
36 'b',
37 'y',
38 'e',
39 '!',
40 };
41 const char* data = reinterpret_cast<const char*>(expected);
42 size_t len = ABSL_ARRAYSIZE(expected);
43 NullDecrypter decrypter(Perspective::IS_SERVER);
44 char buffer[256];
45 size_t length = 0;
46 ASSERT_TRUE(decrypter.DecryptPacket(
47 0, "hello world!", absl::string_view(data, len), buffer, &length, 256));
48 EXPECT_LT(0u, length);
49 EXPECT_EQ("goodbye!", absl::string_view(buffer, length));
50 }
51
TEST_F(NullDecrypterTest,DecryptServer)52 TEST_F(NullDecrypterTest, DecryptServer) {
53 unsigned char expected[] = {
54 // fnv hash
55 0x63,
56 0x5e,
57 0x08,
58 0x03,
59 0x32,
60 0x80,
61 0x8f,
62 0x73,
63 0xdf,
64 0x8d,
65 0x1d,
66 0x1a,
67 // payload
68 'g',
69 'o',
70 'o',
71 'd',
72 'b',
73 'y',
74 'e',
75 '!',
76 };
77 const char* data = reinterpret_cast<const char*>(expected);
78 size_t len = ABSL_ARRAYSIZE(expected);
79 NullDecrypter decrypter(Perspective::IS_CLIENT);
80 char buffer[256];
81 size_t length = 0;
82 ASSERT_TRUE(decrypter.DecryptPacket(
83 0, "hello world!", absl::string_view(data, len), buffer, &length, 256));
84 EXPECT_LT(0u, length);
85 EXPECT_EQ("goodbye!", absl::string_view(buffer, length));
86 }
87
TEST_F(NullDecrypterTest,BadHash)88 TEST_F(NullDecrypterTest, BadHash) {
89 unsigned char expected[] = {
90 // fnv hash
91 0x46,
92 0x11,
93 0xea,
94 0x5f,
95 0xcf,
96 0x1d,
97 0x66,
98 0x5b,
99 0xba,
100 0xf0,
101 0xbc,
102 0xfd,
103 // payload
104 'g',
105 'o',
106 'o',
107 'd',
108 'b',
109 'y',
110 'e',
111 '!',
112 };
113 const char* data = reinterpret_cast<const char*>(expected);
114 size_t len = ABSL_ARRAYSIZE(expected);
115 NullDecrypter decrypter(Perspective::IS_CLIENT);
116 char buffer[256];
117 size_t length = 0;
118 ASSERT_FALSE(decrypter.DecryptPacket(
119 0, "hello world!", absl::string_view(data, len), buffer, &length, 256));
120 }
121
TEST_F(NullDecrypterTest,ShortInput)122 TEST_F(NullDecrypterTest, ShortInput) {
123 unsigned char expected[] = {
124 // fnv hash (truncated)
125 0x46, 0x11, 0xea, 0x5f, 0xcf, 0x1d, 0x66, 0x5b, 0xba, 0xf0, 0xbc,
126 };
127 const char* data = reinterpret_cast<const char*>(expected);
128 size_t len = ABSL_ARRAYSIZE(expected);
129 NullDecrypter decrypter(Perspective::IS_CLIENT);
130 char buffer[256];
131 size_t length = 0;
132 ASSERT_FALSE(decrypter.DecryptPacket(
133 0, "hello world!", absl::string_view(data, len), buffer, &length, 256));
134 }
135
136 } // namespace test
137 } // namespace quic
138