api_impl/
props.rs

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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
// Copyright 2024 The Fuchsia Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Implementation of TEE Internal Core API Specification 4.4 Property Access Functions
// Functions return specific error codes where applicable and panic on all other errors.
use fuchsia_sync::RwLock;
use std::cmp::min;
use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};

use tee_internal::{
    Error, Identity, PropSetHandle, Result as TeeResult, Uuid, TEE_PROPSET_CURRENT_CLIENT,
    TEE_PROPSET_CURRENT_TA, TEE_PROPSET_TEE_IMPLEMENTATION,
};
use tee_properties::{PropEnumerator, PropSet, PropertyError};

static PROP_SETS: OnceLock<Arc<RwLock<PropSets>>> = OnceLock::new();

fn prop_sets() -> Arc<RwLock<PropSets>> {
    PROP_SETS.get_or_init(|| Arc::new(RwLock::new(PropSets::load_prop_sets()))).clone()
}

fn write_c_string_to_buffer<'a>(string_val: &str, buffer: &'a mut [u8]) -> &'a str {
    if buffer.len() == 0 {
        return std::str::from_utf8(buffer).unwrap();
    }
    // buffer.len() - 1 accounts for the NUL byte we need to write at the end.
    let bytes_to_write = min(buffer.len() - 1, string_val.len());
    buffer[..bytes_to_write].clone_from_slice(&string_val.as_bytes()[..bytes_to_write]);
    buffer[bytes_to_write] = 0;

    std::str::from_utf8(&buffer[..bytes_to_write]).unwrap()
}

// The public surface of this module calls into a PropSets singleton.

// A `PropSetHandle` input into the API can be either:
// 1. A handle to an enumerator on a property set, OR
// 2. A pseudo-handle which designates a specific property set: TEE Implementation, Client, or TA.
// This helper fn aims to easily distinguish between the two for easier processing.
pub fn is_propset_pseudo_handle(handle: PropSetHandle) -> bool {
    handle == TEE_PROPSET_TEE_IMPLEMENTATION
        || handle == TEE_PROPSET_CURRENT_CLIENT
        || handle == TEE_PROPSET_CURRENT_TA
}

pub struct GetStringError<'a> {
    pub error: Error,
    pub written: &'a str,
    pub actual_length: usize,
}

pub struct GetBinaryBlockError<'a> {
    pub error: Error,
    pub written: &'a [u8],
    pub actual_length: usize,
}

pub fn get_property_as_string<'a>(
    handle: PropSetHandle,
    name: &str,
    buffer: &'a mut [u8],
) -> Result<&'a str, GetStringError<'a>> {
    let string_val = prop_sets()
        .read()
        .get_string_property(handle, name)
        .map_err(|error| GetStringError { error, written: "", actual_length: 0 })?;
    let buffer_len = buffer.len();
    let string_written = write_c_string_to_buffer(string_val.as_str(), buffer);
    // We're writing a NUL byte to the buffer which is not captured in string_val.len().
    if buffer_len < string_val.len() + 1 {
        return Err(GetStringError {
            error: Error::ShortBuffer,
            written: string_written,
            actual_length: string_val.len(),
        });
    }
    Ok(string_written)
}

pub fn get_property_as_bool(handle: PropSetHandle, name: &str) -> TeeResult<bool> {
    prop_sets().read().get_bool_property(handle, name)
}

pub fn get_property_as_u32(handle: PropSetHandle, name: &str) -> TeeResult<u32> {
    prop_sets().read().get_uint32_property(handle, name)
}

pub fn get_property_as_u64(handle: PropSetHandle, name: &str) -> TeeResult<u64> {
    prop_sets().read().get_uint64_property(handle, name)
}

pub fn get_property_as_binary_block<'a>(
    handle: PropSetHandle,
    name: &str,
    buffer: &'a mut [u8],
) -> Result<&'a [u8], GetBinaryBlockError<'a>> {
    let bytes = prop_sets()
        .read()
        .get_binary_block_property(handle, name)
        .map_err(|error| GetBinaryBlockError { error, written: &[], actual_length: 0 })?;

    let buffer_len = buffer.len();
    let bytes_to_write = min(buffer.len(), bytes.len());
    let written = &mut buffer[..bytes_to_write];
    written.clone_from_slice(&bytes[..bytes_to_write]);

    if buffer_len < bytes.len() {
        return Err(GetBinaryBlockError {
            error: Error::ShortBuffer,
            written,
            actual_length: bytes.len(),
        });
    }
    Ok(written)
}

