blob: 56b86e037b1a8f26039bd3bf44644fb9b922cb4a [file] [log] [blame]
nnoble0c475f02014-12-05 15:37:39 -08001/*
2 *
murgatroid99c3910ca2016-01-06 13:14:23 -08003 * Copyright 2015-2016, Google Inc.
nnoble0c475f02014-12-05 15:37:39 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tillerc7762a82016-03-28 10:13:08 -070034#include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
ctiller33023c42014-12-12 16:28:33 -080035
36#include <string.h>
37
murgatroid99c3910ca2016-01-06 13:14:23 -080038/* This is here for grpc_is_binary_header
39 * TODO(murgatroid99): Remove this
40 */
41#include <grpc/grpc.h>
nnoble0c475f02014-12-05 15:37:39 -080042#include <grpc/support/alloc.h>
43#include <grpc/support/log.h>
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/support/string.h"
nnoble0c475f02014-12-05 15:37:39 -080045
46static int all_ok = 1;
47
48static void expect_slice_eq(gpr_slice expected, gpr_slice slice, char *debug,
49 int line) {
50 if (0 != gpr_slice_cmp(slice, expected)) {
Julien Boeufda13cd22015-06-29 19:25:32 +020051 char *hs = gpr_dump_slice(slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
52 char *he = gpr_dump_slice(expected, GPR_DUMP_HEX | GPR_DUMP_ASCII);
nnoble0c475f02014-12-05 15:37:39 -080053 gpr_log(GPR_ERROR, "FAILED:%d: %s\ngot: %s\nwant: %s", line, debug, hs,
54 he);
55 gpr_free(hs);
56 gpr_free(he);
57 all_ok = 0;
58 }
59 gpr_slice_unref(expected);
60 gpr_slice_unref(slice);
61}
62
63static gpr_slice B64(const char *s) {
64 gpr_slice ss = gpr_slice_from_copied_string(s);
65 gpr_slice out = grpc_chttp2_base64_encode(ss);
66 gpr_slice_unref(ss);
67 return out;
68}
69
70static gpr_slice HUFF(const char *s) {
71 gpr_slice ss = gpr_slice_from_copied_string(s);
72 gpr_slice out = grpc_chttp2_huffman_compress(ss);
73 gpr_slice_unref(ss);
74 return out;
75}
76
77#define EXPECT_SLICE_EQ(expected, slice) \
78 expect_slice_eq( \
79 gpr_slice_from_copied_buffer(expected, sizeof(expected) - 1), slice, \
80 #slice, __LINE__);
81
82static void expect_combined_equiv(const char *s, size_t len, int line) {
83 gpr_slice input = gpr_slice_from_copied_buffer(s, len);
84 gpr_slice base64 = grpc_chttp2_base64_encode(input);
85 gpr_slice expect = grpc_chttp2_huffman_compress(base64);
86 gpr_slice got = grpc_chttp2_base64_encode_and_huffman_compress(input);
87 if (0 != gpr_slice_cmp(expect, got)) {
Julien Boeufda13cd22015-06-29 19:25:32 +020088 char *t = gpr_dump_slice(input, GPR_DUMP_HEX | GPR_DUMP_ASCII);
89 char *e = gpr_dump_slice(expect, GPR_DUMP_HEX | GPR_DUMP_ASCII);
90 char *g = gpr_dump_slice(got, GPR_DUMP_HEX | GPR_DUMP_ASCII);
nnoble0c475f02014-12-05 15:37:39 -080091 gpr_log(GPR_ERROR, "FAILED:%d:\ntest: %s\ngot: %s\nwant: %s", t, g, e);
92 gpr_free(t);
93 gpr_free(e);
94 gpr_free(g);
ctiller33023c42014-12-12 16:28:33 -080095 all_ok = 0;
nnoble0c475f02014-12-05 15:37:39 -080096 }
97 gpr_slice_unref(input);
98 gpr_slice_unref(base64);
99 gpr_slice_unref(expect);
100 gpr_slice_unref(got);
101}
102
103#define EXPECT_COMBINED_EQUIV(x) \
104 expect_combined_equiv(x, sizeof(x) - 1, __LINE__)
105
ctiller33023c42014-12-12 16:28:33 -0800106static void expect_binary_header(const char *hdr, int binary) {
107 if (grpc_is_binary_header(hdr, strlen(hdr)) != binary) {
108 gpr_log(GPR_ERROR, "FAILED: expected header '%s' to be %s", hdr,
109 binary ? "binary" : "not binary");
110 all_ok = 0;
111 }
112}
113
nnoble0c475f02014-12-05 15:37:39 -0800114int main(int argc, char **argv) {
115 /* Base64 test vectors from RFC 4648, with padding removed */
116 /* BASE64("") = "" */
117 EXPECT_SLICE_EQ("", B64(""));
118 /* BASE64("f") = "Zg" */
119 EXPECT_SLICE_EQ("Zg", B64("f"));
120 /* BASE64("fo") = "Zm8" */
121 EXPECT_SLICE_EQ("Zm8", B64("fo"));
122 /* BASE64("foo") = "Zm9v" */
123 EXPECT_SLICE_EQ("Zm9v", B64("foo"));
124 /* BASE64("foob") = "Zm9vYg" */
125 EXPECT_SLICE_EQ("Zm9vYg", B64("foob"));
126 /* BASE64("fooba") = "Zm9vYmE" */
127 EXPECT_SLICE_EQ("Zm9vYmE", B64("fooba"));
128 /* BASE64("foobar") = "Zm9vYmFy" */
129 EXPECT_SLICE_EQ("Zm9vYmFy", B64("foobar"));
130
ctiller33023c42014-12-12 16:28:33 -0800131 EXPECT_SLICE_EQ("wMHCw8TF", B64("\xc0\xc1\xc2\xc3\xc4\xc5"));
132
nnoble0c475f02014-12-05 15:37:39 -0800133 /* Huffman encoding tests */
134 EXPECT_SLICE_EQ("\xf1\xe3\xc2\xe5\xf2\x3a\x6b\xa0\xab\x90\xf4\xff",
135 HUFF("www.example.com"));
136 EXPECT_SLICE_EQ("\xa8\xeb\x10\x64\x9c\xbf", HUFF("no-cache"));
137 EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f", HUFF("custom-key"));
138 EXPECT_SLICE_EQ("\x25\xa8\x49\xe9\x5b\xb8\xe8\xb4\xbf", HUFF("custom-value"));
139 EXPECT_SLICE_EQ("\xae\xc3\x77\x1a\x4b", HUFF("private"));
140 EXPECT_SLICE_EQ(
141 "\xd0\x7a\xbe\x94\x10\x54\xd4\x44\xa8\x20\x05\x95\x04\x0b\x81\x66\xe0\x82"
142 "\xa6\x2d\x1b\xff",
143 HUFF("Mon, 21 Oct 2013 20:13:21 GMT"));
144 EXPECT_SLICE_EQ(
145 "\x9d\x29\xad\x17\x18\x63\xc7\x8f\x0b\x97\xc8\xe9\xae\x82\xae\x43\xd3",
146 HUFF("https://www.example.com"));
147
148 /* Various test vectors for combined encoding */
149 EXPECT_COMBINED_EQUIV("");
150 EXPECT_COMBINED_EQUIV("f");
151 EXPECT_COMBINED_EQUIV("fo");
152 EXPECT_COMBINED_EQUIV("foo");
153 EXPECT_COMBINED_EQUIV("foob");
154 EXPECT_COMBINED_EQUIV("fooba");
155 EXPECT_COMBINED_EQUIV("foobar");
156 EXPECT_COMBINED_EQUIV("www.example.com");
157 EXPECT_COMBINED_EQUIV("no-cache");
158 EXPECT_COMBINED_EQUIV("custom-key");
159 EXPECT_COMBINED_EQUIV("custom-value");
160 EXPECT_COMBINED_EQUIV("private");
161 EXPECT_COMBINED_EQUIV("Mon, 21 Oct 2013 20:13:21 GMT");
162 EXPECT_COMBINED_EQUIV("https://www.example.com");
163 EXPECT_COMBINED_EQUIV(
164 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
165 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
166 "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
167 "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
168 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
169 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
170 "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
171 "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
172 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
173 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
174 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
175 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
176 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
177 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
178 "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
179 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff");
180
ctiller33023c42014-12-12 16:28:33 -0800181 expect_binary_header("foo-bin", 1);
182 expect_binary_header("foo-bar", 0);
183 expect_binary_header("-bin", 0);
184
nnoble0c475f02014-12-05 15:37:39 -0800185 return all_ok ? 0 : 1;
Craig Tiller190d3602015-02-18 09:23:38 -0800186}