1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2021 Gothel Software e.K.
* Copyright (c) 2021 ZAFENA AB
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cstring>
#include <fstream>
#include <iostream>
#define CATCH_CONFIG_RUNNER
// #define CATCH_CONFIG_MAIN
#include <catch2/catch_amalgamated.hpp>
#include <jau/test/catch2_ext.hpp>
#include <cipherpack/cipherpack.hpp>
#include "data_test.hpp"
#include <jau/debug.hpp>
#include <jau/file_util.hpp>
extern "C" {
#include <unistd.h>
}
using namespace jau::fractions_i64_literals;
class Test01Cipherpack : public TestData {
private:
const std::string payload_version = "0";
const std::string payload_version_parent = "0";
const size_t IDX_11kiB = 0;
const size_t IDX_65MiB = 1;
static std::vector<std::string> fname_payload_lst;
static std::vector<std::string> fname_payload_encrypted_lst;
static std::vector<std::string> fname_payload_decrypted_lst;
class data {
private:
static void add_test_file(const std::string name, const size_t size) {
jau::fs::remove(name);
jau::fs::remove(name+".enc");
jau::fs::remove(name+".enc.dec");
{
static const std::string one_line = "Hello World, this is a test and I like it. Exactly 100 characters long. 0123456780 abcdefghjklmnop..";
std::ofstream ofs(name, std::ios::out | std::ios::binary);
REQUIRE( ofs.good() == true );
REQUIRE( ofs.is_open() == true );
for(size_t i=0; i < size; i+=one_line.size()) {
ofs.write(reinterpret_cast<const char*>(one_line.data()), one_line.size());
}
ofs.write("X", 1); // make it odd
}
fname_payload_lst.push_back(name);
fname_payload_encrypted_lst.push_back(name+".enc");
fname_payload_decrypted_lst.push_back(name+".enc.dec");
}
data() {
// setenv("cipherpack.debug", "true", 1 /* overwrite */);
setenv("cipherpack.verbose", "true", 1 /* overwrite */);
add_test_file("test_cipher_01_11kiB.bin", 1024*11);
add_test_file("test_cipher_02_65MiB.bin", 1024*1024*65);
}
public:
static const data& get() {
static data instance;
return instance;
}
};
cipherpack::CipherpackListenerRef silentListener = std::make_shared<cipherpack::CipherpackListener>();
public:
Test01Cipherpack() {
// produce fresh demo data once per whole test class
const data& d = data::get();
(void)d;
}
~Test01Cipherpack() {
if( jau::io::uri_tk::protocol_supported("http:") ) {
int res = std::system("killall mini_httpd");
(void)res;
}
}
static void httpd_start() {
if( jau::io::uri_tk::protocol_supported("http:") ) {
int res = std::system("killall mini_httpd");
const std::string cwd = jau::fs::get_cwd();
const std::string cmd = "/usr/sbin/mini_httpd -p 8080 -l "+cwd+"/mini_httpd.log";
jau::PLAIN_PRINT(true, "%s", cmd.c_str());
res = std::system(cmd.c_str());
(void)res;
}
}
void test01_enc_dec_file_ok() {
const std::vector<std::string> enc_pub_keys { enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname };
const std::vector<std::string> sign_pub_keys { sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname };
{
const size_t file_idx = IDX_11kiB;
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
{
const size_t file_idx = IDX_11kiB;
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key2_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Encrypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key2_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
{
const size_t file_idx = IDX_65MiB;
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key3_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, "", fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
{
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, "", fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
jau::PLAIN_PRINT(true, "");
}
{
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, "SHA-256", fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
hash_retest(fname_payload_lst[file_idx], fname_payload_decrypted_lst[file_idx],
ph2.getPayloadHashAlgo(), ph2.getPayloadHash());
}
{
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, "SHA-512", fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
hash_retest(fname_payload_lst[file_idx], fname_payload_decrypted_lst[file_idx],
ph2.getPayloadHashAlgo(), ph2.getPayloadHash());
}
{
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, "BLAKE2b(512)", fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test01_enc_dec_file_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
hash_retest(fname_payload_lst[file_idx], fname_payload_decrypted_lst[file_idx],
ph2.getPayloadHashAlgo(), ph2.getPayloadHash());
}
}
}
void hash_retest(const std::string& orig_file, const std::string& hashed_decrypted_file,
const std::string& hash_algo, const std::vector<uint8_t>& hash_value)
{
const std::string suffix = cipherpack::hash_util::file_suffix(hash_algo);
const std::string out_file = hashed_decrypted_file + "." + suffix;
jau::fs::remove( out_file );
REQUIRE( true == cipherpack::hash_util::append_to_file(out_file, hashed_decrypted_file, hash_value) );
std::unique_ptr<jau::io::ByteInStream> orig_in = jau::io::to_ByteInStream(orig_file);
REQUIRE( nullptr != orig_in );
const jau::fraction_timespec _t0 = jau::getMonotonicTime();
std::unique_ptr<std::vector<uint8_t>> orig_hash_value = cipherpack::hash_util::calc(hash_algo, *orig_in);
REQUIRE( nullptr != orig_hash_value );
REQUIRE( hash_value == *orig_hash_value );
const jau::fraction_i64 _td = ( jau::getMonotonicTime() - _t0 ).to_fraction_i64();
jau::io::print_stats("Hash "+hash_algo, orig_in->content_size(), _td);
jau::PLAIN_PRINT(true, "");
}
void test02_enc_dec_file_error() {
const size_t file_idx = IDX_11kiB;
const std::vector<std::string> enc_pub_keys { enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname };
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test02_enc_dec_file_error: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test02_enc_dec_file_error: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
{
// Error: Not encrypted for terminal key 4
const std::vector<std::string> sign_pub_keys { sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname };
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key4_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test02_enc_dec_file_error: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test02_enc_dec_file_error: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == false );
}
{
// Error: Not signed from host key 4
const std::vector<std::string> sign_pub_keys_nope { sign_pub_key4_fname };
jau::io::ByteInStream_File enc_stream(fname_payload_encrypted_lst[file_idx], true /* use_binary */);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys_nope, dec_sec_key3_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test02_enc_dec_file_error: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test02_enc_dec_file_error: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == false );
}
}
void test11_dec_http_ok() {
if( !jau::io::uri_tk::protocol_supported("http:") ) {
jau::PLAIN_PRINT(true, "http not supported, abort\n");
return;
}
httpd_start();
const size_t file_idx = IDX_11kiB;
const std::vector<std::string> enc_pub_keys { enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname };
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test11_dec_http_ok: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test11_dec_http_ok: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
const std::string uri_encrypted = url_input_root + fname_payload_encrypted_lst[file_idx];
const std::string file_decrypted = fname_payload_encrypted_lst[file_idx]+".dec";
const std::vector<std::string> sign_pub_keys { sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname };
{
jau::io::ByteInStream_URL enc_stream(uri_encrypted, io_timeout);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), file_decrypted);
jau::PLAIN_PRINT(true, "test11_dec_http_ok: Decypted %s to %s\n", uri_encrypted.c_str(), file_decrypted.c_str());
jau::PLAIN_PRINT(true, "test11_dec_http_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
{
jau::io::ByteInStream_URL enc_stream(uri_encrypted, io_timeout);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key2_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), file_decrypted);
jau::PLAIN_PRINT(true, "test11_dec_http_ok: Decypted %s to %s\n", uri_encrypted.c_str(), file_decrypted.c_str());
jau::PLAIN_PRINT(true, "test11_dec_http_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
{
jau::io::ByteInStream_URL enc_stream(uri_encrypted, io_timeout);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key3_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), file_decrypted);
jau::PLAIN_PRINT(true, "test11_dec_http_ok: Decypted %s to %s\n", uri_encrypted.c_str(), file_decrypted.c_str());
jau::PLAIN_PRINT(true, "test11_dec_http_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
}
void test12_dec_http_ok() {
if( !jau::io::uri_tk::protocol_supported("http:") ) {
jau::PLAIN_PRINT(true, "http not supported, abort\n");
return;
}
httpd_start();
const size_t file_idx = IDX_65MiB;
const std::vector<std::string> enc_pub_keys { enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname };
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test12_dec_http_ok: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test12_dec_http_ok: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
const std::string uri_encrypted = url_input_root + fname_payload_encrypted_lst[file_idx];
const std::string file_decrypted = fname_payload_encrypted_lst[file_idx]+".dec";
const std::vector<std::string> sign_pub_keys { sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname };
{
jau::io::ByteInStream_URL enc_stream(uri_encrypted, io_timeout);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), file_decrypted);
jau::PLAIN_PRINT(true, "test11_dec_http_ok: Decypted %s to %s\n", uri_encrypted.c_str(), file_decrypted.c_str());
jau::PLAIN_PRINT(true, "test11_dec_http_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
}
void test13_dec_http_error() {
if( !jau::io::uri_tk::protocol_supported("http:") ) {
jau::PLAIN_PRINT(true, "http not supported, abort\n");
return;
}
httpd_start();
const size_t file_idx = IDX_11kiB;
const std::vector<std::string> enc_pub_keys { enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname };
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test13_dec_http_error: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test13_dec_http_error: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
const std::string uri_encrypted = url_input_root + fname_payload_encrypted_lst[file_idx];
const std::string file_decrypted = fname_payload_encrypted_lst[file_idx]+".dec";
const std::vector<std::string> sign_pub_keys { sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname };
{
// Error: Not encrypted for terminal key 4
jau::io::ByteInStream_URL enc_stream(uri_encrypted, io_timeout);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key4_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), file_decrypted);
jau::PLAIN_PRINT(true, "test13_dec_http_error: Decypted %s to %s\n", uri_encrypted.c_str(), file_decrypted.c_str());
jau::PLAIN_PRINT(true, "test13_dec_http_error: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == false );
}
{
// Error: Not signed from host key 4
const std::vector<std::string> sign_pub_keys_nope { sign_pub_key4_fname };
jau::io::ByteInStream_URL enc_stream(uri_encrypted, io_timeout);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys_nope, dec_sec_key2_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), file_decrypted);
jau::PLAIN_PRINT(true, "test13_dec_http_error: Decypted %s to %s\n", uri_encrypted.c_str(), file_decrypted.c_str());
jau::PLAIN_PRINT(true, "test13_dec_http_error: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == false );
}
{
// Error: URL file doesn't exist
const std::string uri_encrypted_err = url_input_root + "doesnt_exists.enc";
jau::io::ByteInStream_URL enc_stream(uri_encrypted_err, io_timeout);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key2_fname, dec_sec_key_passphrase,
enc_stream,
silentListener, cipherpack::default_hash_algo(), file_decrypted);
jau::PLAIN_PRINT(true, "test13_dec_http_error: Decypted %s to %s\n", uri_encrypted.c_str(), file_decrypted.c_str());
jau::PLAIN_PRINT(true, "test13_dec_http_error: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == false );
}
}
// throttled, no content size, interruptReader() via set_eof() will avoid timeout
static void feed_source_00(jau::io::ByteInStream_Feed * enc_feed) {
uint64_t xfer_total = 0;
jau::io::ByteInStream_File enc_stream(enc_feed->id(), true /* use_binary */);
while( !enc_stream.end_of_data() ) {
uint8_t buffer[1024]; // 1k
size_t count = enc_stream.read(buffer, sizeof(buffer));
if( 0 < count ) {
xfer_total += count;
enc_feed->write(buffer, count);
jau::sleep_for( 16_ms );
}
}
// probably set after transfering due to above sleep, which also ends when total size has been reached.
enc_feed->set_eof( jau::io::async_io_result_t::SUCCESS );
}
// throttled, with content size
static void feed_source_01(jau::io::ByteInStream_Feed * enc_feed) {
jau::fs::file_stats fs_feed(enc_feed->id());
const uint64_t file_size = fs_feed.size();
enc_feed->set_content_size( file_size );
uint64_t xfer_total = 0;
jau::io::ByteInStream_File enc_stream(enc_feed->id(), true /* use_binary */);
while( !enc_stream.end_of_data() && xfer_total < file_size ) {
uint8_t buffer[1024]; // 1k
size_t count = enc_stream.read(buffer, sizeof(buffer));
if( 0 < count ) {
xfer_total += count;
enc_feed->write(buffer, count);
jau::sleep_for( 16_ms );
}
}
// probably set after transfering due to above sleep, which also ends when total size has been reached.
enc_feed->set_eof( xfer_total == file_size ? jau::io::async_io_result_t::SUCCESS : jau::io::async_io_result_t::FAILED );
}
// full speed, with content size
static void feed_source_10(jau::io::ByteInStream_Feed * enc_feed) {
jau::fs::file_stats fs_feed(enc_feed->id());
const uint64_t file_size = fs_feed.size();
enc_feed->set_content_size( file_size );
uint64_t xfer_total = 0;
jau::io::ByteInStream_File enc_stream(enc_feed->id(), true /* use_binary */);
while( !enc_stream.end_of_data() && xfer_total < file_size ) {
uint8_t buffer[1024]; // 1k
size_t count = enc_stream.read(buffer, sizeof(buffer));
if( 0 < count ) {
xfer_total += count;
enc_feed->write(buffer, count);
}
}
enc_feed->set_eof( xfer_total == file_size ? jau::io::async_io_result_t::SUCCESS : jau::io::async_io_result_t::FAILED );
}
// full speed, no content size, interrupting @ 1024 bytes within our header
static void feed_source_20(jau::io::ByteInStream_Feed * enc_feed) {
uint64_t xfer_total = 0;
jau::io::ByteInStream_File enc_stream(enc_feed->id(), true /* use_binary */);
while( !enc_stream.end_of_data() ) {
uint8_t buffer[1024]; // 1k
size_t count = enc_stream.read(buffer, sizeof(buffer));
if( 0 < count ) {
xfer_total += count;
enc_feed->write(buffer, count);
if( xfer_total >= 1024 ) {
enc_feed->set_eof( jau::io::async_io_result_t::FAILED ); // calls data_feed->interruptReader();
return;
}
}
}
}
// full speed, with content size, interrupting 1/4 way
static void feed_source_21(jau::io::ByteInStream_Feed * enc_feed) {
jau::fs::file_stats fs_feed(enc_feed->id());
const uint64_t file_size = fs_feed.size();
enc_feed->set_content_size( file_size );
uint64_t xfer_total = 0;
jau::io::ByteInStream_File enc_stream(enc_feed->id(), true /* use_binary */);
while( !enc_stream.end_of_data() ) {
uint8_t buffer[1024]; // 1k
size_t count = enc_stream.read(buffer, sizeof(buffer));
if( 0 < count ) {
xfer_total += count;
enc_feed->write(buffer, count);
if( xfer_total >= file_size/4 ) {
enc_feed->set_eof( jau::io::async_io_result_t::FAILED ); // calls data_feed->interruptReader();
return;
}
}
}
}
void test21_enc_dec_fed_ok() {
const std::vector<std::string> enc_pub_keys { enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname };
const std::vector<std::string> sign_pub_keys { sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname };
{
const size_t file_idx = IDX_11kiB;
{
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
}
{
// throttled, no content size, interruptReader() via set_eof() will avoid timeout
jau::io::ByteInStream_Feed enc_feed(fname_payload_encrypted_lst[file_idx], io_timeout);
std::thread feeder_thread= std::thread(&feed_source_00, &enc_feed);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_feed,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
if( feeder_thread.joinable() ) {
feeder_thread.join();
}
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
{
// throttled, with content size
jau::io::ByteInStream_Feed enc_feed(fname_payload_encrypted_lst[file_idx], io_timeout);
std::thread feeder_thread= std::thread(&feed_source_01, &enc_feed);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_feed,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
if( feeder_thread.joinable() ) {
feeder_thread.join();
}
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
}
{
const size_t file_idx = IDX_65MiB;
{
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
}
{
// full speed, with content size
jau::io::ByteInStream_Feed enc_feed(fname_payload_encrypted_lst[file_idx], io_timeout);
std::thread feeder_thread= std::thread(&feed_source_10, &enc_feed);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_feed,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
if( feeder_thread.joinable() ) {
feeder_thread.join();
}
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test21_enc_dec_fed_ok: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == true );
}
}
}
void test22_enc_dec_fed_irq() {
const std::vector<std::string> enc_pub_keys { enc_pub_key1_fname, enc_pub_key2_fname, enc_pub_key3_fname };
const std::vector<std::string> sign_pub_keys { sign_pub_key1_fname, sign_pub_key2_fname, sign_pub_key3_fname };
{
const size_t file_idx = IDX_65MiB;
{
jau::io::ByteInStream_File source(fname_payload_lst[file_idx]);
cipherpack::PackHeader ph1 = cipherpack::encryptThenSign(cipherpack::CryptoConfig::getDefault(),
enc_pub_keys,
sign_sec_key1_fname, sign_sec_key_passphrase,
source, fname_payload_lst[file_idx], "test_case", payload_version, payload_version_parent,
silentListener, cipherpack::default_hash_algo(), fname_payload_encrypted_lst[file_idx]);
jau::PLAIN_PRINT(true, "test22_enc_dec_fed_irq: Encrypted %s to %s\n", fname_payload_lst[file_idx].c_str(), fname_payload_encrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test22_enc_dec_fed_irq: %s\n", ph1.toString(true, true).c_str());
REQUIRE( ph1.isValid() == true );
}
{
// full speed, no content size, interrupting @ 1024 bytes within our header
jau::io::ByteInStream_Feed enc_feed(fname_payload_encrypted_lst[file_idx], io_timeout);
std::thread feeder_thread= std::thread(&feed_source_20, &enc_feed);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_feed,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
if( feeder_thread.joinable() ) {
feeder_thread.join();
}
jau::PLAIN_PRINT(true, "test22_enc_dec_fed_irq: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test22_enc_dec_fed_irq: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == false );
}
{
// full speed, with content size, interrupting 1/4 way
jau::io::ByteInStream_Feed enc_feed(fname_payload_encrypted_lst[file_idx], io_timeout);
std::thread feeder_thread= std::thread(&feed_source_21, &enc_feed);
cipherpack::PackHeader ph2 = cipherpack::checkSignThenDecrypt(sign_pub_keys, dec_sec_key1_fname, dec_sec_key_passphrase,
enc_feed,
silentListener, cipherpack::default_hash_algo(), fname_payload_decrypted_lst[file_idx]);
if( feeder_thread.joinable() ) {
feeder_thread.join();
}
jau::PLAIN_PRINT(true, "test22_enc_dec_fed_irq: Decypted %s to %s\n", fname_payload_encrypted_lst[file_idx].c_str(), fname_payload_decrypted_lst[file_idx].c_str());
jau::PLAIN_PRINT(true, "test22_enc_dec_fed_irq: %s\n", ph2.toString(true, true).c_str());
REQUIRE( ph2.isValid() == false );
}
}
}
};
std::vector<std::string> Test01Cipherpack::fname_payload_lst;
std::vector<std::string> Test01Cipherpack::fname_payload_encrypted_lst;
std::vector<std::string> Test01Cipherpack::fname_payload_decrypted_lst;
METHOD_AS_TEST_CASE( Test01Cipherpack::test01_enc_dec_file_ok, "CipherPack 01 test01_enc_dec_file_ok");
METHOD_AS_TEST_CASE( Test01Cipherpack::test02_enc_dec_file_error, "CipherPack 01 test02_enc_dec_file_error");
METHOD_AS_TEST_CASE( Test01Cipherpack::test11_dec_http_ok, "CipherPack 02 test11_dec_http_ok");
METHOD_AS_TEST_CASE( Test01Cipherpack::test12_dec_http_ok, "CipherPack 02 test12_dec_http_ok");
METHOD_AS_TEST_CASE( Test01Cipherpack::test13_dec_http_error, "CipherPack 02 test13_dec_http_error");
METHOD_AS_TEST_CASE( Test01Cipherpack::test21_enc_dec_fed_ok, "CipherPack 03 test21_enc_dec_fed_ok");
METHOD_AS_TEST_CASE( Test01Cipherpack::test22_enc_dec_fed_irq, "CipherPack 03 test22_enc_dec_fed_irq");
|