1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsNSSIOLayer.h"
11 #include "gtest/gtest.h"
13 /* The following buffers are generated by encoding a sequence of integers from
14 0 to 100 using the corresponding encoding mechanism.
16 They could be re-generated using the following script:
26 encodedBufferExampleZlib = list(zlib.compress(bytes(a)))
27 encodedBufferExampleBrotli = list(brotli.compress(bytes(a)))
28 encodedBufferExampleZstd = list(zstd.compress(bytes(a)))
31 uint8_t encodedBufferExampleZlib
[108] = {
32 0x78, 0x9c, 0x63, 0x60, 0x64, 0x62, 0x66, 0x61, 0x65, 0x63, 0xe7, 0xe0,
33 0xe4, 0xe2, 0xe6, 0xe1, 0xe5, 0xe3, 0x17, 0x10, 0x14, 0x12, 0x16, 0x11,
34 0x15, 0x13, 0x97, 0x90, 0x94, 0x92, 0x96, 0x91, 0x95, 0x93, 0x57, 0x50,
35 0x54, 0x52, 0x56, 0x51, 0x55, 0x53, 0xd7, 0xd0, 0xd4, 0xd2, 0xd6, 0xd1,
36 0xd5, 0xd3, 0x37, 0x30, 0x34, 0x32, 0x36, 0x31, 0x35, 0x33, 0xb7, 0xb0,
37 0xb4, 0xb2, 0xb6, 0xb1, 0xb5, 0xb3, 0x77, 0x70, 0x74, 0x72, 0x76, 0x71,
38 0x75, 0x73, 0xf7, 0xf0, 0xf4, 0xf2, 0xf6, 0xf1, 0xf5, 0xf3, 0x0f, 0x08,
39 0x0c, 0x0a, 0x0e, 0x09, 0x0d, 0x0b, 0x8f, 0x88, 0x8c, 0x8a, 0x8e, 0x89,
40 0x8d, 0x8b, 0x4f, 0x48, 0x4c, 0x4a, 0x06, 0x00, 0x8b, 0x7c, 0x13, 0x57,
43 uint8_t encodedBufferExampleBrotli
[73] = {
44 0x1b, 0x63, 0x0, 0xe8, 0x65, 0x60, 0x9e, 0x29, 0x8f, 0xd2, 0x59,
45 0x86, 0xc8, 0x54, 0x48, 0x4c, 0xb0, 0x89, 0x63, 0x77, 0x7f, 0x0,
46 0x40, 0x8, 0x46, 0x50, 0xc, 0x27, 0x48, 0x8a, 0x66, 0x58, 0x8e,
47 0x17, 0x44, 0x49, 0x56, 0x54, 0x4d, 0x37, 0x4c, 0xcb, 0x76, 0x5c,
48 0xcf, 0xf, 0xc2, 0x28, 0x4e, 0xd2, 0x2c, 0x2f, 0xca, 0xaa, 0x6e,
49 0xda, 0xae, 0x1f, 0xc6, 0x69, 0x5e, 0xd6, 0x6d, 0x3f, 0xce, 0xeb,
50 0x7e, 0x5e, 0xef, 0xcf, 0xf7, 0xf7, 0x7};
52 uint8_t encodedBufferExampleZstd
[113] = {
53 0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x64, 0x21, 0x03, 0x00, 0x00, 0x01, 0x02,
54 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
55 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
56 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
57 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32,
58 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
59 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
60 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
61 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
62 0x63, 0x97, 0x65, 0x16, 0x32};
64 struct CertificateCompressionTestCase
{
66 unsigned int encodedBufLen
;
67 SECStatus (*decode
)(const SECItem
* input
, unsigned char* output
,
68 size_t outputLen
, size_t* usedLen
);
71 static const CertificateCompressionTestCase CertificateCompressionTestCases
[] =
72 {{encodedBufferExampleZlib
, sizeof(encodedBufferExampleZlib
),
73 zlibCertificateDecode
},
74 {encodedBufferExampleBrotli
, sizeof(encodedBufferExampleBrotli
),
75 brotliCertificateDecode
},
76 {encodedBufferExampleZstd
, sizeof(encodedBufferExampleZstd
),
77 zstdCertificateDecode
}};
79 class psm_CertificateCompression
80 : public ::testing::Test
,
81 public ::testing::WithParamInterface
<CertificateCompressionTestCase
> {};
83 TEST_P(psm_CertificateCompression
, DecoderCorrectlyDecodes
) {
84 const CertificateCompressionTestCase
& testCase(GetParam());
86 SECItem encodedItem
= {siBuffer
, testCase
.encodedBuf
, testCase
.encodedBufLen
};
87 uint8_t decodedEncodedBuffer
[100] = {0};
90 SECStatus rv
= testCase
.decode(&encodedItem
, decodedEncodedBuffer
,
91 sizeof(decodedEncodedBuffer
), &usedLen
);
92 ASSERT_EQ(SECSuccess
, rv
);
94 ASSERT_EQ(usedLen
, sizeof(decodedEncodedBuffer
));
95 for (size_t i
= 0; i
< usedLen
; i
++) {
96 ASSERT_EQ(decodedEncodedBuffer
[i
], i
);
100 TEST_P(psm_CertificateCompression
, DecodingFailsEmptyInputItem
) {
101 const CertificateCompressionTestCase
& testCase(GetParam());
102 uint8_t decodedEncodedBuffer
[500] = {0};
104 /* nullptr instead of input */
105 SECStatus rv
= testCase
.decode(nullptr, decodedEncodedBuffer
,
106 sizeof(decodedEncodedBuffer
), &usedLen
);
107 ASSERT_EQ(SECFailure
, rv
);
110 TEST_P(psm_CertificateCompression
, DecodingFailsEmptyInput
) {
111 const CertificateCompressionTestCase
& testCase(GetParam());
112 SECItem encodedItem
= {siBuffer
, nullptr,
113 (unsigned int)sizeof(encodedBufferExampleZlib
)};
114 uint8_t decodedEncodedBuffer
[500] = {0};
116 /* nullptr instead of input.data */
117 SECStatus rv
= testCase
.decode(&encodedItem
, decodedEncodedBuffer
,
118 sizeof(decodedEncodedBuffer
), &usedLen
);
119 ASSERT_EQ(SECFailure
, rv
);
122 TEST_P(psm_CertificateCompression
, DecodingFails0LenInput
) {
123 const CertificateCompressionTestCase
& testCase(GetParam());
124 SECItem encodedItem
= {siBuffer
, encodedBufferExampleZlib
, 0};
125 uint8_t decodedEncodedBuffer
[500] = {0};
127 /* 0 instead of input.len*/
128 SECStatus rv
= testCase
.decode(&encodedItem
, decodedEncodedBuffer
,
129 sizeof(decodedEncodedBuffer
), &usedLen
);
130 ASSERT_EQ(SECFailure
, rv
);
133 TEST_P(psm_CertificateCompression
, DecodingFailsEmptyBuffer
) {
134 const CertificateCompressionTestCase
& testCase(GetParam());
135 unsigned char encodedBuffer
[500] = {0};
136 SECItem encodedItem
= {siBuffer
, encodedBuffer
, 500};
137 uint8_t decodedEncodedBuffer
[500] = {0};
139 /* Empty buffer will return an error if decoded. */
140 SECStatus rv
= testCase
.decode(&encodedItem
, decodedEncodedBuffer
,
141 sizeof(decodedEncodedBuffer
), &usedLen
);
142 ASSERT_EQ(SECFailure
, rv
);
145 TEST_P(psm_CertificateCompression
, DecodingFailsNullOutput
) {
146 const CertificateCompressionTestCase
& testCase(GetParam());
147 SECItem encodedItem
= {siBuffer
, encodedBufferExampleZlib
,
148 (unsigned int)sizeof(encodedBufferExampleZlib
)};
149 uint8_t decodedEncodedBuffer
[5] = {0};
151 /* Empty buffer will return an error if decoded. */
152 SECStatus rv
= testCase
.decode(&encodedItem
, nullptr,
153 sizeof(decodedEncodedBuffer
), &usedLen
);
154 ASSERT_EQ(SECFailure
, rv
);
157 TEST_P(psm_CertificateCompression
, DecodingFailsOutputTooSmall
) {
158 const CertificateCompressionTestCase
& testCase(GetParam());
159 SECItem encodedItem
= {siBuffer
, encodedBufferExampleZlib
,
160 (unsigned int)sizeof(encodedBufferExampleZlib
)};
161 uint8_t decodedEncodedBuffer
[5] = {0};
163 /* Empty buffer will return an error if decoded. */
164 SECStatus rv
= testCase
.decode(&encodedItem
, decodedEncodedBuffer
,
165 sizeof(decodedEncodedBuffer
), &usedLen
);
166 ASSERT_EQ(SECFailure
, rv
);
169 INSTANTIATE_TEST_SUITE_P(psm_CertificateCompression
, psm_CertificateCompression
,
170 ::testing::ValuesIn(CertificateCompressionTestCases
));