xref: /openbsd-src/regress/lib/libssl/tlsfuzzer/tlsfuzzer.py (revision 0970cb57551e0e8df6688f8a475400ea57aba548)
1#   $OpenBSD: tlsfuzzer.py,v 1.5 2020/06/01 10:46:45 tb Exp $
2#
3# Copyright (c) 2020 Theo Buehler <tb@openbsd.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17import getopt
18import os
19import subprocess
20import sys
21from timeit import default_timer as timer
22
23tlsfuzzer_scriptdir = "/usr/local/share/tlsfuzzer/scripts/"
24
25class Test:
26    """
27    Represents a tlsfuzzer test script.
28        name:           the script's name
29        args:           arguments to feed to the script
30        tls12_args:     override args for a TLSv1.2 server
31        tls13_args:     override args for a TLSv1.3 server
32
33    XXX Add client cert support.
34    """
35    def __init__(self, name="", args=[], tls12_args=[], tls13_args=[]):
36        self.name = name
37        self.tls12_args = args
38        self.tls13_args = args
39        if tls12_args:
40            self.tls12_args = tls12_args
41        if tls13_args:
42            self.tls13_args = tls13_args
43
44    def args(self, has_tls1_3: True):
45        if has_tls1_3:
46            return self.tls13_args
47        else:
48            return self.tls12_args
49
50    def __repr__(self):
51        return "<Test: %s tls12_args: %s tls13_args: %s>" % (
52                self.name, self.tls12_args, tls13_args
53            )
54
55class TestGroup:
56    """ A group of Test objects to be run by TestRunner."""
57    def __init__(self, title="Tests", tests=[]):
58        self.title = title
59        self.tests = tests
60
61    def __iter__(self):
62        return iter(self.tests)
63
64# argument to pass to several tests
65tls13_unsupported_ciphers = [
66    "-e", "TLS 1.3 with ffdhe2048",
67    "-e", "TLS 1.3 with ffdhe3072",
68    "-e", "TLS 1.3 with secp521r1",   # XXX: why is this curve problematic?
69    "-e", "TLS 1.3 with x448",
70]
71
72tls13_tests = TestGroup("TLSv1.3 tests", [
73    Test("test-tls13-ccs.py"),
74    Test("test-tls13-conversation.py"),
75    Test("test-tls13-count-tickets.py"),
76    Test("test-tls13-empty-alert.py"),
77    Test("test-tls13-finished-plaintext.py"),
78    Test("test-tls13-hrr.py"),
79    Test("test-tls13-legacy-version.py"),
80    Test("test-tls13-nociphers.py"),
81    Test("test-tls13-record-padding.py"),
82
83    # The skipped tests fail due to a bug in BIO_gets() which masks the retry
84    # signalled from an SSL_read() failure. Testing with httpd(8) shows we're
85    # handling these corner cases correctly since tls13_record_layer.c -r1.47.
86    Test("test-tls13-zero-length-data.py", [
87        "-e", "zero-length app data",
88        "-e", "zero-length app data with large padding",
89        "-e", "zero-length app data with padding",
90    ]),
91])
92
93# Tests that take a lot of time (> ~30s on an x280)
94tls13_slow_tests = TestGroup("slow TLSv1.3 tests", [
95    # XXX: Investigate the occasional message
96    # "Got shared secret with 1 most significant bytes equal to zero."
97    Test("test-tls13-dhe-shared-secret-padding.py", tls13_unsupported_ciphers),
98
99    Test("test-tls13-invalid-ciphers.py"),
100    Test("test-tls13-serverhello-random.py", tls13_unsupported_ciphers),
101])
102
103tls13_extra_cert_tests = TestGroup("TLSv1.3 certificate tests", [
104    # need to set up client certs to run these
105    Test("test-tls13-certificate-request.py"),
106    Test("test-tls13-certificate-verify.py"),
107    Test("test-tls13-ecdsa-in-certificate-verify.py"),
108
109    # Test expects the server to have installed three certificates:
110    # with P-256, P-384 and P-521 curve. Also SHA1+ECDSA is verified
111    # to not work.
112    Test("test-tls13-ecdsa-support.py"),
113])
114
115tls13_failing_tests = TestGroup("failing TLSv1.3 tests", [
116    # Some tests fail because we fail later than the scripts expect us to.
117    # With X25519, we accept weak peer public keys and fail when we actually
118    # compute the keyshare.  Other tests seem to indicate that we could be
119    # stricter about what keyshares we accept.
120    Test("test-tls13-crfg-curves.py"),
121    Test("test-tls13-ecdhe-curves.py"),
122
123    # Expects a missing_extensions alert
124    # AssertionError: Unexpected message from peer: Handshake(server_hello)
125    Test("test-tls13-keyshare-omitted.py"),
126
127    # https://github.com/openssl/openssl/issues/8369
128    Test("test-tls13-obsolete-curves.py"),
129
130    # 3 failing rsa_pss_pss tests
131    Test("test-tls13-rsa-signatures.py"),
132
133    # AssertionError: Unexpected message from peer: ChangeCipherSpec()
134    # Most failing tests expect the CCS right before finished.
135    # What's up with that?
136    Test("test-tls13-version-negotiation.py"),
137])
138
139tls13_slow_failing_tests = TestGroup("slow, failing TLSv1.3 tests", [
140    # After adding record overflow alert, 14 tests fail because
141    # they expect ExpectNewSessionTicket().
142    Test("test-tls13-record-layer-limits.py" ),
143
144    # Other test failures bugs in keyshare/tlsext negotiation?
145    Test("test-tls13-shuffled-extentions.py"),    # should reject 2nd CH
146    Test("test-tls13-unrecognised-groups.py"),    # unexpected closure
147
148    # 5 failures:
149    #   'app data split, conversation with KeyUpdate msg'
150    #   'fragmented keyupdate msg'
151    #   'multiple KeyUpdate messages'
152    #   'post-handshake KeyUpdate msg with update_not_request'
153    #   'post-handshake KeyUpdate msg with update_request'
154    Test("test-tls13-keyupdate.py"),
155
156    Test("test-tls13-symetric-ciphers.py"),       # unexpected message from peer
157
158    # 70 fail and 644 pass. For some reason the tests expect a decode_error
159    # but we send a decrypt_error after the CBS_mem_equal() fails in
160    # tls13_server_finished_recv() (which is correct).
161    Test("test-tls13-finished.py"),               # decrypt_error -> decode_error?
162
163    # The following two tests fail Test (skip empty extensions for the first one):
164    # 'empty unassigned extensions, ids in range from 2 to 4118'
165    # 'unassigned extensions with random payload, ids in range from 2 to 1046'
166    Test("test-tls13-large-number-of-extensions.py"), # 2 fail: empty/random payload
167
168    # 6 tests fail: 'rsa_pkcs1_{md5,sha{1,224,256,384,512}} signature'
169    # We send server hello, but the test expects handshake_failure
170    Test("test-tls13-pkcs-signature.py"),
171    # 8 tests fail: 'tls13 signature rsa_pss_{pss,rsae}_sha{256,384,512}
172    Test("test-tls13-rsapss-signatures.py"),
173
174    # ExpectNewSessionTicket
175    Test("test-tls13-session-resumption.py"),
176])
177
178tls13_unsupported_tests = TestGroup("TLSv1.3 tests for unsupported features", [
179    # Tests for features we don't support
180    Test("test-tls13-0rtt-garbage.py"),
181    Test("test-tls13-ffdhe-groups.py"),
182    Test("test-tls13-ffdhe-sanity.py"),
183    Test("test-tls13-psk_dhe_ke.py"),
184    Test("test-tls13-psk_ke.py"),
185
186    # need server to react to HTTP GET for /keyupdate
187    Test("test-tls13-keyupdate-from-server.py"),
188
189    # Weird test: tests servers that don't support 1.3
190    Test("test-tls13-non-support.py"),
191
192    # broken test script
193    # UnboundLocalError: local variable 'cert' referenced before assignment
194    Test("test-tls13-post-handshake-auth.py"),
195
196    # Server must be configured to support only rsa_pss_rsae_sha512
197    Test("test-tls13-signature-algorithms.py"),
198])
199
200tls12_exclude_legacy_protocols = [
201    # all these have BIO_read timeouts against TLSv1.3
202    "-e", "Protocol (3, 0)",
203    "-e", "Protocol (3, 0) in SSLv2 compatible ClientHello",
204    # the following only fail with TLSv1.3
205    "-e", "Protocol (3, 1) in SSLv2 compatible ClientHello",
206    "-e", "Protocol (3, 2) in SSLv2 compatible ClientHello",
207    "-e", "Protocol (3, 3) in SSLv2 compatible ClientHello",
208    "-e", "Protocol (3, 1) with secp521r1 group",   # XXX
209    "-e", "Protocol (3, 1) with x448 group",
210    "-e", "Protocol (3, 2) with secp521r1 group",   # XXX
211    "-e", "Protocol (3, 2) with x448 group",
212    "-e", "Protocol (3, 3) with secp521r1 group",   # XXX
213    "-e", "Protocol (3, 3) with x448 group",
214]
215
216tls12_tests = TestGroup("TLSv1.2 tests", [
217    # Tests that pass as they are.
218    Test("test-TLSv1_2-rejected-without-TLSv1_2.py"),
219    Test("test-aes-gcm-nonces.py"),
220    Test("test-chacha20.py"),
221    Test("test-conversation.py"),
222    Test("test-cve-2016-2107.py"),
223    Test("test-dhe-rsa-key-exchange.py"),
224    Test("test-dhe-rsa-key-exchange-with-bad-messages.py"),
225    Test("test-early-application-data.py"),
226    Test("test-empty-extensions.py"),
227    Test("test-fuzzed-MAC.py"),
228    Test("test-fuzzed-ciphertext.py"),
229    Test("test-fuzzed-finished.py"),
230    Test("test-fuzzed-padding.py"),
231    Test("test-hello-request-by-client.py"),
232    Test("test-invalid-cipher-suites.py"),
233    Test("test-invalid-content-type.py"),
234    Test("test-invalid-session-id.py"),
235    Test("test-invalid-version.py"),
236    Test("test-message-skipping.py"),
237    Test("test-no-heartbeat.py"),
238    Test("test-sessionID-resumption.py"),
239    Test("test-sslv2-connection.py"),
240    Test("test-truncating-of-finished.py"),
241    Test("test-truncating-of-kRSA-client-key-exchange.py"),
242    Test("test-unsupported-curve-fallback.py"),
243    Test("test-version-numbers.py"),
244    Test("test-zero-length-data.py"),
245
246    # Tests that need tweaking for unsupported features and ciphers.
247    Test(
248        "test-atypical-padding.py", [
249            "-e", "sanity - encrypt then MAC",
250            "-e", "2^14 bytes of AppData with 256 bytes of padding (SHA1 + Encrypt then MAC)",
251        ]
252    ),
253    Test(
254        "test-dhe-rsa-key-exchange-signatures.py", [
255            "-e", "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA sha224 signature",
256            "-e", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 sha224 signature",
257            "-e", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA sha224 signature",
258            "-e", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 sha224 signature",
259            "-e", "TLS_DHE_RSA_WITH_AES_256_CBC_SHA sha224 signature",
260        ]
261    ),
262    Test("test-dhe-key-share-random.py", tls12_exclude_legacy_protocols),
263    Test("test-export-ciphers-rejected.py", ["--min-ver", "TLSv1.0"]),
264    Test(
265        "test-downgrade-protection.py",
266        tls12_args = ["--server-max-protocol", "TLSv1.2"],
267        tls13_args = ["--server-max-protocol", "TLSv1.3"],
268    ),
269    Test("test-fallback-scsv.py", tls13_args = ["--tls-1.3"] ),
270    Test("test-serverhello-random.py", args = tls12_exclude_legacy_protocols),
271])
272
273tls12_slow_tests = TestGroup("slow TLSv1.2 tests", [
274    Test("test-cve-2016-7054.py"),
275    Test("test-dhe-no-shared-secret-padding.py", tls12_exclude_legacy_protocols),
276    Test("test-ecdhe-padded-shared-secret.py", tls12_exclude_legacy_protocols),
277    Test("test-ecdhe-rsa-key-share-random.py", tls12_exclude_legacy_protocols),
278    # This test has some failures once in a while.
279    Test("test-fuzzed-plaintext.py"),
280])
281
282tls12_failing_tests = TestGroup("failing TLSv1.2 tests", [
283    # no shared cipher
284    Test("test-aesccm.py"),
285    # need server to set up alpn
286    Test("test-alpn-negotiation.py"),
287    # many tests fail due to unexpected server_name extension
288    Test("test-bleichenbacher-workaround.py"),
289
290    # need client key and cert plus extra server setup
291    Test("test-certificate-malformed.py"),
292    Test("test-certificate-request.py"),
293    Test("test-certificate-verify-malformed-sig.py"),
294    Test("test-certificate-verify-malformed.py"),
295    Test("test-certificate-verify.py"),
296    Test("test-ecdsa-in-certificate-verify.py"),
297    Test("test-renegotiation-disabled-client-cert.py"),
298    Test("test-rsa-pss-sigs-on-certificate-verify.py"),
299    Test("test-rsa-sigs-on-certificate-verify.py"),
300
301    # test doesn't expect session ticket
302    Test("test-client-compatibility.py"),
303    # abrupt closure
304    Test("test-client-hello-max-size.py"),
305    # unknown signature algorithms
306    Test("test-clienthello-md5.py"),
307    # abrupt closure
308    Test("test-cve-2016-6309.py"),
309
310    # Tests expect an illegal_parameter alert
311    Test("test-ecdhe-rsa-key-exchange-with-bad-messages.py"),
312
313    # We send a handshake_failure while the test expects to succeed
314    Test("test-ecdhe-rsa-key-exchange.py"),
315
316    # unsupported?
317    Test("test-extended-master-secret-extension-with-client-cert.py"),
318
319    # no shared cipher
320    Test("test-ecdsa-sig-flexibility.py"),
321
322    # unsupported
323    Test("test-encrypt-then-mac-renegotiation.py"),
324    Test("test-encrypt-then-mac.py"),
325    Test("test-extended-master-secret-extension.py"),
326    Test("test-ffdhe-negotiation.py"),
327    # unsupported. Expects the server to send the heartbeat extension
328    Test("test-heartbeat.py"),
329
330    # 29 succeed, 263 fail:
331    #   'n extensions', 'n extensions last empty' n in 4086, 4096, 8192, 16383
332    #   'fuzz ext length to n' n in [0..255] with the exception of 41...
333    Test("test-extensions.py"),
334
335    # Tests expect SH but we send unexpected_message or handshake_failure
336    #   'Application data inside Client Hello'
337    #   'Application data inside Client Key Exchange'
338    #   'Application data inside Finished'
339    Test("test-interleaved-application-data-and-fragmented-handshakes-in-renegotiation.py"),
340    # Tests expect SH but we send handshake_failure
341    #   'Application data before Change Cipher Spec'
342    #   'Application data before Client Key Exchange'
343    #   'Application data before Finished'
344    Test("test-interleaved-application-data-in-renegotiation.py"),
345
346    # broken test script
347    # TypeError: '<' not supported between instances of 'int' and 'NoneType'
348    Test("test-invalid-client-hello-w-record-overflow.py"),
349
350    # Lots of failures. abrupt closure
351    Test("test-invalid-client-hello.py"),
352
353    # Test expects illegal_parameter, we send decode_error in ssl_srvr.c:1016
354    # Need to check that this is correct.
355    Test("test-invalid-compression-methods.py"),
356
357    # abrupt closure
358    # 'encrypted premaster set to all zero (n)' n in 256 384 512
359    Test("test-invalid-rsa-key-exchange-messages.py"),
360
361    # test expects illegal_parameter, we send unrecognized_name (which seems
362    # correct according to rfc 6066?)
363    Test("test-invalid-server-name-extension-resumption.py"),
364    # let through some server names without sending an alert
365    # again illegal_parameter vs unrecognized_name
366    Test("test-invalid-server-name-extension.py"),
367
368    Test("test-large-hello.py"),
369
370    # 14 pass
371    # 7 fail
372    # 'n extensions', n in 4095, 4096, 4097, 8191, 8192, 8193, 16383,
373    Test("test-large-number-of-extensions.py"),
374
375    # 4 failures:
376    #   'insecure (legacy) renegotiation with GET after 2nd handshake'
377    #   'insecure (legacy) renegotiation with incomplete GET'
378    #   'secure renegotiation with GET after 2nd handshake'
379    #   'secure renegotiation with incomplete GET'
380    Test("test-legacy-renegotiation.py"),
381
382    # 1 failure (timeout): we don't send the unexpected_message alert
383    # 'duplicate change cipher spec after Finished'
384    Test("test-message-duplication.py"),
385
386    # server should send status_request
387    Test("test-ocsp-stapling.py"),
388
389    # unexpected closure
390    Test("test-openssl-3712.py"),
391
392    # 3 failures:
393    # 'big, needs fragmentation: max fragment - 16336B extension'
394    # 'big, needs fragmentation: max fragment - 32768B extension'
395    # 'maximum size: max fragment - 65531B extension'
396    Test("test-record-layer-fragmentation.py"),
397
398    # wants --reply-AD-size
399    Test("test-record-size-limit.py"),
400
401    # failed: 3 (expect an alert, we send AD)
402    # 'try insecure (legacy) renegotiation with incomplete GET'
403    # 'try secure renegotiation with GET after 2nd CH'
404    # 'try secure renegotiation with incomplete GET'
405    Test("test-renegotiation-disabled.py"),
406
407    # 'resumption of safe session with NULL cipher'
408    # 'resumption with cipher from old CH but not selected by server'
409    Test("test-resumption-with-wrong-ciphers.py"),
410
411    # 5 failures:
412    #   'empty sigalgs'
413    #   'only undefined sigalgs'
414    #   'rsa_pss_pss_sha256 only'
415    #   'rsa_pss_pss_sha384 only'
416    #   'rsa_pss_pss_sha512 only'
417    Test("test-sig-algs.py"),
418
419    # 13 failures:
420    #   'duplicated n non-rsa schemes' for n in 202 2342 8119 23741 32744
421    #   'empty list of signature methods'
422    #   'tolerance n RSA or ECDSA methods' for n in 215 2355 8132 23754
423    #   'tolerance 32758 methods with sig_alg_cert'
424    #   'tolerance max 32744 number of methods with sig_alg_cert'
425    #   'tolerance max (32760) number of methods'
426    Test("test-signature-algorithms.py"),
427
428    # times out
429    Test("test-ssl-death-alert.py"),
430
431    # 17 pass, 13 fail. padding and truncation
432    Test("test-truncating-of-client-hello.py"),
433
434    # x448 tests need disabling plus x25519 corner cases need sorting out
435    Test("test-x25519.py"),
436])
437
438tls12_unsupported_tests = TestGroup("TLSv1.2 for unsupported features", [
439    # protocol_version
440    Test("test-SSLv3-padding.py"),
441])
442
443# These tests take a ton of time to fail against an 1.3 server,
444# so don't run them against 1.3 pending further investigation.
445legacy_tests = TestGroup("Legacy protocol tests", [
446    Test("test-sslv2-force-cipher-3des.py"),
447    Test("test-sslv2-force-cipher-non3des.py"),
448    Test("test-sslv2-force-cipher.py"),
449    Test("test-sslv2-force-export-cipher.py"),
450    Test("test-sslv2hello-protocol.py"),
451])
452
453all_groups = [
454    tls13_tests,
455    tls13_slow_tests,
456    tls13_extra_cert_tests,
457    tls13_failing_tests,
458    tls13_slow_failing_tests,
459    tls13_unsupported_tests,
460    tls12_tests,
461    tls12_slow_tests,
462    tls12_failing_tests,
463    tls12_unsupported_tests,
464    legacy_tests,
465]
466
467failing_groups = [
468    tls13_failing_tests,
469    tls13_slow_failing_tests,
470    tls12_failing_tests,
471]
472
473class TestRunner:
474    """ Runs the given tests troups against a server and displays stats. """
475
476    def __init__(
477        self, timing=False, verbose=False, port=4433, use_tls1_3=True,
478        dry_run=False, tests=[], scriptdir=tlsfuzzer_scriptdir,
479    ):
480        self.tests = []
481
482        self.dryrun = dry_run
483        self.use_tls1_3 = use_tls1_3
484        self.port = str(port)
485        self.scriptdir = scriptdir
486
487        self.stats = []
488        self.failed = []
489
490        self.timing = timing
491        self.verbose = verbose
492
493    def add(self, title="tests", tests=[]):
494        # tests.sort(key=lambda test: test.name)
495        self.tests.append(TestGroup(title, tests))
496
497    def add_group(self, group):
498        self.tests.append(group)
499
500    def run_script(self, test):
501        script = test.name
502        args = ["-p"] + [self.port] + test.args(self.use_tls1_3)
503
504        if self.dryrun:
505            if not self.verbose:
506                args = []
507            print(script , end=' ' if args else '')
508            print(' '.join([f"\"{arg}\"" for arg in args]))
509            return
510
511        if self.verbose:
512            print(script)
513        else:
514            print(f"{script[:68]:<72}", end=" ", flush=True)
515        start = timer()
516        test = subprocess.run(
517            ["python3", os.path.join(self.scriptdir, script)] + args,
518            capture_output=not self.verbose,
519            text=True,
520        )
521        end = timer()
522        self.stats.append((script, end - start))
523        if test.returncode == 0:
524            print("OK")
525            return
526        print("FAILED")
527        self.failed.append(script)
528
529        if self.verbose:
530            return
531
532        print('\n'.join(test.stdout.split("Test end\n", 1)[1:]), end="")
533
534    def run(self):
535        for group in self:
536            print(f"Running {group.title} ...")
537            for test in group:
538                self.run_script(test)
539        return not self.failed
540
541    def __iter__(self):
542        return iter(self.tests)
543
544    def __del__(self):
545        if self.timing and self.stats:
546            total = 0.0
547            for (script, time) in self.stats:
548                print(f"{round(time, 2):6.2f} {script}")
549                total += time
550            print(f"{round(total, 2):6.2f} total")
551
552        if self.failed:
553            print("Failed tests:")
554            print('\n'.join(self.failed))
555
556class TlsServer:
557    """ Spawns an s_server listening on localhost:port if necessary. """
558
559    def __init__(self, port=4433):
560        self.spawn = True
561        # Check whether a server is already listening on localhost:port
562        self.spawn = subprocess.run(
563            ["nc", "-c", "-z", "-T", "noverify", "localhost", str(port)],
564            stderr=subprocess.DEVNULL,
565        ).returncode != 0
566
567        if self.spawn:
568            self.server = subprocess.Popen(
569                [
570                    "openssl",
571                    "s_server",
572                    "-accept",
573                    str(port),
574                    "-key",
575                    "localhost.key",
576                    "-cert",
577                    "localhost.crt",
578                    "-www",
579                ],
580                stdout=subprocess.DEVNULL,
581                stderr=subprocess.PIPE,
582                text=True,
583            )
584
585        # Check whether the server talks TLSv1.3
586        self.has_tls1_3 = True or subprocess.run(
587            [
588                "nc",
589                "-c",
590                "-z",
591                "-T",
592                "noverify",
593                "-T",
594                "protocols=TLSv1.3",
595                "localhost",
596                str(port),
597            ],
598            stderr=subprocess.DEVNULL,
599        ).returncode == 0
600
601        self.check()
602
603    def check(self):
604        if self.spawn and self.server.poll() is not None:
605            print(self.server.stderr.read())
606            raise RuntimeError(
607                f"openssl s_server died. Return code: {self.server.returncode}."
608            )
609        if self.spawn:
610            self.server.stderr.detach()
611
612    def __del__(self):
613        if self.spawn:
614            self.server.terminate()
615
616# Extract the arguments we pass to script
617def defaultargs(script, has_tls1_3):
618    return next(
619        (test for group in all_groups for test in group if test.name == script),
620        Test()
621    ).args(has_tls1_3)
622
623def list_or_missing(missing=True):
624    tests = [test.name for group in all_groups for test in group]
625
626    if missing:
627        scripts = {
628            f for f in os.listdir(tlsfuzzer_scriptdir) if f != "__pycache__"
629        }
630        missing = scripts - set(tests)
631        if missing:
632            print('\n'.join(sorted(missing)))
633        exit(0)
634
635    tests.sort()
636    print('\n'.join(tests))
637    exit(0)
638
639def usage():
640    print("Usage: python3 tlsfuzzer.py [-lmnstv] [-p port] [script [test...]]")
641    print(" --help      help")
642    print(" -f          run failing tests")
643    print(" -l          list tests")
644    print(" -m          list new tests after package update")
645    print(" -n          do not run tests, but list the ones that would be run")
646    print(" -p port     connect to this port - defaults to 4433")
647    print(" -s          run slow tests")
648    print(" -t          show timing stats at end")
649    print(" -v          verbose output")
650    exit(0)
651
652def main():
653    failing = False
654    list = False
655    missing = False
656    dryrun = False
657    port = 4433
658    slow = False
659    timing = False
660    verbose = False
661
662    argv = sys.argv[1:]
663    opts, args = getopt.getopt(argv, "flmnp:stv", ["help"])
664    for opt, arg in opts:
665        if opt == '--help':
666            usage()
667        elif opt == '-f':
668            failing = True
669        elif opt == '-l':
670            list = True
671        elif opt == '-m':
672            missing = True
673        elif opt == '-n':
674            dryrun = True
675        elif opt == '-p':
676            port = int(arg)
677        elif opt == '-s':
678            slow = True
679        elif opt == '-t':
680            timing = True
681        elif opt == '-v':
682            verbose = True
683        else:
684            raise ValueError(f"Unknown option: {opt}")
685
686    if not os.path.exists(tlsfuzzer_scriptdir):
687        print("package py3-tlsfuzzer is required for this regress")
688        exit(1)
689
690    if list and failing:
691        failing = [test.name for group in failing_groups for test in group]
692        failing.sort()
693        print('\n'.join(failing))
694        exit(0)
695
696    if list or missing:
697        list_or_missing(missing)
698
699    tls_server = TlsServer(port)
700
701    tests = TestRunner(timing, verbose, port, tls_server.has_tls1_3, dryrun)
702
703    if args:
704        (dir, script) = os.path.split(args[0])
705        if dir and not dir == '.':
706            tests.scriptdir = dir
707
708        testargs = defaultargs(script, tls_server.has_tls1_3)
709
710        tests.verbose = True
711        tests.add("test from command line", [Test(script, testargs + args[1:])])
712
713        exit(not tests.run())
714
715    if failing:
716        if tls_server.has_tls1_3:
717            tests.add_group(tls13_failing_tests)
718            if slow:
719                tests.add_group(tls13_slow_failing_tests)
720        tests.add_group(tls12_failing_tests)
721
722    if tls_server.has_tls1_3:
723        tests.add_group(tls13_tests)
724        if slow:
725            tests.add_group(tls13_slow_tests)
726    else:
727        tests.add_group(legacy_tests)
728
729    tests.add_group(tls12_tests)
730    if slow:
731        tests.add_group(tls12_slow_tests)
732
733    success = tests.run()
734    del tests
735
736    if not success:
737        print("FAILED")
738        exit(1)
739
740if __name__ == "__main__":
741    main()
742