pub fn get_property_as_uuid(handle: PropSetHandle, name: &str) -> TeeResult<Uuid> {
    prop_sets().read().get_uuid_property(handle, name)
}

pub fn get_property_as_identity(handle: PropSetHandle, name: &str) -> TeeResult<Identity> {
    prop_sets().read().get_identity_property(handle, name)
}

pub fn allocate_property_enumerator() -> PropSetHandle {
    prop_sets().write().allocate_property_enumerator()
}

pub fn free_property_enumerator(handle: PropSetHandle) {
    prop_sets().write().free_property_enumerator(handle);
}

pub fn start_property_enumerator(handle: PropSetHandle, prop_set: PropSetHandle) {
    prop_sets().write().start_property_enumerator(handle, prop_set);
}

pub fn reset_property_enumerator(handle: PropSetHandle) {
    prop_sets().write().reset_property_enumerator(handle);
}

pub fn get_property_name<'a>(
    handle: PropSetHandle,
    buffer: &'a mut [u8],
) -> Result<&'a str, GetStringError<'a>> {
    let string_val = prop_sets()
        .read()
        .get_property_name(handle)
        .map_err(|error| GetStringError { error, written: "", actual_length: 0 })?;

    let buffer_len = buffer.len();
    let string_written = write_c_string_to_buffer(string_val.as_str(), buffer);
    // We're writing a NUL byte to the buffer which is not captured in string_val.len().
    if buffer_len < string_val.len() + 1 {
        return Err(GetStringError {
            error: Error::ShortBuffer,
            written: string_written,
            actual_length: string_val.len(),
        });
    }
    Ok(string_written)
}

pub fn get_next_property(handle: PropSetHandle) -> TeeResult<()> {
    prop_sets().write().get_next_property(handle)
}

struct PropSets {
    tee_implementation_props: Arc<PropSet>,
    ta_props: Arc<PropSet>,
    client_props: Arc<PropSet>,
    // TODO: Scope write locking to this hashmap only.
    prop_enums: HashMap<u64, PropEnumerator>,
    enumerator_handle_id_counter: AtomicU64,
}

impl PropSets {
    fn load_prop_sets() -> Self {
        let tee_impl_props_path = Path::new("/properties/system_properties");
        let tee_impl_props = PropSet::from_config_file(&tee_impl_props_path)
            .expect("TEE prop set loads successfully");

        let ta_props_path = Path::new("/pkg/data/ta_properties");
        let ta_props =
            PropSet::from_config_file(&ta_props_path).expect("TA prop set loads successfully");

        // TODO(b/369916290): Handle this properly.
        let fake_client_props_str: &str = r#"
        [
            {
                name: "gpd.client.identity",
                prop_type: "identity",
                value: "0:9cccff19-13b5-4d4c-aa9e-5c8901a52e2f",
            },
        ]
        "#;
        let client_props = PropSet::from_config_string(fake_client_props_str)
            .expect("load fake client props successfully");

        Self {
            tee_implementation_props: Arc::new(tee_impl_props),
            ta_props: Arc::new(ta_props),
            client_props: Arc::new(client_props),
            prop_enums: HashMap::new(),
            // Handles cannot be 0, as that value is reserved for TEE_HANDLE_NULL, so we start at 1.
            // There are reserved values at 0xFFFFFFFD-0xFFFFFFFF; not expecting allocations to reach them.
            enumerator_handle_id_counter: AtomicU64::new(1),
        }
    }

    fn get_prop_set_from_pseudo_handle(
        &self,
        maybe_pseudo_handle: PropSetHandle,
    ) -> Option<Arc<PropSet>> {
        match maybe_pseudo_handle {
            TEE_PROPSET_TEE_IMPLEMENTATION => Some(self.tee_implementation_props.clone()),
            TEE_PROPSET_CURRENT_CLIENT => Some(self.client_props.clone()),
            TEE_PROPSET_CURRENT_TA => Some(self.ta_props.clone()),
            _ => None,
        }
    }

