Cipherpack v1.2.0-dirty
A Cryprographic Stream Processor
Test01Cipherpack.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package test.org.cipherpack;
26
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.FileOutputStream;
30import java.io.IOException;
31import java.io.InputStream;
32import java.io.OutputStream;
33import java.nio.charset.Charset;
34import java.nio.file.Path;
35import java.nio.file.Paths;
36import java.time.Instant;
37import java.time.temporal.ChronoUnit;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.List;
41
42import org.cipherpack.CPFactory;
43import org.cipherpack.Cipherpack;
44import org.cipherpack.CipherpackListener;
45import org.cipherpack.CryptoConfig;
46import org.cipherpack.PackHeader;
47import org.jau.fs.CopyOptions;
48import org.jau.fs.FileStats;
49import org.jau.fs.FileUtil;
50import org.jau.fs.TraverseOptions;
51import org.jau.io.ByteInStream;
52import org.jau.io.ByteInStreamUtil;
53import org.jau.io.ByteInStream_Feed;
54import org.jau.io.ByteInStream_File;
55import org.jau.io.ByteInStream_URL;
56import org.jau.io.PrintUtil;
57import org.jau.util.BasicTypes;
58import org.junit.AfterClass;
59import org.junit.Assert;
60import org.junit.Assume;
61import org.junit.Before;
62import org.junit.FixMethodOrder;
63import org.junit.Test;
64import org.junit.runners.MethodSorters;
65
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
67public class Test01Cipherpack extends data_test {
68 static final boolean DEBUG = false;
69
70 static final String plaintext_version = "0";
71 static final String plaintext_version_parent = "0";
72
73 static final int IDX_0B = 0;
74 static final int IDX_1B = 1;
75 static final int IDX_11kiB = 2;
76 static final int IDX_xbuffersz = 3;
77 static final int IDX_xbuffersz_minus_tag = 4;
78 static final int IDX_xbuffersz_plus_tag = 5;
79 static final int IDX_46MiB = 6;
80 static final int IDX_65MiB = 7;
81
82 static List<String> fname_plaintext_lst = new ArrayList<String>();
83 static List<String> fname_encrypted_lst = new ArrayList<String>();
84 static List<String> fname_decrypted_lst = new ArrayList<String>();
85
86 static String single_test_name = null;
87
88 static boolean remove_file(final String name) {
89 final File file = new File( name );
90 try {
91 if( file.exists() ) {
92 if( !file.delete() ) {
93 PrintUtil.println(System.err, "Remove.1: Error: Failed deletion of existing file "+name);
94 return false;
95 }
96 }
97 return true;
98 } catch (final Exception ex) {
99 PrintUtil.println(System.err, "Remove.2: Error: Failed deletion of existing file "+name+": "+ex.getMessage());
100 ex.printStackTrace();
101 }
102 return false;
103 }
104 static void add_test_file(final String name, final long size) {
105 Assert.assertTrue( remove_file(name) );
106 Assert.assertTrue( remove_file(name+".enc") );
107 Assert.assertTrue( remove_file(name+".enc.dec") );
108 {
109 final String one_line = "Hello World, this is a test and I like it. Exactly 100 characters long. 0123456780 abcdefghjklmnop..";
110 final Charset charset = Charset.forName("ASCII");
111 final byte[] one_line_bytes = one_line.getBytes(charset);
112
113 final File file = new File( name );
114 Assert.assertFalse( file.exists() );
115
116 try( final OutputStream out = new FileOutputStream(file); ) {
117
118 long written=0;
119 for(; written+one_line_bytes.length <= size; written+=+one_line_bytes.length) {
120 out.write( one_line_bytes );
121 }
122 for(; size-written > 0; ++written ) {
123 out.write( (byte)'_' );
124 }
125 } catch (final Exception ex) {
126 PrintUtil.println(System.err, "Error: Write test file: Failed "+name+": "+ex.getMessage());
127 ex.printStackTrace();
128 }
129 }
130 final FileStats stats = new FileStats(name);
131 Assert.assertTrue( stats.is_file() );
132 Assert.assertEquals( size, stats.size() );
133 fname_plaintext_lst.add(name);
134 fname_encrypted_lst.add(name+".enc");
135 fname_decrypted_lst.add(name+".enc.dec");
136 }
137
138 static {
140 final int buffer_size = 16384;
141 int i=0;
142
143 // Zero size .. Single finish chunk of less than buffer_size including the 16 bytes TAG
144 add_test_file("test_cipher_0"+(i++)+"_0B.bin", 0);
145
146 // Zero size .. Single finish chunk of less than buffer_size including the 16 bytes TAG
147 add_test_file("test_cipher_0"+(i++)+"_1B.bin", 1);
148
149 // Single finish chunk of less than buffer_size including the 16 bytes TAG
150 add_test_file("test_cipher_0"+(i++)+"_11kiB.bin", 1024*11+1);
151
152 // Will end up in a finish chunk of just 16 bytes TAG
153 final long xbuffersz = 4 * buffer_size;
154 add_test_file("test_cipher_0"+(i++)+"_xbuffsz_"+(xbuffersz/1024)+"kiB.bin", xbuffersz);
155
156 // Will end up in a finish chunk of buffer_size including the 16 bytes TAG
157 final long xbuffersz_minus = 4 * buffer_size - 16;
158 add_test_file("test_cipher_0"+(i++)+"_xbuffsz_"+(xbuffersz/1024)+"kiB_sub16.bin", xbuffersz_minus);
159
160 // Will end up in a finish chunk of 1 byte + 16 bytes TAG
161 final long xbuffersz_plus = 4 * buffer_size + 1;
162 add_test_file("test_cipher_0"+(i++)+"_xbuffsz_"+(xbuffersz/1024)+"kiB_add1.bin", xbuffersz_plus);
163
164 // 46MB Bug 574: Plaintext size: 48,001,024, encrypted size 48,007,099
165 add_test_file("test_cipher_0"+(i++)+"_46MiB.bin", 48001024);
166
167 // 65MB big file: Will end up in a finish chunk of 1 byte + 16 bytes TAG, 4160 chunks @ 16384
168 add_test_file("test_cipher_0"+(i++)+"_65MiB.bin", 48007099); // 1024*1024*65+1);
169 }
170
171 static boolean system(final String[] command) {
172 Process proc = null;
173 try{
174 proc = Runtime.getRuntime().exec(command);
175 proc.waitFor();
176 return true;
177 }
178 catch(final Exception ex)
179 {
180 if(proc!=null) {
181 proc.destroy();
182 }
183 ex.printStackTrace();
184 }
185 return false;
186 }
187
188 static final String mini_httpd_exe() {
189 final String os_name = System.getProperty("os.name");
190 if( "FreeBSD".equals(os_name) ) {
191 return "/usr/local/sbin/mini_httpd";
192 } else {
193 return "/usr/sbin/mini_httpd";
194 }
195 }
196
197 @AfterClass
198 public static void httpd_stop() {
199 if( org.jau.io.UriTk.protocol_supported("http:") ) {
200 Assert.assertTrue( system(new String[]{"killall", "mini_httpd"}) );
201 }
202 }
203
204 static void httpd_start() {
205 if( org.jau.io.UriTk.protocol_supported("http:") ) {
206 Assert.assertTrue( system(new String[]{"killall", "mini_httpd"}) );
207 final Path path = Paths.get("");
208 final String directoryName = path.toAbsolutePath().toString();
209 final String[] cmd = new String[]{mini_httpd_exe(), "-p", "8080", "-l", directoryName+"/mini_httpd.log"};
210 PrintUtil.fprintf_td(System.err, "%s\n", Arrays.toString(cmd));
211 Assert.assertTrue( system(cmd) );
212 }
213 }
214
215 public static void hash_retest(final String hashAlgo,
216 final String origFile, final byte[] hashValue_p1,
217 final String hashedDescryptedFile, final byte[] hashValue_p2)
218 {
219 Assert.assertArrayEquals(hashValue_p1, hashValue_p2);
220
221 final String suffix = Cipherpack.HashUtil.fileSuffix(hashAlgo);
222 final String outFile = hashedDescryptedFile + "." + suffix;
223 FileUtil.remove(outFile, TraverseOptions.none);
224
225 Assert.assertTrue( Cipherpack.HashUtil.appendToFile(outFile, hashedDescryptedFile, hashAlgo, hashValue_p2) );
226
227 final ByteInStream origIn = ByteInStreamUtil.to_ByteInStream(origFile);
228 Assert.assertNotNull( origIn );
229 final Instant t0 = org.jau.sys.Clock.getMonotonicTime();
230 final byte[] origHashValue = Cipherpack.HashUtil.calc(hashAlgo, origIn);
231 Assert.assertNotNull( origHashValue );
232 Assert.assertArrayEquals(hashValue_p2, origHashValue);
233
234 final Instant t1 = org.jau.sys.Clock.getMonotonicTime();
235 final long td_ms = t0.until(t1, ChronoUnit.MILLIS);
236 ByteInStreamUtil.print_stats("Hash '"+hashAlgo+"'", origIn.content_size(), td_ms);
237 PrintUtil.fprintf_td(System.err, "\n");
238 }
239
240 public static void hash_retest(final String hashAlgo,
241 final String origFile,
242 final String hashedDescryptedFile, final byte[] hashValue_p2)
243 {
244 final String suffix = Cipherpack.HashUtil.fileSuffix(hashAlgo);
245 final String outFile = hashedDescryptedFile + "." + suffix;
246 FileUtil.remove(outFile, TraverseOptions.none);
247
248 Assert.assertTrue( Cipherpack.HashUtil.appendToFile(outFile, hashedDescryptedFile, hashAlgo, hashValue_p2) );
249
250 final ByteInStream origIn = ByteInStreamUtil.to_ByteInStream(origFile);
251 Assert.assertNotNull( origIn );
252 final Instant t0 = org.jau.sys.Clock.getMonotonicTime();
253 final byte[] origHashValue = Cipherpack.HashUtil.calc(hashAlgo, origIn);
254 Assert.assertNotNull( origHashValue );
255 Assert.assertArrayEquals(hashValue_p2, origHashValue);
256
257 final Instant t1 = org.jau.sys.Clock.getMonotonicTime();
258 final long td_ms = t0.until(t1, ChronoUnit.MILLIS);
259 ByteInStreamUtil.print_stats("Hash '"+hashAlgo+"'", origIn.content_size(), td_ms);
260 PrintUtil.fprintf_td(System.err, "\n");
261 }
262
263 @Before
264 public final void testFilter() {
265 if( null != single_test_name && !single_test_name.equals(getTestMethodName()) ) {
266 System.err.println("++++ TestFilter: Disabled "+getFullTestName(" - "));
267 Assume.assumeTrue(false);
268 } else {
269 System.err.println("++++ TestFilter: Enabled "+getFullTestName(" - "));
270 }
271 }
272
273 @Test(timeout = 120000)
274 public final void test00_enc_dec_file_single() {
276 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
277 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
278 {
279 final int file_idx = IDX_11kiB;
280 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test00.enc", true /* send_content */);
281 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test00.dec", true /* send_content */);
282 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
284 enc_pub_keys,
285 sign_sec_key1_fname, sign_sec_key_passphrase,
286 source,
287 fname_plaintext_lst.get(file_idx), "test00_enc_dec_file_single", plaintext_version, plaintext_version_parent,
288 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
289 PrintUtil.fprintf_td(System.err, "test00_enc_dec_file_single: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
290 PrintUtil.fprintf_td(System.err, "test00_enc_dec_file_single: %s\n", ph1.toString(true, true));
291 Assert.assertTrue( "test00_enc: "+ph1.toString(), ph1.isValid() );
292 enc_listener.check_counter_end(source.content_size());
293
294 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
295 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
296 enc_stream,
297 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
298 PrintUtil.fprintf_td(System.err, "test00_enc_dec_file_single: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
299 PrintUtil.fprintf_td(System.err, "test00_enc_dec_file_single: %s\n", ph2.toString(true, true));
300 Assert.assertTrue( "test00_dec: "+ph2.toString(), ph2.isValid() );
301 dec_listener.check_counter_end(ph2.plaintext_size);
302 }
303 }
304
305 @Test(timeout = 120000)
306 public final void test01_enc_dec_all_files() {
308 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
309 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
310 for(int file_idx = 0; file_idx < fname_plaintext_lst.size(); ++file_idx) {
311 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test01.enc."+file_idx, true /* send_content */);
312 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
314 enc_pub_keys,
315 sign_sec_key1_fname, sign_sec_key_passphrase,
316 source,
317 fname_plaintext_lst.get(file_idx), "test01_enc_dec_all_files", plaintext_version, plaintext_version_parent,
318 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
319 PrintUtil.fprintf_td(System.err, "test01_enc_dec_all_files: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
320 PrintUtil.fprintf_td(System.err, "test01_enc_dec_all_files: %s\n", ph1.toString(true, true));
321 Assert.assertTrue( "test01_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
322 enc_listener.check_counter_end(source.content_size());
323
324 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test01.dec."+file_idx, true /* send_content */);
325 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
326 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
327 enc_stream,
328 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
329 PrintUtil.fprintf_td(System.err, "test01_enc_dec_all_files: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
330 PrintUtil.fprintf_td(System.err, "test01_enc_dec_all_files: %s\n", ph2.toString(true, true));
331 Assert.assertTrue( "test01_dec: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
332 dec_listener.check_counter_end(ph2.plaintext_size);
333 hash_retest(ph1.plaintext_hash_algo,
334 fname_plaintext_lst.get(file_idx), ph1.plaintext_hash,
335 fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
336 }
337 }
338
339 @Test(timeout = 120000)
340 public final void test02_enc_dec_file_misc() {
342 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
343 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
344 {
345 final int file_idx = IDX_11kiB;
346 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test02.enc.1."+file_idx);
347 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
349 enc_pub_keys,
350 sign_sec_key1_fname, sign_sec_key_passphrase,
351 source,
352 fname_plaintext_lst.get(file_idx), "test02_enc_dec_file_misc", plaintext_version, plaintext_version_parent,
353 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
354 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
355 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: %s\n", ph1.toString(true, true));
356 Assert.assertTrue( "test02_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
357 enc_listener.check_counter_end();
358
359 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test02.dec.1."+file_idx);
360 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
361 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
362 enc_stream,
363 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
364 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
365 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: %s\n", ph2.toString(true, true));
366 Assert.assertTrue( "test02_dec: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
367 dec_listener.check_counter_end();
368 }
369 {
370 final int file_idx = IDX_11kiB;
371 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test02.enc.1."+file_idx);
372 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
374 enc_pub_keys,
375 sign_sec_key2_fname, sign_sec_key_passphrase,
376 source,
377 fname_plaintext_lst.get(file_idx), "test02_enc_dec_file_misc", plaintext_version, plaintext_version_parent,
378 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
379 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
380 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: %s\n", ph1.toString(true, true));
381 Assert.assertTrue( "test02_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
382 enc_listener.check_counter_end();
383
384 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test02.dec.1."+file_idx);
385 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
386 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key2_fname, dec_sec_key_passphrase,
387 enc_stream,
388 dec_listener, Cipherpack.default_hash_algo(), fname_decrypted_lst.get(file_idx));
389 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
390 PrintUtil.fprintf_td(System.err, "test02_enc_dec_file_misc: %s\n", ph2.toString(true, true));
391 Assert.assertTrue( "test02_dec: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
392 dec_listener.check_counter_end();
393 }
394 }
395
396 @Test(timeout = 120000)
397 public final void test03_enc_dec_file_perf() {
399 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
400 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
401
402 {
403 final int file_idx = IDX_65MiB;
404 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test03.enc."+file_idx);
405 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
407 enc_pub_keys,
408 sign_sec_key3_fname, sign_sec_key_passphrase,
409 source,
410 fname_plaintext_lst.get(file_idx), "test03_enc_dec_file_perf", plaintext_version, plaintext_version_parent,
411 enc_listener, "", fname_encrypted_lst.get(file_idx));
412 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
413 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: %s\n", ph1.toString(true, true));
414 Assert.assertTrue( "test03_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
415 enc_listener.check_counter_end();
416
417 {
418 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test03.dec.1."+file_idx);
419 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
420 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
421 enc_stream,
422 dec_listener, "", fname_decrypted_lst.get(file_idx));
423 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
424 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: %s\n", ph2.toString(true, true));
425 Assert.assertTrue( "test03_dec.1: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
426 dec_listener.check_counter_end();
427 }
428 {
429 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test03.dec.2."+file_idx);
430 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
431 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
432 enc_stream,
433 dec_listener, "SHA-256", fname_decrypted_lst.get(file_idx));
434 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
435 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: %s\n", ph2.toString(true, true));
436 Assert.assertTrue( "test03_dec.2: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
437 dec_listener.check_counter_end();
438 hash_retest(ph2.plaintext_hash_algo,
439 fname_plaintext_lst.get(file_idx),
440 fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
441 }
442 {
443 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test03.dec.3."+file_idx);
444 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
445 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
446 enc_stream,
447 dec_listener, "SHA-512", fname_decrypted_lst.get(file_idx));
448 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
449 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: %s\n", ph2.toString(true, true));
450 Assert.assertTrue( "test03_dec.3: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
451 dec_listener.check_counter_end();
452 hash_retest(ph2.plaintext_hash_algo,
453 fname_plaintext_lst.get(file_idx),
454 fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
455 }
456 {
457 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test03.dec.4."+file_idx);
458 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
459 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
460 enc_stream,
461 dec_listener, "BLAKE2b(512)", fname_decrypted_lst.get(file_idx));
462 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
463 PrintUtil.fprintf_td(System.err, "test03_enc_dec_file_perf: %s\n", ph2.toString(true, true));
464 Assert.assertTrue( "test03_dec.4: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
465 dec_listener.check_counter_end();
466 hash_retest(ph2.plaintext_hash_algo,
467 fname_plaintext_lst.get(file_idx),
468 fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
469 }
470 }
471 }
472
473 @Test(timeout = 120000)
474 public final void test04_enc_dec_file_error() {
476
477 final int file_idx = IDX_11kiB;
478 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test04.enc."+file_idx);
479 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
480 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
482 enc_pub_keys,
483 sign_sec_key1_fname, sign_sec_key_passphrase,
484 source,
485 fname_plaintext_lst.get(file_idx), "test_case", plaintext_version, plaintext_version_parent,
486 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
487 PrintUtil.fprintf_td(System.err, "test04_enc_dec_file_error: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
488 PrintUtil.fprintf_td(System.err, "test04_enc_dec_file_error: %s\n", ph1.toString(true, true));
489 Assert.assertTrue( "test04_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
490 enc_listener.check_counter_end();
491
492 {
493 // Error: Not encrypted for terminal key 4
494 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test04.dec.e1."+file_idx);
495 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
496 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
497 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key4_fname, dec_sec_key_passphrase,
498 enc_stream,
499 dec_listener, Cipherpack.default_hash_algo(), fname_decrypted_lst.get(file_idx));
500 PrintUtil.fprintf_td(System.err, "test04_enc_dec_file_error: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
501 PrintUtil.fprintf_td(System.err, "test04_enc_dec_file_error: %s\n", ph2.toString(true, true));
502 Assert.assertFalse( "test04_dec.e1: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
503 dec_listener.check_counter_end();
504 }
505 {
506 // Error: Not signed from host key 4
507 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test04.dec.e2."+file_idx);
508 final List<String> sign_pub_keys_nope = Arrays.asList(sign_pub_key4_fname);
509 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
510 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys_nope, dec_sec_key3_fname, dec_sec_key_passphrase,
511 enc_stream,
512 dec_listener, Cipherpack.default_hash_algo(), fname_decrypted_lst.get(file_idx));
513 PrintUtil.fprintf_td(System.err, "test04_enc_dec_file_error: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
514 PrintUtil.fprintf_td(System.err, "test04_enc_dec_file_error: %s\n", ph2.toString(true, true));
515 Assert.assertFalse( "test04_dec.e2: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
516 dec_listener.check_counter_end();
517 }
518 }
519
520 @Test(timeout = 120000)
521 public final void test11_dec_http_all_files() {
523 if( !org.jau.io.UriTk.protocol_supported("http:") ) {
524 PrintUtil.fprintf_td(System.err, "http not supported, abort\n");
525 return;
526 }
527 httpd_start();
528
529 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
530 for(int file_idx = 0; file_idx < fname_plaintext_lst.size(); ++file_idx) {
531 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test11.enc."+file_idx);
532 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
534 enc_pub_keys,
535 sign_sec_key1_fname, sign_sec_key_passphrase,
536 source,
537 fname_plaintext_lst.get(file_idx), "test11_dec_http_all_files", plaintext_version, plaintext_version_parent,
538 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
539 PrintUtil.fprintf_td(System.err, "test11_dec_http_all_files: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
540 PrintUtil.fprintf_td(System.err, "test11_dec_http_all_files: %s\n", ph1.toString(true, true));
541 Assert.assertTrue( "test11_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
542 enc_listener.check_counter_end();
543
544 final String uri_encrypted = url_input_root + fname_encrypted_lst.get(file_idx);
545 final String file_decrypted = fname_encrypted_lst.get(file_idx)+".dec";
546
547 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
548 {
549 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test11.dec."+file_idx);
550 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
551 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
552 enc_stream,
553 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
554 PrintUtil.fprintf_td(System.err, "test11_dec_http_all_files: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
555 PrintUtil.fprintf_td(System.err, "test11_dec_http_all_files: %s\n", ph2.toString(true, true));
556 Assert.assertTrue( "test11_dec: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
557 dec_listener.check_counter_end();
558 }
559 }
560 }
561
562 @Test(timeout = 120000)
563 public final void test12_dec_http_misc() {
565 if( !org.jau.io.UriTk.protocol_supported("http:") ) {
566 PrintUtil.fprintf_td(System.err, "http not supported, abort\n");
567 return;
568 }
569 httpd_start();
570
571 final int file_idx = IDX_11kiB;
572 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
573 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test12.dec."+file_idx);
574 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
576 enc_pub_keys,
577 sign_sec_key1_fname, sign_sec_key_passphrase,
578 source,
579 fname_plaintext_lst.get(file_idx), "test12_dec_http_misc", plaintext_version, plaintext_version_parent,
580 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
581 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
582 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: %s\n", ph1.toString(true, true));
583 Assert.assertTrue( "test12_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
584 enc_listener.check_counter_end();
585
586 final String uri_encrypted = url_input_root + fname_encrypted_lst.get(file_idx);
587 final String file_decrypted = fname_encrypted_lst.get(file_idx)+".dec";
588
589 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
590 {
591 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test12.dec.1."+file_idx);
592 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
593 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
594 enc_stream,
595 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
596 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
597 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: %s\n", ph2.toString(true, true));
598 Assert.assertTrue( "test12_dec.1: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
599 dec_listener.check_counter_end();
600 }
601 {
602 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test12.dec.2."+file_idx);
603 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
604 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key2_fname, dec_sec_key_passphrase,
605 enc_stream,
606 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
607 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
608 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: %s\n", ph2.toString(true, true));
609 Assert.assertTrue( "test12_dec.2: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
610 dec_listener.check_counter_end();
611 }
612 {
613 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test12.dec.3."+file_idx);
614 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
615 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
616 enc_stream,
617 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
618 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
619 PrintUtil.fprintf_td(System.err, "test12_dec_http_misc: %s\n", ph2.toString(true, true));
620 Assert.assertTrue( "test12_dec.3: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
621 dec_listener.check_counter_end();
622 }
623 }
624
625 @Test(timeout = 120000)
626 public final void test13_dec_http_perf() {
628 if( !org.jau.io.UriTk.protocol_supported("http:") ) {
629 PrintUtil.fprintf_td(System.err, "http not supported, abort\n");
630 return;
631 }
632 httpd_start();
633
634 final int file_idx = IDX_65MiB;
635 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
636 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test13.enc."+file_idx);
637 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
639 enc_pub_keys,
640 sign_sec_key1_fname, sign_sec_key_passphrase,
641 source,
642 fname_plaintext_lst.get(file_idx), "test13_dec_http_perf", plaintext_version, plaintext_version_parent,
643 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
644 PrintUtil.fprintf_td(System.err, "test13_dec_http_perf: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
645 PrintUtil.fprintf_td(System.err, "test13_dec_http_perf: %s\n", ph1.toString(true, true));
646 Assert.assertTrue( "test13_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
647 enc_listener.check_counter_end();
648
649 final String uri_encrypted = url_input_root + fname_encrypted_lst.get(file_idx);
650 final String file_decrypted = fname_encrypted_lst.get(file_idx)+".dec";
651
652 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
653 {
654 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test13.dec.1."+file_idx);
655 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
656 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
657 enc_stream,
658 dec_listener, ph1.plaintext_hash_algo, file_decrypted);
659 PrintUtil.fprintf_td(System.err, "test13_dec_http_perf: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
660 PrintUtil.fprintf_td(System.err, "test13_dec_http_perf: %s\n", ph2.toString(true, true));
661 Assert.assertTrue( "test13_dec.1: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
662 dec_listener.check_counter_end();
663
664 hash_retest(ph1.plaintext_hash_algo,
665 fname_plaintext_lst.get(file_idx), ph1.plaintext_hash,
666 fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
667 }
668 {
669 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test13.dec.2."+file_idx);
670 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
671 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
672 enc_stream,
673 dec_listener, "", file_decrypted);
674 PrintUtil.fprintf_td(System.err, "test13_dec_http_perf: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
675 PrintUtil.fprintf_td(System.err, "test13_dec_http_perf: %s\n", ph2.toString(true, true));
676 Assert.assertTrue( "test13_dec.2: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
677 dec_listener.check_counter_end();
678 }
679 }
680
681 @Test(timeout = 120000)
682 public final void test14_dec_http_error() {
684 if( !org.jau.io.UriTk.protocol_supported("http:") ) {
685 PrintUtil.fprintf_td(System.err, "http not supported, abort\n");
686 return;
687 }
688 httpd_start();
689
690 final int file_idx = IDX_11kiB;
691 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
692 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test14.enc."+file_idx);
693 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
695 enc_pub_keys,
696 sign_sec_key1_fname, sign_sec_key_passphrase,
697 source,
698 fname_plaintext_lst.get(file_idx), "test14_dec_http_error", plaintext_version, plaintext_version_parent,
699 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
700 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
701 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: %s\n", ph1.toString(true, true));
702 Assert.assertTrue( "test14_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
703 enc_listener.check_counter_end();
704
705 final String uri_encrypted = url_input_root + fname_encrypted_lst.get(file_idx);
706 final String file_decrypted = fname_encrypted_lst.get(file_idx)+".dec";
707
708 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
709 {
710 // Error: Not encrypted for terminal key 4
711 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test14.dec.e1."+file_idx);
712 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
713 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key4_fname, dec_sec_key_passphrase,
714 enc_stream,
715 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
716 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
717 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: %s\n", ph2.toString(true, true));
718 Assert.assertFalse( "test14_dec.e1: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
719 dec_listener.check_counter_end();
720 }
721 {
722 // Error: Not signed from host key 4
723 final List<String> sign_pub_keys_nope = Arrays.asList( sign_pub_key4_fname );
724 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test14.dec.e2."+file_idx);
725 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
726 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys_nope, dec_sec_key1_fname, dec_sec_key_passphrase,
727 enc_stream,
728 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
729 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
730 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: %s\n", ph2.toString(true, true));
731 Assert.assertFalse( "test14_dec.e2: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
732 dec_listener.check_counter_end();
733 }
734 {
735 // Error: URL file doesn't exist
736 final String uri_encrypted_err = url_input_root + "doesnt_exists.enc";
737 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test14.dec.e3."+file_idx);
738 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted_err, io_timeout);
739 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
740 enc_stream,
741 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
742 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
743 PrintUtil.fprintf_td(System.err, "test14_dec_http_error: %s\n", ph2.toString(true, true));
744 Assert.assertFalse( "test14_dec.e3: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
745 dec_listener.check_counter_end();
746 }
747 }
748
749 static final int slow_buffer_sz = 1024;
750 static final long slow_delay_ms = 8;
751
752 static Thread executeOffThread(final Runnable runobj, final String threadName, final boolean detach) {
753 final Thread t = new Thread( runobj, threadName );
754 t.setDaemon( detach );
755 t.start();
756 return t;
757 }
758
759 static interface FeederFunc {
760 void feed(final ByteInStream_Feed enc_feed);
761 };
762
763 // throttled, no content size, interruptReader() via set_eof() will avoid timeout
764 static FeederFunc feed_source_00_nosize_slow = new FeederFunc() {
765 @Override
766 public void feed(final ByteInStream_Feed enc_feed) {
767 // long xfer_total = 0;
768 final File enc_stream = new File(enc_feed.id());
769 Assert.assertTrue( enc_stream.exists() );
770
771 InputStream in = null;
772 try {
773 in = new FileInputStream(enc_stream);
774 final byte[] buffer = new byte[slow_buffer_sz];
775 while( in.available() > 0 ) {
776 final int count = in.read(buffer);
777 if( 0 < count ) {
778 // xfer_total += count;
779 if( enc_feed.write(buffer, 0, count) ) {
780 try { Thread.sleep( slow_delay_ms ); } catch(final Throwable t) {}
781 } else {
782 PrintUtil.println(System.err, "feed_source_00: Error: Failed to write "+count+" bytes to feed: "+enc_feed.toString());
783 break;
784 }
785 }
786 }
787 } catch (final Exception ex) {
788 PrintUtil.println(System.err, "feed_source_00: Error: "+ex.getMessage());
789 ex.printStackTrace();
790 } finally {
791 try { if( null != in ) { in.close(); } } catch (final IOException e) { e.printStackTrace(); }
792 }
793 // probably set after transfering due to above sleep, which also ends when total size has been reached.
794 enc_feed.set_eof( enc_feed.fail() ? -1 /* FAILED */ : 1 /* SUCCESS */ );
795 } };
796
797 // throttled, with content size
798 static FeederFunc feed_source_01_sized_slow = new FeederFunc() {
799 @Override
800 public void feed(final ByteInStream_Feed enc_feed) {
801 long xfer_total = 0;
802 final File enc_stream = new File(enc_feed.id());
803 Assert.assertTrue( enc_stream.exists() );
804 final long file_size = enc_stream.length();
805 enc_feed.set_content_size( file_size );
806
807 InputStream in = null;
808 try {
809 in = new FileInputStream(enc_stream);
810 final byte[] buffer = new byte[slow_buffer_sz];
811 while( xfer_total < file_size && in.available() > 0 ) {
812 final int count = in.read(buffer);
813 if( 0 < count ) {
814 xfer_total += count;
815 if( enc_feed.write(buffer, 0, count) ) {
816 try { Thread.sleep( slow_delay_ms ); } catch(final Throwable t) {}
817 } else {
818 PrintUtil.println(System.err, "feed_source_01: Error: Failed to write "+count+" bytes to feed: "+enc_feed.toString());
819 break;
820 }
821 }
822 }
823 } catch (final Exception ex) {
824 PrintUtil.println(System.err, "feed_source_01: Error: "+ex.getMessage());
825 ex.printStackTrace();
826 } finally {
827 try { if( null != in ) { in.close(); } } catch (final IOException e) { e.printStackTrace(); }
828 }
829 // probably set after transfering due to above sleep, which also ends when total size has been reached.
830 enc_feed.set_eof( !enc_feed.fail() && xfer_total == file_size ? 1 /* SUCCESS */ : -1 /* FAILED */);
831 } };
832
833 // full speed, no content size
834 static FeederFunc feed_source_10_nosize_fast = new FeederFunc() {
835 @Override
836 public void feed(final ByteInStream_Feed enc_feed) {
837 // long xfer_total = 0;
838 final File enc_stream = new File(enc_feed.id());
839 Assert.assertTrue( enc_stream.exists() );
840
841 InputStream in = null;
842 try {
843 in = new FileInputStream(enc_stream);
844 final byte[] buffer = new byte[Cipherpack.buffer_size];
845 while( in.available() > 0 ) {
846 final int count = in.read(buffer);
847 if( 0 < count ) {
848 // xfer_total += count;
849 if( !enc_feed.write(buffer, 0, count) ) {
850 PrintUtil.println(System.err, "feed_source_10: Error: Failed to write "+count+" bytes to feed: "+enc_feed.toString());
851 break;
852 }
853 }
854 }
855 } catch (final Exception ex) {
856 PrintUtil.println(System.err, "feed_source_10: Error: "+ex.getMessage());
857 ex.printStackTrace();
858 } finally {
859 try { if( null != in ) { in.close(); } } catch (final IOException e) { e.printStackTrace(); }
860 }
861 // probably set after transfering due to above sleep, which also ends when total size has been reached.
862 enc_feed.set_eof( enc_feed.fail() ? -1 /* FAIL */ : 1 /* SUCCESS */ );
863 } };
864
865 // full speed, with content size
866 static FeederFunc feed_source_11_sized_fast = new FeederFunc() {
867 @Override
868 public void feed(final ByteInStream_Feed enc_feed) {
869 long xfer_total = 0;
870 final File enc_stream = new File(enc_feed.id());
871 Assert.assertTrue( enc_stream.exists() );
872 final long file_size = enc_stream.length();
873 enc_feed.set_content_size( file_size );
874
875 InputStream in = null;
876 try {
877 in = new FileInputStream(enc_stream);
878 final byte[] buffer = new byte[Cipherpack.buffer_size];
879 while( xfer_total < file_size && in.available() > 0 ) {
880 final int count = in.read(buffer);
881 if( 0 < count ) {
882 xfer_total += count;
883 if( !enc_feed.write(buffer, 0, count) ) {
884 PrintUtil.println(System.err, "feed_source_11: Error: Failed to write "+count+" bytes to feed: "+enc_feed.toString());
885 break;
886 }
887 }
888 }
889 } catch (final Exception ex) {
890 PrintUtil.println(System.err, "feed_source_10: Error: "+ex.getMessage());
891 ex.printStackTrace();
892 } finally {
893 try { if( null != in ) { in.close(); } } catch (final IOException e) { e.printStackTrace(); }
894 }
895 // probably set after transfering due to above sleep, which also ends when total size has been reached.
896 enc_feed.set_eof( !enc_feed.fail() && xfer_total == file_size ? 1 /* SUCCESS */ : -1 /* FAILED */);
897 } };
898
899 // full speed, with content size, implicit eof based on count
900 static FeederFunc feed_source_12_sized_eof_fast = new FeederFunc() {
901 @Override
902 public void feed(final ByteInStream_Feed enc_feed) {
903 long xfer_total = 0;
904 final File enc_stream = new File(enc_feed.id());
905 Assert.assertTrue( enc_stream.exists() );
906 final long file_size = enc_stream.length();
907 enc_feed.set_content_size( file_size );
908
909 InputStream in = null;
910 try {
911 in = new FileInputStream(enc_stream);
912 boolean in_eof = false; // we can't rely on in.available(), not supported at least on SMB input stream
913 final byte[] buffer = new byte[Cipherpack.buffer_size];
914 while( xfer_total < file_size && !in_eof ) {
915 final int count = in.read(buffer);
916 if( 0 < count ) {
917 xfer_total += count;
918 if( !enc_feed.write(buffer, 0, count) ) {
919 PrintUtil.println(System.err, "feed_source_12: Error: Failed to write "+count+" bytes to feed: "+enc_feed.toString());
920 break;
921 }
922 } else if( 0 > count ) {
923 in_eof = true;
924 }
925 }
926 } catch (final Exception ex) {
927 PrintUtil.println(System.err, "feed_source_12: Error: "+ex.getMessage());
928 ex.printStackTrace();
929 } finally {
930 try { if( null != in ) { in.close(); } } catch (final IOException e) { e.printStackTrace(); }
931 }
932 // probably set after transfering due to above sleep, which also ends when total size has been reached.
933 enc_feed.set_eof( !enc_feed.fail() && xfer_total == file_size ? 1 /* SUCCESS */ : -1 /* FAILED */);
934 } };
935
936 // full speed, no content size, interrupting @ 1024 bytes within our header
937 static void feed_source_20_nosize_irqed_1k(final ByteInStream_Feed enc_feed) {
938 long xfer_total = 0;
939 final File enc_stream = new File(enc_feed.id());
940 Assert.assertTrue( enc_stream.exists() );
941 final long file_size = enc_stream.length();
942 enc_feed.set_content_size( file_size );
943
944 InputStream in = null;
945 try {
946 in = new FileInputStream(enc_stream);
947 final byte[] buffer = new byte[1024];
948 while( xfer_total < file_size && in.available() > 0 ) {
949 final int count = in.read(buffer);
950 if( 0 < count ) {
951 xfer_total += count;
952 if( enc_feed.write(buffer, 0, count) ) {
953 if( xfer_total >= 1024 ) {
954 enc_feed.set_eof( -1 /* FAILED */ ); // calls data_feed->interruptReader();
955 return;
956 }
957 } else {
958 PrintUtil.println(System.err, "feed_source_20: Error: Failed to write "+count+" bytes to feed: "+enc_feed.toString());
959 break;
960 }
961 }
962 }
963 } catch (final Exception ex) {
964 PrintUtil.println(System.err, "feed_source_20: Error: "+ex.getMessage());
965 ex.printStackTrace();
966 } finally {
967 try { if( null != in ) { in.close(); } } catch (final IOException e) { e.printStackTrace(); }
968 }
969 }
970
971 // full speed, with content size, interrupting 1/4 way
972 static void feed_source_21_sized_irqed_quarter(final ByteInStream_Feed enc_feed) {
973 long xfer_total = 0;
974 final File enc_stream = new File(enc_feed.id());
975 Assert.assertTrue( enc_stream.exists() );
976 final long file_size = enc_stream.length();
977 enc_feed.set_content_size( file_size );
978
979 InputStream in = null;
980 try {
981 in = new FileInputStream(enc_stream);
982 final byte[] buffer = new byte[1024];
983 while( xfer_total < file_size && in.available() > 0 ) {
984 final int count = in.read(buffer);
985 if( 0 < count ) {
986 xfer_total += count;
987 if( enc_feed.write(buffer, 0, count) ) {
988 if( xfer_total >= file_size/4 ) {
989 enc_feed.set_eof( -1 /* FAILED */ ); // calls data_feed->interruptReader();
990 return;
991 }
992 } else {
993 PrintUtil.println(System.err, "feed_source_21: Error: Failed to write "+count+" bytes to feed: "+enc_feed.toString());
994 break;
995 }
996 }
997 }
998 } catch (final Exception ex) {
999 PrintUtil.println(System.err, "feed_source_21: Error: "+ex.getMessage());
1000 ex.printStackTrace();
1001 } finally {
1002 try { if( null != in ) { in.close(); } } catch (final IOException e) { e.printStackTrace(); }
1003 }
1004 }
1005
1006 @Test(timeout = 120000)
1007 public final void test31_fed_all_files() {
1009 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
1010 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
1011 for(int file_idx = 0; file_idx < fname_plaintext_lst.size(); ++file_idx) {
1012 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test31.enc."+file_idx);
1013 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1015 enc_pub_keys,
1016 sign_sec_key1_fname, sign_sec_key_passphrase,
1017 source,
1018 fname_plaintext_lst.get(file_idx), "test31_fed_all_files", plaintext_version, plaintext_version_parent,
1019 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
1020 PrintUtil.fprintf_td(System.err, "test31_fed_all_files: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
1021 PrintUtil.fprintf_td(System.err, "test31_fed_all_files: %s\n", ph1.toString(true, true));
1022 Assert.assertTrue( "test31_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
1023 enc_listener.check_counter_end();
1024
1025 final FeederFunc[] feed_funcs = { feed_source_00_nosize_slow, feed_source_01_sized_slow,
1026 feed_source_10_nosize_fast, feed_source_11_sized_fast,
1027 feed_source_12_sized_eof_fast };
1028 final String[] feed_funcs_suffix = { "nosize_slow", "sized_slow", "nosize_fast", "sized_fast", "sized_eof_fast" };
1029 for(int func_idx=0; func_idx < feed_funcs.length; ++func_idx) {
1030 final FeederFunc feed_func = feed_funcs[func_idx];
1031 if( ( IDX_46MiB == file_idx || IDX_65MiB == file_idx ) && ( func_idx == 0 || func_idx == 1 ) ) {
1032 continue; // skip big file, too slow -> takes too long time to test
1033 }
1034 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test31.dec."+file_idx+"."+func_idx);
1035 final String suffix = feed_funcs_suffix[func_idx];
1036 final ByteInStream_Feed enc_feed = new ByteInStream_Feed(fname_encrypted_lst.get(file_idx), io_timeout);
1037 final Thread feeder_thread = executeOffThread( () -> { feed_func.feed(enc_feed); }, "test31_fed_all_files::"+suffix, false /* detach */);
1038
1039 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1040 enc_feed,
1041 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
1042 try {
1043 feeder_thread.join(1000);
1044 } catch (final InterruptedException e) { }
1045
1046 PrintUtil.fprintf_td(System.err, "test31_fed_all_files %s: Decypted %s to %s\n", suffix, fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
1047 PrintUtil.fprintf_td(System.err, "test31_fed_all_files %s: %s\n", suffix, ph2.toString(true, true));
1048 Assert.assertTrue( "test31_dec: file_idx "+file_idx+", "+suffix+", "+ph2.toString(), ph2.isValid() );
1049 dec_listener.check_counter_end();
1050
1051 hash_retest(ph1.plaintext_hash_algo,
1052 fname_plaintext_lst.get(file_idx), ph1.plaintext_hash,
1053 fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
1054 }
1055 }
1056 }
1057
1058 static public class Bug574CipherpackListener extends CipherpackListener {
1059 /** Maximum notifyTransferProgress() is 10Hz, i.e. min_period is 1/10 = 100ms. This throttle is to avoid overloading the UI. */
1060 private static final int MIN_PROGRESS_PERIOD_MS = 10;
1061 private Instant t_last;
1062 private Instant t_last_progress;
1063 private final String name;
1065 public static enum GetTimeMethod {
1066 /**
1067 * Triggers Bug574:
1068 *
1069 * Clock.getMonotonicTime() causes a freeze coming from callback UpdateCipherpackListener.notifyProgress()
1070 * called by org.cipherpack.Cipherpack.checkSignThenDecrypt1(Native Method)
1071 */
1073 /** OK */
1075 /** OK */
1076 SYSTEM_GET_CURRENT_MS
1078 final GetTimeMethod get_time_method = GetTimeMethod.JAU_GET_MONOTONIC_TIME;
1079
1080 public Bug574CipherpackListener(final String _name) {
1081 super();
1082 t_last = null;
1083 t_last_progress = null;
1084 name = _name;
1085 clock_gettime_in_ms = 0;
1086 }
1087
1088 @Override
1089 public void notifyError(final boolean decrypt_mode, final PackHeader header, final String msg) {
1090 PrintUtil.fprintf_td(System.err, "%s: Notify Error: clock_gettime_in %d ms, %s, %s\n",
1091 name, clock_gettime_in_ms, msg, header.toString());
1092 }
1093
1094 @Override
1095 public boolean notifyHeader(final boolean decrypt_mode, final PackHeader header) {
1096 PrintUtil.fprintf_td(System.err, "%s: Notify Header: %s\n", name, header.toString());
1097 return true;
1098 }
1099
1100 @Override
1101 public boolean notifyProgress(final boolean decrypt_mode, final long plaintext_size, final long bytes_processed) {
1102 final Instant t0;
1103 switch( get_time_method ) {
1104 case JAU_GET_MONOTONIC_TIME:
1105 // FIXME: Clock.getMonotonicTime() causes a freeze coming from callback UpdateCipherpackListener.notifyProgress()
1106 // called by org.cipherpack.Cipherpack.checkSignThenDecrypt1(Native Method)
1107 t0 = org.jau.sys.Clock.getMonotonicTime();
1108 break;
1109 case JAU_GET_MONOTONIC_CURRENT_MS:
1110 // OK
1111 t0 = Instant.ofEpochMilli(org.jau.sys.Clock.currentTimeMillis());
1112 break;
1113 case SYSTEM_GET_CURRENT_MS:
1114 default:
1115 // OK
1116 t0 = Instant.ofEpochMilli(System.currentTimeMillis());
1117 break;
1118 }
1119 if( null != t_last ) {
1120 clock_gettime_in_ms = t_last.until(t0, ChronoUnit.MILLIS);
1121 }
1122 t_last = t0;
1123
1124 if( null != t_last_progress ) {
1125 final long td_ms = t_last_progress.until(t0, ChronoUnit.MILLIS);
1126 if( td_ms < MIN_PROGRESS_PERIOD_MS && ( plaintext_size > bytes_processed || 0 >= plaintext_size ) ) {
1127 return true;
1128 }
1129 }
1130 t_last_progress = t0;
1131 final double progress = plaintext_size > 0 ? (double) bytes_processed / (double) plaintext_size * 100.0 : -1.0;
1132 PrintUtil.fprintf_td(System.err, "%s: Notify Progress: %,d bytes received, %.02f%%\n", name, bytes_processed, progress);
1133 return true;
1134 }
1135
1136 @Override
1137 public void notifyEnd(final boolean decrypt_mode, final PackHeader header) {
1138 PrintUtil.fprintf_td(System.err, "%s: Notify End: %s\n", name, header.toString());
1139 }
1140 };
1141
1142 @Test(timeout = 120000)
1143 public final void test32_bug574() {
1145 final String test_plaintext_file = System.getenv("PLAINTEXT_FILE");
1146 final String test_encrypted_file = System.getenv("ENCRYPTED_FILE");
1147 final String test_loops_s = System.getenv("TEST_LOOPS");
1148 int test_loops = 1;
1149 if( null != test_loops_s ) {
1150 try {
1151 test_loops = Integer.parseInt(test_loops_s);
1152 } catch(final Exception ex) {
1153 ex.printStackTrace();
1154 }
1155 }
1156 String fname_plaintext;
1157 String fname_encrypted;
1158 String fname_decrypted;
1159 if( null == test_plaintext_file ) {
1160 final int file_idx = IDX_46MiB; // IDX_65MiB;
1161 fname_plaintext = fname_plaintext_lst.get(file_idx);
1162 fname_encrypted = fname_encrypted_lst.get(file_idx);
1163 fname_decrypted = fname_decrypted_lst.get(file_idx);
1164 } else {
1165 fname_plaintext = test_plaintext_file;
1166 if( null == test_encrypted_file ) {
1167 fname_encrypted = test_plaintext_file+".enc";
1168 } else {
1169 fname_encrypted = test_encrypted_file;
1170 }
1171 fname_decrypted = test_plaintext_file+".enc.dec";
1172 }
1173
1174 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname, enc_pub_key4_fname);
1175 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
1176
1177 if( null == test_encrypted_file ) {
1178 // 46MB Bug 574: Plaintext size: 48,001,024, encrypted size 48,007,099
1179 // adding 1077 bytes to achieve encrypted size 48,007,099
1180 final String plaintext_version_bytes = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
1181 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test32.enc");
1182 final ByteInStream_File source = new ByteInStream_File(fname_plaintext);
1184 enc_pub_keys,
1185 sign_sec_key1_fname, sign_sec_key_passphrase,
1186 source,
1187 fname_plaintext, "test32_bug574", plaintext_version_bytes, plaintext_version_parent,
1188 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted);
1189 PrintUtil.fprintf_td(System.err, "test32_bug574: Encrypted %s to %s\n", fname_plaintext, fname_encrypted);
1190 PrintUtil.fprintf_td(System.err, "test32_bug574: %s\n", ph1.toString(true, true));
1191 Assert.assertTrue( "test32_bug574: "+ph1.toString(), ph1.isValid() );
1192 enc_listener.check_counter_end();
1193 }
1194
1195 for(int loop_idx = 0; loop_idx < test_loops; ++loop_idx) {
1196 final String suffix = "sized_eof_fast_"+Integer.toString(loop_idx);
1197 final FeederFunc feed_func = feed_source_12_sized_eof_fast;
1198 final Bug574CipherpackListener dec_listener = new Bug574CipherpackListener(suffix);
1199 final ByteInStream_Feed enc_feed = new ByteInStream_Feed(fname_encrypted, 5000); // 5_s only // io_timeout);
1200 final Thread feeder_thread = executeOffThread( () -> { feed_func.feed(enc_feed); }, "test32_bug574::"+suffix, false /* detach */);
1201
1202 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1203 enc_feed,
1204 dec_listener, "", fname_decrypted);
1205 try {
1206 feeder_thread.join(1000);
1207 } catch (final InterruptedException e) { }
1208
1209 PrintUtil.fprintf_td(System.err, "test32_bug574 %s: Decypted %s to %s\n", suffix, fname_encrypted, fname_decrypted);
1210 PrintUtil.fprintf_td(System.err, "test32_bug574 %s: clock_gettime_in %d ms, %s\n",
1211 suffix, dec_listener.clock_gettime_in_ms, ph2.toString(true, true));
1212 Assert.assertTrue( "Bug574: Hang within clock_gettime "+dec_listener.clock_gettime_in_ms+" ms, "+suffix, dec_listener.clock_gettime_in_ms < 10 );
1213 Assert.assertTrue( "test32_bug574: "+suffix+", "+ph2.toString(), ph2.isValid() );
1214
1215 // hash_retest(ph1.plaintext_hash_algo,
1216 // fname_plaintext_lst.get(file_idx), ph1.plaintext_hash,
1217 // fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
1218 }
1219 }
1220
1221 @Test(timeout = 120000)
1222 public final void test34_enc_dec_fed_irq() {
1224 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
1225 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
1226 {
1227 final int file_idx = IDX_65MiB;
1228 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test34.enc."+file_idx);
1229 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1231 enc_pub_keys,
1232 sign_sec_key1_fname, sign_sec_key_passphrase,
1233 source,
1234 fname_plaintext_lst.get(file_idx), "test34_enc_dec_fed_irq", plaintext_version, plaintext_version_parent,
1235 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
1236 PrintUtil.fprintf_td(System.err, "test34_enc_dec_fed_irq: Encrypted %s to %s\n", fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
1237 PrintUtil.fprintf_td(System.err, "test34_enc_dec_fed_irq: %s\n", ph1.toString(true, true));
1238 Assert.assertTrue( "test34_enc: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
1239 enc_listener.check_counter_end();
1240
1241 final String file_decrypted = fname_encrypted_lst.get(file_idx)+".dec";
1242 {
1243 // full speed, no content size, interrupting @ 1024 bytes within our header
1244 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test34.dec.e1."+file_idx);
1245 final ByteInStream_Feed enc_feed = new ByteInStream_Feed(fname_encrypted_lst.get(file_idx), io_timeout);
1246 final Thread feeder_thread = executeOffThread( () -> { feed_source_20_nosize_irqed_1k(enc_feed); }, "test22_enc_dec_fed_irq::feed_source_20", false /* detach */);
1247
1248 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1249 enc_feed,
1250 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
1251 try {
1252 feeder_thread.join(1000);
1253 } catch (final InterruptedException e) { }
1254
1255 PrintUtil.fprintf_td(System.err, "test34_enc_dec_fed_irq: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
1256 PrintUtil.fprintf_td(System.err, "test34_enc_dec_fed_irq: %s\n", ph2.toString(true, true));
1257 Assert.assertFalse( "test34_dec.e1: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
1258 dec_listener.check_counter_end();
1259 }
1260 {
1261 // full speed, with content size, interrupting 1/4 way
1262 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test34.dec.e2."+file_idx);
1263 final ByteInStream_Feed enc_feed = new ByteInStream_Feed(fname_encrypted_lst.get(file_idx), io_timeout);
1264 final Thread feeder_thread = executeOffThread( () -> { feed_source_21_sized_irqed_quarter(enc_feed); }, "test22_enc_dec_fed_irq::feed_source_21", false /* detach */);
1265
1266 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1267 enc_feed,
1268 dec_listener, Cipherpack.default_hash_algo(), file_decrypted);
1269 try {
1270 feeder_thread.join(1000);
1271 } catch (final InterruptedException e) { }
1272
1273 PrintUtil.fprintf_td(System.err, "test34_enc_dec_fed_irq: Decypted %s to %s\n", fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
1274 PrintUtil.fprintf_td(System.err, "test34_enc_dec_fed_irq: %s\n", ph2.toString(true, true));
1275 Assert.assertFalse( "test34_dec.e2: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
1276 dec_listener.check_counter_end();
1277 }
1278 }
1279 }
1280
1281 @Test(timeout = 120000)
1282 public final void test41_abort_stream() {
1284 final List<String> enc_pub_keys = Arrays.asList(enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname);
1285 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
1286 for(int abort_item=1; abort_item<=3; ++abort_item) {
1287 final int file_idx = IDX_xbuffersz;
1288 PackHeader ph1;
1289 {
1290 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test41.enc.1."+abort_item, true /* send_content */);
1291 enc_listener.set_abort(abort_item);
1292 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1294 enc_pub_keys,
1295 sign_sec_key1_fname, sign_sec_key_passphrase,
1296 source,
1297 fname_plaintext_lst.get(file_idx), "test41_enc_1."+abort_item, plaintext_version, plaintext_version_parent,
1298 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
1299 PrintUtil.fprintf_td(System.err, "test41_enc_1.%d: Encrypted %s to %s\n", abort_item, fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
1300 PrintUtil.fprintf_td(System.err, "test41_enc_1.%d: %s\n", abort_item, ph1.toString(true, true));
1301 Assert.assertFalse( "test41_enc.1: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
1302 enc_listener.check_counter_end(0);
1303 }
1304 {
1305 final LoggingCipherpackListener enc_listener = new LoggingCipherpackListener("test41.enc.2."+abort_item, true /* send_content */);
1306 final ByteInStream_File source = new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1308 enc_pub_keys,
1309 sign_sec_key1_fname, sign_sec_key_passphrase,
1310 source,
1311 fname_plaintext_lst.get(file_idx), "test41_enc_2."+abort_item, plaintext_version, plaintext_version_parent,
1312 enc_listener, Cipherpack.default_hash_algo(), fname_encrypted_lst.get(file_idx));
1313 PrintUtil.fprintf_td(System.err, "test41_enc_2.%d: Encrypted %s to %s\n", abort_item, fname_plaintext_lst.get(file_idx), fname_encrypted_lst.get(file_idx));
1314 PrintUtil.fprintf_td(System.err, "test41_enc_2.%d: %s\n", abort_item, ph1.toString(true, true));
1315 Assert.assertTrue( "test41_enc.2: file_idx "+file_idx+", "+ph1.toString(), ph1.isValid() );
1316 enc_listener.check_counter_end(source.content_size());
1317 }
1318 {
1319 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test41.dec.1"+abort_item, true /* send_content */);
1320 dec_listener.set_abort(abort_item);
1321 final ByteInStream_File enc_stream = new ByteInStream_File(fname_encrypted_lst.get(file_idx));
1322 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1323 enc_stream,
1324 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
1325 PrintUtil.fprintf_td(System.err, "test41_dec.1.%d: Decypted %s to %s\n", abort_item, fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
1326 PrintUtil.fprintf_td(System.err, "test41_dec.1.%d: %s\n", abort_item, ph2.toString(true, true));
1327 Assert.assertFalse( "test41_dec.1: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
1328 dec_listener.check_counter_end(0);
1329 }
1330 if( !org.jau.io.UriTk.protocol_supported("http:") ) {
1331 PrintUtil.fprintf_td(System.err, "http not supported, abort\n");
1332 } else {
1333 httpd_start();
1334 final String uri_encrypted = url_input_root + fname_encrypted_lst.get(file_idx);
1335 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test41.dec.2"+abort_item, true /* send_content */);
1336 dec_listener.set_abort(abort_item);
1337 final ByteInStream_URL enc_stream = new ByteInStream_URL(uri_encrypted, io_timeout);
1338 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1339 enc_stream,
1340 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
1341 PrintUtil.fprintf_td(System.err, "test41_dec.2.%d: Decypted %s to %s\n", abort_item, fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
1342 PrintUtil.fprintf_td(System.err, "test41_dec.2.%d: %s\n", abort_item, ph2.toString(true, true));
1343 Assert.assertFalse( "test41_dec.2: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
1344 dec_listener.check_counter_end(0);
1345 }
1346 {
1347 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test41.dec.3"+abort_item, true /* send_content */);
1348 dec_listener.set_abort(abort_item);
1349 final ByteInStream_Feed enc_feed = new ByteInStream_Feed(fname_encrypted_lst.get(file_idx), io_timeout);
1350 final Thread feeder_thread = executeOffThread( () -> { feed_source_11_sized_fast.feed(enc_feed); }, "test41.dec.3."+abort_item, false /* detach */);
1351 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1352 enc_feed,
1353 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
1354 try {
1355 enc_feed.closeStream(); // ends feeder_thread loop
1356 feeder_thread.join(1000);
1357 } catch (final InterruptedException e) { }
1358 PrintUtil.fprintf_td(System.err, "test41_dec.3.%d: Decypted %s to %s\n", abort_item, fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
1359 PrintUtil.fprintf_td(System.err, "test41_dec.3.%d: %s\n", abort_item, ph2.toString(true, true));
1360 Assert.assertFalse( "test41_dec.3: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
1361 dec_listener.check_counter_end(0);
1362 }
1363 {
1364 final LoggingCipherpackListener dec_listener = new LoggingCipherpackListener("test41.dec.4"+abort_item, true /* send_content */);
1365 dec_listener.set_abort(abort_item);
1366 final ByteInStream_Feed enc_feed = new ByteInStream_Feed(fname_encrypted_lst.get(file_idx), io_timeout);
1367 final Thread feeder_thread = executeOffThread( () -> { feed_source_10_nosize_fast.feed(enc_feed); }, "test41.dec.4."+abort_item, false /* detach */);
1368 final PackHeader ph2 = Cipherpack.checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
1369 enc_feed,
1370 dec_listener, ph1.plaintext_hash_algo, fname_decrypted_lst.get(file_idx));
1371 try {
1372 enc_feed.closeStream(); // ends feeder_thread loop
1373 feeder_thread.join(1000);
1374 } catch (final InterruptedException e) { }
1375 PrintUtil.fprintf_td(System.err, "test41_dec.4.%d: Decypted %s to %s\n", abort_item, fname_encrypted_lst.get(file_idx), fname_decrypted_lst.get(file_idx));
1376 PrintUtil.fprintf_td(System.err, "test41_dec.4.%d: %s\n", abort_item, ph2.toString(true, true));
1377 Assert.assertFalse( "test41_dec.4: file_idx "+file_idx+", "+ph2.toString(), ph2.isValid() );
1378 dec_listener.check_counter_end(0);
1379 }
1380 }
1381 }
1382
1383 final static String root = "test_data";
1384 // submodule location with jaulib directly hosted below main project
1385 final static String project_root2 = "../../../../jaulib/test_data";
1386
1387 @Test(timeout = 120000)
1388 public final void test50_copy_and_verify() {
1389 final String title = "test50_copy_and_verify";
1390 final String hash_file = title+".hash";
1391
1392 PrintUtil.fprintf_td(System.err, "\n");
1393 PrintUtil.fprintf_td(System.err, "%s\n", title);
1394
1395 FileUtil.remove(hash_file, TraverseOptions.none);
1396
1397 final FileStats source_stats = new FileStats(project_root2);
1398 Assert.assertTrue( source_stats.exists() );
1399 Assert.assertTrue( source_stats.is_dir() );
1400
1401 final long[] source_bytes_hashed = { 0 };
1402 final byte[] source_hash = Cipherpack.HashUtil.calc(Cipherpack.default_hash_algo(), source_stats.path(), source_bytes_hashed);
1403 Assert.assertNotNull( source_hash );
1404 Assert.assertTrue( Cipherpack.HashUtil.appendToFile(hash_file, source_stats.path(), Cipherpack.default_hash_algo(), source_hash));
1405
1406 // copy folder
1407 final String dest = root+"_copy_verify_test50";
1408 {
1409 final CopyOptions copts = new CopyOptions();
1410 copts.set(CopyOptions.Bit.recursive);
1411 copts.set(CopyOptions.Bit.preserve_all);
1412 copts.set(CopyOptions.Bit.sync);
1413 copts.set(CopyOptions.Bit.verbose);
1414
1415 FileUtil.remove(dest, TraverseOptions.recursive);
1416 Assert.assertTrue( true == FileUtil.copy(source_stats.path(), dest, copts) );
1417 }
1418 final FileStats dest_stats = new FileStats(dest);
1419 Assert.assertTrue( source_stats.exists() );
1420 Assert.assertTrue( source_stats.ok() );
1421 Assert.assertTrue( source_stats.is_dir() );
1422
1423 final long[] dest_bytes_hashed = { 0 };
1424 final byte[] dest_hash = Cipherpack.HashUtil.calc(Cipherpack.default_hash_algo(), dest_stats.path(), dest_bytes_hashed);
1425 Assert.assertNotNull( dest_hash );
1426 Assert.assertTrue( Cipherpack.HashUtil.appendToFile(hash_file, dest_stats.path(), Cipherpack.default_hash_algo(), dest_hash));
1427
1428 // actual validation of hash values, i.e. same content
1429 Assert.assertArrayEquals(source_hash, dest_hash);
1430 Assert.assertEquals( source_bytes_hashed[0], dest_bytes_hashed[0] );
1431
1432 PrintUtil.fprintf_td(System.err, "%s: bytes %,d, '%s'\n", title, dest_bytes_hashed[0],
1433 BasicTypes.bytesHexString(dest_hash, 0, dest_hash.length, true /* lsbFirst */));
1434
1435 Assert.assertTrue( FileUtil.remove(dest, TraverseOptions.recursive) );
1436 }
1437
1438 public static void main(final String args[]) {
1439 if( args.length > 0 ) {
1440 System.err.println("Launching test: class "+Test01Cipherpack.class.getName()+", method "+args[0]);
1441 single_test_name = args[0];
1442 org.junit.runner.JUnitCore.main(Test01Cipherpack.class.getName());
1443 } else {
1444 org.junit.runner.JUnitCore.main(Test01Cipherpack.class.getName());
1445 }
1446 }
1447}
Cipherpack Factory, called by automatically to load the native library.
Definition: CPFactory.java:47
static void checkInitialized()
Definition: CPFactory.java:269
Listener for events occurring while processing a cipherpack message via encryptThenSign() and checkSi...
Hash utility functions to produce a hash file compatible to sha256sum as well as to produce the hash ...
static boolean appendToFile(final String outFileName, final String hashedFile, final String hashAlgo, final byte[] hashValue)
Append the hash signature to the text file out_file.
static byte[] calc(final String algo, final ByteInStream source)
Return the calculated hash value using given algo name and byte input stream.
static String fileSuffix(final String algo)
Return a lower-case file suffix used to store a sha256sum compatible hash signature w/o dot and w/o d...
static PackHeader encryptThenSign(final CryptoConfig crypto_cfg, final List< String > enc_pub_keys, final String sign_sec_key_fname, final ByteBuffer passphrase, final ByteInStream source, final String target_path, final String subject, final String plaintext_version, final String plaintext_version_parent, final CipherpackListener listener, final String plaintext_hash_algo, final String destination_fname)
Encrypt then sign the source producing a cipherpack stream passed to the CipherpackListener if opt-in...
static PackHeader checkSignThenDecrypt(final List< String > sign_pub_keys, final String dec_sec_key_fname, final ByteBuffer passphrase, final ByteInStream source, final CipherpackListener listener, final String plaintext_hash_algo, final String destination_fname)
Verify signature then decrypt the source passing to the CipherpackListener if opt-in and also optiona...
static final String default_hash_algo()
Name of default hash algo for the plaintext message, e.g.
CryptoConfig, contains crypto algorithms settings given at encryption wired via the Cipherpack Data S...
static CryptoConfig getDefault()
Returns default CryptoConfig.
Cipherpack header less encrypted keys or signatures as described in Cipherpack Data Stream.
Definition: PackHeader.java:40
String toString(final boolean show_crypto_algos, final boolean force_all_fingerprints)
Return a string representation.
final boolean isValid()
final long plaintext_size
Plaintext message size in bytes, zero if not determined yet.
Definition: PackHeader.java:45
final String plaintext_hash_algo
Optional hash algorithm for the plaintext message, produced for convenience and not wired.
Definition: PackHeader.java:78
final byte[] plaintext_hash
Optional hash value of the plaintext message, produced for convenience and not wired.
Definition: PackHeader.java:85
void notifyError(final boolean decrypt_mode, final PackHeader header, final String msg)
User notification about an error via text message and preliminary PackHeader.
void notifyEnd(final boolean decrypt_mode, final PackHeader header)
User notification of successful completion.
boolean notifyProgress(final boolean decrypt_mode, final long plaintext_size, final long bytes_processed)
User notification about content streaming progress.
boolean notifyHeader(final boolean decrypt_mode, final PackHeader header)
User notification of preliminary PackHeader w/o optional hash of the plaintext message.
static void main(final String args[])
static void hash_retest(final String hashAlgo, final String origFile, final String hashedDescryptedFile, final byte[] hashValue_p2)
static void hash_retest(final String hashAlgo, final String origFile, final byte[] hashValue_p1, final String hashedDescryptedFile, final byte[] hashValue_p2)