def test_ignores_other_transactions(self): sock = patch_socket(self) self.patch_recv(sock, 1) self.patch_offer_packet() other_transaction_id = factory.getRandomBytes(4) self.assertEqual(set(), receive_offers(other_transaction_id))
def test_emits_base64(self): # Piston hooks onto an __emittable__() method, if present. # Bin() returns a base-64 encoded string so that it can be # transmitted in JSON. self.assertEqual(b"", Bin(b"").__emittable__()) example_bytes = factory.getRandomBytes() self.assertEqual(b64encode(example_bytes), Bin(example_bytes).__emittable__())
def patch_offer_packet(self): """Patch a mock `DHCPOfferPacket`.""" transaction_id = factory.getRandomBytes(4) packet = mock.MagicMock() packet.transaction_ID = transaction_id packet.dhcp_server_ID = factory.getRandomIPAddress() self.patch(detect_module, 'DHCPOfferPacket').return_value = packet return packet
def test_emits_base64(self): # Piston hooks onto an __emittable__() method, if present. # Bin() returns a base-64 encoded string so that it can be # transmitted in JSON. self.assertEqual(b"", Bin(b"").__emittable__()) example_bytes = factory.getRandomBytes() self.assertEqual( b64encode(example_bytes), Bin(example_bytes).__emittable__())
def test_propagates_errors_other_than_timeout(self): class InducedError(Exception): """Deliberately induced error for testing.""" sock = patch_socket(self) sock.recv = mock.MagicMock(side_effect=InducedError) self.assertRaises(InducedError, receive_offers, factory.getRandomBytes(4))
def patch_recv(self, sock, num_packets=0): """Patch up socket's `recv` to return `num_packets` arbitrary packets. After that, further calls to `recv` will raise a timeout. """ packets = [factory.getRandomBytes() for _ in range(num_packets)] receiver = FakePacketReceiver(packets) self.patch(sock, 'recv', receiver) return receiver
def test_merge_with_all_invalid_details(self): xml = self.do_merge_details({ "lshw": b"<gibber></ish>", "foom": b"<not>well</formed>", "zoom": b"<>" + factory.getRandomBytes(), "oops": None, }) expected = """\ <list xmlns:foom="foom" xmlns:lshw="lshw" xmlns:oops="oops" xmlns:zoom="zoom"/> """ self.assertThat(xml, EqualsXML(expected)) # The error is logged however. self.assertDocTestMatches( """\ Invalid foom details: ... Invalid lshw details: ... Invalid zoom details: ... """, self.logger.output)
def test_merge_with_all_invalid_details(self): # merge_details() differs from merge_details_cleanly() in that # it first attempts to use the lshw details as the root # # element. If they're invalid the log message is therefore # printed first. xml = self.do_merge_details({ "lshw": b"<gibber></ish>", "foom": b"<not>well</formed>", "zoom": b"<>" + factory.getRandomBytes(), "oops": None, }) expected = """\ <list xmlns:foom="foom" xmlns:lshw="lshw" xmlns:oops="oops" xmlns:zoom="zoom"/> """ self.assertThat(xml, EqualsXML(expected)) # The error is logged however. self.assertDocTestMatches( """\ Invalid lshw details: ... Invalid foom details: ... Invalid zoom details: ... """, self.logger.output)
def test_process_OK_response_with_other_content(self): data = factory.getRandomBytes() response = make_response(httplib.OK, data, "application/octet-stream") self.assertEqual(data, tags.process_response(response))