Merge pull request #115 from hfmanson/master
[tlspool] / test / pulleybacksimu.c
1 /* pulleybacksimu.c -- Simulation of driving the Pulley backend to the TLS Pool
2  *
3  * This simulates a series of calls that the SteamWorks Pulley could do on
4  * the backend in ../pulleyback/ -- it links against the library formed there,
5  * and tests the output with the ../test/get_xxx routines.
6  *
7  * From: Rick van Rein <rick@openfortress.nl>
8  */
9
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include <time.h>
16 #include <unistd.h>
17
18 #include <libgen.h>
19 #include <syslog.h>
20
21
22 #include "../pulleyback/api.h"
23
24
25 #define REMOTEID "remoteid@tlspool.arpa2.lab"
26 #define REMOTEID_PATN "remoteid@.arpa2.lab"
27 #define LOCALID  "localid@tlspool.arpa2.lab"
28
29 #define CONFFILE "../etc/tlspool.conf"
30
31 #define X509FILE "tlspool-test-client-cert.der"
32 #define PGPFILE "tlspool-test-client-pubkey.pgp"
33
34 #define ANCHOR_HEX "aabbccddeeff"
35 #define ANCHOR_BYTES 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
36
37 void inline testcmd (char *cmd) {
38         int exitval;
39         printf ("shell$ %s\n", cmd);
40         exitval = system (cmd);
41         printf ("$? = %d\n\n", exitval);
42 }
43
44 //TODO// Calling these subcommands causes trouble with the shared environment
45 //TODO// Note that db_stat does not give that same trouble with -h / -d ?!?
46 void showstatus (char *title) {
47         printf ("\n\n\n# %s\n", title);
48         // testcmd ("db_stat -h ../testdata/tlspool.env -d ../disclose.db");
49         // printf ("\n\n\n## Disclosure database\n\n\n");
50         // testcmd ("../tool/get_disclose "CONFFILE" "REMOTEID" >/dev/null");
51         // testcmd ("../tool/get_disclose "CONFFILE" "REMOTEID_PATN" >/dev/null");
52         // testcmd ("../tool/get_disclose "CONFFILE" "LOCALID" >/dev/null");
53         // printf ("\n\n\n## LocalID database\n\n\n");
54         // testcmd ("../tool/get_localid "CONFFILE" "REMOTEID" X.509,client,server"" >/dev/null");
55         // testcmd ("../tool/get_localid "CONFFILE" "LOCALID" X.509,client,server"" >/dev/null");
56         // testcmd ("../tool/get_localid "CONFFILE" "REMOTEID" OpenPGP,client,server"" >/dev/null");
57         // testcmd ("../tool/get_localid "CONFFILE" "LOCALID" OpenPGP,client,server"" >/dev/null");
58         // printf ("\n\n\n## Trust database\n\n\n");
59         // testcmd ("../tool/get_trust "CONFFILE" x509,client,server "ANCHOR_HEX" >/dev/null");
60         // testcmd ("../tool/get_trust "CONFFILE" x509,client,server "ANCHOR_HEX" >/dev/null");
61         // testcmd ("../tool/get_trust "CONFFILE" pgp,client,server "ANCHOR_HEX" >/dev/null");
62         // testcmd ("../tool/get_trust "CONFFILE" pgp,client,server "ANCHOR_HEX" >/dev/null");
63         // printf ("\nThat was our last test for %s\n\n\n", title);
64 }
65
66
67 void *open_backend (char *argv []) {
68         int argc = 0;
69         int varc = 0;
70         char *argsp;
71         void *backend;
72         for (argc = 0; argv [argc] != NULL; argc++) {
73                 if (strncmp (argv [argc], "args=", 5) != 0) {
74                         continue;
75                 }
76                 for (argsp = argv [argc]; argsp != NULL; argsp = strchr (argsp + 1, ',')) {
77                         varc++;
78                 }
79         }
80         backend = pulleyback_open (argc, argv, varc);
81         if (backend == NULL) {
82                 printf ("\n\nERROR: BACKEND DID NOT OPEN PROPERLY\n\n");
83         }
84         return backend;
85 }
86
87 void close_backend (void *backend) {
88         struct pulleyback_tlspool *self = backend;
89         if (backend != NULL) {
90                 pulleyback_close (backend);
91         }
92 }
93
94
95 char *test0 [] = { "pulleyback_tlspool", "config=../etc/tlspool.conf", "type=disclose", "args=localid,remoteid", NULL };
96 char *test1 [] = { "pulleyback_tlspool", "config=../etc/tlspool.conf", "type=disclose", "args=remoteid,localid", NULL };
97 char *test2 [] = { "pulleyback_tlspool", "config=../etc/tlspool.conf", "type=localid", "args=localid,pkcs11,cred", "subtype=x509,client", NULL };
98
99 uint8_t *args0 [2] = { (uint8_t*)"\x0c\x1clocalid@pulleyback.arpa2.lab", (uint8_t*)"\x0c\x1dremoteid@pulleyback.arpa2.lab" };
100 uint8_t *args1 [2] = { (uint8_t*)"\x0c\x1dremoteid@pulleyback.arpa2.lab", (uint8_t*)"\x0c\x1clocalid@pulleyback.arpa2.lab" };
101 uint8_t *args2 [3] = { (uint8_t*)"\x0c\x1clocalid@pulleyback.arpa2.lab", (uint8_t*)"\x0c\x24pkcs11:manuf=OpenFortress;serial=123", (uint8_t*)"\x0c\x1a-----LIKE A PGP KEY-----\r\n" };
102
103 char **tests [] = { test0, test1, test2, NULL };
104 uint8_t **argss [] = { args0, args1, args2, NULL };
105
106
107 int main (int argc, char *argv []) {
108         char ***testp;
109         uint8_t ***argsp;
110         uint8_t derargs [3] [130];
111         void *backend;
112         int testnr = 0;
113         char testtitle [105];
114         close (2);
115         dup2 (1, 2);
116         openlog (basename (argv [0]), LOG_PERROR, LOG_LOCAL0);
117         // showstatus ("Initial status");
118         for (testp = tests, argsp = argss; *testp != NULL; testp++, argsp++) {
119                 backend = open_backend (*testp);
120                 if (backend == NULL) {
121                         continue;
122                 }
123                 snprintf (testtitle, 100, "Test number %d\n", testnr);
124                 showstatus (testtitle);
125                 pulleyback_add (backend, *argsp);
126                 if (pulleyback_commit (backend)) {
127                         printf ("Go have a look...\n");
128                 } else {
129                         printf ("Failed, so you probably won't find it...\n");
130                 }
131                 sleep (10);
132                 snprintf (testtitle, 100, "Added in test number %d\n", testnr++);
133                 showstatus (testtitle);
134                 pulleyback_del (backend, *argsp);
135                 if (pulleyback_commit (backend)) {
136                         printf ("It should be gone...\n");
137                 } else {
138                         printf ("Failed, so it is probably still there...\n");
139                 }
140                 close_backend (backend);
141         }
142         // showstatus ("Final status");
143         closelog ();
144         exit (0);
145 }