Use the -allow-unimplemented feature of the BoringSSL runner
[openssl.git] / test / ossl_shim / test_config.cc
1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include "test_config.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <memory>
22
23 #include <openssl/evp.h>
24
25 namespace {
26
27 template <typename T>
28 struct Flag {
29   const char *flag;
30   T TestConfig::*member;
31 };
32
33 // FindField looks for the flag in |flags| that matches |flag|. If one is found,
34 // it returns a pointer to the corresponding field in |config|. Otherwise, it
35 // returns NULL.
36 template<typename T, size_t N>
37 T *FindField(TestConfig *config, const Flag<T> (&flags)[N], const char *flag) {
38   for (size_t i = 0; i < N; i++) {
39     if (strcmp(flag, flags[i].flag) == 0) {
40       return &(config->*(flags[i].member));
41     }
42   }
43   return NULL;
44 }
45
46 const Flag<bool> kBoolFlags[] = {
47   { "-server", &TestConfig::is_server },
48   { "-dtls", &TestConfig::is_dtls },
49   { "-fallback-scsv", &TestConfig::fallback_scsv },
50   { "-require-any-client-certificate",
51     &TestConfig::require_any_client_certificate },
52   { "-async", &TestConfig::async },
53   { "-write-different-record-sizes",
54     &TestConfig::write_different_record_sizes },
55   { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
56   { "-partial-write", &TestConfig::partial_write },
57   { "-no-tls13", &TestConfig::no_tls13 },
58   { "-no-tls12", &TestConfig::no_tls12 },
59   { "-no-tls11", &TestConfig::no_tls11 },
60   { "-no-tls1", &TestConfig::no_tls1 },
61   { "-no-ssl3", &TestConfig::no_ssl3 },
62   { "-shim-writes-first", &TestConfig::shim_writes_first },
63   { "-expect-session-miss", &TestConfig::expect_session_miss },
64   { "-decline-alpn", &TestConfig::decline_alpn },
65   { "-expect-extended-master-secret",
66     &TestConfig::expect_extended_master_secret },
67   { "-implicit-handshake", &TestConfig::implicit_handshake },
68   { "-use-early-callback", &TestConfig::use_early_callback },
69   { "-fail-early-callback", &TestConfig::fail_early_callback },
70   { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
71   { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
72   { "-handshake-never-done", &TestConfig::handshake_never_done },
73   { "-use-export-context", &TestConfig::use_export_context },
74   { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
75   { "-expect-no-session", &TestConfig::expect_no_session },
76   { "-use-ticket-callback", &TestConfig::use_ticket_callback },
77   { "-renew-ticket", &TestConfig::renew_ticket },
78   { "-enable-client-custom-extension",
79     &TestConfig::enable_client_custom_extension },
80   { "-enable-server-custom-extension",
81     &TestConfig::enable_server_custom_extension },
82   { "-custom-extension-skip", &TestConfig::custom_extension_skip },
83   { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
84   { "-check-close-notify", &TestConfig::check_close_notify },
85   { "-shim-shuts-down", &TestConfig::shim_shuts_down },
86   { "-verify-fail", &TestConfig::verify_fail },
87   { "-verify-peer", &TestConfig::verify_peer },
88   { "-expect-verify-result", &TestConfig::expect_verify_result },
89   { "-renegotiate-freely", &TestConfig::renegotiate_freely },
90   { "-p384-only", &TestConfig::p384_only },
91   { "-enable-all-curves", &TestConfig::enable_all_curves },
92   { "-use-sparse-dh-prime", &TestConfig::use_sparse_dh_prime },
93   { "-use-old-client-cert-callback",
94     &TestConfig::use_old_client_cert_callback },
95   { "-use-null-client-ca-list", &TestConfig::use_null_client_ca_list },
96   { "-peek-then-read", &TestConfig::peek_then_read },
97   { "-enable-grease", &TestConfig::enable_grease },
98 };
99
100 const Flag<std::string> kStringFlags[] = {
101   { "-key-file", &TestConfig::key_file },
102   { "-cert-file", &TestConfig::cert_file },
103   { "-expect-server-name", &TestConfig::expected_server_name },
104   { "-advertise-npn", &TestConfig::advertise_npn },
105   { "-expect-next-proto", &TestConfig::expected_next_proto },
106   { "-select-next-proto", &TestConfig::select_next_proto },
107   { "-host-name", &TestConfig::host_name },
108   { "-advertise-alpn", &TestConfig::advertise_alpn },
109   { "-expect-alpn", &TestConfig::expected_alpn },
110   { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
111   { "-select-alpn", &TestConfig::select_alpn },
112   { "-psk", &TestConfig::psk },
113   { "-psk-identity", &TestConfig::psk_identity },
114   { "-srtp-profiles", &TestConfig::srtp_profiles },
115   { "-cipher", &TestConfig::cipher },
116   { "-export-label", &TestConfig::export_label },
117   { "-export-context", &TestConfig::export_context },
118 };
119
120 const Flag<std::string> kBase64Flags[] = {
121   { "-expect-certificate-types", &TestConfig::expected_certificate_types },
122   { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
123   { "-expect-signed-cert-timestamps",
124     &TestConfig::expected_signed_cert_timestamps },
125 };
126
127 const Flag<int> kIntFlags[] = {
128   { "-port", &TestConfig::port },
129   { "-resume-count", &TestConfig::resume_count },
130   { "-min-version", &TestConfig::min_version },
131   { "-max-version", &TestConfig::max_version },
132   { "-mtu", &TestConfig::mtu },
133   { "-export-keying-material", &TestConfig::export_keying_material },
134   { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
135   { "-expect-peer-signature-algorithm",
136     &TestConfig::expect_peer_signature_algorithm },
137   { "-expect-curve-id", &TestConfig::expect_curve_id },
138   { "-expect-dhe-group-size", &TestConfig::expect_dhe_group_size },
139   { "-max-cert-list", &TestConfig::max_cert_list },
140 };
141
142 }  // namespace
143
144 bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
145   for (int i = 0; i < argc; i++) {
146     bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
147     if (bool_field != NULL) {
148       *bool_field = true;
149       continue;
150     }
151
152     std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
153     if (string_field != NULL) {
154       i++;
155       if (i >= argc) {
156         fprintf(stderr, "Missing parameter\n");
157         return false;
158       }
159       string_field->assign(argv[i]);
160       continue;
161     }
162
163     std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
164     if (base64_field != NULL) {
165       i++;
166       if (i >= argc) {
167         fprintf(stderr, "Missing parameter\n");
168         return false;
169       }
170       std::unique_ptr<uint8_t[]> decoded(new uint8_t[strlen(argv[i])]);
171       int len = EVP_DecodeBlock(decoded.get(),
172                                 reinterpret_cast<const uint8_t *>(argv[i]),
173                                 strlen(argv[i]));
174       if (len < 0) {
175         fprintf(stderr, "Invalid base64: %s\n", argv[i]);
176         return false;
177       }
178       base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
179       continue;
180     }
181
182     int *int_field = FindField(out_config, kIntFlags, argv[i]);
183     if (int_field) {
184       i++;
185       if (i >= argc) {
186         fprintf(stderr, "Missing parameter\n");
187         return false;
188       }
189       *int_field = atoi(argv[i]);
190       continue;
191     }
192
193     fprintf(stderr, "Unknown argument: %s\n", argv[i]);
194     exit(89);
195     return false;
196   }
197
198   return true;
199 }