/* Ċ Ġ* ĠCopyright Ġ( c ) Ġ 2 0 2 0 , ĠTw ilio ĠInc . Ċ Ġ*/ Ċ package Ġcom . tw ilio . security . storage . key Ċ Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġcom . tw ilio . security . crypto . Key Exception Ċ import Ġcom . tw ilio . security . crypto . Key Manager Ċ import Ġcom . tw ilio . security . crypto . key . cipher . Algorithm Parameters Spec Ċ import Ġcom . tw ilio . security . crypto . key . cipher . Cipher Ċ import Ġcom . tw ilio . security . crypto . key . cipher . Encrypted Data Ċ import Ġcom . tw ilio . security . crypto . key . cipher . from ByteArray Ċ import Ġcom . tw ilio . security . crypto . key . cipher . toByteArray Ċ import Ġcom . tw ilio . security . crypto . key . template . Cipher Template Ċ import Ġkotlin . random . Random . Default . next Bytes Ċ import Ġorg . junit . Assert . assertEquals Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . ro bo lectric . Ro bo lectric TestRunner Ċ Ċ @ RunWith ( Ro bo lectric TestRunner :: class ) Ċ class ĠSecret Key Cipher Test Ġ{ ĊĊĠ Ġprivate Ġval Ġkey Manager : ĠKey Manager Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġtemplate : ĠCipher Template Ġ= Ġmock () ĊĠ Ġprivate Ġlateinit Ġvar Ġsecret Key Cipher : ĠSecret Key Cipher ĊĊĠ Ġ@ Before ĊĠ Ġfun Ġsetup () Ġ{ ĊĠĠĠ Ġsecret Key Cipher Ġ= ĠSecret Key Cipher ( template , Ġkey Manager ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Create _ should Call Key Manager () Ġ{ ĊĠĠĠ Ġsecret Key Cipher . create () ĊĠĠĠ Ġverify ( key Manager ). cipher ( template . template For Creation ()) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Encrypt _ with Data _ should Return Encrypted () Ġ{ ĊĠĠĠ Ġval Ġdata Ġ= ĠByteArray ( 5 ). apply Ġ{ Ġnext Bytes ( this ) Ġ} ĊĠĠĠ Ġval Ġcipher : ĠCipher Ġ= Ġmock () ĊĠĠĠ Ġval Ġexpected Encrypted Data Ġ= ĠEn crypted Data ( Algorithm Parameters Spec ( data , Ġ" test ", Ġ" test "), Ġdata ) ĊĠĠĠ Ġwhenever ( key Manager . cipher ( template )). thenReturn ( cipher ) ĊĠĠĠ Ġwhenever ( cipher . encrypt ( data )). thenReturn ( expected Encrypted Data ) ĊĠĠĠ Ġval Ġencrypted Ġ= Ġsecret Key Cipher . encrypt ( data ) ĊĠĠĠ Ġval Ġencrypted Data Ġ= Ġfrom ByteArray ( encrypted ) ĊĠĠĠ ĠassertEquals ( expected Encrypted Data , Ġencrypted Data ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ( expected Ġ= ĠKey Exception :: class ) ĊĠ Ġfun Ġtest Encrypt _ with Error Encrypt ing _ should Throw Error () Ġ{ ĊĠĠĠ Ġval Ġdata Ġ= ĠByteArray ( 5 ). apply Ġ{ Ġnext Bytes ( this ) Ġ} ĊĠĠĠ Ġval Ġcipher : ĠCipher Ġ= Ġmock () ĊĠĠĠ Ġval Ġexception : ĠKey Exception Ġ= Ġmock () ĊĠĠĠ Ġwhenever ( key Manager . cipher ( template )). thenReturn ( cipher ) ĊĠĠĠ Ġwhenever ( cipher . encrypt ( data )). then Throw ( exception ) ĊĠĠĠ Ġsecret Key Cipher . encrypt ( data ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Decrypt _ with Data _ should Return De crypted () Ġ{ ĊĠĠĠ Ġval Ġexpected Data Ġ= ĠByteArray ( 5 ). apply Ġ{ Ġnext Bytes ( this ) Ġ} ĊĠĠĠ Ġval Ġencrypted Data Ġ= ĊĠĠĠĠĠ ĠEn crypted Data ( Algorithm Parameters Spec ( expected Data , Ġ" test ", Ġ" test "), Ġexpected Data ) ĊĠĠĠ Ġval Ġserialized Encrypted Data Ġ= Ġto ByteArray ( encrypted Data ) ĊĠĠĠ Ġval Ġcipher : ĠCipher Ġ= Ġmock () ĊĠĠĠ Ġwhenever ( key Manager . cipher ( template )). thenReturn ( cipher ) ĊĠĠĠ Ġwhenever ( cipher . decrypt ( encrypted Data )). thenReturn ( expected Data ) ĊĠĠĠ Ġval Ġdata Ġ= Ġsecret Key Cipher . decrypt ( serialized Encrypted Data ) ĊĠĠĠ ĠassertEquals ( expected Data , Ġdata ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ( expected Ġ= ĠKey Exception :: class ) ĊĠ Ġfun Ġtest Decrypt _ with Error Encrypt ing _ should Throw Error () Ġ{ ĊĠĠĠ Ġval Ġexpected Data Ġ= ĠByteArray ( 5 ). apply Ġ{ Ġnext Bytes ( this ) Ġ} ĊĠĠĠ Ġval Ġencrypted Data Ġ= ĊĠĠĠĠĠ ĠEn crypted Data ( Algorithm Parameters Spec ( expected Data , Ġ" test ", Ġ" test "), Ġexpected Data ) ĊĠĠĠ Ġval Ġserialized Encrypted Data Ġ= Ġto ByteArray ( encrypted Data ) ĊĠĠĠ Ġval Ġcipher : ĠCipher Ġ= Ġmock () ĊĠĠĠ Ġval Ġexception : ĠKey Exception Ġ= Ġmock () ĊĠĠĠ Ġwhenever ( key Manager . cipher ( template )). thenReturn ( cipher ) ĊĠĠĠ Ġwhenever ( cipher . decrypt ( encrypted Data )). then Throw ( exception ) ĊĠĠĠ Ġsecret Key Cipher . decrypt ( serialized Encrypted Data ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Delete _ should Call Key Manager () Ġ{ ĊĠĠĠ Ġval Ġalias Ġ= Ġ" alias " ĊĠĠĠ Ġwhenever ( template . alias ). thenReturn ( alias ) ĊĠĠĠ Ġsecret Key Cipher . delete () ĊĠĠĠ Ġverify ( key Manager ). delete ( alias ) ĊĠ Ġ} Ċ } Ċ