@@ -127,6 +127,51 @@ describe("DstackApp", function () {
127127 } ) ;
128128 } ) ;
129129
130+ describe ( "TCB policy (IAppTcbPolicy)" , function ( ) {
131+ const testPolicy = '{"version":1,"intel_qal":["policy1"]}' ;
132+
133+ it ( "Should return empty string by default" , async function ( ) {
134+ expect ( await appAuth . tcbPolicy ( ) ) . to . equal ( "" ) ;
135+ } ) ;
136+
137+ it ( "Should allow owner to set TCB policy" , async function ( ) {
138+ await appAuth . setTcbPolicy ( testPolicy ) ;
139+ expect ( await appAuth . tcbPolicy ( ) ) . to . equal ( testPolicy ) ;
140+ } ) ;
141+
142+ it ( "Should emit TcbPolicySet event" , async function ( ) {
143+ await expect ( appAuth . setTcbPolicy ( testPolicy ) )
144+ . to . emit ( appAuth , "TcbPolicySet" )
145+ . withArgs ( testPolicy ) ;
146+ } ) ;
147+
148+ it ( "Should allow clearing TCB policy with empty string" , async function ( ) {
149+ await appAuth . setTcbPolicy ( testPolicy ) ;
150+ await appAuth . setTcbPolicy ( "" ) ;
151+ expect ( await appAuth . tcbPolicy ( ) ) . to . equal ( "" ) ;
152+ } ) ;
153+
154+ it ( "Should prevent non-owners from setting TCB policy" , async function ( ) {
155+ await expect (
156+ appAuth . connect ( user ) . setTcbPolicy ( testPolicy )
157+ ) . to . be . revertedWithCustomError ( appAuth , "OwnableUnauthorizedAccount" ) ;
158+ } ) ;
159+
160+ it ( "Should support IAppTcbPolicy interface (ERC-165)" , async function ( ) {
161+ // IAppTcbPolicy interfaceId = tcbPolicy() ^ setTcbPolicy(string)
162+ // We verify via supportsInterface
163+ const iface = new ethers . Interface ( [
164+ "function tcbPolicy() view returns (string)" ,
165+ "function setTcbPolicy(string)" ,
166+ ] ) ;
167+ const interfaceId =
168+ BigInt ( iface . getFunction ( "tcbPolicy" ) ! . selector ) ^
169+ BigInt ( iface . getFunction ( "setTcbPolicy" ) ! . selector ) ;
170+ const id = "0x" + ( interfaceId & BigInt ( "0xffffffff" ) ) . toString ( 16 ) . padStart ( 8 , "0" ) ;
171+ expect ( await appAuth . supportsInterface ( id ) ) . to . be . true ;
172+ } ) ;
173+ } ) ;
174+
130175 describe ( "Initialize with device and hash" , function ( ) {
131176 let appAuthWithData : DstackApp ;
132177 const testDevice = ethers . randomBytes ( 32 ) ;
@@ -251,3 +296,52 @@ describe("DstackApp", function () {
251296 } ) ;
252297 } ) ;
253298} ) ;
299+
300+ describe ( "DstackKms TCB policy" , function ( ) {
301+ let kmsContract : any ;
302+ let owner : SignerWithAddress ;
303+ let user : SignerWithAddress ;
304+
305+ beforeEach ( async function ( ) {
306+ [ owner , user ] = await ethers . getSigners ( ) ;
307+ kmsContract = await deployContract ( hre , "DstackKms" , [
308+ owner . address ,
309+ ethers . ZeroAddress ,
310+ ] , true ) ;
311+ } ) ;
312+
313+ it ( "Should return empty string by default" , async function ( ) {
314+ expect ( await kmsContract . tcbPolicy ( ) ) . to . equal ( "" ) ;
315+ } ) ;
316+
317+ it ( "Should allow owner to set TCB policy" , async function ( ) {
318+ const policy = '{"version":1,"intel_qal":[]}' ;
319+ await kmsContract . setTcbPolicy ( policy ) ;
320+ expect ( await kmsContract . tcbPolicy ( ) ) . to . equal ( policy ) ;
321+ } ) ;
322+
323+ it ( "Should emit TcbPolicySet event" , async function ( ) {
324+ const policy = '{"version":1}' ;
325+ await expect ( kmsContract . setTcbPolicy ( policy ) )
326+ . to . emit ( kmsContract , "TcbPolicySet" )
327+ . withArgs ( policy ) ;
328+ } ) ;
329+
330+ it ( "Should prevent non-owners from setting TCB policy" , async function ( ) {
331+ await expect (
332+ kmsContract . connect ( user ) . setTcbPolicy ( "test" )
333+ ) . to . be . revertedWithCustomError ( kmsContract , "OwnableUnauthorizedAccount" ) ;
334+ } ) ;
335+
336+ it ( "Should support IAppTcbPolicy interface (ERC-165)" , async function ( ) {
337+ const iface = new ethers . Interface ( [
338+ "function tcbPolicy() view returns (string)" ,
339+ "function setTcbPolicy(string)" ,
340+ ] ) ;
341+ const interfaceId =
342+ BigInt ( iface . getFunction ( "tcbPolicy" ) ! . selector ) ^
343+ BigInt ( iface . getFunction ( "setTcbPolicy" ) ! . selector ) ;
344+ const id = "0x" + ( interfaceId & BigInt ( "0xffffffff" ) ) . toString ( 16 ) . padStart ( 8 , "0" ) ;
345+ expect ( await kmsContract . supportsInterface ( id ) ) . to . be . true ;
346+ } ) ;
347+ } ) ;
0 commit comments