add test for serializer
authorEugene Crosser <crosser@average.org>
Thu, 31 Oct 2013 13:44:22 +0000 (17:44 +0400)
committerEugene Crosser <crosser@average.org>
Thu, 31 Oct 2013 13:44:22 +0000 (17:44 +0400)
Makefile.am
serial.c
test_serial.c [new file with mode: 0644]

index 3559ca10e74ccf43fc0327353b4477f6a5e0c722..850b33dbc69e04706f581b4e5390f08d7dd79af1 100644 (file)
@@ -15,10 +15,11 @@ lib_LTLIBRARIES = pam_pcsc_cr.la
 pam_pcsc_cr_la_LDFLAGS = -module -avoid-version
 pam_pcsc_cr_la_LIBADD = libpcsc_cr.la
 
-check_PROGRAMS = test_crypto test_chalresp
+check_PROGRAMS = test_serial test_crypto test_chalresp
+test_serial_LDADD = libpcsc_cr.la
 test_crypto_LDADD = libpcsc_cr.la
 test_chalresp_LDADD = libpcsc_cr.la
 
 EXTRA_DIST = autogen.sh
 
-TESTS = test_crypto test_chalresp
+TESTS = test_serial test_crypto test_chalresp
index 0719746a52eb7c001a6605c716e1de62df7c1d11..6bce59e8cf7b282a6718d4f6b2d9e6be09673223 100644 (file)
--- a/serial.c
+++ b/serial.c
@@ -8,6 +8,7 @@ int serial_init(serializer_t *srl, void *buffer, int size)
 {
        srl->buffer = srl->cursor = buffer;
        srl->bufsize = size;
+       return 0;
 }
 
 int serial_switch(serializer_t *srl, void *buffer, int size)
diff --git a/test_serial.c b/test_serial.c
new file mode 100644 (file)
index 0000000..b9d5e4b
--- /dev/null
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <string.h>
+#include "serial.h"
+
+int main(int argc, char *argv[])
+{
+       char *in[] = {
+               "My",
+               "Little",
+               "Pony",
+               NULL,
+       };
+       char buffer[256];
+       int i, rc;
+       serializer_t srl;
+
+       if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) {
+               printf("serial_init rc=%d\n", rc);
+               return 1;
+       }
+       for (i = 0; in[i]; i++) {
+               int size = strlen(in[i]);
+               if ((rc = serial_put(&srl, in[i], size)) != size) {
+                       printf("serial_put(..., \"%s\", %d) = %d\n",
+                               in[i], size, rc);
+                       return 1;
+               }
+       }
+       if ((rc = serial_put(&srl, NULL, 0)) != 0) {
+               printf("serial_put(..., NULL, 0) = %d\n", rc);
+               return 1;
+       }
+       printf("serialized size=%d\n", serial_size(&srl));
+       if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) {
+               printf("second serial_init rc=%d\n", rc);
+               return 1;
+       }
+       for (i = 0; i < 4; i++) {
+               char item[32];
+               memset(item, 0, sizeof(item));
+               int size = serial_get(&srl, item, sizeof(item));
+               if (size > sizeof(item)) {
+                       printf("serial_get(..., item, %d) = %d\n",
+                               (int)sizeof(item), size);
+                       rc = 1;
+               } else if (size == 0) {
+                       printf("serial_get(...) = 0\n");
+               } else {
+                       printf("serial_get(...) = %d: \"%s\"\n", size, item);
+                       if (memcmp(in[i], item, size)) {
+                               printf("\"%s\" != \"%s\" (%d)\n",
+                                       in[i], item, size);
+                               rc = 1;
+                       }
+               }
+       }
+       return rc;
+}