diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2009-11-08 22:00:16 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2009-11-08 22:00:16 +0800 |
commit | ce77ea6d53b822b1f8ae96ea5b58b7b87e69c887 (patch) | |
tree | 79d0cc207d633274abfce9c2d0e805b9ba16080e | |
parent | d3e423430a8db14c8cf3583051c0f23241a8df21 (diff) |
milkway/test: add a test for encrypt/decrypt
-rw-r--r-- | milkway/test/Makefile.am | 6 | ||||
-rw-r--r-- | milkway/test/crypt-test.c | 72 |
2 files changed, 77 insertions, 1 deletions
diff --git a/milkway/test/Makefile.am b/milkway/test/Makefile.am index 9168c4f..2497f56 100644 --- a/milkway/test/Makefile.am +++ b/milkway/test/Makefile.am @@ -1,5 +1,5 @@ SUBDIRS = -check_PROGRAMS = hexlify checksum object poll main-loop socket +check_PROGRAMS = hexlify checksum object poll main-loop socket crypt hexlify_SOURCES = hexlify-test.c hexlify_CFLAGS = -I$(top_srcdir)/ hexlify_LDADD = $(top_builddir)/milkway/libmilkway.la @@ -30,3 +30,7 @@ socket_CFLAGS = -I$(top_srcdir)/ socket_LDADD = $(top_builddir)/milkway/libmilkway.la socket_LDFLAGS = $(MILKWAY_LIBS) +crypt_SOURCES = crypt-test.c +crypt_CFLAGS = -I$(top_srcdir)/ +crypt_LDADD = $(top_builddir)/milkway/libmilkway.la +crypt_LDFLAGS = $(MILKWAY_LIBS) diff --git a/milkway/test/crypt-test.c b/milkway/test/crypt-test.c new file mode 100644 index 0000000..d58b82c --- /dev/null +++ b/milkway/test/crypt-test.c @@ -0,0 +1,72 @@ +/* + * Copyright © 2009 Luo Jinghua <sunmoon1997@gmail.com> + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of Keith Packard not be used in + * advertising or publicity pertaining to distribution of the software without + * specific, written prior permission. Keith Packard makes no + * representations about the suitability of this software for any purpose. It + * is provided "as is" without express or implied warranty. + * + * LUO JINGHUA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ +#include <milkway/milkway.h> + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +static void +test_decrypt(const char *data, size_t datalen) +{ + char *out; + size_t outlen = datalen; + int len; + + out = malloc(outlen + 1); + mw_unless(out); + + len = mw_decrypt(data, datalen, out, outlen); + mw_unless(len >= 0 && len <= outlen); + + out[len - 1] = '\0'; + printf ("decrypted data:\n%s\n", out); + free(out); +} + +int main(int argc, char **argv) +{ + FILE *fp; + char *data; + size_t len; + + if (argc < 2) { + printf ("usage: file\n"); + return -1; + } + + fp = fopen(argv[1], "rb"); + mw_unless(fp); + + fseek(fp, 0, SEEK_END); + len = ftell(fp); + + fseek(fp, 0, SEEK_SET); + data = malloc(len); + mw_unless(data); + + fread(data, 1, len, fp); + fclose(fp); + + test_decrypt(data, len); + return 0; +} |