1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25
26 #include "src/core/lib/gpr/useful.h"
27 #include "src/core/tsi/alts/frame_protector/frame_handler.h"
28 #include "test/core/tsi/alts/crypt/gsec_test_util.h"
29
30 const size_t kFrameHandlerTestBufferSize = 1024;
31
32 typedef struct frame_handler {
33 alts_frame_writer* writer;
34 alts_frame_reader* reader;
35 unsigned char* buffer;
36 size_t buffer_size;
37 } frame_handler;
38
frame_length(size_t payload_length)39 static size_t frame_length(size_t payload_length) {
40 return payload_length + kFrameHeaderSize;
41 }
42
create_frame_handler()43 static frame_handler* create_frame_handler() {
44 frame_handler* handler =
45 static_cast<frame_handler*>(gpr_malloc(sizeof(frame_handler)));
46 handler->writer = alts_create_frame_writer();
47 handler->reader = alts_create_frame_reader();
48 handler->buffer = nullptr;
49 handler->buffer_size = 0;
50 return handler;
51 }
52
destroy_frame_handler(frame_handler * handler)53 static void destroy_frame_handler(frame_handler* handler) {
54 if (handler != nullptr) {
55 alts_destroy_frame_reader(handler->reader);
56 alts_destroy_frame_writer(handler->writer);
57 if (handler->buffer != nullptr) gpr_free(handler->buffer);
58 gpr_free(handler);
59 }
60 }
61
frame(frame_handler * handler,unsigned char * payload,size_t payload_length,size_t write_length)62 static void frame(frame_handler* handler, unsigned char* payload,
63 size_t payload_length, size_t write_length) {
64 handler->buffer_size = frame_length(payload_length);
65 handler->buffer =
66 static_cast<unsigned char*>(gpr_malloc(handler->buffer_size));
67 GPR_ASSERT(alts_reset_frame_writer(handler->writer, payload, payload_length));
68 size_t offset = 0;
69 while (offset < handler->buffer_size &&
70 !alts_is_frame_writer_done(handler->writer)) {
71 size_t bytes_written = GPR_MIN(write_length, handler->buffer_size - offset);
72 GPR_ASSERT(alts_write_frame_bytes(handler->writer, handler->buffer + offset,
73 &bytes_written));
74 offset += bytes_written;
75 }
76 GPR_ASSERT(alts_is_frame_writer_done(handler->writer));
77 GPR_ASSERT(handler->buffer_size == offset);
78 }
79
deframe(frame_handler * handler,unsigned char * bytes,size_t read_length)80 static size_t deframe(frame_handler* handler, unsigned char* bytes,
81 size_t read_length) {
82 GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
83 size_t offset = 0;
84 while (offset < handler->buffer_size &&
85 !alts_is_frame_reader_done(handler->reader)) {
86 size_t bytes_read = GPR_MIN(read_length, handler->buffer_size - offset);
87 GPR_ASSERT(alts_read_frame_bytes(handler->reader, handler->buffer + offset,
88 &bytes_read));
89 offset += bytes_read;
90 }
91 GPR_ASSERT(alts_is_frame_reader_done(handler->reader));
92 GPR_ASSERT(handler->buffer_size == offset);
93 return offset - handler->reader->header_bytes_read;
94 }
95
frame_n_deframe(frame_handler * handler,unsigned char * payload,size_t payload_length,size_t write_length,size_t read_length)96 static void frame_n_deframe(frame_handler* handler, unsigned char* payload,
97 size_t payload_length, size_t write_length,
98 size_t read_length) {
99 frame(handler, payload, payload_length, write_length);
100 unsigned char* bytes =
101 static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
102 size_t deframed_payload_length = deframe(handler, bytes, read_length);
103 GPR_ASSERT(payload_length == deframed_payload_length);
104 GPR_ASSERT(memcmp(payload, bytes, payload_length) == 0);
105 gpr_free(bytes);
106 }
107
frame_handler_test_frame_deframe()108 static void frame_handler_test_frame_deframe() {
109 unsigned char payload[] = "hello world";
110 size_t payload_length = strlen((char*)payload) + 1;
111 frame_handler* handler = create_frame_handler();
112 frame_n_deframe(handler, payload, payload_length,
113 frame_length(payload_length), frame_length(payload_length));
114 destroy_frame_handler(handler);
115 }
116
frame_handler_test_small_buffer()117 static void frame_handler_test_small_buffer() {
118 unsigned char payload[] = "hello world";
119 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
120 frame_handler* handler = create_frame_handler();
121 frame_n_deframe(handler, payload, payload_length, 1, 1);
122 destroy_frame_handler(handler);
123 }
124
frame_handler_test_null_input_stream()125 static void frame_handler_test_null_input_stream() {
126 frame_handler* handler = create_frame_handler();
127 GPR_ASSERT(!alts_reset_frame_writer(handler->writer, nullptr, 0));
128 destroy_frame_handler(handler);
129 }
130
frame_handler_test_bad_input_length()131 static void frame_handler_test_bad_input_length() {
132 unsigned char payload[] = "hello world";
133 frame_handler* handler = create_frame_handler();
134 GPR_ASSERT(!alts_reset_frame_writer(handler->writer, payload, SIZE_MAX));
135 destroy_frame_handler(handler);
136 }
137
frame_handler_test_null_writer_byte_length()138 static void frame_handler_test_null_writer_byte_length() {
139 unsigned char payload[] = "hello world";
140 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
141 frame_handler* handler = create_frame_handler();
142 GPR_ASSERT(alts_reset_frame_writer(handler->writer, payload, payload_length));
143 GPR_ASSERT(
144 !alts_write_frame_bytes(handler->writer, handler->buffer, nullptr));
145 destroy_frame_handler(handler);
146 }
147
frame_handler_test_null_writer_bytes()148 static void frame_handler_test_null_writer_bytes() {
149 unsigned char payload[] = "hello world";
150 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
151 frame_handler* handler = create_frame_handler();
152 GPR_ASSERT(alts_reset_frame_writer(handler->writer, payload, payload_length));
153 GPR_ASSERT(
154 !alts_write_frame_bytes(handler->writer, nullptr, &payload_length));
155 destroy_frame_handler(handler);
156 }
157
frame_handler_test_bad_frame_length()158 static void frame_handler_test_bad_frame_length() {
159 unsigned char payload[] = "hello world";
160 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
161 frame_handler* handler = create_frame_handler();
162 frame(handler, payload, payload_length, payload_length);
163 memset(handler->buffer, 0x00, kFrameLengthFieldSize);
164 unsigned char* bytes =
165 static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
166 GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
167 size_t bytes_read = handler->buffer_size;
168 GPR_ASSERT(
169 !alts_read_frame_bytes(handler->reader, handler->buffer, &bytes_read));
170 GPR_ASSERT(alts_is_frame_reader_done(handler->reader));
171 GPR_ASSERT(bytes_read == 0);
172 gpr_free(bytes);
173 destroy_frame_handler(handler);
174 }
175
frame_handler_test_unsupported_message_type()176 static void frame_handler_test_unsupported_message_type() {
177 unsigned char payload[] = "hello world";
178 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
179 frame_handler* handler = create_frame_handler();
180 frame(handler, payload, payload_length, payload_length);
181 memset(handler->buffer + kFrameLengthFieldSize, 0x00,
182 kFrameMessageTypeFieldSize);
183 unsigned char* bytes =
184 static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
185 GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
186 size_t bytes_read = handler->buffer_size;
187 GPR_ASSERT(
188 !alts_read_frame_bytes(handler->reader, handler->buffer, &bytes_read));
189 GPR_ASSERT(alts_is_frame_reader_done(handler->reader));
190 GPR_ASSERT(bytes_read == 0);
191 gpr_free(bytes);
192 destroy_frame_handler(handler);
193 }
194
frame_handler_test_null_output_stream()195 static void frame_handler_test_null_output_stream() {
196 unsigned char payload[] = "hello world";
197 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
198 frame_handler* handler = create_frame_handler();
199 frame(handler, payload, payload_length, payload_length);
200 GPR_ASSERT(!alts_reset_frame_reader(handler->reader, nullptr));
201 destroy_frame_handler(handler);
202 }
203
frame_handler_test_null_reader_byte_length()204 static void frame_handler_test_null_reader_byte_length() {
205 unsigned char payload[] = "hello world";
206 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
207 frame_handler* handler = create_frame_handler();
208 frame(handler, payload, payload_length, payload_length);
209 unsigned char* bytes =
210 static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
211 GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
212 GPR_ASSERT(!alts_read_frame_bytes(handler->reader, handler->buffer, nullptr));
213 gpr_free(bytes);
214 destroy_frame_handler(handler);
215 }
216
frame_handler_test_null_reader_bytes()217 static void frame_handler_test_null_reader_bytes() {
218 unsigned char payload[] = "hello world";
219 size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
220 frame_handler* handler = create_frame_handler();
221 frame(handler, payload, payload_length, payload_length);
222 unsigned char* bytes =
223 static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
224 GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
225 size_t bytes_read = handler->buffer_size;
226 GPR_ASSERT(!alts_read_frame_bytes(handler->reader, nullptr, &bytes_read));
227 gpr_free(bytes);
228 destroy_frame_handler(handler);
229 }
230
main(int argc,char ** argv)231 int main(int argc, char** argv) {
232 frame_handler_test_frame_deframe();
233 frame_handler_test_small_buffer();
234 frame_handler_test_null_input_stream();
235 frame_handler_test_bad_input_length();
236 frame_handler_test_null_writer_byte_length();
237 frame_handler_test_null_writer_bytes();
238 frame_handler_test_bad_frame_length();
239 frame_handler_test_unsupported_message_type();
240 frame_handler_test_null_output_stream();
241 frame_handler_test_null_reader_byte_length();
242 frame_handler_test_null_reader_bytes();
243 return 0;
244 }
245