    // TODO(https://fxbug.dev/376130726): Use &str as return value instead of String.
    fn get_string_property(
        &self,
        handle: PropSetHandle,
        name: &str,
    ) -> Result<String, tee_internal::Error> {
        assert!(!handle.is_null());
        let string_val = if let Some(prop_set) = self.get_prop_set_from_pseudo_handle(handle) {
            prop_set
                .get_string_property(name.to_string())
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        } else {
            self.prop_enums
                .get(&handle)
                .expect("enumerator handle must be valid")
                .get_property_as_string()
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        };
        Ok(string_val)
    }

    fn get_bool_property(
        &self,
        handle: PropSetHandle,
        name: &str,
    ) -> Result<bool, tee_internal::Error> {
        assert!(!handle.is_null());
        let bool_val = if let Some(prop_set) = self.get_prop_set_from_pseudo_handle(handle) {
            prop_set
                .get_boolean_property(name.to_string())
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        } else {
            self.prop_enums
                .get(&handle)
                .expect("enumerator handle must be valid")
                .get_property_as_bool()
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        };
        Ok(bool_val)
    }

    fn get_uint32_property(
        &self,
        handle: PropSetHandle,
        name: &str,
    ) -> Result<u32, tee_internal::Error> {
        assert!(!handle.is_null());
        let u32_val = if let Some(prop_set) = self.get_prop_set_from_pseudo_handle(handle) {
            prop_set
                .get_uint32_property(name.to_string())
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        } else {
            self.prop_enums
                .get(&handle)
                .expect("enumerator handle must be valid")
                .get_property_as_u32()
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        };
        Ok(u32_val)
    }

    fn get_uint64_property(
        &self,
        handle: PropSetHandle,
        name: &str,
    ) -> Result<u64, tee_internal::Error> {
        assert!(!handle.is_null());
        let u64_val = if let Some(prop_set) = self.get_prop_set_from_pseudo_handle(handle) {
            prop_set
                .get_uint64_property(name.to_string())
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        } else {
            self.prop_enums
                .get(&handle)
                .expect("enumerator handle must be valid")
                .get_property_as_u64()
                .map_err(|_| tee_internal::Error::ItemNotFound)?
        };
        Ok(u64_val)
    }

    // TODO(https://fxbug.dev/376130726): Use &[u8] as return value instead of Vec<u8>.
    fn get_binary_block_property(
        &self,
        handle: PropSetHandle,
        name: &str,
    ) -> Result<Vec<u8>, tee_internal::Error> {
        assert!(!handle.is_null());
        let bytes = if let Some(prop_set) = self.get_prop_set_from_pseudo_handle(handle) {
            prop_set.get_binary_block_property(name.to_string()).map_err(
                |error: PropertyError| match error {
                    PropertyError::BadFormat { .. } => tee_internal::Error::BadFormat,
                    PropertyError::ItemNotFound { .. } => return tee_internal::Error::ItemNotFound,
                    PropertyError::Generic { msg } => {
                        panic!("Error getting property as binary block: {:?}", msg)
                    }
                },
            )?
        } else {
            self.prop_enums
                .get(&handle)
                .expect("enumerator handle must be valid")
                .get_property_as_binary_block()
                .map_err(|error: PropertyError| match error {
                    PropertyError::BadFormat { .. } => tee_internal::Error::BadFormat,
                    PropertyError::ItemNotFound { .. } => return tee_internal::Error::ItemNotFound,
                    PropertyError::Generic { msg } => {
                        panic!("Error getting property as binary block: {:?}", msg)
                    }
                })?
        };

        Ok(bytes)
    }

    fn get_uuid_property(
        &self,
        handle: PropSetHandle,
        name: &str,
    ) -> Result<Uuid, tee_internal::Error> {
        assert!(!handle.is_null());
        let uuid = if let Some(prop_set) = self.get_prop_set_from_pseudo_handle(handle) {
            prop_set.get_uuid_property(name.to_string()).map_err(|error| match error {
                PropertyError::BadFormat { .. } => tee_internal::Error::BadFormat,
                PropertyError::ItemNotFound { .. } => return tee_internal::Error::ItemNotFound,
                PropertyError::Generic { msg } => {
                    panic!("Error getting property as uuid: {:?}", msg)
                }
            })?
        } else {
            self.prop_enums
                .get(&handle)
                .expect("enumerator handle must be valid")
                .get_property_as_uuid()
                .map_err(|error| match error {
                    PropertyError::BadFormat { .. } => tee_internal::Error::BadFormat,
                    PropertyError::ItemNotFound { .. } => return tee_internal::Error::ItemNotFound,
                    PropertyError::Generic { msg } => {
                        panic!("Error getting property as uuid: {:?}", msg)
                    }
                })?
        };

        Ok(uuid)
    }

