def test_buffer_read(hack, app, basic_name_type_pairs): buffer = gh.Buffer(hack, app.sizes.Program) for name, type_name in basic_name_type_pairs: for addr in app.addr.roots: buffer.read_from(addr) assert_basic_value(app, buffer, 0, name, type_name)
def test_buffer_read_string(hack, app): for addr in app.addr.roots: buffer = gh.Buffer(hack, 32) buffer.read_from(addr + app.offsets.Basic.str, buffer.size) assert buffer.strlen(0) == len("TestStr") assert buffer.read_string(0, len("TestStr")) == "TestStr" assert buffer.read_bytes(0, len("TestStr")) == b"TestStr"
def test_buffer_create_incorrect(): hack = gh.Hack() buf1 = gh.Buffer(hack, 32) # Buffer with size=0 with pytest.raises(RuntimeError): buf2 = gh.Buffer(hack, 0) # View with size=0 with pytest.raises(RuntimeError): buf2 = gh.Buffer(buf1, 16, 0) # View that exceeds memory range of parent with pytest.raises(RuntimeError): buf2 = gh.Buffer(buf1, 32, 16) with pytest.raises(RuntimeError): buf2 = gh.Buffer(buf1, 16, 32)
def test_buffer_write_string(hack, app, reset_app): for addr in app.addr.roots: buffer = gh.Buffer(hack, 32) buffer.read_from(addr + app.offsets.Basic.str, buffer.size) buffer.write_string(0, "StrTest") assert buffer.read_string(0, len("StrTest")) == "StrTest" with pytest.raises(RuntimeError): buffer.write_string(0, "0" * 100)
def test_buffer_create(): hack = gh.Hack() # Owning buf1 = gh.Buffer(hack, 32) assert 32 == buf1.size assert 0 == buf1.read_u32(16) buf1.write_u32(16, 10) assert 10 == buf1.read_u32(16) # View buf2 = gh.Buffer(buf1, 16, 16) assert 16 == buf2.size assert 10 == buf2.read_u32(0) buf2.write_u32(0, 4) assert 4 == buf1.read_u32(16) assert 4 == buf2.read_u32(0) # Clear buf1.clear() assert 0 == buf1.read_u32(16) assert 0 == buf2.read_u32(0)