This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_common.py
67 lines (53 loc) · 2.34 KB
/
test_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import streamdal_protos.protos as protos
import streamdal.common as common
from wasmtime import Store, Memory, MemoryType, Limits
class TestCommon:
def test_aud_to_str(self):
aud = protos.Audience(
component_name="kafka",
service_name="testing",
operation_name="test-topic",
operation_type=protos.OperationType.OPERATION_TYPE_PRODUCER,
)
assert common.aud_to_str(aud) == "testing.kafka.2.test-topic"
def test_str_to_aud(self):
aud = protos.Audience(
component_name="kafka",
service_name="testing",
operation_name="test-topic",
operation_type=protos.OperationType.OPERATION_TYPE_PRODUCER,
)
parsed = common.str_to_aud("testing.kafka.2.test-topic")
assert parsed.component_name == aud.component_name
assert parsed.service_name == aud.service_name
assert parsed.operation_name == aud.operation_name
assert parsed.operation_type == aud.operation_type
def test_read_memory_within_bounds(self):
"""Test reading within bounds of memory"""
store = Store()
memory = Memory(store, MemoryType(Limits(1, 1024)))
data = b"Hello, World!"
memory.write(store, data, 0)
result = common.read_memory(memory, store, 0, len(data))
assert result == b"Hello, World!"
def test_read_memory_all(self):
"""Test not passing a length should read all memory"""
store = Store()
memory = Memory(store, MemoryType(Limits(1, 1)))
data = b"Hello, world!"
memory.write(store, data, 0)
result = common.read_memory(memory, store, 0)
# Allocated memory should be the same as the length of the result data
assert memory.data_len(store) == len(result)
# Beginning of result should contain the data we wrote
assert result.startswith(data)
def test_read_memory_unpack_ptr(self):
"""Test passing a length of '-1' should unpack the length from the pointer"""
store = Store()
memory = Memory(store, MemoryType(Limits(1, 1)))
data = b"Hello, world!"
ptr_packed = 0 << 32 | len(data)
# Write data from the start
memory.write(store, data, 0)
result = common.read_memory(memory, store, ptr_packed, -1)
assert result == data