test_pcmp.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright 2024 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import unittest
  15. try:
  16. from unittest import mock
  17. except ImportError:
  18. import mock
  19. from pebble.pulse2 import pcmp
  20. from .fake_timer import FakeTimer
  21. class TestPCMP(unittest.TestCase):
  22. def setUp(self):
  23. self.uut = pcmp.PulseControlMessageProtocol(mock.Mock(), 1)
  24. def test_close_unregisters_the_socket(self):
  25. self.uut.close()
  26. self.uut.transport.unregister_socket.assert_called_once_with(1)
  27. def test_close_is_idempotent(self):
  28. self.uut.close()
  29. self.uut.close()
  30. self.assertEqual(1, self.uut.transport.unregister_socket.call_count)
  31. def test_send_unknown_code(self):
  32. self.uut.send_unknown_code(42)
  33. self.uut.transport.send.assert_called_once_with(1, b'\x82\x2a')
  34. def test_send_echo_request(self):
  35. self.uut.send_echo_request(b'abcdefg')
  36. self.uut.transport.send.assert_called_once_with(1, b'\x01abcdefg')
  37. def test_send_echo_reply(self):
  38. self.uut.send_echo_reply(b'abcdefg')
  39. self.uut.transport.send.assert_called_once_with(1, b'\x02abcdefg')
  40. def test_on_receive_empty_packet(self):
  41. self.uut.on_receive(b'')
  42. self.uut.transport.send.assert_not_called()
  43. def test_on_receive_message_with_unknown_code(self):
  44. self.uut.on_receive(b'\x00')
  45. self.uut.transport.send.assert_called_once_with(1, b'\x82\x00')
  46. def test_on_receive_malformed_unknown_code_message_1(self):
  47. self.uut.on_receive(b'\x82')
  48. self.uut.transport.send.assert_not_called()
  49. def test_on_receive_malformed_unknown_code_message_2(self):
  50. self.uut.on_receive(b'\x82\x00\x01')
  51. self.uut.transport.send.assert_not_called()
  52. def test_on_receive_discard_request(self):
  53. self.uut.on_receive(b'\x03')
  54. self.uut.transport.send.assert_not_called()
  55. def test_on_receive_discard_request_with_data(self):
  56. self.uut.on_receive(b'\x03asdfasdfasdf')
  57. self.uut.transport.send.assert_not_called()
  58. def test_on_receive_echo_request(self):
  59. self.uut.on_receive(b'\x01')
  60. self.uut.transport.send.assert_called_once_with(1, b'\x02')
  61. def test_on_receive_echo_request_with_data(self):
  62. self.uut.on_receive(b'\x01a')
  63. self.uut.transport.send.assert_called_once_with(1, b'\x02a')
  64. def test_on_receive_echo_reply(self):
  65. self.uut.on_receive(b'\x02')
  66. self.uut.transport.send.assert_not_called()
  67. def test_on_receive_echo_reply_with_data(self):
  68. self.uut.on_receive(b'\x02abc')
  69. self.uut.transport.send.assert_not_called()
  70. def test_on_receive_port_closed_with_no_handler(self):
  71. self.uut.on_receive(b'\x81\xab\xcd')
  72. self.uut.transport.send.assert_not_called()
  73. def test_on_receive_port_closed(self):
  74. self.uut.on_port_closed = mock.Mock()
  75. self.uut.on_receive(b'\x81\xab\xcd')
  76. self.uut.on_port_closed.assert_called_once_with(0xabcd)
  77. def test_on_receive_malformed_port_closed_message_1(self):
  78. self.uut.on_port_closed = mock.Mock()
  79. self.uut.on_receive(b'\x81\xab')
  80. self.uut.on_port_closed.assert_not_called()
  81. def test_on_receive_malformed_port_closed_message_2(self):
  82. self.uut.on_port_closed = mock.Mock()
  83. self.uut.on_receive(b'\x81\xab\xcd\xef')
  84. self.uut.on_port_closed.assert_not_called()
  85. class TestPing(unittest.TestCase):
  86. def setUp(self):
  87. FakeTimer.clear_timer_list()
  88. timer_patcher = mock.patch('threading.Timer', new=FakeTimer)
  89. timer_patcher.start()
  90. self.addCleanup(timer_patcher.stop)
  91. self.uut = pcmp.PulseControlMessageProtocol(mock.Mock(), 1)
  92. def test_successful_ping(self):
  93. cb = mock.Mock()
  94. self.uut.ping(cb)
  95. self.uut.on_receive(b'\x02')
  96. cb.assert_called_once_with(True)
  97. self.assertFalse(FakeTimer.get_active_timers())
  98. def test_ping_succeeds_after_retry(self):
  99. cb = mock.Mock()
  100. self.uut.ping(cb, attempts=2)
  101. FakeTimer.TIMERS[-1].expire()
  102. self.uut.on_receive(b'\x02')
  103. cb.assert_called_once_with(True)
  104. self.assertFalse(FakeTimer.get_active_timers())
  105. def test_ping_succeeds_after_multiple_retries(self):
  106. cb = mock.Mock()
  107. self.uut.ping(cb, attempts=3)
  108. timer1 = FakeTimer.TIMERS[-1]
  109. timer1.expire()
  110. timer2 = FakeTimer.TIMERS[-1]
  111. self.assertIsNot(timer1, timer2)
  112. timer2.expire()
  113. self.uut.on_receive(b'\x02')
  114. cb.assert_called_once_with(True)
  115. self.assertFalse(FakeTimer.get_active_timers())
  116. def test_failed_ping(self):
  117. cb = mock.Mock()
  118. self.uut.ping(cb, attempts=1)
  119. FakeTimer.TIMERS[-1].expire()
  120. cb.assert_called_once_with(False)
  121. self.assertFalse(FakeTimer.get_active_timers())
  122. def test_ping_fails_after_multiple_retries(self):
  123. cb = mock.Mock()
  124. self.uut.ping(cb, attempts=3)
  125. for _ in range(3):
  126. FakeTimer.TIMERS[-1].expire()
  127. cb.assert_called_once_with(False)
  128. self.assertFalse(FakeTimer.get_active_timers())
  129. def test_socket_close_aborts_ping(self):
  130. cb = mock.Mock()
  131. self.uut.ping(cb, attempts=3)
  132. self.uut.close()
  133. cb.assert_not_called()
  134. self.assertFalse(FakeTimer.get_active_timers())