Debian Codegraph @

crt/aws-c-auth/tests/sso_token_util_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 "shared_credentials_test_definitions.h"
9 #include <aws/auth/private/sso_token_utils.h>
10
11 static int s_parse_token_location_url_test(struct aws_allocator *allocator, void *ctx) {
12 (void)ctx;
13 aws_auth_library_init(allocator);
14 struct aws_string *start_url = aws_string_new_from_c_str(allocator, "https://d-92671207e4.awsapps.com/start");
15 struct aws_string *token_path = aws_construct_sso_token_path(allocator, start_url);
16
17 struct aws_byte_cursor token_cursor = aws_byte_cursor_from_string(token_path);
18 struct aws_byte_cursor expected_token_cursor =
19 aws_byte_cursor_from_c_str("13f9d35043871d073ab260e020f0ffde092cb14b.json");
20 struct aws_byte_cursor find_cursor;
21 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&token_cursor, &expected_token_cursor, &find_cursor));
22
23 aws_string_destroy(start_url);
24 aws_string_destroy(token_path);
25 aws_auth_library_clean_up();
26 return AWS_OP_SUCCESS;
27 }
28 AWS_TEST_CASE(parse_token_location_url_test, s_parse_token_location_url_test);
29
30 static int s_parse_token_location_session_test(struct aws_allocator *allocator, void *ctx) {
31 (void)ctx;
32 aws_auth_library_init(allocator);
33 struct aws_string *session = aws_string_new_from_c_str(allocator, "admin");
34 struct aws_string *token_path = aws_construct_sso_token_path(allocator, session);
35 struct aws_byte_cursor token_cursor = aws_byte_cursor_from_string(token_path);
36 struct aws_byte_cursor expected_token_cursor =
37 aws_byte_cursor_from_c_str("d033e22ae348aeb5660fc2140aec35850c4da997.json");
38 struct aws_byte_cursor find_cursor;
39 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&token_cursor, &expected_token_cursor, &find_cursor));
40
41 aws_string_destroy(session);
42 aws_string_destroy(token_path);
43 aws_auth_library_clean_up();
44 return AWS_OP_SUCCESS;
45 }
46 AWS_TEST_CASE(parse_token_location_session_test, s_parse_token_location_session_test);
47
48 AWS_STATIC_STRING_FROM_LITERAL(
49 s_valid_token_json,
50 "{\"accessToken\": \"string\",\"expiresAt\": \"2019-11-14T04:05:45Z\",\"refreshToken\": \"string\",\"clientId\": "
51 "\"123321\",\"clientSecret\": \"ABCDE123\",\"registrationExpiresAt\": "
52 "\"2022-03-06T19:53:17Z\",\"region\": \"us-west-2\",\"startUrl\": \"https://d-abc123.awsapps.com/start\"}");
53 static int s_parse_sso_token_valid(struct aws_allocator *allocator, void *ctx) {
54 (void)ctx;
55 aws_auth_library_init(allocator);
56 struct aws_string *file_path = aws_create_process_unique_file_name(allocator);
57 ASSERT_SUCCESS(aws_create_profile_file(file_path, s_valid_token_json));
58 struct aws_sso_token *sso_token = aws_sso_token_new_from_file(allocator, file_path);
59 ASSERT_TRUE(aws_string_eq_c_str(sso_token->access_token, "string"));
60 ASSERT_INT_EQUALS((uint64_t)aws_date_time_as_epoch_secs(&sso_token->expiration), 1573704345);
61 aws_string_destroy(file_path);
62 aws_sso_token_destroy(sso_token);
63 aws_auth_library_clean_up();
64 return AWS_OP_SUCCESS;
65 }
66 AWS_TEST_CASE(parse_sso_token_valid, s_parse_sso_token_valid);
67
68 AWS_STATIC_STRING_FROM_LITERAL(s_invalid_token_json, "invalid json");
69 static int s_parse_sso_token_invalid(struct aws_allocator *allocator, void *ctx) {
70 (void)ctx;
71 aws_auth_library_init(allocator);
72 struct aws_string *file_path = aws_create_process_unique_file_name(allocator);
73 ASSERT_SUCCESS(aws_create_profile_file(file_path, s_invalid_token_json));
74 ASSERT_NULL(aws_sso_token_new_from_file(allocator, file_path));
75 ASSERT_INT_EQUALS(AWS_AUTH_SSO_TOKEN_INVALID, aws_last_error());
76 aws_string_destroy(file_path);
77 aws_auth_library_clean_up();
78 return AWS_OP_SUCCESS;
79 }
80 AWS_TEST_CASE(parse_sso_token_invalid, s_parse_sso_token_invalid);
81
82 AWS_STATIC_STRING_FROM_LITERAL(
83 s_missing_access_token_json,
84 "{\"expiresAt\": \"2019-11-14T04:05:45Z\",\"refreshToken\": \"string\",\"clientId\": "
85 "\"123321\",\"clientSecret\": \"ABCDE123\",\"registrationExpiresAt\": "
86 "\"2022-03-06T19:53:17Z\",\"region\": \"us-west-2\",\"startUrl\": \"https://d-abc123.awsapps.com/start\"}");
87 static int s_parse_sso_token_invalid_missing_access_token(struct aws_allocator *allocator, void *ctx) {
88 (void)ctx;
89 aws_auth_library_init(allocator);
90 struct aws_string *file_path = aws_create_process_unique_file_name(allocator);
91 ASSERT_SUCCESS(aws_create_profile_file(file_path, s_missing_access_token_json));
92 ASSERT_NULL(aws_sso_token_new_from_file(allocator, file_path));
93 ASSERT_INT_EQUALS(AWS_AUTH_SSO_TOKEN_INVALID, aws_last_error());
94 aws_string_destroy(file_path);
95 aws_auth_library_clean_up();
96 return AWS_OP_SUCCESS;
97 }
98 AWS_TEST_CASE(parse_sso_token_invalid_missing_access_token, s_parse_sso_token_invalid_missing_access_token);
99
100 AWS_STATIC_STRING_FROM_LITERAL(
101 s_missing_expires_at_json,
102 "{\"accessToken\": \"string\",\"refreshToken\": \"string\",\"clientId\": "
103 "\"123321\",\"clientSecret\": \"ABCDE123\",\"registrationExpiresAt\": "
104 "\"2022-03-06T19:53:17Z\",\"region\": \"us-west-2\",\"startUrl\": \"https://d-abc123.awsapps.com/start\"}");
105 static int s_parse_sso_token_missing_expires_at(struct aws_allocator *allocator, void *ctx) {
106 (void)ctx;
107 aws_auth_library_init(allocator);
108 struct aws_string *file_path = aws_create_process_unique_file_name(allocator);
109 ASSERT_SUCCESS(aws_create_profile_file(file_path, s_missing_expires_at_json));
110 ASSERT_NULL(aws_sso_token_new_from_file(allocator, file_path));
111 ASSERT_INT_EQUALS(AWS_AUTH_SSO_TOKEN_INVALID, aws_last_error());
112 aws_string_destroy(file_path);
113 aws_auth_library_clean_up();
114 return AWS_OP_SUCCESS;
115 }
116 AWS_TEST_CASE(parse_sso_token_missing_expires_at, s_parse_sso_token_missing_expires_at);
117
118 AWS_STATIC_STRING_FROM_LITERAL(
119 s_invalid_expires_at_json,
120 "{\"accessToken\": \"string\",\"expiresAt\": \"1234567\",\"refreshToken\": \"string\",\"clientId\": "
121 "\"123321\",\"clientSecret\": \"ABCDE123\",\"registrationExpiresAt\": "
122 "\"2022-03-06T19:53:17Z\",\"region\": \"us-west-2\",\"startUrl\": \"https://d-abc123.awsapps.com/start\"}");
123 static int s_parse_sso_token_invalid_expires_at(struct aws_allocator *allocator, void *ctx) {
124 (void)ctx;
125 aws_auth_library_init(allocator);
126 struct aws_string *file_path = aws_create_process_unique_file_name(allocator);
127 ASSERT_SUCCESS(aws_create_profile_file(file_path, s_invalid_expires_at_json));
128 ASSERT_NULL(aws_sso_token_new_from_file(allocator, file_path));
129 ASSERT_INT_EQUALS(AWS_AUTH_SSO_TOKEN_INVALID, aws_last_error());
130 aws_string_destroy(file_path);
131 aws_auth_library_clean_up();
132 return AWS_OP_SUCCESS;
133 }
134 AWS_TEST_CASE(parse_sso_token_invalid_expires_at, s_parse_sso_token_invalid_expires_at);