    fn get_identity_property(
        &self,
        handle: PropSetHandle,
        name: &str,
    ) -> Result<Identity, tee_internal::Error> {
        assert!(!handle.is_null());
        let identity = if let Some(prop_set) = self.get_prop_set_from_pseudo_handle(handle) {
            prop_set.get_identity_property(name.to_string()).map_err(|error: PropertyError| {
                match error {
                    PropertyError::BadFormat { .. } => tee_internal::Error::BadFormat,
                    PropertyError::ItemNotFound { .. } => return tee_internal::Error::ItemNotFound,
                    PropertyError::Generic { msg } => {
                        panic!("Error getting property as identity: {:?}", msg)
                    }
                }
            })?
        } else {
            self.prop_enums
                .get(&handle)
                .expect("enumerator handle must be valid")
                .get_property_as_identity()
                .map_err(|error| match error {
                    PropertyError::BadFormat { .. } => tee_internal::Error::BadFormat,
                    PropertyError::ItemNotFound { .. } => return tee_internal::Error::ItemNotFound,
                    PropertyError::Generic { msg } => {
                        panic!("Error getting property as identity: {:?}", msg)
                    }
                })?
        };

        Ok(identity)
    }

    fn new_enumerator_handle(&mut self) -> u64 {
        assert!(
            self.enumerator_handle_id_counter.load(Ordering::Relaxed)
                <= *TEE_PROPSET_TEE_IMPLEMENTATION
        );
        self.enumerator_handle_id_counter.fetch_add(1, Ordering::Relaxed)
    }

    fn allocate_property_enumerator(&mut self) -> PropSetHandle {
        // TODO(b/369916290): Check for out-of-memory conditions to return TEE_ERROR_OUT_OF_MEMORY.
        let handle = self.new_enumerator_handle();
        let _ = self.prop_enums.insert(handle, PropEnumerator::new());
        PropSetHandle::from_value(handle)
    }

    fn free_property_enumerator(&mut self, handle: PropSetHandle) {
        assert!(!handle.is_null());
        let _ = self.prop_enums.remove_entry(&handle);
    }

    fn start_property_enumerator(&mut self, handle: PropSetHandle, prop_set: PropSetHandle) {
        assert!(!handle.is_null());
        let Some(prop_set) = self.get_prop_set_from_pseudo_handle(prop_set) else {
            panic!("Invalid TEE_PropSetHandle pseudo-handle provided");
        };

        let Some(prop_enumerator) = self.prop_enums.get_mut(&handle) else {
            panic!("Invalid enumerator handle provided");
        };

        prop_enumerator.start(prop_set);
    }

    fn reset_property_enumerator(&mut self, handle: PropSetHandle) {
        assert!(!handle.is_null());
        let Some(prop_enumerator) = self.prop_enums.get_mut(&handle) else {
            panic!("Invalid enumerator handle provided");
        };
        prop_enumerator.reset();
    }

    fn get_property_name(&self, handle: PropSetHandle) -> Result<String, tee_internal::Error> {
        assert!(!handle.is_null());
        let Some(prop_enumerator) = self.prop_enums.get(&handle) else {
            panic!("Invalid enumerator handle provided");
        };
        prop_enumerator.get_property_name().map_err(|_| tee_internal::Error::ItemNotFound)
    }

    fn get_next_property(&mut self, handle: PropSetHandle) -> Result<(), tee_internal::Error> {
        assert!(!handle.is_null());
        let Some(prop_enumerator) = self.prop_enums.get_mut(&handle) else {
            panic!("Invalid enumerator handle provided");
        };

        prop_enumerator.next().map_err(|_| tee_internal::Error::ItemNotFound)
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;

    // TODO: Re-use test values from //src/tee/lib/tee_properties tests to avoid duplicating these.
    const TEST_TEE_IMPL_PROPS_STR: &str = r#"[
        {
            name: "gpd.tee.test.string",
            prop_type: "string",
            value: "asdf",
        },
        {
            name: "gpd.tee.test.bool",
            prop_type: "boolean",
            value: "true",
        },
        {
            name: "gpd.tee.test.u32",
            prop_type: "unsigned_int32",
            value: "57",
        },
        {
            name: "gpd.tee.test.u64",
            prop_type: "unsigned_int64",
            value: "4294967296",
        },
        {
            name: "gpd.tee.test.binaryBlock",
            prop_type: "binary_block",
            value: "ZnVjaHNpYQ==",
        },
        {
            name: "gpd.tee.test.uuid",
            prop_type: "uuid",
            value: "9cccff19-13b5-4d4c-aa9e-5c8901a52e2f",
        },
        {
            name: "gpd.tee.test.identity",
            prop_type: "identity",
            value: "4026531840:9cccff19-13b5-4d4c-aa9e-5c8901a52e2f",
        },
    ]
    "#;

