xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/crypto/null_encrypter_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_encrypter.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 #include "quiche/common/test_tools/quiche_test_utils.h"
11 
12 namespace quic {
13 namespace test {
14 
15 class NullEncrypterTest : public QuicTestWithParam<bool> {};
16 
TEST_F(NullEncrypterTest,EncryptClient)17 TEST_F(NullEncrypterTest, EncryptClient) {
18   unsigned char expected[] = {
19       // fnv hash
20       0x97,
21       0xdc,
22       0x27,
23       0x2f,
24       0x18,
25       0xa8,
26       0x56,
27       0x73,
28       0xdf,
29       0x8d,
30       0x1d,
31       0xd0,
32       // payload
33       'g',
34       'o',
35       'o',
36       'd',
37       'b',
38       'y',
39       'e',
40       '!',
41   };
42   char encrypted[256];
43   size_t encrypted_len = 0;
44   NullEncrypter encrypter(Perspective::IS_CLIENT);
45   ASSERT_TRUE(encrypter.EncryptPacket(0, "hello world!", "goodbye!", encrypted,
46                                       &encrypted_len, 256));
47   quiche::test::CompareCharArraysWithHexError(
48       "encrypted data", encrypted, encrypted_len,
49       reinterpret_cast<const char*>(expected), ABSL_ARRAYSIZE(expected));
50 }
51 
TEST_F(NullEncrypterTest,EncryptServer)52 TEST_F(NullEncrypterTest, EncryptServer) {
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   char encrypted[256];
78   size_t encrypted_len = 0;
79   NullEncrypter encrypter(Perspective::IS_SERVER);
80   ASSERT_TRUE(encrypter.EncryptPacket(0, "hello world!", "goodbye!", encrypted,
81                                       &encrypted_len, 256));
82   quiche::test::CompareCharArraysWithHexError(
83       "encrypted data", encrypted, encrypted_len,
84       reinterpret_cast<const char*>(expected), ABSL_ARRAYSIZE(expected));
85 }
86 
TEST_F(NullEncrypterTest,GetMaxPlaintextSize)87 TEST_F(NullEncrypterTest, GetMaxPlaintextSize) {
88   NullEncrypter encrypter(Perspective::IS_CLIENT);
89   EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012));
90   EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112));
91   EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22));
92   EXPECT_EQ(0u, encrypter.GetMaxPlaintextSize(11));
93 }
94 
TEST_F(NullEncrypterTest,GetCiphertextSize)95 TEST_F(NullEncrypterTest, GetCiphertextSize) {
96   NullEncrypter encrypter(Perspective::IS_CLIENT);
97   EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000));
98   EXPECT_EQ(112u, encrypter.GetCiphertextSize(100));
99   EXPECT_EQ(22u, encrypter.GetCiphertextSize(10));
100 }
101 
102 }  // namespace test
103 }  // namespace quic
104