1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 #include <aws/event-stream/event_stream.h>
7 #include <aws/testing/aws_test_harness.h>
8 
s_test_outgoing_no_op_valid_fn(struct aws_allocator * allocator,void * ctx)9 static int s_test_outgoing_no_op_valid_fn(struct aws_allocator *allocator, void *ctx) {
10     (void)allocator;
11     (void)ctx;
12     uint8_t test_data[] = {
13         0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x05, 0xc2, 0x48, 0xeb, 0x7d, 0x98, 0xc8, 0xff};
14 
15     struct aws_event_stream_message message;
16     struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data));
17     ASSERT_SUCCESS(
18         aws_event_stream_message_from_buffer(&message, NULL, &test_buf), "Message validation should have succeeded");
19 
20     ASSERT_INT_EQUALS(
21         0x00000010, aws_event_stream_message_total_length(&message), "Message length should have been 0x10");
22     ASSERT_INT_EQUALS(
23         0x00000000, aws_event_stream_message_headers_len(&message), "Headers Length should have been 0x00");
24     ASSERT_INT_EQUALS(
25         0x05c248eb, aws_event_stream_message_prelude_crc(&message), "Prelude CRC should have been 0x05c248eb");
26     ASSERT_INT_EQUALS(
27         0x7d98c8ff, aws_event_stream_message_message_crc(&message), "Message CRC should have been 0x7d98c8ff");
28 
29     aws_event_stream_message_clean_up(&message);
30 
31     return 0;
32 }
33 
AWS_TEST_CASE(test_outgoing_no_op_valid,s_test_outgoing_no_op_valid_fn)34 AWS_TEST_CASE(test_outgoing_no_op_valid, s_test_outgoing_no_op_valid_fn)
35 
36 static int s_test_outgoing_application_data_no_headers_valid_fn(struct aws_allocator *allocator, void *ctx) {
37     (void)allocator;
38     (void)ctx;
39     uint8_t test_data[] = {0x00, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x52, 0x8c, 0x5a, 0x7b, 0x27, 0x66,
40                            0x6f, 0x6f, 0x27, 0x3a, 0x27, 0x62, 0x61, 0x72, 0x27, 0x7d, 0xc3, 0x65, 0x39, 0x36};
41 
42     struct aws_event_stream_message message;
43     struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data));
44 
45     ASSERT_SUCCESS(
46         aws_event_stream_message_from_buffer(&message, NULL, &test_buf), "Message validation should have succeeded");
47 
48     ASSERT_INT_EQUALS(
49         0x0000001D, aws_event_stream_message_total_length(&message), "Message length should have been 0x0000001D");
50     ASSERT_INT_EQUALS(
51         0x00000000, aws_event_stream_message_headers_len(&message), "Headers Length should have been 0x00");
52     ASSERT_INT_EQUALS(
53         0xfd528c5a, aws_event_stream_message_prelude_crc(&message), "Prelude CRC should have been 0xfd528c5a");
54 
55     const char *expected_str = "{'foo':'bar'}";
56     ASSERT_INT_EQUALS(
57         strlen(expected_str),
58         aws_event_stream_message_payload_len(&message),
59         "payload length should have been %d",
60         (int)(strlen(expected_str)));
61 
62     ASSERT_BIN_ARRAYS_EQUALS(
63         expected_str,
64         strlen(expected_str),
65         aws_event_stream_message_payload(&message),
66         aws_event_stream_message_payload_len(&message),
67         "payload should have been %s",
68         expected_str);
69     ASSERT_INT_EQUALS(
70         0xc3653936, aws_event_stream_message_message_crc(&message), "Message CRC should have been 0xc3653936");
71 
72     aws_event_stream_message_clean_up(&message);
73 
74     return 0;
75 }
76 
AWS_TEST_CASE(test_outgoing_application_data_no_headers_valid,s_test_outgoing_application_data_no_headers_valid_fn)77 AWS_TEST_CASE(test_outgoing_application_data_no_headers_valid, s_test_outgoing_application_data_no_headers_valid_fn)
78 
79 static int s_test_outgoing_application_one_compressed_header_pair_valid_fn(struct aws_allocator *allocator, void *ctx) {
80     (void)allocator;
81     (void)ctx;
82     uint8_t test_data[] = {0x00, 0x00, 0x00, 0x3D, 0x00, 0x00, 0x00, 0x20, 0x07, 0xFD, 0x83, 0x96, 0x0C,
83                            'c',  'o',  'n',  't',  'e',  'n',  't',  '-',  't',  'y',  'p',  'e',  0x07,
84                            0x00, 0x10, 'a',  'p',  'p',  'l',  'i',  'c',  'a',  't',  'i',  'o',  'n',
85                            '/',  'j',  's',  'o',  'n',  0x7b, 0x27, 0x66, 0x6f, 0x6f, 0x27, 0x3a, 0x27,
86                            0x62, 0x61, 0x72, 0x27, 0x7d, 0x8D, 0x9C, 0x08, 0xB1};
87 
88     struct aws_event_stream_message message;
89     struct aws_byte_buf test_buf = aws_byte_buf_from_array(test_data, sizeof(test_data));
90 
91     ASSERT_SUCCESS(
92         aws_event_stream_message_from_buffer(&message, allocator, &test_buf),
93         "Message validation should have succeeded");
94 
95     ASSERT_INT_EQUALS(
96         0x0000003D, aws_event_stream_message_total_length(&message), "Message length should have been 0x0000003D");
97     ASSERT_INT_EQUALS(
98         0x00000020, aws_event_stream_message_headers_len(&message), "Headers Length should have been 0x00000020");
99     ASSERT_INT_EQUALS(
100         0x07FD8396, aws_event_stream_message_prelude_crc(&message), "Prelude CRC should have been 0x07FD8396");
101 
102     const char *expected_str = "{'foo':'bar'}";
103     ASSERT_INT_EQUALS(
104         strlen(expected_str),
105         aws_event_stream_message_payload_len(&message),
106         "payload length should have been %d",
107         (int)(strlen(expected_str)));
108 
109     ASSERT_BIN_ARRAYS_EQUALS(
110         expected_str,
111         strlen(expected_str),
112         aws_event_stream_message_payload(&message),
113         aws_event_stream_message_payload_len(&message),
114         "payload should have been %s",
115         expected_str);
116 
117     struct aws_array_list headers;
118     ASSERT_SUCCESS(aws_event_stream_headers_list_init(&headers, allocator), "Header initialization failed");
119 
120     ASSERT_SUCCESS(aws_event_stream_message_headers(&message, &headers), "Header parsing should have succeeded");
121     ASSERT_INT_EQUALS(1, headers.length, "There should be exactly one header found");
122     struct aws_event_stream_header_value_pair header;
123     ASSERT_SUCCESS(
124         aws_array_list_front(&headers, &header),
125         "accessing the first element of an array of size 1 should have succeeded");
126 
127     const char *content_type = "content-type";
128     const char *content_type_value = "application/json";
129 
130     struct aws_byte_buf header_name_buf = aws_event_stream_header_name(&header);
131     ASSERT_BIN_ARRAYS_EQUALS(
132         content_type,
133         strlen(content_type),
134         header_name_buf.buffer,
135         header_name_buf.len,
136         "header name should have been %s",
137         content_type);
138 
139     struct aws_byte_buf header_value_buf = aws_event_stream_header_value_as_string(&header);
140 
141     ASSERT_BIN_ARRAYS_EQUALS(
142         content_type_value,
143         strlen(content_type_value),
144         header_value_buf.buffer,
145         header_value_buf.len,
146         "header value should have been %s",
147         content_type_value);
148 
149     ASSERT_INT_EQUALS(
150         0x8D9C08B1, aws_event_stream_message_message_crc(&message), "Message CRC should have been 0x8D9C08B1");
151 
152     aws_event_stream_headers_list_cleanup(&headers);
153     aws_event_stream_message_clean_up(&message);
154 
155     return 0;
156 }
157 
158 AWS_TEST_CASE(
159     test_outgoing_application_one_compressed_header_pair_valid,
160     s_test_outgoing_application_one_compressed_header_pair_valid_fn)
161