@@ -370,7 +370,7 @@ impl TCBLevel {
370370 // in the TCB Level. If it is greater or equal to the value in TCB Level, read status
371371 // assigned to this TCB level (in case of SGX) or go to c (in case of TDX). Otherwise,
372372 // move to the next item on TCB Levels list.
373- if self . tcb . pcesvn < pcesvn {
373+ if pcesvn < self . tcb . pcesvn {
374374 return false ;
375375 }
376376
@@ -694,3 +694,115 @@ pub struct EnclaveTCBVersions {
694694 #[ serde( rename = "isvsvn" ) ]
695695 pub isv_svn : u16 ,
696696}
697+
698+ #[ cfg( test) ]
699+ mod tests {
700+ use super :: * ;
701+
702+ #[ test]
703+ fn test_tcb_level_matches ( ) {
704+ let pcesvn: u32 = 10 ;
705+ let sgx_svn: [ u32 ; 16 ] = [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 , 20 , 22 , 24 , 26 , 28 , 30 ] ;
706+ let tdx_svn: [ u32 ; 16 ] = [ 1 , 3 , 5 , 7 , 9 , 11 , 13 , 15 , 17 , 19 , 21 , 23 , 25 , 27 , 29 , 31 ] ;
707+
708+ let mut tl = TCBLevel :: default ( ) ;
709+ tl. tcb . pcesvn = pcesvn;
710+
711+ for i in 0 ..16 {
712+ tl. tcb . sgx_components [ i] . svn = sgx_svn[ i] ;
713+ tl. tcb . tdx_components [ i] . svn = tdx_svn[ i] ;
714+ }
715+
716+ struct TestCase {
717+ name : & ' static str ,
718+ pcesvn : u32 ,
719+ sgx_svn : [ u32 ; 16 ] ,
720+ tdx_svn : Option < [ u32 ; 16 ] > ,
721+ matches : bool ,
722+ }
723+
724+ let tcs = vec ! [
725+ TestCase {
726+ name: "same values" ,
727+ pcesvn,
728+ sgx_svn,
729+ tdx_svn: Some ( tdx_svn) ,
730+ matches: true ,
731+ } ,
732+ TestCase {
733+ name: "higher pcesvn" ,
734+ pcesvn: pcesvn + 1 ,
735+ sgx_svn,
736+ tdx_svn: Some ( tdx_svn) ,
737+ matches: true ,
738+ } ,
739+ TestCase {
740+ name: "lower pcesvn" ,
741+ pcesvn: pcesvn - 1 ,
742+ sgx_svn,
743+ tdx_svn: Some ( tdx_svn) ,
744+ matches: false ,
745+ } ,
746+ TestCase {
747+ name: "higher sgx svn" ,
748+ pcesvn,
749+ sgx_svn: {
750+ let mut ss = sgx_svn;
751+ ss[ 5 ] += 1 ;
752+ ss
753+ } ,
754+ tdx_svn: Some ( tdx_svn) ,
755+ matches: true ,
756+ } ,
757+ TestCase {
758+ name: "lower sgx svn" ,
759+ pcesvn,
760+ sgx_svn: {
761+ let mut ss = sgx_svn;
762+ ss[ 5 ] -= 1 ;
763+ ss
764+ } ,
765+ tdx_svn: Some ( tdx_svn) ,
766+ matches: false ,
767+ } ,
768+ TestCase {
769+ name: "higher tdx svn" ,
770+ pcesvn,
771+ sgx_svn,
772+ tdx_svn: {
773+ let mut ts = tdx_svn;
774+ ts[ 5 ] += 1 ;
775+ Some ( ts)
776+ } ,
777+ matches: true ,
778+ } ,
779+ TestCase {
780+ name: "lower tdx svn" ,
781+ pcesvn,
782+ sgx_svn,
783+ tdx_svn: {
784+ let mut ts = tdx_svn;
785+ ts[ 5 ] -= 1 ;
786+ Some ( ts)
787+ } ,
788+ matches: false ,
789+ } ,
790+ TestCase {
791+ name: "no tdx svn" ,
792+ pcesvn,
793+ sgx_svn,
794+ tdx_svn: None ,
795+ matches: true ,
796+ } ,
797+ ] ;
798+
799+ for tc in tcs {
800+ let result = tl. matches ( & tc. sgx_svn , tc. tdx_svn . as_ref ( ) , tc. pcesvn ) ;
801+ if tc. matches {
802+ assert ! ( result, "tcb level should match when {}" , tc. name) ;
803+ } else {
804+ assert ! ( !result, "tcb level should not match when {}" , tc. name) ;
805+ }
806+ }
807+ }
808+ }
0 commit comments