From deace4109c62d72b846263144532ec1118f84aef Mon Sep 17 00:00:00 2001 From: "git@daemon.de" Date: Wed, 26 Feb 2014 15:31:45 +0100 Subject: [PATCH] added tests/pipetest.c so I can test pcpstream with armoring with various blocksizes --- tests/Makefile.am | 5 +++- tests/pipetest.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++ tests/streamtest.c | 11 +++++--- 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 tests/pipetest.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 80bc842..e6afd77 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -20,7 +20,7 @@ # AM_CFLAGS = -I../include/pcp -I../src -I../libpcp/scrypt/crypto -Wall -g -check_PROGRAMS = col invalidkeys pwhashes gencheader statictest cpptest buffertest sample streamtest +check_PROGRAMS = col invalidkeys pwhashes gencheader statictest cpptest buffertest sample streamtest pipetest gencheader_LDADD = ../libpcp/.libs/libpcp1.a gencheader_SOURCES = gencheader.c @@ -37,6 +37,9 @@ sample_SOURCES = sample.c streamtest_LDADD = ../libpcp/.libs/libpcp1.a streamtest_SOURCES = streamtest.c +pipetest_LDADD = ../libpcp/.libs/libpcp1.a +pipetest_SOURCES = pipetest.c + col_LDADD = ../libpcp/.libs/libpcp1.a col_SOURCES = collisions.c ../src/compat_getopt.c diff --git a/tests/pipetest.c b/tests/pipetest.c new file mode 100644 index 0000000..605c28e --- /dev/null +++ b/tests/pipetest.c @@ -0,0 +1,62 @@ +#include +#include +#include + +#include + +int main(int argc, char **argv) { + if(argc < 4) { + fprintf(stderr, "Usage: pipetest \n"); + fprintf(stderr, "d - decode\ne - encode\n"); + return 1; + } + + size_t rblocksize; + size_t zblocksize; + char mode; + + if((rblocksize = strtol(argv[1], NULL, 0)) == 0) { + fprintf(stderr, "Error: invalid read blocksize %s\n", argv[1]); + return 1; + } + + if((zblocksize = strtol(argv[2], NULL, 0)) == 0) { + fprintf(stderr, "Error: invalid z85 blocksize %s\n", argv[2]); + return 1; + } + + if(zblocksize % 4 != 0) { + fprintf(stderr, "Error: z85 blocksize shall be divisible by 4\n"); + return 1; + } + + mode = argv[3][0]; + + if(mode != 'd' && mode != 'e') { + fprintf(stderr, "Error: invalid mode %s\n", argv[3]); + return 1; + } + + Pcpstream *in = ps_new_file(stdin); + Pcpstream *out = ps_new_file(stdout); + size_t got; + + if(mode == 'e') + ps_armor(out, zblocksize); + else + ps_setdetermine(in, zblocksize); + + void *buf = ucmalloc(rblocksize); + + while(!ps_end(in)) { + got = ps_read(in, buf, rblocksize); + if(got > 0) + ps_write(out, buf, got); + } + + ps_finish(out); + ps_close(in); + ps_close(out); + + return 0; +} diff --git a/tests/streamtest.c b/tests/streamtest.c index 101dcb7..dd879d1 100644 --- a/tests/streamtest.c +++ b/tests/streamtest.c @@ -10,7 +10,8 @@ int main() { unsigned char clear[8] = "ABCDEFGH"; unsigned char key[8] = "IxD8Lq1K"; unsigned char crypt[8] = {0}; - int blocks = 48; + int blocks = 8; + size_t blocksize = 4; int i = 0; if((out = fopen("teststream.out", "wb+")) == NULL) { @@ -21,17 +22,19 @@ int main() { /* out output stream, z85 encoded, use z85 blocksize 8 */ Pcpstream *pout = ps_new_file(out); ps_print(pout, "~~~~~ BEGIN ~~~~~\r\n"); - ps_armor(pout, 8); + ps_armor(pout, blocksize); /* "encrypt" a couple of times into the output stream */ for(i=0; iarmor = 0; ps_print(pout, "\r\n~~~~~ END ~~~~~\r\n"); ps_close(pout); @@ -45,7 +48,7 @@ int main() { Pcpstream *pin = ps_new_file(in); /* enable autmatically encoding detection. */ - ps_setdetermine(pin, 8); + ps_setdetermine(pin, blocksize); /* we'll use this stream to put the "decrypted" data in. note, that this could be a file as well. */ @@ -55,6 +58,7 @@ int main() { for(i=0; i