Skip to content

Commit 0352c76

Browse files
author
Johannes Holland
committed
readme: fix the api examples and add tests
1 parent 9750373 commit 0352c76

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ converted to a python representation of the respective datatype (e.g. a
6161
`TPMS_SIG_SCHEME_ECDSA` object).
6262

6363
```python
64-
from tpmstream.common.event import events_to_obj
64+
from tpmstream.common.object import events_to_obj
6565
from tpmstream.io.binary import Binary
66-
from tpmstream.spec.commands.commands import Command
66+
from tpmstream.spec.commands import Command
6767
from tpmstream.spec.structures.constants import TPM_SU
6868

6969
events = Binary.marshal(tpm_type=Command, buffer=b"\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00")
70-
command = events_to_obj(Command, events)
70+
command = events_to_obj(events)
7171

7272
print(command.parameters.startupType) # prints TPM_SU.CLEAR
7373
```
@@ -76,11 +76,24 @@ Likewise, these python objects can be turned into a sequence of events, again.
7676

7777

7878
```python
79-
from tpmstream.common.event import obj_to_events
80-
81-
# ...
82-
83-
events = obj_to_events(command)
79+
from tpmstream.common.object import obj_to_events
80+
from tpmstream.io.binary import Binary
81+
from tpmstream.spec.commands import Command
82+
from tpmstream.spec.commands.commands_handles import TPMS_COMMAND_HANDLES_STARTUP
83+
from tpmstream.spec.commands.commands_params import TPMS_COMMAND_PARAMS_STARTUP
84+
from tpmstream.spec.structures.base_types import UINT32
85+
from tpmstream.spec.structures.constants import TPM_CC, TPM_ST, TPM_SU
86+
from tpmstream.spec.structures.interface_types import TPMI_ST_COMMAND_TAG
87+
88+
startup_command = Command(
89+
tag=TPMI_ST_COMMAND_TAG(TPM_ST.NO_SESSIONS),
90+
commandSize=UINT32(12),
91+
commandCode=TPM_CC.Startup,
92+
handles=TPMS_COMMAND_HANDLES_STARTUP(),
93+
parameters=TPMS_COMMAND_PARAMS_STARTUP(startupType=TPM_SU.CLEAR),
94+
)
95+
96+
events = obj_to_events(startup_command)
8497

8598
# Note that `events` is a generator. You can obtain a list by via `list(events)`
8699
```
@@ -93,7 +106,7 @@ format (binary, pretty print, ...).
93106
```python
94107
from tpmstream.io.binary import Binary
95108
from tpmstream.io.pretty import Pretty
96-
from tpmstream.spec.commands.commands import Command
109+
from tpmstream.spec.commands import Command
97110

98111
events = Binary.marshal(tpm_type=Command, buffer=b"\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00")
99112
pretty = Pretty.unmarshal(events=events)

test/test_readme.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class TestReadme:
2+
def test_marshal_from_bytes(self):
3+
from tpmstream.common.object import events_to_obj
4+
from tpmstream.io.binary import Binary
5+
from tpmstream.spec.commands import Command
6+
from tpmstream.spec.structures.constants import TPM_SU
7+
8+
events = Binary.marshal(
9+
tpm_type=Command, buffer=b"\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"
10+
)
11+
command = events_to_obj(events)
12+
13+
print(command.parameters.startupType) # prints TPM_SU.CLEAR
14+
assert command.parameters.startupType == TPM_SU.CLEAR
15+
16+
def test_marshal_from_command(self):
17+
from tpmstream.common.object import obj_to_events
18+
from tpmstream.io.binary import Binary
19+
from tpmstream.spec.commands import Command
20+
from tpmstream.spec.commands.commands_handles import (
21+
TPMS_COMMAND_HANDLES_STARTUP,
22+
)
23+
from tpmstream.spec.commands.commands_params import TPMS_COMMAND_PARAMS_STARTUP
24+
from tpmstream.spec.structures.base_types import UINT32
25+
from tpmstream.spec.structures.constants import TPM_CC, TPM_ST, TPM_SU
26+
from tpmstream.spec.structures.interface_types import TPMI_ST_COMMAND_TAG
27+
28+
startup_command = Command(
29+
tag=TPMI_ST_COMMAND_TAG(TPM_ST.NO_SESSIONS),
30+
commandSize=UINT32(12),
31+
commandCode=TPM_CC.Startup,
32+
handles=TPMS_COMMAND_HANDLES_STARTUP(),
33+
parameters=TPMS_COMMAND_PARAMS_STARTUP(startupType=TPM_SU.CLEAR),
34+
)
35+
36+
events = obj_to_events(startup_command)
37+
assert list(events) == list(
38+
Binary.marshal(
39+
tpm_type=Command,
40+
buffer=b"\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00",
41+
)
42+
)
43+
44+
def test_unmarshal(self):
45+
from tpmstream.io.binary import Binary
46+
from tpmstream.io.pretty import Pretty
47+
from tpmstream.spec.commands import Command
48+
49+
events = Binary.marshal(
50+
tpm_type=Command, buffer=b"\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"
51+
)
52+
pretty = Pretty.unmarshal(events=events)
53+
54+
for line in pretty:
55+
print(line)

0 commit comments

Comments
 (0)