Debian Codegraph @

crt/aws-c-sdkutils/tests/aws_profile_tests.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/testing/aws_test_harness.h>
7
8 #include <aws/common/environment.h>
9 #include <aws/common/string.h>
10 #include <aws/sdkutils/aws_profile.h>
11
12 #define EXPECT_SECTION_COUNT(profile_collection, section_type, section_count) \
13 { ASSERT_TRUE(aws_profile_collection_get_section_count(profile_collection, section_type) == (section_count)); }
14
15 #define EXPECT_SECTION(profile_collection, section_type, section_name) \
16 { \
17 struct aws_string *section_name_str = aws_string_new_from_c_str(allocator, section_name); \
18 const struct aws_profile *profile = \
19 aws_profile_collection_get_section(profile_collection, section_type, section_name_str); \
20 aws_string_destroy(section_name_str); \
21 ASSERT_TRUE(profile != NULL); \
22 }
23
24 #define EXPECT_PROPERTY_COUNT(profile_collection, section_type, section_name, expected_property_count) \
25 { \
26 struct aws_string *section_name_str = aws_string_new_from_c_str(allocator, section_name); \
27 const struct aws_profile *profile = \
28 aws_profile_collection_get_section(profile_collection, section_type, section_name_str); \
29 aws_string_destroy(section_name_str); \
30 ASSERT_TRUE(aws_profile_get_property_count(profile) == (expected_property_count)); \
31 }
32 #define EXPECT_PROPERTY(profile_collection, section_type, section_name, property_name, expected_property_value) \
33 { \
34 struct aws_string *section_name_str = aws_string_new_from_c_str(allocator, section_name); \
35 const struct aws_profile *profile = \
36 aws_profile_collection_get_section(profile_collection, section_type, section_name_str); \
37 struct aws_string *property_name_str = aws_string_new_from_c_str(allocator, property_name); \
38 const struct aws_profile_property *property = aws_profile_get_property(profile, property_name_str); \
39 aws_string_destroy(property_name_str); \
40 aws_string_destroy(section_name_str); \
41 ASSERT_TRUE( \
42 property != NULL && \
43 strcmp(expected_property_value, aws_string_c_str(aws_profile_property_get_value(property))) == 0); \
44 }
45
46 #define EXPECT_SUB_PROPERTY_COUNT( \
47 profile_collection, section_type, section_name, property_name, expected_sub_property_count) \
48 { \
49 struct aws_string *section_name_str = aws_string_new_from_c_str(allocator, section_name); \
50 const struct aws_profile *profile = \
51 aws_profile_collection_get_section(profile_collection, section_type, section_name_str); \
52 struct aws_string *property_name_str = aws_string_new_from_c_str(allocator, property_name); \
53 const struct aws_profile_property *property = aws_profile_get_property(profile, property_name_str); \
54 aws_string_destroy(property_name_str); \
55 aws_string_destroy(section_name_str); \
56 ASSERT_UINT_EQUALS((expected_sub_property_count), aws_profile_property_get_sub_property_count(property)); \
57 }
58
59 #define EXPECT_SUB_PROPERTY( \
60 profile_collection, section_type, profile_name, property_name, sub_property_name, expected_sub_property_value) \
61 { \
62 struct aws_string *profile_name_str = aws_string_new_from_c_str(allocator, profile_name); \
63 const struct aws_profile *profile = \
64 aws_profile_collection_get_section(profile_collection, section_type, profile_name_str); \
65 struct aws_string *property_name_str = aws_string_new_from_c_str(allocator, property_name); \
66 const struct aws_profile_property *property = aws_profile_get_property(profile, property_name_str); \
67 struct aws_string *sub_property_name_str = aws_string_new_from_c_str(allocator, sub_property_name); \
68 const struct aws_string *sub_property_value = \
69 aws_profile_property_get_sub_property(property, sub_property_name_str); \
70 aws_string_destroy(sub_property_name_str); \
71 aws_string_destroy(property_name_str); \
72 aws_string_destroy(profile_name_str); \
73 ASSERT_TRUE(strcmp(expected_sub_property_value, aws_string_c_str(sub_property_value)) == 0); \
74 }
75
76 /*
77 * profile collection setup
78 */
79 struct aws_profile_collection *aws_prepare_profile_test(
80 struct aws_allocator *allocator,
81 const struct aws_string *profile_contents,
82 enum aws_profile_source_type source) {
83
84 struct aws_byte_cursor contents = aws_byte_cursor_from_string(profile_contents);
85 struct aws_byte_buf buffer;
86 AWS_ZERO_STRUCT(buffer);
87 aws_byte_buf_init_copy_from_cursor(&buffer, allocator, contents);
88
89 struct aws_profile_collection *profile_collection =
90 aws_profile_collection_new_from_buffer(allocator, &buffer, source);
91
92 aws_byte_buf_clean_up(&buffer);
93
94 return profile_collection;
95 }
96
97 struct aws_profile_collection *aws_prepare_merged_profile_test(
98 struct aws_allocator *allocator,
99 const struct aws_string *config_contents,
100 const struct aws_string *credentials_contents) {
101
102 struct aws_byte_cursor config_cursor = aws_byte_cursor_from_string(config_contents);
103 struct aws_byte_buf config_buffer;
104 aws_byte_buf_init_copy_from_cursor(&config_buffer, allocator, config_cursor);
105
106 struct aws_profile_collection *config_profile_collection =
107 aws_profile_collection_new_from_buffer(allocator, &config_buffer, AWS_PST_CONFIG);
108
109 aws_byte_buf_clean_up(&config_buffer);
110
111 struct aws_byte_cursor credentials_cursor = aws_byte_cursor_from_string(credentials_contents);
112 struct aws_byte_buf credentials_buffer;
113 aws_byte_buf_init_copy_from_cursor(&credentials_buffer, allocator, credentials_cursor);
114
115 struct aws_profile_collection *credentials_profile_collection =
116 aws_profile_collection_new_from_buffer(allocator, &credentials_buffer, AWS_PST_CREDENTIALS);
117
118 aws_byte_buf_clean_up(&credentials_buffer);
119
120 struct aws_profile_collection *merged =
121 aws_profile_collection_new_from_merge(allocator, config_profile_collection, credentials_profile_collection);
122
123 if (config_profile_collection) {
124 aws_profile_collection_destroy(config_profile_collection);
125 }
126
127 if (credentials_profile_collection) {
128 aws_profile_collection_destroy(credentials_profile_collection);
129 }
130
131 return merged;
132 }
133
134 /*
135 * Nothing at all
136 */
137 AWS_STATIC_STRING_FROM_LITERAL(s_empty_string, "");
138
139 static int s_aws_profile_empty_test(struct aws_allocator *allocator, void *ctx) {
140 (void)ctx;
141
142 struct aws_profile_collection *profile_collection =
143 aws_prepare_profile_test(allocator, s_empty_string, AWS_PST_CONFIG);
144
145 ASSERT_TRUE(profile_collection != NULL);
146 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 0);
147
148 aws_profile_collection_destroy(profile_collection);
149
150 return 0;
151 }
152
153 AWS_TEST_CASE(aws_profile_empty_test, s_aws_profile_empty_test);
154
155 /*
156 * A single empty profile
157 */
158 AWS_STATIC_STRING_FROM_LITERAL(s_empty_profile, "[profile foo]");
159
160 static int s_aws_profile_empty_profile_test(struct aws_allocator *allocator, void *ctx) {
161 (void)ctx;
162
163 struct aws_profile_collection *profile_collection =
164 aws_prepare_profile_test(allocator, s_empty_profile, AWS_PST_CONFIG);
165
166 ASSERT_TRUE(profile_collection != NULL);
167 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
168 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
169 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 0);
170
171 aws_profile_collection_destroy(profile_collection);
172
173 return 0;
174 }
175
176 AWS_TEST_CASE(aws_profile_empty_profile_test, s_aws_profile_empty_profile_test);
177
178 /*
179 * Whitespace in a single empty profile
180 */
181 AWS_STATIC_STRING_FROM_LITERAL(s_whitespace_empty_profile, "[profile \tfoo \t]");
182
183 static int s_aws_profile_whitespace_empty_profile_test(struct aws_allocator *allocator, void *ctx) {
184 (void)ctx;
185
186 struct aws_profile_collection *profile_collection =
187 aws_prepare_profile_test(allocator, s_whitespace_empty_profile, AWS_PST_CONFIG);
188
189 ASSERT_TRUE(profile_collection != NULL);
190 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
191 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
192 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 0);
193
194 aws_profile_collection_destroy(profile_collection);
195
196 return 0;
197 }
198
199 AWS_TEST_CASE(aws_profile_whitespace_empty_profile_test, s_aws_profile_whitespace_empty_profile_test);
200
201 /*
202 * Tab-separated, a single empty profile
203 */
204 AWS_STATIC_STRING_FROM_LITERAL(s_tab_empty_profile, "[profile\tfoo]");
205
206 static int s_aws_profile_tab_empty_profile_test(struct aws_allocator *allocator, void *ctx) {
207 (void)ctx;
208
209 struct aws_profile_collection *profile_collection =
210 aws_prepare_profile_test(allocator, s_tab_empty_profile, AWS_PST_CONFIG);
211
212 ASSERT_TRUE(profile_collection != NULL);
213 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
214 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
215 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 0);
216
217 aws_profile_collection_destroy(profile_collection);
218
219 return 0;
220 }
221
222 AWS_TEST_CASE(aws_profile_tab_empty_profile_test, s_aws_profile_tab_empty_profile_test);
223
224 /*
225 * One profile with a single, simple property
226 */
227 AWS_STATIC_STRING_FROM_LITERAL(s_single_simple_property_profile, "[profile foo]\nname = value");
228
229 static int s_aws_profile_single_simple_property_profile_test(struct aws_allocator *allocator, void *ctx) {
230 (void)ctx;
231
232 struct aws_profile_collection *profile_collection =
233 aws_prepare_profile_test(allocator, s_single_simple_property_profile, AWS_PST_CONFIG);
234
235 ASSERT_TRUE(profile_collection != NULL);
236 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
237 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
238 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
239 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
240
241 aws_profile_collection_destroy(profile_collection);
242
243 return 0;
244 }
245
246 AWS_TEST_CASE(aws_profile_single_simple_property_profile_test, s_aws_profile_single_simple_property_profile_test);
247
248 /*
249 * Check that = can appear in a property value
250 */
251 AWS_STATIC_STRING_FROM_LITERAL(s_equal_containing_property_profile, "[profile foo]\nname = val=ue");
252
253 static int s_aws_profile_equal_containing_property_profile_test(struct aws_allocator *allocator, void *ctx) {
254 (void)ctx;
255
256 struct aws_profile_collection *profile_collection =
257 aws_prepare_profile_test(allocator, s_equal_containing_property_profile, AWS_PST_CONFIG);
258
259 ASSERT_TRUE(profile_collection != NULL);
260 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
261 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
262 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
263 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "val=ue");
264
265 aws_profile_collection_destroy(profile_collection);
266
267 return 0;
268 }
269
270 AWS_TEST_CASE(aws_profile_equal_containing_property_profile_test, s_aws_profile_equal_containing_property_profile_test);
271
272 /*
273 * Check that non-ascii unicode can appear in a property value
274 */
275 AWS_STATIC_STRING_FROM_LITERAL(s_unicode_containing_property_profile, "[profile foo]\nname = \xF0\x9F\x98\x82");
276
277 static int s_aws_profile_unicode_containing_property_profile_test(struct aws_allocator *allocator, void *ctx) {
278 (void)ctx;
279
280 struct aws_profile_collection *profile_collection =
281 aws_prepare_profile_test(allocator, s_unicode_containing_property_profile, AWS_PST_CONFIG);
282
283 ASSERT_TRUE(profile_collection != NULL);
284 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
285 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
286 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
287 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "\xF0\x9F\x98\x82");
288
289 aws_profile_collection_destroy(profile_collection);
290
291 return 0;
292 }
293
294 AWS_TEST_CASE(
295 aws_profile_unicode_containing_property_profile_test,
296 s_aws_profile_unicode_containing_property_profile_test);
297
298 /*
299 * Profiles can contain multiple properties
300 */
301 AWS_STATIC_STRING_FROM_LITERAL(s_multiple_property_profile, "[profile foo]\nname = value\nname2 = value2");
302
303 static int s_aws_profile_multiple_property_profile_test(struct aws_allocator *allocator, void *ctx) {
304 (void)ctx;
305
306 struct aws_profile_collection *profile_collection =
307 aws_prepare_profile_test(allocator, s_multiple_property_profile, AWS_PST_CONFIG);
308
309 ASSERT_TRUE(profile_collection != NULL);
310 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
311 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
312 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 2);
313 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
314 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name2", "value2");
315
316 aws_profile_collection_destroy(profile_collection);
317
318 return 0;
319 }
320
321 AWS_TEST_CASE(aws_profile_multiple_property_profile_test, s_aws_profile_multiple_property_profile_test);
322
323 /*
324 * Property name and values get trimmed
325 */
326 AWS_STATIC_STRING_FROM_LITERAL(s_trimmable_property_profile, "[profile foo]\nname \t= \tvalue \t");
327
328 static int s_aws_profile_trimmable_property_profile_test(struct aws_allocator *allocator, void *ctx) {
329 (void)ctx;
330
331 struct aws_profile_collection *profile_collection =
332 aws_prepare_profile_test(allocator, s_trimmable_property_profile, AWS_PST_CONFIG);
333
334 ASSERT_TRUE(profile_collection != NULL);
335 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
336 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
337 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
338 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
339
340 aws_profile_collection_destroy(profile_collection);
341
342 return 0;
343 }
344
345 AWS_TEST_CASE(aws_profile_trimmable_property_profile_test, s_aws_profile_trimmable_property_profile_test);
346
347 /*
348 * Property values can be empty
349 */
350 AWS_STATIC_STRING_FROM_LITERAL(s_empty_property_profile, "[profile foo]\nname =");
351
352 static int s_aws_profile_empty_property_profile_test(struct aws_allocator *allocator, void *ctx) {
353 (void)ctx;
354
355 struct aws_profile_collection *profile_collection =
356 aws_prepare_profile_test(allocator, s_empty_property_profile, AWS_PST_CONFIG);
357
358 ASSERT_TRUE(profile_collection != NULL);
359 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
360 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
361 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
362 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "");
363
364 aws_profile_collection_destroy(profile_collection);
365
366 return 0;
367 }
368
369 AWS_TEST_CASE(aws_profile_empty_property_profile_test, s_aws_profile_empty_property_profile_test);
370
371 /*
372 * Multiple empty profiles
373 */
374 AWS_STATIC_STRING_FROM_LITERAL(s_multiple_empty_profile, "[profile foo]\n[profile bar]");
375
376 static int s_aws_profile_multiple_empty_profile_test(struct aws_allocator *allocator, void *ctx) {
377 (void)ctx;
378
379 struct aws_profile_collection *profile_collection =
380 aws_prepare_profile_test(allocator, s_multiple_empty_profile, AWS_PST_CONFIG);
381
382 ASSERT_TRUE(profile_collection != NULL);
383 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 2);
384 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
385 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 0);
386 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar");
387 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", 0);
388
389 aws_profile_collection_destroy(profile_collection);
390
391 return 0;
392 }
393
394 AWS_TEST_CASE(aws_profile_multiple_empty_profile_test, s_aws_profile_multiple_empty_profile_test);
395
396 /*
397 * Multiple profiles with properties
398 */
399 AWS_STATIC_STRING_FROM_LITERAL(s_multiple_profile, "[profile foo]\nname = value\n[profile bar]\nname2 = value2");
400
401 static int s_aws_profile_multiple_profile_test(struct aws_allocator *allocator, void *ctx) {
402 (void)ctx;
403
404 struct aws_profile_collection *profile_collection =
405 aws_prepare_profile_test(allocator, s_multiple_profile, AWS_PST_CONFIG);
406
407 ASSERT_TRUE(profile_collection != NULL);
408 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 2);
409 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
410 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
411 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
412 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar");
413 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", 1);
414 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", "name2", "value2");
415
416 aws_profile_collection_destroy(profile_collection);
417
418 return 0;
419 }
420
421 AWS_TEST_CASE(aws_profile_multiple_profile_test, s_aws_profile_multiple_profile_test);
422
423 /*
424 * Multiple profiles with properties and sso-session
425 */
426 AWS_STATIC_STRING_FROM_LITERAL(
427 s_credentials_sso_session,
428 "[profile foo]\nname = value\n[profile bar]\nname2 = value2\nservices = test-service\n"
429 "[sso-session session]\nname3 = value3\ns3 =\n name4 = value4\n"
430 "[services test-service]\nname4 = value4\ns3 =\n name5 = value5\n");
431
432 static int s_aws_profile_multiple_sections(struct aws_allocator *allocator, void *ctx) {
433 (void)ctx;
434
435 struct aws_profile_collection *profile_collection =
436 aws_prepare_profile_test(allocator, s_credentials_sso_session, AWS_PST_CONFIG);
437
438 ASSERT_NOT_NULL(profile_collection);
439 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 2);
440 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
441 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
442 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
443 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar");
444 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", 2);
445 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", "name2", "value2");
446 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", "services", "test-service");
447
448 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, 1);
449 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session");
450 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session", 2);
451 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session", "name3", "value3");
452 EXPECT_SUB_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session", "s3", 1);
453 EXPECT_SUB_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session", "s3", "name4", "value4");
454
455 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, 1);
456 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service");
457 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", 2);
458 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", "name4", "value4");
459 EXPECT_SUB_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", "s3", 1);
460 EXPECT_SUB_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", "s3", "name5", "value5");
461
462 aws_profile_collection_destroy(profile_collection);
463
464 return 0;
465 }
466
467 AWS_TEST_CASE(aws_profile_multiple_sections, s_aws_profile_multiple_sections);
468
469 /*
470 * sections in credentials file are ignored
471 */
472 AWS_STATIC_STRING_FROM_LITERAL(
473 s_sso_session_in_credentials,
474 "[profile foo]\nname = value\n"
475 "[sso-session session]\nname2 = value2\n"
476 "[services test-service]\nname3 = value3\n");
477
478 static int s_aws_profile_sections_in_credentials_test(struct aws_allocator *allocator, void *ctx) {
479 (void)ctx;
480
481 struct aws_profile_collection *profile_collection =
482 aws_prepare_profile_test(allocator, s_sso_session_in_credentials, AWS_PST_CREDENTIALS);
483
484 ASSERT_NOT_NULL(profile_collection);
485 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 0);
486 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, 0);
487 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, 0);
488 aws_profile_collection_destroy(profile_collection);
489
490 return 0;
491 }
492
493 AWS_TEST_CASE(aws_profile_sections_in_credentials_test, s_aws_profile_sections_in_credentials_test);
494
495 /*
496 * sso-session without name is ignored
497 */
498 AWS_STATIC_STRING_FROM_LITERAL(s_sso_session_without_name, "[sso-session session]\nname = value\n[sso-session ]");
499 static int s_aws_profile_sso_session_without_name_test(struct aws_allocator *allocator, void *ctx) {
500 (void)ctx;
501
502 struct aws_profile_collection *profile_collection =
503 aws_prepare_profile_test(allocator, s_sso_session_without_name, AWS_PST_CONFIG);
504
505 ASSERT_NOT_NULL(profile_collection);
506 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 0);
507 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, 1);
508 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session");
509 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session", 1);
510 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "session", "name", "value");
511
512 aws_profile_collection_destroy(profile_collection);
513
514 return 0;
515 }
516
517 AWS_TEST_CASE(aws_profile_sso_session_without_name_test, s_aws_profile_sso_session_without_name_test);
518
519 /*
520 * services without name is ignored
521 */
522 AWS_STATIC_STRING_FROM_LITERAL(
523 s_services_with_invali_prefix,
524 "[services test-service]\nname = value\n"
525 "[services ]\n"
526 "[profilesso-sessionservices test]\nname = value\n"
527 "[services\rtest-service]\nname = value\n");
528 static int s_aws_profile_services_invalid_prefix_test(struct aws_allocator *allocator, void *ctx) {
529 (void)ctx;
530
531 struct aws_profile_collection *profile_collection =
532 aws_prepare_profile_test(allocator, s_services_with_invali_prefix, AWS_PST_CONFIG);
533
534 ASSERT_NOT_NULL(profile_collection);
535 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, 1);
536 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service");
537 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", 1);
538 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", "name", "value");
539
540 aws_profile_collection_destroy(profile_collection);
541
542 return 0;
543 }
544
545 AWS_TEST_CASE(aws_profile_services_invalid_prefix_test, s_aws_profile_services_invalid_prefix_test);
546 /*
547 * Blank lines are ignored
548 */
549 AWS_STATIC_STRING_FROM_LITERAL(
550 s_blank_lines_ignored_profile,
551 "\t \n[profile foo]\n\t\n \nname = value\n\t \n[profile bar]\n \t");
552
553 static int s_aws_profile_blank_lines_ignored_test(struct aws_allocator *allocator, void *ctx) {
554 (void)ctx;
555
556 struct aws_profile_collection *profile_collection =
557 aws_prepare_profile_test(allocator, s_blank_lines_ignored_profile, AWS_PST_CONFIG);
558
559 ASSERT_TRUE(profile_collection != NULL);
560 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 2);
561 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
562 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
563 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
564 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar");
565 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", 0);
566
567 aws_profile_collection_destroy(profile_collection);
568
569 return 0;
570 }
571
572 AWS_TEST_CASE(aws_profile_blank_lines_ignored_test, s_aws_profile_blank_lines_ignored_test);
573
574 /*
575 * # comments are ignored
576 */
577 AWS_STATIC_STRING_FROM_LITERAL(
578 s_pound_comments_ignored_profile,
579 "# Comment\n[profile foo] # Comment\nname = value # Comment with # sign");
580
581 static int s_aws_profile_pound_comments_ignored_test(struct aws_allocator *allocator, void *ctx) {
582 (void)ctx;
583
584 struct aws_profile_collection *profile_collection =
585 aws_prepare_profile_test(allocator, s_pound_comments_ignored_profile, AWS_PST_CONFIG);
586
587 ASSERT_TRUE(profile_collection != NULL);
588 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
589 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
590 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
591 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
592
593 aws_profile_collection_destroy(profile_collection);
594
595 return 0;
596 }
597
598 AWS_TEST_CASE(aws_profile_pound_comments_ignored_test, s_aws_profile_pound_comments_ignored_test);
599
600 /*
601 * ; comments are ignored
602 */
603 AWS_STATIC_STRING_FROM_LITERAL(
604 s_semicolon_comments_ignored_profile,
605 "; Comment\n[profile foo] ; Comment\nname = value ; Comment with ; sign");
606
607 static int s_aws_profile_semicolon_comments_ignored_test(struct aws_allocator *allocator, void *ctx) {
608 (void)ctx;
609
610 struct aws_profile_collection *profile_collection =
611 aws_prepare_profile_test(allocator, s_semicolon_comments_ignored_profile, AWS_PST_CONFIG);
612
613 ASSERT_TRUE(profile_collection != NULL);
614 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
615 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
616 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
617 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
618
619 aws_profile_collection_destroy(profile_collection);
620
621 return 0;
622 }
623
624 AWS_TEST_CASE(aws_profile_semicolon_comments_ignored_test, s_aws_profile_semicolon_comments_ignored_test);
625
626 /*
627 * mixed comments are ignored
628 */
629 AWS_STATIC_STRING_FROM_LITERAL(
630 s_mixed_comments_ignored_profile,
631 "# Comment\n[profile foo] ; Comment\nname = value # Comment with ; sign");
632
633 static int s_aws_profile_mixed_comments_ignored_test(struct aws_allocator *allocator, void *ctx) {
634 (void)ctx;
635
636 struct aws_profile_collection *profile_collection =
637 aws_prepare_profile_test(allocator, s_mixed_comments_ignored_profile, AWS_PST_CONFIG);
638
639 ASSERT_TRUE(profile_collection != NULL);
640 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
641 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
642 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
643 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
644
645 aws_profile_collection_destroy(profile_collection);
646
647 return 0;
648 }
649
650 AWS_TEST_CASE(aws_profile_mixed_comments_ignored_test, s_aws_profile_mixed_comments_ignored_test);
651
652 /*
653 * empty comments are ignored
654 */
655 AWS_STATIC_STRING_FROM_LITERAL(s_empty_comments_ignored_profile, ";\n[profile foo];\nname = value ;\n");
656
657 static int s_aws_profile_empty_comments_ignored_test(struct aws_allocator *allocator, void *ctx) {
658 (void)ctx;
659
660 struct aws_profile_collection *profile_collection =
661 aws_prepare_profile_test(allocator, s_empty_comments_ignored_profile, AWS_PST_CONFIG);
662
663 ASSERT_TRUE(profile_collection != NULL);
664 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
665 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
666 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
667 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
668
669 aws_profile_collection_destroy(profile_collection);
670
671 return 0;
672 }
673
674 AWS_TEST_CASE(aws_profile_empty_comments_ignored_test, s_aws_profile_empty_comments_ignored_test);
675
676 /*
677 * comments can be adjacent to profile declaration
678 */
679 AWS_STATIC_STRING_FROM_LITERAL(
680 s_profile_adjacent_comment_profile,
681 "[profile foo]; Adjacent semicolons\n[profile bar]# Adjacent pound signs");
682
683 static int s_aws_profile_profile_adjacent_comment_test(struct aws_allocator *allocator, void *ctx) {
684 (void)ctx;
685
686 struct aws_profile_collection *profile_collection =
687 aws_prepare_profile_test(allocator, s_profile_adjacent_comment_profile, AWS_PST_CONFIG);
688
689 ASSERT_TRUE(profile_collection != NULL);
690 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 2);
691 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
692 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 0);
693 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar");
694 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "bar", 0);
695
696 aws_profile_collection_destroy(profile_collection);
697
698 return 0;
699 }
700
701 AWS_TEST_CASE(aws_profile_profile_adjacent_comment_test, s_aws_profile_profile_adjacent_comment_test);
702
703 /*
704 * comments adjacent to values are included in the value
705 */
706 AWS_STATIC_STRING_FROM_LITERAL(
707 s_value_adjacent_comment_profile,
708 "[profile foo]\nname = value; Adjacent semicolons\nname2 = value# Adjacent pound signs");
709
710 static int s_aws_profile_value_adjacent_comment_test(struct aws_allocator *allocator, void *ctx) {
711 (void)ctx;
712
713 struct aws_profile_collection *profile_collection =
714 aws_prepare_profile_test(allocator, s_value_adjacent_comment_profile, AWS_PST_CONFIG);
715
716 ASSERT_TRUE(profile_collection != NULL);
717 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
718 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
719 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 2);
720 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value; Adjacent semicolons");
721 EXPECT_PROPERTY(
722 profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name2", "value# Adjacent pound signs");
723
724 aws_profile_collection_destroy(profile_collection);
725
726 return 0;
727 }
728
729 AWS_TEST_CASE(aws_profile_value_adjacent_comment_test, s_aws_profile_value_adjacent_comment_test);
730
731 /*
732 * property values can be continued
733 */
734 AWS_STATIC_STRING_FROM_LITERAL(s_continued_property_value_profile, "[profile foo]\nname = value\n -continued");
735
736 static int s_aws_profile_continued_property_value_test(struct aws_allocator *allocator, void *ctx) {
737 (void)ctx;
738
739 struct aws_profile_collection *profile_collection =
740 aws_prepare_profile_test(allocator, s_continued_property_value_profile, AWS_PST_CONFIG);
741
742 ASSERT_TRUE(profile_collection != NULL);
743 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
744 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
745 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
746 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value\n-continued");
747
748 aws_profile_collection_destroy(profile_collection);
749
750 return 0;
751 }
752
753 AWS_TEST_CASE(aws_profile_continued_property_value_test, s_aws_profile_continued_property_value_test);
754
755 /*
756 * property values can be continued across multiple lines
757 */
758 AWS_STATIC_STRING_FROM_LITERAL(
759 s_multiline_continued_property_value_profile,
760 "[profile foo]\nname = value\n -continued\n -and-continued");
761
762 static int s_aws_profile_multiline_continued_property_value_test(struct aws_allocator *allocator, void *ctx) {
763 (void)ctx;
764
765 struct aws_profile_collection *profile_collection =
766 aws_prepare_profile_test(allocator, s_multiline_continued_property_value_profile, AWS_PST_CONFIG);
767
768 ASSERT_TRUE(profile_collection != NULL);
769 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
770 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
771 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
772 EXPECT_PROPERTY(
773 profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value\n-continued\n-and-continued");
774
775 aws_profile_collection_destroy(profile_collection);
776
777 return 0;
778 }
779
780 AWS_TEST_CASE(
781 aws_profile_multiline_continued_property_value_test,
782 s_aws_profile_multiline_continued_property_value_test);
783
784 /*
785 * property value continuations get trimmed
786 */
787 AWS_STATIC_STRING_FROM_LITERAL(
788 s_continued_property_value_trim_profile,
789 "[profile foo]\nname = value\n \t -continued \t ");
790
791 static int s_aws_profile_continued_property_value_trim_test(struct aws_allocator *allocator, void *ctx) {
792 (void)ctx;
793
794 struct aws_profile_collection *profile_collection =
795 aws_prepare_profile_test(allocator, s_continued_property_value_trim_profile, AWS_PST_CONFIG);
796
797 ASSERT_TRUE(profile_collection != NULL);
798 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
799 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
800 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
801 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value\n-continued");
802
803 aws_profile_collection_destroy(profile_collection);
804
805 return 0;
806 }
807
808 AWS_TEST_CASE(aws_profile_continued_property_value_trim_test, s_aws_profile_continued_property_value_trim_test);
809
810 /*
811 * property value continuations include # comments
812 */
813 AWS_STATIC_STRING_FROM_LITERAL(
814 s_continued_property_value_pound_comment_profile,
815 "[profile foo]\nname = value\n -continued # Comment");
816
817 static int s_aws_profile_continued_property_value_pound_comment_test(struct aws_allocator *allocator, void *ctx) {
818 (void)ctx;
819
820 struct aws_profile_collection *profile_collection =
821 aws_prepare_profile_test(allocator, s_continued_property_value_pound_comment_profile, AWS_PST_CONFIG);
822
823 ASSERT_TRUE(profile_collection != NULL);
824 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
825 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
826 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
827 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value\n-continued # Comment");
828
829 aws_profile_collection_destroy(profile_collection);
830
831 return 0;
832 }
833
834 AWS_TEST_CASE(
835 aws_profile_continued_property_value_pound_comment_test,
836 s_aws_profile_continued_property_value_pound_comment_test);
837
838 /*
839 * property value continuations include ; comments
840 */
841 AWS_STATIC_STRING_FROM_LITERAL(
842 s_continued_property_value_semicolon_comment_profile,
843 "[profile foo]\nname = value\n -continued ; Comment");
844
845 static int s_aws_profile_continued_property_value_semicolon_comment_test(struct aws_allocator *allocator, void *ctx) {
846 (void)ctx;
847
848 struct aws_profile_collection *profile_collection =
849 aws_prepare_profile_test(allocator, s_continued_property_value_semicolon_comment_profile, AWS_PST_CONFIG);
850
851 ASSERT_TRUE(profile_collection != NULL);
852 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
853 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
854 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
855 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value\n-continued ; Comment");
856
857 aws_profile_collection_destroy(profile_collection);
858
859 return 0;
860 }
861
862 AWS_TEST_CASE(
863 aws_profile_continued_property_value_semicolon_comment_test,
864 s_aws_profile_continued_property_value_semicolon_comment_test);
865
866 /*
867 * duplicate profiles, sso-session, services merge properties
868 */
869 AWS_STATIC_STRING_FROM_LITERAL(
870 s_duplicate_profiles_merge_profile,
871 "[profile foo]\nname = value\n[profile foo]\nname2 = value2\n"
872 "[sso-session foo]\nname3 = value-3\n[sso-session foo]\nname3 = value3\nname4 = value4\n"
873 "[services test-service]\nname5 = value-5\n[services test-service]\nname5 = value5\nname6 = value6\n");
874
875 static int s_aws_profile_duplicate_profiles_merge_test(struct aws_allocator *allocator, void *ctx) {
876 (void)ctx;
877
878 struct aws_profile_collection *profile_collection =
879 aws_prepare_profile_test(allocator, s_duplicate_profiles_merge_profile, AWS_PST_CONFIG);
880
881 ASSERT_TRUE(profile_collection != NULL);
882 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
883 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
884 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 2);
885 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
886 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name2", "value2");
887
888 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, 1);
889 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "foo");
890 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "foo", 2);
891 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "foo", "name3", "value3");
892 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, "foo", "name4", "value4");
893
894 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, 1);
895 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service");
896 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", 2);
897 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", "name5", "value5");
898 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, "test-service", "name6", "value6");
899
900 aws_profile_collection_destroy(profile_collection);
901
902 return 0;
903 }
904
905 AWS_TEST_CASE(aws_profile_duplicate_profiles_merge_test, s_aws_profile_duplicate_profiles_merge_test);
906
907 /*
908 * duplicate properties in a single profile use the last property definition
909 */
910 AWS_STATIC_STRING_FROM_LITERAL(
911 s_duplicate_properties_last_property_value_profile,
912 "[profile foo]\nname = value\nname = value2");
913
914 static int s_aws_profile_duplicate_properties_last_property_value_test(struct aws_allocator *allocator, void *ctx) {
915 (void)ctx;
916
917 struct aws_profile_collection *profile_collection =
918 aws_prepare_profile_test(allocator, s_duplicate_properties_last_property_value_profile, AWS_PST_CONFIG);
919
920 ASSERT_TRUE(profile_collection != NULL);
921 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
922 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
923 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
924 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value2");
925
926 aws_profile_collection_destroy(profile_collection);
927
928 return 0;
929 }
930
931 AWS_TEST_CASE(
932 aws_profile_duplicate_properties_last_property_value_test,
933 s_aws_profile_duplicate_properties_last_property_value_test);
934
935 /*
936 * duplicate profiles use the last property definition
937 */
938 AWS_STATIC_STRING_FROM_LITERAL(
939 s_duplicate_profiles_last_property_value_profile,
940 "[profile foo]\nname = value\n[profile foo]\nname = value2");
941
942 static int s_aws_profile_duplicate_profiles_last_property_value_test(struct aws_allocator *allocator, void *ctx) {
943 (void)ctx;
944
945 struct aws_profile_collection *profile_collection =
946 aws_prepare_profile_test(allocator, s_duplicate_profiles_last_property_value_profile, AWS_PST_CONFIG);
947
948 ASSERT_TRUE(profile_collection != NULL);
949 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
950 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
951 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
952 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value2");
953
954 aws_profile_collection_destroy(profile_collection);
955
956 return 0;
957 }
958
959 AWS_TEST_CASE(
960 aws_profile_duplicate_profiles_last_property_value_test,
961 s_aws_profile_duplicate_profiles_last_property_value_test);
962
963 /*
964 * Default profile with profile prefix overrides default profile without prefix when profile prefix is first
965 */
966 AWS_STATIC_STRING_FROM_LITERAL(
967 s_duplicate_default_profiles_property_resolution1_profile,
968 "[profile default]\nname = value\n[default]\nname2 = value2");
969
970 static int s_aws_profile_duplicate_default_profiles_property_resolution1_test(
971 struct aws_allocator *allocator,
972 void *ctx) {
973 (void)ctx;
974
975 struct aws_profile_collection *profile_collection =
976 aws_prepare_profile_test(allocator, s_duplicate_default_profiles_property_resolution1_profile, AWS_PST_CONFIG);
977
978 ASSERT_TRUE(profile_collection != NULL);
979 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
980 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default");
981 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default", 1);
982 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default", "name", "value");
983
984 aws_profile_collection_destroy(profile_collection);
985
986 return 0;
987 }
988
989 AWS_TEST_CASE(
990 aws_profile_duplicate_default_profiles_property_resolution1_test,
991 s_aws_profile_duplicate_default_profiles_property_resolution1_test);
992
993 /*
994 * Default profile with profile prefix overrides default profile without prefix when profile prefix is last
995 */
996 AWS_STATIC_STRING_FROM_LITERAL(
997 s_duplicate_default_profiles_property_resolution2_profile,
998 "[default]\nname2 = value2\n[profile default]\nname = value");
999
1000 static int s_aws_profile_duplicate_default_profiles_property_resolution2_test(
1001 struct aws_allocator *allocator,
1002 void *ctx) {
1003 (void)ctx;
1004
1005 struct aws_profile_collection *profile_collection =
1006 aws_prepare_profile_test(allocator, s_duplicate_default_profiles_property_resolution2_profile, AWS_PST_CONFIG);
1007
1008 ASSERT_TRUE(profile_collection != NULL);
1009 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1010 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default");
1011 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default", 1);
1012 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default", "name", "value");
1013
1014 aws_profile_collection_destroy(profile_collection);
1015
1016 return 0;
1017 }
1018
1019 AWS_TEST_CASE(
1020 aws_profile_duplicate_default_profiles_property_resolution2_test,
1021 s_aws_profile_duplicate_default_profiles_property_resolution2_test);
1022
1023 /*
1024 * Invalid profile names are ignored
1025 */
1026 AWS_STATIC_STRING_FROM_LITERAL(s_invalid_profile_names_config_profile, "[profile in valid]\nname = value");
1027 AWS_STATIC_STRING_FROM_LITERAL(s_invalid_profile_names_credentials_profile, "[in valid 2]\nname2 = value2");
1028
1029 static int s_aws_profile_invalid_profile_names_merge_test(struct aws_allocator *allocator, void *ctx) {
1030 (void)ctx;
1031
1032 struct aws_profile_collection *profile_collection = aws_prepare_merged_profile_test(
1033 allocator, s_invalid_profile_names_config_profile, s_invalid_profile_names_credentials_profile);
1034
1035 ASSERT_TRUE(profile_collection != NULL);
1036 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 0);
1037
1038 aws_profile_collection_destroy(profile_collection);
1039
1040 return 0;
1041 }
1042
1043 AWS_TEST_CASE(aws_profile_invalid_profile_names_merge_test, s_aws_profile_invalid_profile_names_merge_test);
1044
1045 /*
1046 * Invalid property names are ignored
1047 */
1048 AWS_STATIC_STRING_FROM_LITERAL(s_invalid_property_names_ignored_profile, "[profile foo]\nin valid = value");
1049
1050 static int s_aws_profile_invalid_property_names_ignored_test(struct aws_allocator *allocator, void *ctx) {
1051 (void)ctx;
1052
1053 struct aws_profile_collection *profile_collection =
1054 aws_prepare_profile_test(allocator, s_invalid_property_names_ignored_profile, AWS_PST_CONFIG);
1055
1056 ASSERT_TRUE(profile_collection != NULL);
1057 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1058 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1059 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 0);
1060
1061 aws_profile_collection_destroy(profile_collection);
1062
1063 return 0;
1064 }
1065
1066 AWS_TEST_CASE(aws_profile_invalid_property_names_ignored_test, s_aws_profile_invalid_property_names_ignored_test);
1067
1068 /*
1069 * All valid profile name characters are supported
1070 */
1071 AWS_STATIC_STRING_FROM_LITERAL(
1072 s_all_valid_profile_characters_profile,
1073 "[profile ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_/.%@:+]");
1074
1075 static int s_aws_profile_all_valid_profile_characters_test(struct aws_allocator *allocator, void *ctx) {
1076 (void)ctx;
1077
1078 struct aws_profile_collection *profile_collection =
1079 aws_prepare_profile_test(allocator, s_all_valid_profile_characters_profile, AWS_PST_CONFIG);
1080
1081 ASSERT_TRUE(profile_collection != NULL);
1082 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1083 EXPECT_SECTION(
1084 profile_collection,
1085 AWS_PROFILE_SECTION_TYPE_PROFILE,
1086 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_/.%@:+");
1087 EXPECT_PROPERTY_COUNT(
1088 profile_collection,
1089 AWS_PROFILE_SECTION_TYPE_PROFILE,
1090 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_/.%@:+",
1091 0);
1092
1093 aws_profile_collection_destroy(profile_collection);
1094
1095 return 0;
1096 }
1097
1098 AWS_TEST_CASE(aws_profile_all_valid_profile_characters_test, s_aws_profile_all_valid_profile_characters_test);
1099
1100 /*
1101 * All valid profile name characters are supported
1102 */
1103 AWS_STATIC_STRING_FROM_LITERAL(
1104 s_all_valid_property_characters_profile,
1105 "[profile foo]\nABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_/.%@:+ = value");
1106
1107 static int s_aws_profile_all_valid_property_characters_test(struct aws_allocator *allocator, void *ctx) {
1108 (void)ctx;
1109
1110 struct aws_profile_collection *profile_collection =
1111 aws_prepare_profile_test(allocator, s_all_valid_property_characters_profile, AWS_PST_CONFIG);
1112
1113 ASSERT_TRUE(profile_collection != NULL);
1114 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1115 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1116 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
1117 EXPECT_PROPERTY(
1118 profile_collection,
1119 AWS_PROFILE_SECTION_TYPE_PROFILE,
1120 "foo",
1121 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_/.%@:+",
1122 "value");
1123
1124 aws_profile_collection_destroy(profile_collection);
1125
1126 return 0;
1127 }
1128
1129 AWS_TEST_CASE(aws_profile_all_valid_property_characters_test, s_aws_profile_all_valid_property_characters_test);
1130
1131 /*
1132 * Properties can have sub properties
1133 */
1134 AWS_STATIC_STRING_FROM_LITERAL(s_basic_sub_property_profile, "[profile foo]\ns3 =\n name = value");
1135
1136 static int s_aws_profile_basic_sub_property_test(struct aws_allocator *allocator, void *ctx) {
1137 (void)ctx;
1138
1139 struct aws_profile_collection *profile_collection =
1140 aws_prepare_profile_test(allocator, s_basic_sub_property_profile, AWS_PST_CONFIG);
1141
1142 ASSERT_TRUE(profile_collection != NULL);
1143 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1144 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1145 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
1146 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "\nname = value");
1147 EXPECT_SUB_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", 1);
1148 EXPECT_SUB_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "name", "value");
1149
1150 aws_profile_collection_destroy(profile_collection);
1151
1152 return 0;
1153 }
1154
1155 AWS_TEST_CASE(aws_profile_basic_sub_property_test, s_aws_profile_basic_sub_property_test);
1156
1157 /*
1158 * Sub properties can have an empty value
1159 */
1160
1161 AWS_STATIC_STRING_FROM_LITERAL(s_empty_sub_property_profile, "[profile foo]\ns3 =\n name =");
1162
1163 static int s_aws_profile_empty_sub_property_test(struct aws_allocator *allocator, void *ctx) {
1164 (void)ctx;
1165
1166 struct aws_profile_collection *profile_collection =
1167 aws_prepare_profile_test(allocator, s_empty_sub_property_profile, AWS_PST_CONFIG);
1168
1169 ASSERT_TRUE(profile_collection != NULL);
1170 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1171 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1172 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
1173 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "\nname =");
1174 EXPECT_SUB_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", 1);
1175 EXPECT_SUB_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "name", "");
1176
1177 aws_profile_collection_destroy(profile_collection);
1178
1179 return 0;
1180 }
1181
1182 AWS_TEST_CASE(aws_profile_empty_sub_property_test, s_aws_profile_empty_sub_property_test);
1183
1184 /*
1185 * An invalid subproperty name is not a fatal parse error
1186 */
1187
1188 AWS_STATIC_STRING_FROM_LITERAL(s_invalid_sub_property_name_profile, "[profile foo]\ns3 =\n in valid = value");
1189
1190 static int s_aws_profile_invalid_sub_property_name_test(struct aws_allocator *allocator, void *ctx) {
1191 (void)ctx;
1192
1193 struct aws_profile_collection *profile_collection =
1194 aws_prepare_profile_test(allocator, s_invalid_sub_property_name_profile, AWS_PST_CONFIG);
1195
1196 ASSERT_TRUE(profile_collection != NULL);
1197 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1198 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1199 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
1200 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "\nin valid = value");
1201 EXPECT_SUB_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", 0);
1202
1203 aws_profile_collection_destroy(profile_collection);
1204
1205 return 0;
1206 }
1207
1208 AWS_TEST_CASE(aws_profile_invalid_sub_property_name_test, s_aws_profile_invalid_sub_property_name_test);
1209
1210 /*
1211 * Sub properties can have blank lines that get ignored
1212 */
1213
1214 AWS_STATIC_STRING_FROM_LITERAL(
1215 s_sub_property_blank_line_profile,
1216 "[profile foo]\ns3 =\n name = value\n\t \n name2 = value2");
1217
1218 static int s_aws_profile_sub_property_blank_line_test(struct aws_allocator *allocator, void *ctx) {
1219 (void)ctx;
1220
1221 struct aws_profile_collection *profile_collection =
1222 aws_prepare_profile_test(allocator, s_sub_property_blank_line_profile, AWS_PST_CONFIG);
1223
1224 ASSERT_TRUE(profile_collection != NULL);
1225 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1226 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1227 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
1228 EXPECT_PROPERTY(
1229 profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "\nname = value\nname2 = value2");
1230 EXPECT_SUB_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", 2);
1231 EXPECT_SUB_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "name", "value");
1232 EXPECT_SUB_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "s3", "name2", "value2");
1233
1234 aws_profile_collection_destroy(profile_collection);
1235
1236 return 0;
1237 }
1238
1239 AWS_TEST_CASE(aws_profile_sub_property_blank_line_test, s_aws_profile_sub_property_blank_line_test);
1240
1241 /*
1242 * Profiles duplicated in multiple files are merged.
1243 */
1244 AWS_STATIC_STRING_FROM_LITERAL(s_basic_duplicate_config_profile, "[profile foo]\nname = value");
1245 AWS_STATIC_STRING_FROM_LITERAL(s_basic_duplicate_credentials_profile, "[foo]\nname2 = value2");
1246
1247 static int s_aws_profile_basic_duplicate_merge_test(struct aws_allocator *allocator, void *ctx) {
1248 (void)ctx;
1249
1250 struct aws_profile_collection *profile_collection = aws_prepare_merged_profile_test(
1251 allocator, s_basic_duplicate_config_profile, s_basic_duplicate_credentials_profile);
1252
1253 ASSERT_TRUE(profile_collection != NULL);
1254 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1255 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1256 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 2);
1257 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value");
1258 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name2", "value2");
1259
1260 aws_profile_collection_destroy(profile_collection);
1261
1262 return 0;
1263 }
1264
1265 AWS_TEST_CASE(aws_profile_basic_duplicate_merge_test, s_aws_profile_basic_duplicate_merge_test);
1266
1267 /*
1268 * When merging default profile in config file, the one without the prefix gets ignored
1269 */
1270
1271 AWS_STATIC_STRING_FROM_LITERAL(
1272 s_mixed_prefix_default_profile,
1273 "[profile default]\nname = value\n[default]\nname2 = value2\n[profile default]\nname3 = value3");
1274
1275 static int s_aws_profile_mixed_prefix_default_test(struct aws_allocator *allocator, void *ctx) {
1276 (void)ctx;
1277
1278 struct aws_profile_collection *profile_collection =
1279 aws_prepare_profile_test(allocator, s_mixed_prefix_default_profile, AWS_PST_CONFIG);
1280
1281 ASSERT_TRUE(profile_collection != NULL);
1282 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1283 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default");
1284 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default", 2);
1285 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default", "name", "value");
1286 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "default", "name3", "value3");
1287
1288 aws_profile_collection_destroy(profile_collection);
1289
1290 return 0;
1291 }
1292
1293 AWS_TEST_CASE(aws_profile_mixed_prefix_default_test, s_aws_profile_mixed_prefix_default_test);
1294
1295 /*
1296 * Duplicate properties between files use credentials property
1297 */
1298
1299 AWS_STATIC_STRING_FROM_LITERAL(s_override_duplicate_config_profile, "[profile foo]\nname = value");
1300 AWS_STATIC_STRING_FROM_LITERAL(s_override_duplicate_credentials_profile, "[foo]\nname = value2");
1301
1302 static int s_aws_profile_override_duplicate_merge_test(struct aws_allocator *allocator, void *ctx) {
1303 (void)ctx;
1304
1305 struct aws_profile_collection *profile_collection = aws_prepare_merged_profile_test(
1306 allocator, s_override_duplicate_config_profile, s_override_duplicate_credentials_profile);
1307
1308 ASSERT_TRUE(profile_collection != NULL);
1309 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 1);
1310 EXPECT_SECTION(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo");
1311 EXPECT_PROPERTY_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", 1);
1312 EXPECT_PROPERTY(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, "foo", "name", "value2");
1313
1314 aws_profile_collection_destroy(profile_collection);
1315
1316 return 0;
1317 }
1318
1319 AWS_TEST_CASE(aws_profile_override_duplicate_merge_test, s_aws_profile_override_duplicate_merge_test);
1320
1321 /*
1322 * Non-default config profiles without prefix are ignored
1323 */
1324
1325 AWS_STATIC_STRING_FROM_LITERAL(s_no_prefix_nondefault_profile, "[foo]\nname = value");
1326
1327 static int s_aws_profile_no_prefix_nondefault_test(struct aws_allocator *allocator, void *ctx) {
1328 (void)ctx;
1329
1330 struct aws_profile_collection *profile_collection =
1331 aws_prepare_profile_test(allocator, s_no_prefix_nondefault_profile, AWS_PST_CONFIG);
1332
1333 ASSERT_TRUE(profile_collection != NULL);
1334 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 0);
1335
1336 aws_profile_collection_destroy(profile_collection);
1337
1338 return 0;
1339 }
1340
1341 AWS_TEST_CASE(aws_profile_no_prefix_nondefault_test, s_aws_profile_no_prefix_nondefault_test);
1342
1343 /*
1344 * Credentials profiles with prefix are ignored
1345 */
1346
1347 AWS_STATIC_STRING_FROM_LITERAL(s_prefix_credentials_profile, "[profile foo]\nname = value");
1348
1349 static int s_aws_profile_prefix_credentials_test(struct aws_allocator *allocator, void *ctx) {
1350 (void)ctx;
1351
1352 struct aws_profile_collection *profile_collection =
1353 aws_prepare_profile_test(allocator, s_prefix_credentials_profile, AWS_PST_CREDENTIALS);
1354
1355 ASSERT_TRUE(profile_collection != NULL);
1356 EXPECT_SECTION_COUNT(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, 0);
1357
1358 aws_profile_collection_destroy(profile_collection);
1359
1360 return 0;
1361 }
1362
1363 AWS_TEST_CASE(aws_profile_prefix_credentials_test, s_aws_profile_prefix_credentials_test);
1364
1365 AWS_STATIC_STRING_FROM_LITERAL(s_config_override_path, "/tmp/.aws/config");
1366
1367 #ifdef _WIN32
1368 AWS_STATIC_STRING_FROM_LITERAL(s_config_override_path_result, "\\tmp\\.aws\\config");
1369 #else
1370 AWS_STATIC_STRING_FROM_LITERAL(s_config_override_path_result, "/tmp/.aws/config");
1371 #endif /* _WIN32 */
1372
1373 static int s_config_file_path_override_test(struct aws_allocator *allocator, void *ctx) {
1374 (void)ctx;
1375
1376 struct aws_byte_cursor override_cursor = aws_byte_cursor_from_string(s_config_override_path);
1377 struct aws_string *path = aws_get_config_file_path(allocator, &override_cursor);
1378 ASSERT_TRUE(aws_string_compare(path, s_config_override_path_result) == 0);
1379
1380 aws_string_destroy(path);
1381
1382 return 0;
1383 }
1384
1385 AWS_TEST_CASE(config_file_path_override_test, s_config_file_path_override_test);
1386
1387 AWS_STATIC_STRING_FROM_LITERAL(s_config_env_var, "AWS_CONFIG_FILE");
1388
1389 static int s_config_file_path_environment_test(struct aws_allocator *allocator, void *ctx) {
1390 (void)ctx;
1391
1392 aws_set_environment_value(s_config_env_var, s_config_override_path);
1393
1394 struct aws_string *path = aws_get_config_file_path(allocator, NULL);
1395 ASSERT_TRUE(aws_string_compare(path, s_config_override_path_result) == 0);
1396
1397 aws_string_destroy(path);
1398
1399 return 0;
1400 }
1401
1402 AWS_TEST_CASE(config_file_path_environment_test, s_config_file_path_environment_test);
1403
1404 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_override_path, "/tmp/.aws/credentials");
1405
1406 #ifdef _WIN32
1407 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_override_path_result, "\\tmp\\.aws\\credentials");
1408 #else
1409 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_override_path_result, "/tmp/.aws/credentials");
1410 #endif /* _WIN32 */
1411
1412 static int s_credentials_file_path_override_test(struct aws_allocator *allocator, void *ctx) {
1413 (void)ctx;
1414
1415 struct aws_byte_cursor override_cursor = aws_byte_cursor_from_string(s_credentials_override_path);
1416 struct aws_string *path = aws_get_credentials_file_path(allocator, &override_cursor);
1417 ASSERT_TRUE(aws_string_compare(path, s_credentials_override_path_result) == 0);
1418
1419 aws_string_destroy(path);
1420
1421 return 0;
1422 }
1423
1424 AWS_TEST_CASE(credentials_file_path_override_test, s_credentials_file_path_override_test);
1425
1426 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_env_var, "AWS_SHARED_CREDENTIALS_FILE");
1427
1428 static int s_credentials_file_path_environment_test(struct aws_allocator *allocator, void *ctx) {
1429 (void)ctx;
1430
1431 aws_set_environment_value(s_credentials_env_var, s_credentials_override_path);
1432
1433 struct aws_string *path = aws_get_credentials_file_path(allocator, NULL);
1434 ASSERT_TRUE(aws_string_compare(path, s_credentials_override_path_result) == 0);
1435
1436 aws_string_destroy(path);
1437
1438 return 0;
1439 }
1440
1441 AWS_TEST_CASE(credentials_file_path_environment_test, s_credentials_file_path_environment_test);
1442
1443 AWS_STATIC_STRING_FROM_LITERAL(s_profile_env_var, "AWS_PROFILE");
1444 AWS_STATIC_STRING_FROM_LITERAL(s_profile_override, "NotTheDefault");
1445
1446 static int s_profile_override_test(struct aws_allocator *allocator, void *ctx) {
1447 (void)ctx;
1448 /* The envrionment value should only override the default when user not set one */
1449 aws_set_environment_value(s_profile_env_var, s_profile_override);
1450
1451 struct aws_byte_cursor override_cursor = aws_byte_cursor_from_string(s_profile_override);
1452 struct aws_string *profile_name = aws_get_profile_name(allocator, &override_cursor);
1453 ASSERT_TRUE(aws_string_compare(profile_name, s_profile_override) == 0);
1454
1455 aws_string_destroy(profile_name);
1456
1457 return 0;
1458 }
1459
1460 AWS_TEST_CASE(profile_override_test, s_profile_override_test);
1461
1462 static int s_profile_environment_test(struct aws_allocator *allocator, void *ctx) {
1463 (void)ctx;
1464
1465 aws_set_environment_value(s_profile_env_var, s_profile_override);
1466
1467 struct aws_string *profile_name = aws_get_profile_name(allocator, NULL);
1468 ASSERT_TRUE(aws_string_compare(profile_name, s_profile_override) == 0);
1469
1470 aws_string_destroy(profile_name);
1471
1472 return 0;
1473 }
1474
1475 AWS_TEST_CASE(profile_environment_test, s_profile_environment_test);