    const TEST_TA_PROPS_STR: &str = r#"[
        {
            name: "gpd.ta.appID",
            prop_type: "uuid",
            value: "9cccff19-13b5-4d4c-aa9e-5c8901a52e2f",
        },
        {
            name: "gpd.ta.singleInstance",
            prop_type: "boolean",
            value: "true",
        },
        {
            name: "gpd.ta.multiSession",
            prop_type: "boolean",
            value: "true",
        },
        {
            name: "gpd.ta.instanceKeepAlive",
            prop_type: "boolean",
            value: "false",
        },
    ]
    "#;

    const TEST_CLIENT_PROPS_STR: &str = r#"
    [
        {
            name: "gpd.client.identity",
            prop_type: "identity",
            value: "0:9cccff19-13b5-4d4c-aa9e-5c8901a52e2f",
        },
    ]
    "#;

    fn init_test_tee_impl_props() {
        let test_props = PropSet::from_config_string(TEST_TEE_IMPL_PROPS_STR)
            .expect("load fake tee props successfully");
        let ta_props = PropSet::from_config_string(TEST_TA_PROPS_STR)
            .expect("load fake ta props successfully");
        let client_props = PropSet::from_config_string(TEST_CLIENT_PROPS_STR)
            .expect("load fake client props successfully");

        let prop_sets = PropSets {
            tee_implementation_props: Arc::new(test_props),
            ta_props: Arc::new(ta_props),
            client_props: Arc::new(client_props),
            prop_enums: HashMap::new(),
            enumerator_handle_id_counter: AtomicU64::new(1),
        };
        let _ = PROP_SETS.set(Arc::new(RwLock::new(prop_sets)));
    }

    #[fuchsia::test]
    pub fn test_get_string_property() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_string_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.string")
            .unwrap();
        assert_eq!("asdf".to_string(), value);
    }

    #[fuchsia::test]
    pub fn test_get_string_property_not_found() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value =
            prop_sets.read().get_string_property(TEE_PROPSET_TEE_IMPLEMENTATION, "name_not_in_set");
        assert_eq!(tee_internal::Error::ItemNotFound, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_bool_property() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_bool_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.bool")
            .unwrap();
        assert!(value);
    }

    #[fuchsia::test]
    pub fn test_get_bool_property_not_found() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value =
            prop_sets.read().get_bool_property(TEE_PROPSET_TEE_IMPLEMENTATION, "name_not_in_set");
        assert_eq!(tee_internal::Error::ItemNotFound, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_u32_property() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_uint32_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.u32")
            .unwrap();
        assert_eq!(57, value);
    }

    #[fuchsia::test]
    pub fn test_get_u32_property_not_found() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value =
            prop_sets.read().get_uint32_property(TEE_PROPSET_TEE_IMPLEMENTATION, "name_not_in_set");
        assert_eq!(tee_internal::Error::ItemNotFound, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_u64_property() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_uint64_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.u64")
            .unwrap();
        assert_eq!(4294967296, value);
    }

    #[fuchsia::test]
    pub fn test_get_u64_property_not_found() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value =
            prop_sets.read().get_uint64_property(TEE_PROPSET_TEE_IMPLEMENTATION, "name_not_in_set");
        assert_eq!(tee_internal::Error::ItemNotFound, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_binary_block_property() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_binary_block_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.binaryBlock")
            .unwrap();

        // base64 string ZnVjaHNpYQ== resolves to hex [66 75 63 68 73 69 61], which is len 7.
        // This maps to [102, 117, 99, 104, 115, 105, 97] as u8 bytes.
        let expected_val_as_u8: Vec<u8> = vec![102, 117, 99, 104, 115, 105, 97];
        assert_eq!(7, value.len());
        assert_eq!(expected_val_as_u8, value);
    }

    #[fuchsia::test]
    pub fn test_get_binary_block_property_not_found() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_binary_block_property(TEE_PROPSET_TEE_IMPLEMENTATION, "name_not_in_set");
        assert_eq!(tee_internal::Error::ItemNotFound, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_binary_block_property_bad_format() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_binary_block_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.u32");
        assert_eq!(tee_internal::Error::BadFormat, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_uuid_property() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_uuid_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.uuid")
            .unwrap();

        assert_eq!(0x9cccff19, value.time_low);
        assert_eq!(0x13b5, value.time_mid);
        assert_eq!(0x4d4c, value.time_hi_and_version);
        assert_eq!([0xaa, 0x9e, 0x5c, 0x89, 0x01, 0xa5, 0x2e, 0x2f], value.clock_seq_and_node);
    }

    #[fuchsia::test]
    pub fn test_get_uuid_property_not_found() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value =
            prop_sets.read().get_uuid_property(TEE_PROPSET_TEE_IMPLEMENTATION, "name_not_in_set");
        assert_eq!(tee_internal::Error::ItemNotFound, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_uuid_property_bad_format() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value =
            prop_sets.read().get_uuid_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.u32");
        assert_eq!(tee_internal::Error::BadFormat, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_identity_property() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_identity_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.identity")
            .unwrap();

        // Login::TrustedApp = TEE_LOGIN_TRUSTED_APP = 4026531840, as encoded in TEST_TEE_IMPL_PROPS_STR.
        assert_eq!(tee_internal::Login::TrustedApp, value.login);

        assert_eq!(0x9cccff19, value.uuid.time_low);
        assert_eq!(0x13b5, value.uuid.time_mid);
        assert_eq!(0x4d4c, value.uuid.time_hi_and_version);
        assert_eq!([0xaa, 0x9e, 0x5c, 0x89, 0x01, 0xa5, 0x2e, 0x2f], value.uuid.clock_seq_and_node);
    }

    #[fuchsia::test]
    pub fn test_get_identity_property_not_found() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_identity_property(TEE_PROPSET_TEE_IMPLEMENTATION, "name_not_in_set");
        assert_eq!(tee_internal::Error::ItemNotFound, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_get_identity_property_bad_format() {
        init_test_tee_impl_props();
        let prop_sets = prop_sets();
        let value = prop_sets
            .read()
            .get_identity_property(TEE_PROPSET_TEE_IMPLEMENTATION, "gpd.tee.test.uuid");
        assert_eq!(tee_internal::Error::BadFormat, value.err().unwrap());
    }

    #[fuchsia::test]
    pub fn test_enumerator_allocate() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        assert_eq!(0, prop_sets.prop_enums.len());
        let handle = prop_sets.allocate_property_enumerator();
        assert!(prop_sets.prop_enums.get(&handle).is_some());
    }

    #[fuchsia::test]
    pub fn test_enumerator_free() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        assert_eq!(0, prop_sets.prop_enums.len());
        let handle = prop_sets.allocate_property_enumerator();
        assert!(prop_sets.prop_enums.get(&handle).is_some());
        prop_sets.free_property_enumerator(handle);
        assert_eq!(0, prop_sets.prop_enums.len());
    }

    #[fuchsia::test]
    pub fn test_enumerator_start() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();
        // We can't inspect the internal state of the enumerator, so this tests not panicking.
        prop_sets.start_property_enumerator(handle, TEE_PROPSET_TEE_IMPLEMENTATION);
    }

    #[fuchsia::test]
    #[should_panic]
    pub fn test_enumerator_start_bad_handle() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let _handle = prop_sets.allocate_property_enumerator();
        // Handle value of 0 corresponds to TEE_HANDLE_NULL, which is not valid for this API.
        let bad_handle = PropSetHandle::from_value(0);
        prop_sets.start_property_enumerator(bad_handle, TEE_PROPSET_TEE_IMPLEMENTATION);
    }

    #[fuchsia::test]
    #[should_panic(expected = "Invalid TEE_PropSetHandle pseudo-handle provided")]
    pub fn test_enumerator_start_bad_prop_set_pseudo_handle() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();
        // Pseudo handle is not one of the 3 reserved values.
        prop_sets.start_property_enumerator(handle, PropSetHandle::from_value(0));
    }

    #[fuchsia::test]
    pub fn test_enumerator_get_name() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();

        prop_sets.start_property_enumerator(handle, TEE_PROPSET_TEE_IMPLEMENTATION);
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.tee.test.string".to_string(), name);
    }

    #[fuchsia::test]
    pub fn test_enumerator_next_property() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();

        prop_sets.start_property_enumerator(handle, TEE_PROPSET_TEE_IMPLEMENTATION);
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.tee.test.string".to_string(), name);

        prop_sets.get_next_property(handle).unwrap();
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.tee.test.bool".to_string(), name);
    }

    #[fuchsia::test]
    pub fn test_enumerator_reset() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();

        prop_sets.start_property_enumerator(handle, TEE_PROPSET_TEE_IMPLEMENTATION);
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.tee.test.string".to_string(), name);

        prop_sets.get_next_property(handle).unwrap();
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.tee.test.bool".to_string(), name);

        prop_sets.reset_property_enumerator(handle);
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.tee.test.string".to_string(), name);
    }

    #[fuchsia::test]
    pub fn test_enumerator_get_name_ta_prop_set() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();

        prop_sets.start_property_enumerator(handle, TEE_PROPSET_CURRENT_TA);
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.ta.appID".to_string(), name);
    }

    #[fuchsia::test]
    pub fn test_enumerator_get_name_client_prop_set() {
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();

        prop_sets.start_property_enumerator(handle, TEE_PROPSET_CURRENT_CLIENT);
        let name = prop_sets.get_property_name(handle).unwrap();
        assert_eq!("gpd.client.identity".to_string(), name);
    }

    #[fuchsia::test]
    pub fn test_enumerator_all_prop_types() {
        // This test uses the enumerator to run through all the test types in the following order:
        // string -> bool -> u32 -> u64 -> binary block -> uuid -> identity
        init_test_tee_impl_props();
        let binding = prop_sets();
        let mut prop_sets = binding.write();
        let handle = prop_sets.allocate_property_enumerator();

        prop_sets.start_property_enumerator(handle, TEE_PROPSET_TEE_IMPLEMENTATION);
        let string_val = prop_sets.get_string_property(handle, "").unwrap();
        assert_eq!("asdf".to_string(), string_val);

        prop_sets.get_next_property(handle).unwrap();
        let bool_val = prop_sets.get_bool_property(handle, "").unwrap();
        assert!(bool_val);

        prop_sets.get_next_property(handle).unwrap();
        let u32_val = prop_sets.get_uint32_property(handle, "").unwrap();
        assert_eq!(57, u32_val);

        prop_sets.get_next_property(handle).unwrap();
        let u64_val = prop_sets.get_uint64_property(handle, "").unwrap();
        assert_eq!(4294967296, u64_val);

        prop_sets.get_next_property(handle).unwrap();
        let binary_block = prop_sets.get_binary_block_property(handle, "").unwrap();
        let expected_val_as_u8: Vec<u8> = vec![102, 117, 99, 104, 115, 105, 97];
        assert_eq!(7, binary_block.len());
        assert_eq!(expected_val_as_u8, binary_block);

        prop_sets.get_next_property(handle).unwrap();
        let uuid_val = prop_sets.get_uuid_property(handle, "").unwrap();
        assert_eq!(0x9cccff19, uuid_val.time_low);
        assert_eq!(0x13b5, uuid_val.time_mid);
        assert_eq!(0x4d4c, uuid_val.time_hi_and_version);
        assert_eq!([0xaa, 0x9e, 0x5c, 0x89, 0x01, 0xa5, 0x2e, 0x2f], uuid_val.clock_seq_and_node);

        prop_sets.get_next_property(handle).unwrap();
        let identity_val = prop_sets.get_identity_property(handle, "").unwrap();
        assert_eq!(tee_internal::Login::TrustedApp, identity_val.login);
        assert_eq!(0x9cccff19, identity_val.uuid.time_low);
        assert_eq!(0x13b5, identity_val.uuid.time_mid);
        assert_eq!(0x4d4c, identity_val.uuid.time_hi_and_version);
        assert_eq!(
            [0xaa, 0x9e, 0x5c, 0x89, 0x01, 0xa5, 0x2e, 0x2f],
            identity_val.uuid.clock_seq_and_node
        );

        let res = prop_sets.get_next_property(handle);
        assert_eq!(tee_internal::Error::ItemNotFound, res.err().unwrap())
    }
}