33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .assertj .core .api .Assertions .assertThatThrownBy ;
55
6- import com .contentgrid .appserver .contentstore .api .ContentReader ;
76import com .contentgrid .appserver .contentstore .api .ContentReference ;
87import com .contentgrid .appserver .contentstore .api .range .ResolvedContentRange ;
98import com .contentgrid .appserver .contentstore .impl .encryption .engine .ContentEncryptionEngine .EncryptionParameters ;
109import com .contentgrid .appserver .contentstore .impl .encryption .keys .KeyBytes ;
10+ import com .contentgrid .appserver .contentstore .impl .encryption .testing .ResourceContentReader ;
1111
12- import java .io .IOException ;
1312import java .io .InputStream ;
14- import java .util .Arrays ;
13+ import java .util .ArrayList ;
1514import java .util .HexFormat ;
1615import java .util .List ;
1716
1817import org .junit .jupiter .api .Test ;
1918import org .junit .jupiter .params .ParameterizedTest ;
2019import org .junit .jupiter .params .provider .Arguments ;
2120import org .junit .jupiter .params .provider .FieldSource ;
22- import org .junit .jupiter .params .provider .ValueSource ;
2321
2422class AlfrescoCompatibleEncryptionEngineTest
2523{
2624
27- // KEYS, RESOURCES, SIZES all correlate to another and must have consistent order
25+ // the following static variables correlate to another and must have consistent order
2826 // KEYS = symmetric encryption keys generated in an alfresco-simple-content-stores ACS instance
2927 // RESOURCES = encrypted/decrypted resources (lorem ipsum-like)
30- // SIZES = unencrypted resource sizes
28+ // ALGORITHMS = encryption algorithms used for resources
29+ // DECRYPTED_SIZES = unencrypted resource sizes
30+ // ENCRYPTED_SIZES = encrypted resource sizes
3131 private static final String [] KEYS = { "156c8bc259cd3e4ad1d9c38cf6361847" , "566cf243d7ee9cf69df6ec004bf5f35c" ,
3232 "2405ae5a16b3909abb9ece58833c08bc" , "72c51fcefd0a6da181dc4c98bbcc08e5" , "89cf56ab800a394d95c10548065aae5d" , "e39d23642ca81c68" ,
3333 "13320e7520e60e26133ef8372a0b7aad9e9e8a0bba5bf2ab" };
@@ -42,14 +42,27 @@ class AlfrescoCompatibleEncryptionEngineTest
4242 private static final long [] DECRYPTED_SIZES = { 6094l , 4240l , 4117l , 4162l , 1001l , 2859l , 5853l };
4343
4444 private static final long [] ENCRYPTED_SIZES = { 6096l , 4256l , 4128l , 4176l , 1008l , 2864l , 5856l };
45+
46+ private static final long [] START_BYTES = { 128l , 3096l , 1234l , 3210l , 512l , 1536l , 4096l };
47+
48+ private static final List <Arguments > fullComparison ;
49+ private static final List <Arguments > rangedComparison ;
50+ static {
51+ fullComparison = new ArrayList <>();
52+ rangedComparison = new ArrayList <>();
53+ for (int i = 0 ; i < KEYS .length ; i ++)
54+ {
55+ fullComparison .add (Arguments .of (ALGORITHMS [i ], KEYS [i ], RESOURCES [i ], DECRYPTED_SIZES [i ]));
56+ rangedComparison .add (Arguments .of (ALGORITHMS [i ], KEYS [i ], RESOURCES [i ], DECRYPTED_SIZES [i ], ENCRYPTED_SIZES [i ], START_BYTES [i ]));
57+ }
58+ }
4559
46- private static final List < Arguments > RANGED_ARGUMENTS = Arrays . asList ( Arguments . of ( 0 , 128l ), Arguments . of ( 1 , 3096l ),
47- Arguments . of ( 2 , 1234l ), Arguments . of ( 3 , 3210l ), Arguments . of ( 4 , 512l ), Arguments . of ( 5 , 1536l ), Arguments . of ( 6 , 4096l ) );
60+ // engine is stateless
61+ private final AlfrescoCompatibleEncryptionEngine engine = new AlfrescoCompatibleEncryptionEngine ( );
4862
4963 @ Test
5064 void decryptionOnly () throws Exception
5165 {
52- var engine = new AlfrescoCompatibleEncryptionEngine ();
5366 assertThatThrownBy (engine ::createNewParameters ).isInstanceOf (UnsupportedOperationException .class );
5467
5568 var params = new EncryptionParameters (DataEncryptionAlgorithm .of ("Alfresco-AES" ), KeyBytes .adopt (HexFormat .of ().parseHex (KEYS [0 ])),
@@ -65,8 +78,6 @@ void supportCheck()
6578 {
6679 // test various general algorithms and explicit modes
6780 // supported + unsupported
68-
69- var engine = new AlfrescoCompatibleEncryptionEngine ();
7081 assertThat (engine .supports (DataEncryptionAlgorithm .of ("AES" ))).isFalse ();
7182 assertThat (engine .supports (DataEncryptionAlgorithm .of ("Alfresco-AES" ))).isTrue ();
7283 assertThat (engine .supports (DataEncryptionAlgorithm .of ("Alfresco-AES/CTR/PKCS5Padding" ))).isFalse ();
@@ -108,35 +119,32 @@ public long getContentSize()
108119 }
109120
110121 @ ParameterizedTest
111- @ ValueSource ( ints = { 0 , 1 , 2 , 3 , 4 })
112- void fullComparison (int resIdx ) throws Exception
122+ @ FieldSource
123+ void fullComparison (String alg , String keyHex , String resource , long decryptedSize ) throws Exception
113124 {
114- var engine = new AlfrescoCompatibleEncryptionEngine ();
115- var alg = ALGORITHMS [resIdx ];
116- var params = new EncryptionParameters (DataEncryptionAlgorithm .of (alg ), KeyBytes .adopt (HexFormat .of ().parseHex (KEYS [resIdx ])),
125+ var params = new EncryptionParameters (DataEncryptionAlgorithm .of (alg ), KeyBytes .adopt (HexFormat .of ().parseHex (keyHex )),
117126 new byte [0 ]);
118127
119- var encryptedResource = "/alfresco/encrypted/" + RESOURCES [ resIdx ] ;
120- var decryptedResource = "/alfresco/decrypted/" + RESOURCES [ resIdx ] ;
128+ var encryptedResource = "/alfresco/encrypted/" + resource ;
129+ var decryptedResource = "/alfresco/decrypted/" + resource ;
121130
122131 var reader = engine .decrypt (r -> new ResourceContentReader (encryptedResource ), params ,
123- ResolvedContentRange .fullRange (DECRYPTED_SIZES [ resIdx ] ));
132+ ResolvedContentRange .fullRange (decryptedSize ));
124133
125134 assertThat (reader .getContentInputStream ())
126135 .hasSameContentAs (AlfrescoCompatibleEncryptionEngineTest .class .getResourceAsStream (decryptedResource ));
127136 }
128137
129138 @ ParameterizedTest
130- @ FieldSource ("RANGED_ARGUMENTS" )
131- void rangedComparison (int resIdx , long startByte ) throws Exception
139+ @ FieldSource
140+ void rangedComparison (String alg , String keyHex , String resource , long decryptedSize , long encryptedSize , long startByte )
141+ throws Exception
132142 {
133- var engine = new AlfrescoCompatibleEncryptionEngine ();
134- var alg = ALGORITHMS [resIdx ];
135- var params = new EncryptionParameters (DataEncryptionAlgorithm .of (alg ), KeyBytes .adopt (HexFormat .of ().parseHex (KEYS [resIdx ])),
143+ var params = new EncryptionParameters (DataEncryptionAlgorithm .of (alg ), KeyBytes .adopt (HexFormat .of ().parseHex (keyHex )),
136144 new byte [0 ]);
137145
138- var encryptedResource = "/alfresco/encrypted/" + RESOURCES [ resIdx ] ;
139- var decryptedResource = "/alfresco/decrypted/" + RESOURCES [ resIdx ] ;
146+ var encryptedResource = "/alfresco/encrypted/" + resource ;
147+ var decryptedResource = "/alfresco/decrypted/" + resource ;
140148
141149 ResolvedContentRange contentRange = new ResolvedContentRange ()
142150 {
@@ -151,17 +159,17 @@ public long getStartByte()
151159 @ Override
152160 public long getEndByteInclusive ()
153161 {
154- return DECRYPTED_SIZES [ resIdx ] - 1 ;
162+ return decryptedSize - 1 ;
155163 }
156164
157165 @ Override
158166 public long getContentSize ()
159167 {
160- return DECRYPTED_SIZES [ resIdx ] ;
168+ return decryptedSize ;
161169 }
162170 };
163171 var reader = engine .decrypt (r -> new ResourceContentReader (encryptedResource ), params , contentRange );
164- assertThat (reader .getContentSize ()).isEqualTo (ENCRYPTED_SIZES [ resIdx ] );
172+ assertThat (reader .getContentSize ()).isEqualTo (encryptedSize );
165173 assertThat (reader .getReference ()).isEqualTo (ContentReference .of (encryptedResource ));
166174 assertThat (reader .getDescription ()).isEqualTo ("Decrypted resource file " + encryptedResource );
167175
@@ -173,56 +181,4 @@ public long getContentSize()
173181 assertThat (is2 ).hasSameContentAs (is1 );
174182 }
175183 }
176-
177- private static class ResourceContentReader implements ContentReader
178- {
179-
180- private final String resourceName ;
181-
182- private final long contentSize ;
183-
184- ResourceContentReader (String resourceName )
185- {
186- this .resourceName = resourceName ;
187- long size = 0 ;
188- try (InputStream is = getContentInputStream ())
189- {
190- byte [] buf = new byte [1024 ];
191- int bytesRead = 0 ;
192- while ((bytesRead = is .read (buf )) != -1 )
193- {
194- size += bytesRead ;
195- }
196- }
197- catch (IOException ioex )
198- {
199- throw new RuntimeException (ioex );
200- }
201- this .contentSize = size ;
202- }
203-
204- @ Override
205- public ContentReference getReference ()
206- {
207- return ContentReference .of (this .resourceName );
208- }
209-
210- @ Override
211- public String getDescription ()
212- {
213- return "resource file " + this .resourceName ;
214- }
215-
216- @ Override
217- public long getContentSize ()
218- {
219- return this .contentSize ;
220- }
221-
222- @ Override
223- public InputStream getContentInputStream ()
224- {
225- return AlfrescoCompatibleEncryptionEngineTest .class .getResourceAsStream (resourceName );
226- }
227- }
228184}
0 commit comments