add readme_cr, chalresp test accepts challenge
[pam_pcsc_cr.git] / test_serial.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "serial.h"
4
5 int main(int argc, char *argv[])
6 {
7         char *in[] = {
8                 "My",
9                 "Little",
10                 "Pony",
11                 NULL,
12         };
13         char buffer[256];
14         int i, rc;
15         serializer_t srl;
16
17         if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) {
18                 printf("serial_init rc=%d\n", rc);
19                 return 1;
20         }
21         for (i = 0; in[i]; i++) {
22                 int size = strlen(in[i]);
23                 if ((rc = serial_put(&srl, in[i], size)) != size) {
24                         printf("serial_put(..., \"%s\", %d) = %d\n",
25                                 in[i], size, rc);
26                         return 1;
27                 }
28         }
29         if ((rc = serial_put(&srl, NULL, 0)) != 0) {
30                 printf("serial_put(..., NULL, 0) = %d\n", rc);
31                 return 1;
32         }
33         printf("serialized size=%d\n", serial_size(&srl));
34         if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) {
35                 printf("second serial_init rc=%d\n", rc);
36                 return 1;
37         }
38         for (i = 0; i < 4; i++) {
39                 char item[32];
40                 memset(item, 0, sizeof(item));
41                 int size = serial_get(&srl, item, sizeof(item));
42                 if (size > sizeof(item)) {
43                         printf("serial_get(..., item, %d) = %d\n",
44                                 (int)sizeof(item), size);
45                         rc = 1;
46                 } else if (size == 0) {
47                         printf("serial_get(...) = 0\n");
48                 } else {
49                         printf("serial_get(...) = %d: \"%s\"\n", size, item);
50                         if (memcmp(in[i], item, size)) {
51                                 printf("\"%s\" != \"%s\" (%d)\n",
52                                         in[i], item, size);
53                                 rc = 1;
54                         }
55                 }
56         }
57         return rc;
58 }