|
16 | 16 | from mbedtls.x509 import CRT, CSR, BasicConstraints |
17 | 17 |
|
18 | 18 |
|
| 19 | +@pytest.fixture(scope="module") |
| 20 | +def rootpath(): |
| 21 | + return Path(__file__).parent.parent |
| 22 | + |
| 23 | + |
19 | 24 | class TestPickle: |
20 | 25 | @pytest.mark.parametrize( |
21 | 26 | "obj", |
@@ -623,73 +628,100 @@ def test_dtls(self): |
623 | 628 |
|
624 | 629 |
|
625 | 630 | @pytest.mark.local |
626 | | -class TestPrograms: |
| 631 | +class TestProgramsTLS: |
627 | 632 | @pytest.fixture |
628 | | - def rootpath(self): |
629 | | - return Path(__file__).parent.parent |
| 633 | + def port(self): |
| 634 | + """Return a free port |
630 | 635 |
|
631 | | - @pytest.fixture |
632 | | - def tls_server(self, rootpath): |
633 | | - proc = subprocess.Popen( |
634 | | - [ |
635 | | - rootpath / "programs" / "server.py", |
636 | | - "--tls", |
637 | | - "--psk-store", |
638 | | - "cli=secret", |
639 | | - ] |
640 | | - ) |
641 | | - yield proc |
642 | | - proc.kill() |
643 | | - proc.wait(1.0) |
| 636 | + Note: |
| 637 | + Not 100% race condition free. |
| 638 | +
|
| 639 | + """ |
| 640 | + port = 0 |
| 641 | + with socket.socket() as sock: |
| 642 | + sock.bind(("", port)) |
| 643 | + port = sock.getsockname()[1] |
| 644 | + return port |
644 | 645 |
|
645 | 646 | @pytest.fixture |
646 | | - def dtls_server(self, rootpath): |
647 | | - proc = subprocess.Popen( |
648 | | - [ |
649 | | - rootpath / "programs" / "server.py", |
650 | | - "--dtls", |
651 | | - "--psk-store", |
652 | | - "cli=secret", |
653 | | - ] |
654 | | - ) |
| 647 | + def server(self, rootpath, port): |
| 648 | + args = [ |
| 649 | + rootpath / "programs" / "server.py", |
| 650 | + "--port", |
| 651 | + f"{port}", |
| 652 | + "--tls", |
| 653 | + "--psk-store", |
| 654 | + "cli=secret", |
| 655 | + ] |
| 656 | + proc = subprocess.Popen(args) |
655 | 657 | yield proc |
656 | 658 | proc.kill() |
657 | 659 | proc.wait(1.0) |
658 | 660 |
|
659 | | - @pytest.mark.usefixtures("tls_server") |
| 661 | + @pytest.mark.usefixtures("server") |
660 | 662 | @pytest.mark.timeout(10) |
661 | | - def test_e2e_tls(self, rootpath): |
| 663 | + def test_communicate(self, rootpath, port): |
662 | 664 | secret = b"a very secret message" |
663 | | - |
| 665 | + args = [ |
| 666 | + rootpath / "programs" / "client.py", |
| 667 | + "--port", |
| 668 | + f"{port}", |
| 669 | + "--tls", |
| 670 | + "--psk", |
| 671 | + "cli=secret", |
| 672 | + secret, |
| 673 | + ] |
664 | 674 | for _ in range(3): |
665 | | - with subprocess.Popen( |
666 | | - [ |
667 | | - rootpath / "programs" / "client.py", |
668 | | - "--tls", |
669 | | - "--psk", |
670 | | - "cli=secret", |
671 | | - secret, |
672 | | - ], |
673 | | - stdout=subprocess.PIPE, |
674 | | - ) as client: |
| 675 | + with subprocess.Popen(args, stdout=subprocess.PIPE) as client: |
675 | 676 | out, err = client.communicate() |
676 | 677 | assert out == secret + b"\n" |
677 | 678 |
|
678 | | - @pytest.mark.usefixtures("dtls_server") |
| 679 | + |
| 680 | +@pytest.mark.local |
| 681 | +class TestProgramsDTLS: |
| 682 | + @pytest.fixture |
| 683 | + def port(self): |
| 684 | + """Return a free port |
| 685 | +
|
| 686 | + Note: |
| 687 | + Not 100% race condition free. |
| 688 | +
|
| 689 | + """ |
| 690 | + port = 0 |
| 691 | + with socket.socket() as sock: |
| 692 | + sock.bind(("", port)) |
| 693 | + port = sock.getsockname()[1] |
| 694 | + return port |
| 695 | + |
| 696 | + @pytest.fixture |
| 697 | + def server(self, rootpath, port): |
| 698 | + args = [ |
| 699 | + rootpath / "programs" / "server.py", |
| 700 | + "--port", |
| 701 | + f"{port}", |
| 702 | + "--dtls", |
| 703 | + "--psk-store", |
| 704 | + "cli=secret", |
| 705 | + ] |
| 706 | + proc = subprocess.Popen(args) |
| 707 | + yield proc |
| 708 | + proc.kill() |
| 709 | + proc.wait(1.0) |
| 710 | + |
| 711 | + @pytest.mark.usefixtures("server") |
679 | 712 | @pytest.mark.timeout(10) |
680 | | - def test_e2e_dtls(self, rootpath): |
| 713 | + def test_communication(self, rootpath, port): |
681 | 714 | secret = b"a very secret message" |
682 | | - |
| 715 | + args = [ |
| 716 | + rootpath / "programs" / "client.py", |
| 717 | + "--port", |
| 718 | + f"{port}", |
| 719 | + "--dtls", |
| 720 | + "--psk", |
| 721 | + "cli=secret", |
| 722 | + secret, |
| 723 | + ] |
683 | 724 | for _ in range(3): |
684 | | - with subprocess.Popen( |
685 | | - [ |
686 | | - rootpath / "programs" / "client.py", |
687 | | - "--dtls", |
688 | | - "--psk", |
689 | | - "cli=secret", |
690 | | - secret, |
691 | | - ], |
692 | | - stdout=subprocess.PIPE, |
693 | | - ) as client: |
| 725 | + with subprocess.Popen(args, stdout=subprocess.PIPE) as client: |
694 | 726 | out, err = client.communicate() |
695 | 727 | assert out == secret + b"\n" |
0 commit comments