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