Debian Codegraph @

crt/aws-c-auth/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/auth/private/aws_profile.h>
9 #include <aws/common/environment.h>
10 #include <aws/common/string.h>
11 #include <aws/io/file_utils.h>
12
13 AWS_STATIC_STRING_FROM_LITERAL(s_config_override_path, "/tmp/.aws/config");
14
15 #ifdef _WIN32
16 AWS_STATIC_STRING_FROM_LITERAL(s_config_override_path_result, "\\tmp\\.aws\\config");
17 #else
18 AWS_STATIC_STRING_FROM_LITERAL(s_config_override_path_result, "/tmp/.aws/config");
19 #endif /* _WIN32 */
20
21 static int s_config_file_path_override_test(struct aws_allocator *allocator, void *ctx) {
22 (void)ctx;
23
24 struct aws_byte_cursor override_cursor = aws_byte_cursor_from_string(s_config_override_path);
25 struct aws_string *path = aws_get_config_file_path(allocator, &override_cursor);
26 ASSERT_TRUE(aws_string_compare(path, s_config_override_path_result) == 0);
27
28 aws_string_destroy(path);
29
30 return 0;
31 }
32
33 AWS_TEST_CASE(config_file_path_override_test, s_config_file_path_override_test);
34
35 AWS_STATIC_STRING_FROM_LITERAL(s_config_env_var, "AWS_CONFIG_FILE");
36
37 static int s_config_file_path_environment_test(struct aws_allocator *allocator, void *ctx) {
38 (void)ctx;
39
40 aws_set_environment_value(s_config_env_var, s_config_override_path);
41
42 struct aws_string *path = aws_get_config_file_path(allocator, NULL);
43 ASSERT_TRUE(aws_string_compare(path, s_config_override_path_result) == 0);
44
45 aws_string_destroy(path);
46
47 return 0;
48 }
49
50 AWS_TEST_CASE(config_file_path_environment_test, s_config_file_path_environment_test);
51
52 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_override_path, "/tmp/.aws/credentials");
53
54 #ifdef _WIN32
55 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_override_path_result, "\\tmp\\.aws\\credentials");
56 #else
57 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_override_path_result, "/tmp/.aws/credentials");
58 #endif /* _WIN32 */
59
60 static int s_credentials_file_path_override_test(struct aws_allocator *allocator, void *ctx) {
61 (void)ctx;
62
63 struct aws_byte_cursor override_cursor = aws_byte_cursor_from_string(s_credentials_override_path);
64 struct aws_string *path = aws_get_credentials_file_path(allocator, &override_cursor);
65 ASSERT_TRUE(aws_string_compare(path, s_credentials_override_path_result) == 0);
66
67 aws_string_destroy(path);
68
69 return 0;
70 }
71
72 AWS_TEST_CASE(credentials_file_path_override_test, s_credentials_file_path_override_test);
73
74 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_env_var, "AWS_SHARED_CREDENTIALS_FILE");
75
76 static int s_credentials_file_path_environment_test(struct aws_allocator *allocator, void *ctx) {
77 (void)ctx;
78
79 aws_set_environment_value(s_credentials_env_var, s_credentials_override_path);
80
81 struct aws_string *path = aws_get_credentials_file_path(allocator, NULL);
82 ASSERT_TRUE(aws_string_compare(path, s_credentials_override_path_result) == 0);
83
84 aws_string_destroy(path);
85
86 return 0;
87 }
88
89 AWS_TEST_CASE(credentials_file_path_environment_test, s_credentials_file_path_environment_test);
90
91 AWS_STATIC_STRING_FROM_LITERAL(s_profile_env_var, "AWS_PROFILE");
92 AWS_STATIC_STRING_FROM_LITERAL(s_profile_override, "NotTheDefault");
93
94 static int s_profile_override_test(struct aws_allocator *allocator, void *ctx) {
95 (void)ctx;
96
97 struct aws_byte_cursor override_cursor = aws_byte_cursor_from_string(s_profile_override);
98 struct aws_string *profile_name = aws_get_profile_name(allocator, &override_cursor);
99 ASSERT_TRUE(aws_string_compare(profile_name, s_profile_override) == 0);
100
101 aws_string_destroy(profile_name);
102
103 return 0;
104 }
105
106 AWS_TEST_CASE(profile_override_test, s_profile_override_test);
107
108 static int s_profile_environment_test(struct aws_allocator *allocator, void *ctx) {
109 (void)ctx;
110
111 aws_set_environment_value(s_profile_env_var, s_profile_override);
112
113 struct aws_string *profile_name = aws_get_profile_name(allocator, NULL);
114 ASSERT_TRUE(aws_string_compare(profile_name, s_profile_override) == 0);
115
116 aws_string_destroy(profile_name);
117
118 return 0;
119 }
120
121 AWS_TEST_CASE(profile_environment_test, s_profile_environment_test);