-
Notifications
You must be signed in to change notification settings - Fork 145
/
test_suite.c
301 lines (239 loc) · 9.67 KB
/
test_suite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include "vendor/cmocka/include/cmocka.h"
#include "aws_functions.h"
ngx_pool_t *pool;
static void assert_ngx_string_equal(ngx_str_t a, ngx_str_t b) {
int len = a.len < b.len ? a.len : b.len;
assert_memory_equal(a.data, b.data, len);
}
static void null_test_success(void **state) {
(void) state; /* unused */
}
static void host_header_ctor(void **state) {
ngx_str_t bucket;
const ngx_str_t* host;
(void) state; /* unused */
bucket.data = "test-es-three";
bucket.len = strlen(bucket.data);
host = ngx_aws_auth__host_from_bucket(pool, &bucket);
assert_string_equal("test-es-three.s3.amazonaws.com", host->data);
bucket.data = "complex.sub.domain.test";
bucket.len = strlen(bucket.data);
host = ngx_aws_auth__host_from_bucket(pool, &bucket);
assert_string_equal("complex.sub.domain.test.s3.amazonaws.com", host->data);
}
static void x_amz_date(void **state) {
time_t t;
const ngx_str_t* date;
(void) state; /* unused */
t = 1;
date = ngx_aws_auth__compute_request_time(pool, &t);
assert_int_equal(date->len, 16);
assert_string_equal("19700101T000001Z", date->data);
t = 1456036272;
date = ngx_aws_auth__compute_request_time(pool, &t);
assert_int_equal(date->len, 16);
assert_string_equal("20160221T063112Z", date->data);
}
static void hmac_sha256(void **state) {
ngx_str_t key;
ngx_str_t text;
ngx_str_t* hash;
(void) state; /* unused */
key.data = "abc"; key.len=3;
text.data = "asdf"; text.len=4;
hash = ngx_aws_auth__sign_sha256_hex(pool, &text, &key);
assert_int_equal(64, hash->len);
assert_string_equal("07e434c45d15994e620bf8e43da6f652d331989be1783cdfcc989ddb0a2358e2", hash->data);
key.data = "\011\001\057asf"; key.len=6;
text.data = "lorem ipsum"; text.len=11;
hash = ngx_aws_auth__sign_sha256_hex(pool, &text, &key);
assert_int_equal(64, hash->len);
assert_string_equal("827ce31c45e77292af25fef980c3e7afde23abcde622ecd8e82e1be6dd94fad3", hash->data);
}
static void sha256(void **state) {
ngx_str_t text;
ngx_str_t* hash;
(void) state; /* unused */
text.data = "asdf"; text.len=4;
hash = ngx_aws_auth__hash_sha256(pool, &text);
assert_int_equal(64, hash->len);
assert_string_equal("f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b", hash->data);
text.len=0;
hash = ngx_aws_auth__hash_sha256(pool, &text);
assert_int_equal(64, hash->len);
assert_string_equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hash->data);
}
static void canon_header_string(void **state) {
(void) state; /* unused */
ngx_str_t bucket, date, hash, endpoint;
struct AwsCanonicalHeaderDetails retval;
bucket.data = "bugait"; bucket.len = 6;
date.data = "20160221T063112Z"; date.len = 16;
hash.data = "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"; hash.len = 64;
endpoint.data = "s3.amazonaws.com"; endpoint.len = 16;
retval = ngx_aws_auth__canonize_headers(pool, NULL, &bucket, &date, &hash, &endpoint);
assert_string_equal(retval.canon_header_str->data,
"host:bugait.s3.amazonaws.com\nx-amz-content-sha256:f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b\nx-amz-date:20160221T063112Z\n");
}
static void signed_headers(void **state) {
(void) state; /* unused */
ngx_str_t bucket, date, hash, endpoint;
struct AwsCanonicalHeaderDetails retval;
bucket.data = "bugait"; bucket.len = 6;
date.data = "20160221T063112Z"; date.len = 16;
hash.data = "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"; hash.len = 64;
endpoint.data = "s3.amazonaws.com"; endpoint.len = 16;
retval = ngx_aws_auth__canonize_headers(pool, NULL, &bucket, &date, &hash, &endpoint);
assert_string_equal(retval.signed_header_names->data, "host;x-amz-content-sha256;x-amz-date");
}
static void canonical_qs_empty(void **state) {
(void) state; /* unused */
ngx_http_request_t request;
request.args = EMPTY_STRING;
request.connection = NULL;
const ngx_str_t *canon_qs = ngx_aws_auth__canonize_query_string(pool, &request);
assert_ngx_string_equal(*canon_qs, EMPTY_STRING);
}
static void canonical_qs_single_arg(void **state) {
(void) state; /* unused */
ngx_http_request_t request;
ngx_str_t args = ngx_string("arg1=val1");
request.args = args;
request.connection = NULL;
const ngx_str_t *canon_qs = ngx_aws_auth__canonize_query_string(pool, &request);
assert_ngx_string_equal(*canon_qs, args);
}
static void canonical_qs_two_arg_reverse(void **state) {
(void) state; /* unused */
ngx_http_request_t request;
ngx_str_t args = ngx_string("brg1=val2&arg1=val1");
ngx_str_t cargs = ngx_string("arg1=val1&brg1=val");
request.args = args;
request.connection = NULL;
const ngx_str_t *canon_qs = ngx_aws_auth__canonize_query_string(pool, &request);
assert_ngx_string_equal(*canon_qs, cargs);
}
static void canonical_qs_subrequest(void **state) {
(void) state; /* unused */
ngx_http_request_t request;
ngx_str_t args = ngx_string("acl");
ngx_str_t cargs = ngx_string("acl=");
request.args = args;
request.connection = NULL;
const ngx_str_t *canon_qs = ngx_aws_auth__canonize_query_string(pool, &request);
assert_ngx_string_equal(*canon_qs, cargs);
}
static void canonical_url_sans_qs(void **state) {
(void) state; /* unused */
ngx_http_request_t request;
ngx_str_t url = ngx_string("foo.php");
request.uri = url;
request.uri_start = request.uri.data;
request.args_start = url.data + url.len;
request.args = EMPTY_STRING;
request.connection = NULL;
const ngx_str_t *canon_url = ngx_aws_auth__canon_url(pool, &request);
assert_int_equal(canon_url->len, url.len);
assert_ngx_string_equal(*canon_url, url);
}
static void canonical_url_with_qs(void **state) {
(void) state; /* unused */
ngx_http_request_t request;
ngx_str_t url = ngx_string("foo.php?arg1=var1");
ngx_str_t curl = ngx_string("foo.php");
ngx_str_t args;
args.data = url.data + 8;
args.len = 9;
request.uri = url;
request.uri_start = request.uri.data;
request.args_start = url.data + 8;
request.args = args;
request.connection = NULL;
const ngx_str_t *canon_url = ngx_aws_auth__canon_url(pool, &request);
assert_int_equal(canon_url->len, curl.len);
assert_ngx_string_equal(*canon_url, curl);
}
static void canonical_url_with_special_chars(void **state) {
(void) state; /* unused */
ngx_str_t url = ngx_string("f&o@o/b ar.php");
ngx_str_t expected_canon_url = ngx_string("f%26o%40o/b%20ar.php");
ngx_http_request_t request;
request.uri = url;
request.uri_start = request.uri.data;
request.args_start = url.data + url.len;
request.args = EMPTY_STRING;
request.connection = NULL;
const ngx_str_t *canon_url = ngx_aws_auth__canon_url(pool, &request);
assert_int_equal(canon_url->len, expected_canon_url.len);
assert_ngx_string_equal(*canon_url, expected_canon_url);
}
static void canonical_request_sans_qs(void **state) {
(void) state; /* unused */
const ngx_str_t bucket = ngx_string("example");
const ngx_str_t aws_date = ngx_string("20160221T063112Z");
const ngx_str_t url = ngx_string("/");
const ngx_str_t method = ngx_string("GET");
const ngx_str_t endpoint = ngx_string("s3.amazonaws.com");
struct AwsCanonicalRequestDetails result;
ngx_http_request_t request;
request.uri = url;
request.method_name = method;
request.args = EMPTY_STRING;
request.connection = NULL;
result = ngx_aws_auth__make_canonical_request(pool, &request, &bucket, &aws_date, &endpoint);
assert_string_equal(result.canon_request->data, "GET\n\
/\n\
\n\
host:example.s3.amazonaws.com\n\
x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n\
x-amz-date:20160221T063112Z\n\
\n\
host;x-amz-content-sha256;x-amz-date\n\
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
}
static void basic_get_signature(void **state) {
(void) state; /* unused */
const ngx_str_t url = ngx_string("/");
const ngx_str_t method = ngx_string("GET");
const ngx_str_t key_scope = ngx_string("20150830/us-east-1/service/aws4_request");
const ngx_str_t bucket = ngx_string("example");
const ngx_str_t endpoint = ngx_string("s3.amazonaws.com");
ngx_str_t signing_key, signing_key_b64e = ngx_string("k4EntTNoEN22pdavRF/KyeNx+e1BjtOGsCKu2CkBvnU=");
ngx_http_request_t request;
request.start_sec = 1440938160; /* 20150830T123600Z */
request.uri = url;
request.method_name = method;
request.args = EMPTY_STRING;
request.connection = NULL;
signing_key.len = 64;
signing_key.data = ngx_palloc(pool, signing_key.len );
ngx_decode_base64(&signing_key, &signing_key_b64e);
struct AwsSignedRequestDetails result = ngx_aws_auth__compute_signature(pool, &request,
&signing_key, &key_scope, &bucket, &endpoint);
assert_string_equal(result.signature->data, "4ed4ec875ff02e55c7903339f4f24f8780b986a9cc9eff03f324d31da6a57690");
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(null_test_success),
cmocka_unit_test(x_amz_date),
cmocka_unit_test(host_header_ctor),
cmocka_unit_test(hmac_sha256),
cmocka_unit_test(sha256),
cmocka_unit_test(canon_header_string),
cmocka_unit_test(canonical_qs_empty),
cmocka_unit_test(canonical_qs_single_arg),
cmocka_unit_test(canonical_qs_two_arg_reverse),
cmocka_unit_test(canonical_qs_subrequest),
cmocka_unit_test(canonical_url_sans_qs),
cmocka_unit_test(canonical_url_with_qs),
cmocka_unit_test(canonical_url_with_special_chars),
cmocka_unit_test(signed_headers),
cmocka_unit_test(canonical_request_sans_qs),
cmocka_unit_test(basic_get_signature),
};
pool = ngx_create_pool(1000000, NULL);
return cmocka_run_group_tests(tests, NULL, NULL);
}