okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
1"""The network guard of conftest.py (lane NET): it refuses what is not this computer, passes three named things (a UDP connect, this computer's own host 2name, the RFC 5737 documentation addresses), counts every use of them per test, and nothing else gets through. These tests call the guard's own 3functions with the real connect replaced, so no packet is ever sent; each one that makes the guard refuse clears the guard's list before it ends 4(the autouse fixture would otherwise fail the test for the reach it was checking).""" 5 6from __future__import annotations 7 8import os 9import socket 10import unittest 11from unittest import mock 12 13 14class _Live : 15"""The root conftest.py's own namespace, the live one (several conftest.py files share the module name "conftest", so it is found through the 16function the guard put on socket.socket.connect, not by an import).""" 17 18def __init__ (self ,names :dict ): 19object .__setattr__ (self ,"_names" ,names ) 20 21def __getattr__ (self ,name ): 22try : 23return self ._names [name ] 24except KeyError : 25raise AttributeError (name )from None 26 27def __setattr__ (self ,name ,value ): 28self ._names [name ]= value 29 30def __delattr__ (self ,name ): 31del self ._names [name ] 32 33 34assert socket .socket .connect .__name__ == "_guarded_connect" ,"the network guard of conftest.py is not installed" 35guard = _Live (socket .socket .connect .__globals__ ) 36 37 38def tcp ()-> socket .socket : 39return socket .socket (socket .AF_INET ,socket .SOCK_STREAM ) 40 41 42def udp ()-> socket .socket : 43return socket .socket (socket .AF_INET ,socket .SOCK_DGRAM ) 44 45 46class Guarded (unittest .TestCase ): 47"""The guard is on for these tests whatever the run says: a run with STRATA_TEST_ALLOW_NETWORK=1 (a live run, or a check that sets it) switches it 48off for the others, and these tests are about the guard itself.""" 49 50def setUp (self ): 51p = mock .patch .dict (os .environ ) 52p .start () 53self .addCleanup (p .stop ) 54os .environ .pop ("STRATA_TEST_ALLOW_NETWORK" ,None ) 55 56 57class Refused (Guarded ): 58def tearDown (self ): 59del guard ._reached [:]# (the refusals checked here are not this test's failure) 60 61def test_a_tcp_connect_to_an_address_outside_the_allowances_is_refused (self ): 62for host in ("203.0.114.1" ,"192.0.3.1" ,"198.51.101.1" ,"10.255.255.254" ,"8.8.8.8" ,"93.184.216.34" ): 63with tcp ()as s ,mock .patch .object (guard ,"_real_connect" ,side_effect = AssertionError ("sent" )): 64with self .assertRaises (ConnectionRefusedError ,msg = host ): 65s .connect ((host ,80 )) 66self .assertEqual (len (guard ._reached ),6 ) 67 68def test_a_real_host_name_is_refused (self ): 69for host in ("github.com" ,"huggingface.co" ,"example.com" ,"strata-feed.invalid" ): 70with self .assertRaises (socket .gaierror ,msg = host ): 71socket .getaddrinfo (host ,443 ) 72self .assertEqual (len (guard ._reached ),4 ) 73 74def test_a_name_that_only_looks_like_this_computers_is_refused (self ): 75with self .assertRaises (socket .gaierror ): 76socket .getaddrinfo (guard ._OWN_NAME + ".example.com" ,80 ) 77 78def test_the_failure_says_where_it_came_from (self ): 79with self .assertRaises (socket .gaierror ): 80socket .getaddrinfo ("github.com" ,443 ) 81self .assertRegex (guard ._reached [- 1 ],r"^github\.com \[test_net_guard\.py:\d+\]$" ) 82 83def test_a_tcp_connect_with_the_ipv6_documentation_range_is_not_covered (self ): 84with socket .socket (socket .AF_INET6 ,socket .SOCK_STREAM )as s ,mock .patch .object (guard ,"_real_connect" ,side_effect = AssertionError ("sent" )): 85with self .assertRaises (ConnectionRefusedError ): 86s .connect (("2001:db8::1" ,80 ,0 ,0 ))# (only the three IPv4 ranges are allowed) 87 88 89class Allowed (Guarded ): 90def setUp (self ): 91super ().setUp () 92self .before = len (guard ._allowed ) 93 94def used (self ): 95return guard ._allowed [self .before :] 96 97def test_loopback_is_not_counted_at_all (self ): 98with tcp ()as s ,mock .patch .object (guard ,"_real_connect" ,return_value = None ): 99s .connect (("127.0.0.1" ,9 )) 100socket .getaddrinfo ("localhost" ,80 ) 101self .assertEqual (self .used (), []) 102 103def test_a_udp_connect_passes_and_is_counted (self ): 104with udp ()as s ,mock .patch .object (guard ,"_real_connect" ,return_value = None ): 105s .connect (("10.255.255.255" ,1 )) 106self .assertEqual ([(w ,a )for _t ,w ,a in self .used ()], [("(a) UDP connect" ,"10.255.255.255:1" )]) 107self .assertTrue (self .used ()[0 ][0 ].endswith ("::Allowed::test_a_udp_connect_passes_and_is_counted" ),self .used ()) 108 109def test_this_computers_own_name_passes_and_is_counted (self ): 110with mock .patch .object (guard ,"_real_getaddrinfo" ,return_value = []): 111socket .getaddrinfo (socket .gethostname (),0 ) 112self .assertEqual ([(w ,a .lower ())for _t ,w ,a in self .used ()], [("(b) own host name" ,guard ._OWN_NAME )]) 113 114def test_the_three_documentation_ranges_pass_and_are_counted (self ): 115for host in ("192.0.2.1" ,"198.51.100.7" ,"203.0.113.1" ): 116with tcp ()as s ,mock .patch .object (guard ,"_real_connect" ,return_value = None ): 117s .connect ((host ,8080 )) 118self .assertEqual ([(w ,a )for _t ,w ,a in self .used ()], 119 [("(c) documentation address" ,"192.0.2.1:8080" ), ("(c) documentation address" ,"198.51.100.7:8080" ), 120 ("(c) documentation address" ,"203.0.113.1:8080" )]) 121 122 123class OptOut (Guarded ): 124def test_a_live_test_that_sets_the_variable_is_let_through_and_nothing_is_counted (self ): 125os .environ ["STRATA_TEST_ALLOW_NETWORK" ]= "1" 126before = (len (guard ._reached ),len (guard ._allowed )) 127with tcp ()as s ,mock .patch .object (guard ,"_real_connect" ,return_value = None ): 128s .connect (("93.184.216.34" ,80 )) 129with mock .patch .object (guard ,"_real_getaddrinfo" ,return_value = []): 130socket .getaddrinfo ("github.com" ,443 ) 131self .assertEqual ((len (guard ._reached ),len (guard ._allowed )),before ) 132 133 134if __name__ == "__main__" : 135unittest .main ()