25package test.org.cipherpack;
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;
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;
64import org.junit.runners.MethodSorters;
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
68 static final boolean DEBUG =
false;
70 static final String plaintext_version =
"0";
71 static final String plaintext_version_parent =
"0";
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;
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>();
86 static String single_test_name =
null;
88 static boolean remove_file(
final String name) {
89 final File file =
new File( name );
92 if( !file.delete() ) {
93 PrintUtil.println(System.err,
"Remove.1: Error: Failed deletion of existing file "+name);
98 }
catch (
final Exception ex) {
99 PrintUtil.println(System.err,
"Remove.2: Error: Failed deletion of existing file "+name+
": "+ex.getMessage());
100 ex.printStackTrace();
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") );
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);
113 final File file =
new File( name );
114 Assert.assertFalse( file.exists() );
116 try(
final OutputStream out =
new FileOutputStream(file); ) {
119 for(; written+one_line_bytes.length <= size; written+=+one_line_bytes.length) {
120 out.write( one_line_bytes );
122 for(; size-written > 0; ++written ) {
123 out.write( (
byte)
'_' );
125 }
catch (
final Exception ex) {
126 PrintUtil.println(System.err,
"Error: Write test file: Failed "+name+
": "+ex.getMessage());
127 ex.printStackTrace();
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");
140 final int buffer_size = 16384;
144 add_test_file(
"test_cipher_0"+(i++)+
"_0B.bin", 0);
147 add_test_file(
"test_cipher_0"+(i++)+
"_1B.bin", 1);
150 add_test_file(
"test_cipher_0"+(i++)+
"_11kiB.bin", 1024*11+1);
153 final long xbuffersz = 4 * buffer_size;
154 add_test_file(
"test_cipher_0"+(i++)+
"_xbuffsz_"+(xbuffersz/1024)+
"kiB.bin", xbuffersz);
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);
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);
165 add_test_file(
"test_cipher_0"+(i++)+
"_46MiB.bin", 48001024);
168 add_test_file(
"test_cipher_0"+(i++)+
"_65MiB.bin", 48007099);
171 static boolean system(
final String[] command) {
174 proc = Runtime.getRuntime().exec(command);
178 catch(
final Exception ex)
183 ex.printStackTrace();
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";
193 return "/usr/sbin/mini_httpd";
199 if( org.jau.io.UriTk.protocol_supported(
"http:") ) {
200 Assert.assertTrue( system(
new String[]{
"killall",
"mini_httpd"}) );
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) );
216 final String origFile,
final byte[] hashValue_p1,
217 final String hashedDescryptedFile,
final byte[] hashValue_p2)
219 Assert.assertArrayEquals(hashValue_p1, hashValue_p2);
222 final String outFile = hashedDescryptedFile +
"." + suffix;
223 FileUtil.remove(outFile, TraverseOptions.none);
227 final ByteInStream origIn = ByteInStreamUtil.to_ByteInStream(origFile);
228 Assert.assertNotNull( origIn );
229 final Instant t0 = org.jau.sys.Clock.getMonotonicTime();
231 Assert.assertNotNull( origHashValue );
232 Assert.assertArrayEquals(hashValue_p2, origHashValue);
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");
241 final String origFile,
242 final String hashedDescryptedFile,
final byte[] hashValue_p2)
245 final String outFile = hashedDescryptedFile +
"." + suffix;
246 FileUtil.remove(outFile, TraverseOptions.none);
250 final ByteInStream origIn = ByteInStreamUtil.to_ByteInStream(origFile);
251 Assert.assertNotNull( origIn );
252 final Instant t0 = org.jau.sys.Clock.getMonotonicTime();
254 Assert.assertNotNull( origHashValue );
255 Assert.assertArrayEquals(hashValue_p2, origHashValue);
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");
265 if(
null != single_test_name && !single_test_name.equals(getTestMethodName()) ) {
266 System.err.println(
"++++ TestFilter: Disabled "+getFullTestName(
" - "));
267 Assume.assumeTrue(
false);
269 System.err.println(
"++++ TestFilter: Enabled "+getFullTestName(
" - "));
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);
279 final int file_idx = IDX_11kiB;
282 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
285 sign_sec_key1_fname, sign_sec_key_passphrase,
287 fname_plaintext_lst.get(file_idx),
"test00_enc_dec_file_single", plaintext_version, plaintext_version_parent,
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));
294 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_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));
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) {
312 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
315 sign_sec_key1_fname, sign_sec_key_passphrase,
317 fname_plaintext_lst.get(file_idx),
"test01_enc_dec_all_files", plaintext_version, plaintext_version_parent,
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() );
325 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_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() );
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);
345 final int file_idx = IDX_11kiB;
347 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
350 sign_sec_key1_fname, sign_sec_key_passphrase,
352 fname_plaintext_lst.get(file_idx),
"test02_enc_dec_file_misc", plaintext_version, plaintext_version_parent,
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() );
360 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_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() );
370 final int file_idx = IDX_11kiB;
372 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
375 sign_sec_key2_fname, sign_sec_key_passphrase,
377 fname_plaintext_lst.get(file_idx),
"test02_enc_dec_file_misc", plaintext_version, plaintext_version_parent,
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() );
385 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_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() );
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);
403 final int file_idx = IDX_65MiB;
405 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
408 sign_sec_key3_fname, sign_sec_key_passphrase,
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() );
419 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_lst.get(file_idx));
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() );
430 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_lst.get(file_idx));
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() );
439 fname_plaintext_lst.get(file_idx),
444 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_lst.get(file_idx));
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() );
453 fname_plaintext_lst.get(file_idx),
458 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_lst.get(file_idx));
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() );
467 fname_plaintext_lst.get(file_idx),
473 @Test(timeout = 120000)
474 public final
void test04_enc_dec_file_error() {
477 final int file_idx = IDX_11kiB;
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));
483 sign_sec_key1_fname, sign_sec_key_passphrase,
485 fname_plaintext_lst.get(file_idx),
"test_case", plaintext_version, plaintext_version_parent,
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() );
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));
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() );
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));
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() );
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");
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) {
532 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
535 sign_sec_key1_fname, sign_sec_key_passphrase,
537 fname_plaintext_lst.get(file_idx),
"test11_dec_http_all_files", plaintext_version, plaintext_version_parent,
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() );
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";
547 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
550 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
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");
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);
574 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
577 sign_sec_key1_fname, sign_sec_key_passphrase,
579 fname_plaintext_lst.get(file_idx),
"test12_dec_http_misc", plaintext_version, plaintext_version_parent,
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() );
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";
589 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
592 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
603 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
614 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
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");
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);
637 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
640 sign_sec_key1_fname, sign_sec_key_passphrase,
642 fname_plaintext_lst.get(file_idx),
"test13_dec_http_perf", plaintext_version, plaintext_version_parent,
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() );
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";
652 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
655 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
670 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
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");
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);
693 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
696 sign_sec_key1_fname, sign_sec_key_passphrase,
698 fname_plaintext_lst.get(file_idx),
"test14_dec_http_error", plaintext_version, plaintext_version_parent,
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() );
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";
708 final List<String> sign_pub_keys = Arrays.asList(sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname);
712 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
723 final List<String> sign_pub_keys_nope = Arrays.asList( sign_pub_key4_fname );
725 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
736 final String uri_encrypted_err = url_input_root +
"doesnt_exists.enc";
738 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted_err, io_timeout);
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() );
749 static final int slow_buffer_sz = 1024;
750 static final long slow_delay_ms = 8;
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 );
759 static interface FeederFunc {
760 void feed(
final ByteInStream_Feed enc_feed);
764 static FeederFunc feed_source_00_nosize_slow =
new FeederFunc() {
766 public void feed(
final ByteInStream_Feed enc_feed) {
768 final File enc_stream =
new File(enc_feed.id());
769 Assert.assertTrue( enc_stream.exists() );
771 InputStream in =
null;
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);
779 if( enc_feed.write(buffer, 0, count) ) {
780 try { Thread.sleep( slow_delay_ms ); }
catch(
final Throwable t) {}
782 PrintUtil.println(System.err,
"feed_source_00: Error: Failed to write "+count+
" bytes to feed: "+enc_feed.toString());
787 }
catch (
final Exception ex) {
788 PrintUtil.println(System.err,
"feed_source_00: Error: "+ex.getMessage());
789 ex.printStackTrace();
791 try {
if(
null != in ) { in.close(); } }
catch (
final IOException e) { e.printStackTrace(); }
794 enc_feed.set_eof( enc_feed.fail() ? -1 : 1 );
798 static FeederFunc feed_source_01_sized_slow =
new FeederFunc() {
800 public void feed(
final ByteInStream_Feed enc_feed) {
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 );
807 InputStream in =
null;
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);
815 if( enc_feed.write(buffer, 0, count) ) {
816 try { Thread.sleep( slow_delay_ms ); }
catch(
final Throwable t) {}
818 PrintUtil.println(System.err,
"feed_source_01: Error: Failed to write "+count+
" bytes to feed: "+enc_feed.toString());
823 }
catch (
final Exception ex) {
824 PrintUtil.println(System.err,
"feed_source_01: Error: "+ex.getMessage());
825 ex.printStackTrace();
827 try {
if(
null != in ) { in.close(); } }
catch (
final IOException e) { e.printStackTrace(); }
830 enc_feed.set_eof( !enc_feed.fail() && xfer_total == file_size ? 1 : -1 );
834 static FeederFunc feed_source_10_nosize_fast =
new FeederFunc() {
836 public void feed(
final ByteInStream_Feed enc_feed) {
838 final File enc_stream =
new File(enc_feed.id());
839 Assert.assertTrue( enc_stream.exists() );
841 InputStream in =
null;
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);
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());
855 }
catch (
final Exception ex) {
856 PrintUtil.println(System.err,
"feed_source_10: Error: "+ex.getMessage());
857 ex.printStackTrace();
859 try {
if(
null != in ) { in.close(); } }
catch (
final IOException e) { e.printStackTrace(); }
862 enc_feed.set_eof( enc_feed.fail() ? -1 : 1 );
866 static FeederFunc feed_source_11_sized_fast =
new FeederFunc() {
868 public void feed(
final ByteInStream_Feed enc_feed) {
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 );
875 InputStream in =
null;
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);
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());
889 }
catch (
final Exception ex) {
890 PrintUtil.println(System.err,
"feed_source_10: Error: "+ex.getMessage());
891 ex.printStackTrace();
893 try {
if(
null != in ) { in.close(); } }
catch (
final IOException e) { e.printStackTrace(); }
896 enc_feed.set_eof( !enc_feed.fail() && xfer_total == file_size ? 1 : -1 );
900 static FeederFunc feed_source_12_sized_eof_fast =
new FeederFunc() {
902 public void feed(
final ByteInStream_Feed enc_feed) {
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 );
909 InputStream in =
null;
911 in =
new FileInputStream(enc_stream);
912 boolean in_eof =
false;
913 final byte[] buffer =
new byte[
Cipherpack.buffer_size];
914 while( xfer_total < file_size && !in_eof ) {
915 final int count = in.read(buffer);
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());
922 }
else if( 0 > count ) {
926 }
catch (
final Exception ex) {
927 PrintUtil.println(System.err,
"feed_source_12: Error: "+ex.getMessage());
928 ex.printStackTrace();
930 try {
if(
null != in ) { in.close(); } }
catch (
final IOException e) { e.printStackTrace(); }
933 enc_feed.set_eof( !enc_feed.fail() && xfer_total == file_size ? 1 : -1 );
937 static void feed_source_20_nosize_irqed_1k(
final ByteInStream_Feed enc_feed) {
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 );
944 InputStream in =
null;
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);
952 if( enc_feed.write(buffer, 0, count) ) {
953 if( xfer_total >= 1024 ) {
954 enc_feed.set_eof( -1 );
958 PrintUtil.println(System.err,
"feed_source_20: Error: Failed to write "+count+
" bytes to feed: "+enc_feed.toString());
963 }
catch (
final Exception ex) {
964 PrintUtil.println(System.err,
"feed_source_20: Error: "+ex.getMessage());
965 ex.printStackTrace();
967 try {
if(
null != in ) { in.close(); } }
catch (
final IOException e) { e.printStackTrace(); }
972 static void feed_source_21_sized_irqed_quarter(
final ByteInStream_Feed enc_feed) {
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 );
979 InputStream in =
null;
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);
987 if( enc_feed.write(buffer, 0, count) ) {
988 if( xfer_total >= file_size/4 ) {
989 enc_feed.set_eof( -1 );
993 PrintUtil.println(System.err,
"feed_source_21: Error: Failed to write "+count+
" bytes to feed: "+enc_feed.toString());
998 }
catch (
final Exception ex) {
999 PrintUtil.println(System.err,
"feed_source_21: Error: "+ex.getMessage());
1000 ex.printStackTrace();
1002 try {
if(
null != in ) { in.close(); } }
catch (
final IOException e) { e.printStackTrace(); }
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) {
1013 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1016 sign_sec_key1_fname, sign_sec_key_passphrase,
1018 fname_plaintext_lst.get(file_idx),
"test31_fed_all_files", plaintext_version, plaintext_version_parent,
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() );
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 ) ) {
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 );
1043 feeder_thread.join(1000);
1044 }
catch (
final InterruptedException e) { }
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() );
1053 fname_decrypted_lst.get(file_idx), ph2.plaintext_hash);
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;
1076 SYSTEM_GET_CURRENT_MS
1083 t_last_progress =
null;
1085 clock_gettime_in_ms = 0;
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());
1096 PrintUtil.fprintf_td(System.err,
"%s: Notify Header: %s\n", name, header.
toString());
1101 public boolean notifyProgress(
final boolean decrypt_mode,
final long plaintext_size,
final long bytes_processed) {
1103 switch( get_time_method ) {
1104 case JAU_GET_MONOTONIC_TIME:
1107 t0 = org.jau.sys.Clock.getMonotonicTime();
1109 case JAU_GET_MONOTONIC_CURRENT_MS:
1111 t0 = Instant.ofEpochMilli(org.jau.sys.Clock.currentTimeMillis());
1113 case SYSTEM_GET_CURRENT_MS:
1116 t0 = Instant.ofEpochMilli(System.currentTimeMillis());
1119 if(
null != t_last ) {
1120 clock_gettime_in_ms = t_last.until(t0, ChronoUnit.MILLIS);
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 ) ) {
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);
1138 PrintUtil.fprintf_td(System.err,
"%s: Notify End: %s\n", name, header.
toString());
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");
1149 if(
null != test_loops_s ) {
1151 test_loops = Integer.parseInt(test_loops_s);
1152 }
catch(
final Exception ex) {
1153 ex.printStackTrace();
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;
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);
1165 fname_plaintext = test_plaintext_file;
1166 if(
null == test_encrypted_file ) {
1167 fname_encrypted = test_plaintext_file+
".enc";
1169 fname_encrypted = test_encrypted_file;
1171 fname_decrypted = test_plaintext_file+
".enc.dec";
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);
1177 if(
null == test_encrypted_file ) {
1180 final String plaintext_version_bytes =
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
1182 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext);
1185 sign_sec_key1_fname, sign_sec_key_passphrase,
1187 fname_plaintext,
"test32_bug574", plaintext_version_bytes, plaintext_version_parent,
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));
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;
1199 final ByteInStream_Feed enc_feed =
new ByteInStream_Feed(fname_encrypted, 5000);
1200 final Thread feeder_thread = executeOffThread( () -> { feed_func.feed(enc_feed); },
"test32_bug574::"+suffix,
false );
1204 dec_listener,
"", fname_decrypted);
1206 feeder_thread.join(1000);
1207 }
catch (
final InterruptedException e) { }
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",
1213 Assert.assertTrue(
"test32_bug574: "+suffix+
", "+ph2.toString(), ph2.isValid() );
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);
1227 final int file_idx = IDX_65MiB;
1229 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1232 sign_sec_key1_fname, sign_sec_key_passphrase,
1234 fname_plaintext_lst.get(file_idx),
"test34_enc_dec_fed_irq", plaintext_version, plaintext_version_parent,
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() );
1241 final String file_decrypted = fname_encrypted_lst.get(file_idx)+
".dec";
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 );
1252 feeder_thread.join(1000);
1253 }
catch (
final InterruptedException e) { }
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() );
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 );
1270 feeder_thread.join(1000);
1271 }
catch (
final InterruptedException e) { }
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() );
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;
1292 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1295 sign_sec_key1_fname, sign_sec_key_passphrase,
1297 fname_plaintext_lst.get(file_idx),
"test41_enc_1."+abort_item, plaintext_version, plaintext_version_parent,
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() );
1306 final ByteInStream_File source =
new ByteInStream_File(fname_plaintext_lst.get(file_idx));
1309 sign_sec_key1_fname, sign_sec_key_passphrase,
1311 fname_plaintext_lst.get(file_idx),
"test41_enc_2."+abort_item, plaintext_version, plaintext_version_parent,
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() );
1321 final ByteInStream_File enc_stream =
new ByteInStream_File(fname_encrypted_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() );
1330 if( !org.jau.io.UriTk.protocol_supported(
"http:") ) {
1331 PrintUtil.fprintf_td(System.err,
"http not supported, abort\n");
1334 final String uri_encrypted = url_input_root + fname_encrypted_lst.get(file_idx);
1337 final ByteInStream_URL enc_stream =
new ByteInStream_URL(uri_encrypted, io_timeout);
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() );
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 );
1355 enc_feed.closeStream();
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() );
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 );
1372 enc_feed.closeStream();
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() );
1383 final static String root =
"test_data";
1385 final static String project_root2 =
"../../../../jaulib/test_data";
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";
1392 PrintUtil.fprintf_td(System.err,
"\n");
1393 PrintUtil.fprintf_td(System.err,
"%s\n", title);
1395 FileUtil.remove(hash_file, TraverseOptions.none);
1397 final FileStats source_stats =
new FileStats(project_root2);
1398 Assert.assertTrue( source_stats.exists() );
1399 Assert.assertTrue( source_stats.is_dir() );
1401 final long[] source_bytes_hashed = { 0 };
1403 Assert.assertNotNull( source_hash );
1407 final String dest = root+
"_copy_verify_test50";
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);
1415 FileUtil.remove(dest, TraverseOptions.recursive);
1416 Assert.assertTrue(
true == FileUtil.copy(source_stats.path(), dest, copts) );
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() );
1423 final long[] dest_bytes_hashed = { 0 };
1425 Assert.assertNotNull( dest_hash );
1429 Assert.assertArrayEquals(source_hash, dest_hash);
1430 Assert.assertEquals( source_bytes_hashed[0], dest_bytes_hashed[0] );
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 ));
1435 Assert.assertTrue( FileUtil.remove(dest, TraverseOptions.recursive) );
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];
Cipherpack Factory, called by automatically to load the native library.
static void checkInitialized()
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.
void set_abort(final int what)
Bug574CipherpackListener(final String _name)
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)
JAU_GET_MONOTONIC_CURRENT_MS
OK.
JAU_GET_MONOTONIC_TIME
Triggers Bug574: