def bad_reader(): with pytest.raises(ValueError): yield from iofree.read_more(-1) with pytest.raises(ValueError): yield from iofree.peek(-1) with pytest.raises(ValueError): yield from iofree.read_int(-1) yield from iofree.wait() yield "bad"
def http_response(): response = yield from HTTPResponse assert response.ver == b"HTTP/1.1" assert response.code == b"200" assert response.status == b"OK" yield from iofree.read_until(b"\n", return_tail=True) data = yield from iofree.read(4) assert data == b"haha" (number, ) = yield from iofree.read_struct("!H") assert number == 8 * 256 + 8 number = yield from iofree.read_int(3) assert number == int.from_bytes(b"\x11\x11\x11", "big") assert (yield from iofree.peek(2)) == b"co" assert (yield from iofree.read(7)) == b"content" yield from iofree.wait() assert len((yield from iofree.read_more(5))) >= 5 yield from iofree.read() yield from iofree.wait_event() return b"\r\n".join(response.header_lines)
def tls1_2_response(plugin): tls_version = plugin.tls_version with memoryview((yield from iofree.read(5))) as tls_plaintext_head: assert (tls_plaintext_head[:3] == b"\x16\x03\x03" ), "invalid tls head: handshake(22) protocol_version(3.1)" length = int.from_bytes(tls_plaintext_head[-2:], "big") assert length == length & 0x3FFF, f"{length} is over 2^14" with memoryview((yield from iofree.read(length))) as fragment: assert fragment[ 0] == 2, f"expect server_hello(2), bug got: {fragment[0]}" handshake_length = int.from_bytes(fragment[1:4], "big") server_hello = fragment[4:handshake_length + 4] assert server_hello[:2] == tls_version, "expect: server_version(3.3)" verify_id = server_hello[2:34] sha1 = hmac.new( plugin.client.ns.cipher.master_key + plugin.session_id, verify_id[:-10], hashlib.sha1, ).digest()[:10] assert sha1 == verify_id[-10:], "hmac verify failed" assert server_hello[34] == 32, f"expect 32, but got {server_hello[34]}" # verify_id = server_hello[35:67] # sha1 = hmac.new( # plugin.client.ns.cipher.master_key + plugin.session_id, # fragment[:-10], # hashlib.sha1, # ).digest()[:10] # assert sha1 == fragment[-10:], "hmac verify failed" while True: x = yield from iofree.peek(1) if x[0] != 22: break with memoryview((yield from iofree.read(5))) as ticket_head: length = int.from_bytes(ticket_head[-2:], "big") assert length == length & 0x3FFF, f"{length} is over 2^14" yield from iofree.read(length) yield from ChangeCipherReader(plugin, plugin.client.ns.cipher.master_key, plugin.session_id) yield from application_data(plugin)