File: command_code_attributes_tests.rs

package info (click to toggle)
rust-tss-esapi 7.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,432 kB
  • sloc: sh: 130; makefile: 2
file content (332 lines) | stat: -rw-r--r-- 10,880 bytes parent folder | download | duplicates (2)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2022 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use bitfield::bitfield;
use std::convert::{TryFrom, TryInto};
use tss_esapi::{
    attributes::CommandCodeAttributes, constants::CommandCode, tss2_esys::TPMA_CC, Error,
    WrapperErrorKind,
};

bitfield! {
    #[derive(Copy, Clone, Eq, PartialEq)]
    struct ExpectedAttributes(TPMA_CC);
    impl Debug;
    u16, command_index, set_command_index: 15, 0;
    u8, reserved, set_reserved: 21, 16;
    nv, set_nv: 22;
    extensive, set_extensive: 23;
    flushed, set_flushed: 24;
    u8, c_handles, set_c_handles: 27, 25;
    r_handle, set_r_handle: 28;
    is_vendor_specific, set_vendor_specific: 29;
    u8, res, set_res: 31, 30; // shall be zero
}

#[test]
fn test_conversions_non_vendor_specific() {
    let expected = {
        let mut ea = ExpectedAttributes(0);
        ea.set_vendor_specific(false);
        // Because it is not vendor specific it needs the
        // command index needs to be set to a value present
        // in CommandCodes.
        ea.set_command_index(
            u32::from(CommandCode::AcGetCapability)
                .try_into()
                .expect("Failed to convert CommandCode to an u16 command code index value"),
        );
        ea.set_nv(true);
        ea.set_extensive(true);
        ea.set_flushed(true);
        ea.set_c_handles(6);
        ea.set_r_handle(true);
        ea
    };

    let command_code_attributes: CommandCodeAttributes = expected
        .0
        .try_into()
        .expect("Failed to convert TPMA_CC to CommandCodeAttributes");

    assert_eq!(
        expected.command_index(),
        command_code_attributes.command_index(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for command index"
    );

    assert_eq!(
        expected.nv(),
        command_code_attributes.nv(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for NV"
    );

    assert_eq!(
        expected.extensive(),
        command_code_attributes.extensive(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for Extensive"
    );

    assert_eq!(
        expected.flushed(),
        command_code_attributes.flushed(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for Flushed"
    );

    assert_eq!(
        expected.c_handles(),
        command_code_attributes.c_handles(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for CHandles"
    );

    assert_eq!(
        expected.r_handle(),
        command_code_attributes.r_handle(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for RHandle"
    );

    assert_eq!(
        expected.is_vendor_specific(),
        command_code_attributes.is_vendor_specific(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for Vendor Specific(V)"
    );

    assert_eq!(
        expected.0,
        command_code_attributes.into(),
        "CommandCodeAttributes did not convert into the expected TPMA_CC value"
    );
}

#[test]
fn test_conversions_vendor_specific() {
    let expected = {
        let mut ea = ExpectedAttributes(0);
        ea.set_vendor_specific(true);
        // Vendor specific is set to true so any
        // u16 value is valid.
        ea.set_command_index(0xFFFFu16);
        ea.set_nv(true);
        ea.set_extensive(true);
        ea.set_flushed(true);
        ea.set_c_handles(6);
        ea.set_r_handle(true);
        ea
    };

    let command_code_attributes: CommandCodeAttributes = expected
        .0
        .try_into()
        .expect("Failed to convert TPMA_CC to CommandCodeAttributes");

    assert_eq!(
        expected.command_index(),
        command_code_attributes.command_index(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for command index"
    );

    assert_eq!(
        expected.nv(),
        command_code_attributes.nv(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for NV"
    );

    assert_eq!(
        expected.extensive(),
        command_code_attributes.extensive(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for Extensive"
    );

    assert_eq!(
        expected.flushed(),
        command_code_attributes.flushed(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for Flushed"
    );

    assert_eq!(
        expected.c_handles(),
        command_code_attributes.c_handles(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for CHandles"
    );

    assert_eq!(
        expected.r_handle(),
        command_code_attributes.r_handle(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for RHandle"
    );

    assert_eq!(
        expected.is_vendor_specific(),
        command_code_attributes.is_vendor_specific(),
        "CommandCodeAttributes converted from TPMA_CC did not contain the expected value for Vendor Specific(V)"
    );

    assert_eq!(
        expected.0,
        command_code_attributes.into(),
        "CommandCodeAttributes did not convert into the expected TPMA_CC value"
    );
}

#[test]
fn test_invalid_conversions_non_vendor_specific_invalid_command_index() {
    let invalid_tpma_cc = {
        let mut ea = ExpectedAttributes(0);
        ea.set_vendor_specific(false);
        // Vendor specific is set to false and
        // 0xFFFFu16 does not correspond to any
        // CommandCode so this will be invalid.
        ea.set_command_index(0xFFFFu16);
        ea.set_nv(true);
        ea.set_extensive(true);
        ea.set_flushed(true);
        ea.set_c_handles(6);
        ea.set_r_handle(true);
        ea.0
    };

    assert_eq!(
        Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
        CommandCodeAttributes::try_from(invalid_tpma_cc),
        "Converting TPMA_CC witrh invalid command code index into CommandCodeAttributes did no produce the expected error"
    );
}

#[test]
fn test_invalid_conversions_with_reserve_bits_set() {
    let invalid_tpma_cc_with_set_reserve_bits = {
        let mut ea = ExpectedAttributes(0);
        ea.set_reserved(2); // Specification says the reserved bits 21:16 shall be zero so this will be invalid.
        ea.set_vendor_specific(true);
        // Vendor specific is set to true so any
        // u16 value is valid.
        ea.set_command_index(0xFFFFu16);
        ea.set_nv(true);
        ea.set_extensive(true);
        ea.set_flushed(true);
        ea.set_c_handles(6);
        ea.set_r_handle(true);
        ea.0
    };

    assert_eq!(
        Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
        CommandCodeAttributes::try_from(invalid_tpma_cc_with_set_reserve_bits),
        "Converting TPMA_CC with reserved bits 21:16 set into CommandCodeAttributes did no produce the expected error"
    );

    let invalid_tpma_cc_with_set_res_bits = {
        let mut ea = ExpectedAttributes(0);
        ea.set_vendor_specific(true);
        // Vendor specific is set to true so any
        // u16 value is valid.
        ea.set_command_index(0xFFFFu16);
        ea.set_nv(true);
        ea.set_extensive(true);
        ea.set_flushed(true);
        ea.set_c_handles(6);
        ea.set_r_handle(true);
        ea.set_res(1); // Speacification says res bits 31:30 shall be zero so this will be invalid.
        ea.0
    };

    assert_eq!(
        Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
        CommandCodeAttributes::try_from(invalid_tpma_cc_with_set_res_bits),
        "Converting TPMA_CC with reserved bits 31:30 set into CommandCodeAttributes did no produce the expected error"
    );
}

#[test]
fn test_builder() {
    let expected = {
        let mut ea = ExpectedAttributes(0);
        ea.set_vendor_specific(true);
        // Vendor specific is set to true so any
        // u16 value is valid.
        ea.set_command_index(0xFFFFu16);
        ea.set_nv(true);
        ea.set_extensive(true);
        ea.set_flushed(true);
        ea.set_c_handles(6);
        ea.set_r_handle(true);
        ea
    };

    let actual = CommandCodeAttributes::builder()
        .with_vendor_specific(expected.is_vendor_specific())
        .with_command_index(expected.command_index())
        .with_nv(expected.nv())
        .with_extensive(expected.extensive())
        .with_flushed(expected.flushed())
        .with_c_handles(expected.c_handles())
        .with_r_handle(expected.r_handle())
        .build()
        .expect("Failed to buiild command code attributes");

    assert_eq!(
        expected.command_index(),
        actual.command_index(),
        "Command index value mismatch between expected and attributes built with builder"
    );

    assert_eq!(
        expected.nv(),
        actual.nv(),
        "Nv value mismatch between expected and attributes built with builder"
    );

    assert_eq!(
        expected.extensive(),
        actual.extensive(),
        "Extensive value mismatch between expected and attributes built with builder"
    );

    assert_eq!(
        expected.flushed(),
        actual.flushed(),
        "Flushed value mismatch between expected and attributes built with builder"
    );

    assert_eq!(
        expected.c_handles(),
        actual.c_handles(),
        "C Handles value mismatch between expected and attributes built with builder"
    );

    assert_eq!(
        expected.r_handle(),
        actual.r_handle(),
        "R Handle value mismatch between expected and attributes built with builder"
    );

    assert_eq!(
        expected.is_vendor_specific(),
        actual.is_vendor_specific(),
        "Vendor specific (V) value mismatch between expected and attributes built with builder"
    );

    assert_eq!(
            expected.0,
            actual.into(),
            "CommandCodeAttributes built using the builder did not convert into the expected TPMA_CC value"
    );
}

#[test]
fn test_builder_errors() {
    let expected_result = Err(Error::WrapperError(WrapperErrorKind::InvalidParam));
    let actual_result = CommandCodeAttributes::builder()
        .with_vendor_specific(false) // Vendor specific is false so the value must be a command code
        .with_command_index(0xFFFFu16) // This is not a command code so this should make the build fail.
        .with_nv(true)
        .with_extensive(true)
        .with_flushed(true)
        .with_c_handles(6)
        .with_r_handle(true)
        .build();

    assert_eq!(
        expected_result, actual_result,
        "Building command code arguments with bad combination of vendor specific and command index did not produce the expected error",
    );
}