@@ -416,6 +416,7 @@ import tempfile
416416= Test DoIPSocket
417417
418418server_up = threading.Event()
419+ sniff_up = threading.Event()
419420def server():
420421 buffer = b'\x02\xfd\x80\x02\x00\x00\x00\x05\x00\x00\x00\x00\x00\x02\xfd\x80\x01\x00\x00\x00\n\x10\x10\x0e\x80P\x03\x002\x01\xf4'
421422 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -426,6 +427,7 @@ def server():
426427 sock.listen(1)
427428 server_up.set()
428429 connection, address = sock.accept()
430+ sniff_up.wait(timeout=1)
429431 connection.send(buffer)
430432 connection.close()
431433 finally:
@@ -437,7 +439,7 @@ server_thread.start()
437439server_up.wait(timeout=1)
438440sock = DoIPSocket(activate_routing=False)
439441
440- pkts = sock.sniff(timeout=1, count=2)
442+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
441443server_thread.join(timeout=1)
442444assert len(pkts) == 2
443445
@@ -446,6 +448,7 @@ assert len(pkts) == 2
446448~ linux
447449
448450server_up = threading.Event()
451+ sniff_up = threading.Event()
449452def server():
450453 buffer = b'\x02\xfd\x80\x02\x00\x00\x00\x05\x00\x00\x00\x00\x00\x02\xfd\x80\x01\x00\x00\x00\n\x10\x10\x0e\x80P\x03\x002\x01\xf4'
451454 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -456,6 +459,7 @@ def server():
456459 sock.listen(1)
457460 server_up.set()
458461 connection, address = sock.accept()
462+ sniff_up.wait(timeout=1)
459463 for i in range(len(buffer)):
460464 connection.send(buffer[i:i+1])
461465 time.sleep(0.01)
@@ -469,13 +473,14 @@ server_thread.start()
469473server_up.wait(timeout=1)
470474sock = DoIPSocket(activate_routing=False)
471475
472- pkts = sock.sniff(timeout=1, count=2)
476+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
473477server_thread.join(timeout=1)
474478assert len(pkts) == 2
475479
476480= Test DoIPSocket 3
477481
478482server_up = threading.Event()
483+ sniff_up = threading.Event()
479484def server():
480485 buffer = b'\x02\xfd\x80\x02\x00\x00\x00\x05\x00\x00\x00\x00\x00\x02\xfd\x80\x01\x00\x00\x00\n\x10\x10\x0e\x80P\x03\x002\x01\xf4'
481486 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -486,6 +491,7 @@ def server():
486491 sock.listen(1)
487492 server_up.set()
488493 connection, address = sock.accept()
494+ sniff_up.wait(timeout=1)
489495 while buffer:
490496 randlen = random.randint(0, len(buffer))
491497 connection.send(buffer[:randlen])
@@ -501,14 +507,15 @@ server_thread.start()
501507server_up.wait(timeout=1)
502508sock = DoIPSocket(activate_routing=False)
503509
504- pkts = sock.sniff(timeout=1, count=2)
510+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
505511server_thread.join(timeout=1)
506512assert len(pkts) == 2
507513
508514
509515= Test DoIPSocket6
510516
511517server_up = threading.Event()
518+ sniff_up = threading.Event()
512519def server():
513520 buffer = b'\x02\xfd\x80\x02\x00\x00\x00\x05\x00\x00\x00\x00\x00\x02\xfd\x80\x01\x00\x00\x00\n\x10\x10\x0e\x80P\x03\x002\x01\xf4'
514521 sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
@@ -519,6 +526,7 @@ def server():
519526 sock.listen(1)
520527 server_up.set()
521528 connection, address = sock.accept()
529+ sniff_up.wait(timeout=1)
522530 connection.send(buffer)
523531 connection.close()
524532 finally:
@@ -530,7 +538,7 @@ server_thread.start()
530538server_up.wait(timeout=1)
531539sock = DoIPSocket(ip="::1", activate_routing=False)
532540
533- pkts = sock.sniff(timeout=1, count=2)
541+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
534542server_thread.join(timeout=1)
535543assert len(pkts) == 2
536544
@@ -604,6 +612,7 @@ def _load_certificate_chain(context) -> None:
604612
605613
606614server_up = threading.Event()
615+ sniff_up = threading.Event()
607616def server():
608617 context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
609618 _load_certificate_chain(context)
@@ -619,6 +628,7 @@ def server():
619628 ssock.listen(1)
620629 server_up.set()
621630 connection, address = ssock.accept()
631+ sniff_up.wait(timeout=1)
622632 connection.send(buffer)
623633 connection.close()
624634 finally:
@@ -633,14 +643,15 @@ context.check_hostname = False
633643context.verify_mode = ssl.CERT_NONE
634644sock = DoIPSocket(activate_routing=False, force_tls=True, context=context)
635645
636- pkts = sock.sniff(timeout=1, count=2)
646+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
637647server_thread.join(timeout=1)
638648assert len(pkts) == 2
639649
640650= Test DoIPSslSocket6
641651~ broken_windows
642652
643653server_up = threading.Event()
654+ sniff_up = threading.Event()
644655def server():
645656 context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
646657 _load_certificate_chain(context)
@@ -656,6 +667,7 @@ def server():
656667 ssock.listen(1)
657668 server_up.set()
658669 connection, address = ssock.accept()
670+ sniff_up.wait(timeout=1)
659671 connection.send(buffer)
660672 connection.close()
661673 finally:
@@ -670,14 +682,15 @@ context.check_hostname = False
670682context.verify_mode = ssl.CERT_NONE
671683sock = DoIPSocket(ip="::1", activate_routing=False, force_tls=True, context=context)
672684
673- pkts = sock.sniff(timeout=1, count=2)
685+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
674686server_thread.join(timeout=1)
675687assert len(pkts) == 2
676688
677689= Test UDS_DoIPSslSocket6
678690~ broken_windows
679691
680692server_up = threading.Event()
693+ sniff_up = threading.Event()
681694def server():
682695 context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
683696 _load_certificate_chain(context)
@@ -693,6 +706,7 @@ def server():
693706 ssock.listen(1)
694707 server_up.set()
695708 connection, address = ssock.accept()
709+ sniff_up.wait(timeout=1)
696710 connection.send(buffer)
697711 connection.close()
698712 finally:
@@ -707,15 +721,16 @@ context.check_hostname = False
707721context.verify_mode = ssl.CERT_NONE
708722sock = UDS_DoIPSocket(ip="::1", activate_routing=False, force_tls=True, context=context)
709723
710- pkts = sock.sniff(timeout=1, count=2)
724+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
711725server_thread.join(timeout=1)
712726assert len(pkts) == 2
713727
714728= Test UDS_DualDoIPSslSocket6
715- ~ broken_windows
729+ ~ broken_windows not_pypy
716730
717731server_tcp_up = threading.Event()
718732server_tls_up = threading.Event()
733+ sniff_up = threading.Event()
719734def server_tls():
720735 context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
721736 _load_certificate_chain(context)
@@ -732,6 +747,7 @@ def server_tls():
732747 ssock.listen(1)
733748 server_tls_up.set()
734749 connection, address = ssock.accept()
750+ sniff_up.wait(timeout=1)
735751 connection.send(buffer)
736752 connection.close()
737753 finally:
@@ -748,7 +764,7 @@ def server_tcp():
748764 server_tcp_up.set()
749765 connection, address = sock.accept()
750766 connection.send(buffer)
751- connection.shutdown()
767+ connection.shutdown(socket.SHUT_RDWR )
752768 connection.close()
753769 finally:
754770 sock.close()
@@ -767,7 +783,7 @@ context.verify_mode = ssl.CERT_NONE
767783
768784sock = UDS_DoIPSocket(ip="::1", context=context)
769785
770- pkts = sock.sniff(timeout=1, count=2)
786+ pkts = sock.sniff(timeout=1, count=2, started_callback=sniff_up.set )
771787server_tcp_thread.join(timeout=1)
772788server_tls_thread.join(timeout=1)
773789assert len(pkts) == 2
0 commit comments