Debian Codegraph @

crt/aws-c-auth/tests/seed_tests.py

raw symbols SCIP
1 #
2 # Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License").
5 # You may not use this file except in compliance with the License.
6 # A copy of the License is located at
7 #
8 # http://aws.amazon.com/apache2.0
9 #
10 # or in the "license" file accompanying this file. This file is distributed
11 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 # express or implied. See the License for the specific language governing
13 # permissions and limitations under the License.
14 #
15
16 import os
17 import copy
18 import argparse
19 import pdb
20 import json
21
22 base_context = {
23 "region":"us-east-1",
24 "service":"service",
25 "timestamp":"2015-08-30T12:36:00Z",
26 "expiration_in_seconds":3600,
27 "credentials": {
28 "access_key_id" : "AKIDEXAMPLE",
29 "secret_access_key" : "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"
30 },
31 "sign_body": False
32 }
33
34 def parse_arguments():
35 parser = argparse.ArgumentParser(description="Sigv4 Test Suite Generation Script")
36 parser.add_argument("source_dir", action="store")
37 parser.add_argument("dest_dir", action="store")
38
39 args = vars( parser.parse_args() )
40
41 return args
42
43 def merge_dicts(source, destination):
44 for key, value in source.items():
45 if isinstance(value, dict):
46 # get node or create one
47 node = destination.setdefault(key, {})
48 merge_dicts(value, node)
49 else:
50 destination[key] = value
51
52 return destination
53
54 def generate_test_case(source_dir, dest_dir, test_name, context_map):
55 source_request_filename = os.path.join(source_dir, test_name + ".req")
56 if not os.path.exists(source_request_filename):
57 return
58
59 if not os.path.exists(dest_dir):
60 os.makedirs(dest_dir)
61
62 dest_request_filename = os.path.join(dest_dir, "request.txt")
63
64 with open(source_request_filename, "r") as source_file:
65 with open(dest_request_filename, "w") as dest_file:
66 for _, line in enumerate(source_file):
67 if not line.startswith("X-Amz-Date"):
68 dest_file.write(line)
69
70 test_context = copy.deepcopy(base_context)
71 test_context = merge_dicts(context_map, test_context)
72
73 context_contents = json.dumps(test_context, sort_keys=True, indent=4)
74
75 dest_context_filename = os.path.join(dest_dir, "context.json")
76 context_file = open(dest_context_filename,"w")
77 context_file.write(context_contents)
78 context_file.close()
79
80 return
81
82 normalized_context = {
83 "normalize" : True
84 }
85
86 unnormalized_context = {
87 "normalize" : False
88 }
89
90 token_context = {
91 "normalize" : True,
92 "credentials" : {
93 "token" : "6e86291e8372ff2a2260956d9b8aae1d763fbf315fa00fa31553b73ebf194267"
94 }
95 }
96
97 sign_body_context = {
98 "normalize": True,
99 "sign_body": True
100 }
101
102 def generate_tests(source_dir, dest_dir, suffix, default_context_map):
103 for root_dir, dir_names, file_names in os.walk( source_dir ):
104 if root_dir == source_dir:
105 for dir_name in dir_names:
106 test_case_source_dir = os.path.join(root_dir, dir_name)
107
108 context_map = default_context_map
109 if dir_name == "get-vanilla-with-session-token":
110 context_map = token_context
111 elif dir_name.startswith('post-x-www-form'):
112 context_map = sign_body_context
113
114 v4_test_case_dest_dir = os.path.join(dest_dir, "v4", dir_name + suffix)
115 v4a_test_case_dest_dir = os.path.join(dest_dir, "v4a", dir_name + suffix)
116
117 generate_test_case(test_case_source_dir, v4_test_case_dest_dir, dir_name, context_map)
118 generate_test_case(test_case_source_dir, v4a_test_case_dest_dir, dir_name, context_map)
119
120 return
121
122
123 def main():
124 args = parse_arguments()
125
126 source_dir = args["source_dir"]
127 if not os.path.exists(source_dir):
128 print("Source directory {0} does not exist".format(source_dir))
129 return
130
131 dest_dir = args["dest_dir"]
132 if not os.path.exists(dest_dir):
133 os.makedirs(dest_dir)
134
135 generate_tests(source_dir, dest_dir, "", normalized_context)
136
137 post_sts_token_dir = os.path.join(source_dir, "post-sts-token")
138 generate_tests(post_sts_token_dir, dest_dir, "", normalized_context)
139
140 normalize_dir = os.path.join(source_dir, "normalize-path")
141 generate_tests(normalize_dir, dest_dir, "-normalized", normalized_context)
142 generate_tests(normalize_dir, dest_dir, "-unnormalized", unnormalized_context)
143
144 main()