blob: 560929c646beac7ade7c47e4acdba458bd4db702 [file] [log] [blame]
Lev Walkin1e609cf2004-09-27 20:50:48 +00001#undef NDEBUG
2#include <stdio.h>
3#include <stdlib.h>
4#include <sys/types.h>
Lev Walkine0d321a2014-09-11 01:28:57 -07005#include <unistd.h> /* for chdir(2), getcwd(3) */
Lev Walkin1e609cf2004-09-27 20:50:48 +00006#include <string.h>
7#include <dirent.h>
8#include <assert.h>
9#include <errno.h>
10
11#include <T.h>
12
Lev Walkine0d321a2014-09-11 01:28:57 -070013#ifndef SRCDIR
14#define SRCDIR_S ".."
15#else
16#define STRINGIFY_MACRO2(x) #x
17#define STRINGIFY_MACRO(x) STRINGIFY_MACRO2(x)
18#define SRCDIR_S STRINGIFY_MACRO(SRCDIR)
19#endif
20
Lev Walkin1e609cf2004-09-27 20:50:48 +000021enum expectation {
22 EXP_OK, /* Encoding/decoding must succeed */
23 EXP_BROKEN, /* Decoding must fail */
24 EXP_RECLESS, /* Reconstruction is allowed to yield less data */
Lev Walkinc5364602004-10-05 06:38:38 +000025 EXP_DIFFERENT, /* Reconstruction will yield different encoding */
Lev Walkin1e609cf2004-09-27 20:50:48 +000026};
27
28static unsigned char buf[4096];
29static int buf_offset;
30
31static int
32_buf_writer(const void *buffer, size_t size, void *app_key) {
33 unsigned char *b, *bend;
34 (void)app_key;
35 assert(buf_offset + size < sizeof(buf));
36 memcpy(buf + buf_offset, buffer, size);
37 b = buf + buf_offset;
38 bend = b + size;
39 printf("=> [");
40 for(; b < bend; b++)
41 printf(" %02X", *b);
Lev Walkin1715b322013-03-28 04:02:13 -070042 printf("]:%zd\n", size);
Lev Walkin1e609cf2004-09-27 20:50:48 +000043 buf_offset += size;
44 return 0;
45}
46
47static int
48save_object(T_t *st) {
49 asn_enc_rval_t rval; /* Return value */
50
51 buf_offset = 0;
52
Lev Walkinc7400c52004-09-29 13:14:36 +000053 rval = der_encode(&asn_DEF_T, st, _buf_writer, 0);
Lev Walkin1e609cf2004-09-27 20:50:48 +000054 if (rval.encoded == -1) {
55 fprintf(stderr,
56 "Cannot encode %s: %s\n",
57 rval.failed_type->name, strerror(errno));
58 assert(rval.encoded != -1);
59 return -1; /* JIC */
60 }
61
62 fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
63
64 return 0;
65}
66
67static T_t *
Lev Walkin5b63acf2014-01-14 01:48:37 -080068load_object(enum expectation expectation, unsigned char *fbuf, size_t size) {
Lev Walkin90bf7ae2004-10-20 15:46:56 +000069 asn_dec_rval_t rval;
Lev Walkin1e609cf2004-09-27 20:50:48 +000070 T_t *st = 0;
71 int csize;
72
Lev Walkin5b63acf2014-01-14 01:48:37 -080073 fprintf(stderr, "LOADING OBJECT OF SIZE %d\n", (int)size);
Lev Walkin1e609cf2004-09-27 20:50:48 +000074
75 /* Perform multiple iterations with multiple chunks sizes */
76 for(csize = 1; csize < 20; csize += 1) {
77 int fbuf_offset = 0;
78 int fbuf_left = size;
79 int fbuf_chunk = csize;
80
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080081 if(st) ASN_STRUCT_FREE(asn_DEF_T, st);
Lev Walkin1e609cf2004-09-27 20:50:48 +000082 st = 0;
83
84 do {
85 fprintf(stderr, "Decoding from %d with %d (left %d)\n",
86 fbuf_offset, fbuf_chunk, fbuf_left);
Lev Walkinc7400c52004-09-29 13:14:36 +000087 rval = ber_decode(0, &asn_DEF_T, (void **)&st,
Lev Walkin1e609cf2004-09-27 20:50:48 +000088 fbuf + fbuf_offset,
89 fbuf_chunk < fbuf_left
90 ? fbuf_chunk : fbuf_left);
91 fbuf_offset += rval.consumed;
92 fbuf_left -= rval.consumed;
93 if(rval.code == RC_WMORE)
94 fbuf_chunk += 1; /* Give little more */
95 else
96 fbuf_chunk = csize; /* Back off */
97 } while(fbuf_left && rval.code == RC_WMORE);
98
99 if(expectation != EXP_BROKEN) {
100 assert(rval.code == RC_OK);
Lev Walkin5b63acf2014-01-14 01:48:37 -0800101 assert(fbuf_offset == (ssize_t)size);
Lev Walkin1e609cf2004-09-27 20:50:48 +0000102 } else {
103 assert(rval.code != RC_OK);
104 fprintf(stderr, "Failed, but this was expected\n");
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800105 ASN_STRUCT_FREE(asn_DEF_T, st);
Lev Walkin1e609cf2004-09-27 20:50:48 +0000106 st = 0; /* ignore leak for now */
107 }
108 }
109
Lev Walkinc7400c52004-09-29 13:14:36 +0000110 if(st) asn_fprint(stderr, &asn_DEF_T, st);
Lev Walkin1e609cf2004-09-27 20:50:48 +0000111 return st;
112}
113
114
115static void
Lev Walkin5b63acf2014-01-14 01:48:37 -0800116process_data(enum expectation expectation, unsigned char *fbuf, ssize_t size) {
Lev Walkin1e609cf2004-09-27 20:50:48 +0000117 T_t *st;
118 int ret;
119
120 st = load_object(expectation, fbuf, size);
121 if(!st) return;
122
123 ret = save_object(st);
Lev Walkin5b63acf2014-01-14 01:48:37 -0800124 assert(buf_offset < (ssize_t)sizeof(buf));
Lev Walkin1e609cf2004-09-27 20:50:48 +0000125 assert(ret == 0);
126
Lev Walkinc5364602004-10-05 06:38:38 +0000127 switch(expectation) {
128 case EXP_RECLESS:
Lev Walkin1e609cf2004-09-27 20:50:48 +0000129 assert(buf_offset > 0 && buf_offset < size);
130 assert(memcmp(buf + 2, fbuf + 2, buf_offset - 2) == 0);
Lev Walkinc5364602004-10-05 06:38:38 +0000131 break;
132 case EXP_DIFFERENT:
133 assert(buf_offset > 0 && buf_offset < size);
134 break;
135 case EXP_BROKEN:
Lev Walkinb5ce57f2005-02-18 09:41:13 +0000136 assert(buf_offset != size
137 || memcmp(buf, fbuf, buf_offset));
Lev Walkinc5364602004-10-05 06:38:38 +0000138 break;
Lev Walkin1c830032004-10-28 13:24:27 +0000139 case EXP_OK:
Lev Walkin5b63acf2014-01-14 01:48:37 -0800140 assert(buf_offset == (ssize_t)size);
Lev Walkinb5ce57f2005-02-18 09:41:13 +0000141 assert(memcmp(buf, fbuf, buf_offset) == 0);
Lev Walkin1c830032004-10-28 13:24:27 +0000142 break;
Lev Walkin1e609cf2004-09-27 20:50:48 +0000143 }
144
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800145 ASN_STRUCT_FREE(asn_DEF_T, st);
Lev Walkin1e609cf2004-09-27 20:50:48 +0000146}
147
148/*
149 * Decode the .der files and try to regenerate them.
150 */
151static int
152process(const char *fname) {
Lev Walkine0d321a2014-09-11 01:28:57 -0700153 char prevdir[256];
Lev Walkin5b63acf2014-01-14 01:48:37 -0800154 unsigned char fbuf[4096];
Lev Walkin1e609cf2004-09-27 20:50:48 +0000155 char *ext = strrchr(fname, '.');
156 enum expectation expectation;
157 int ret;
158 int rd;
159 FILE *fp;
160
161 if(ext == 0 || strcmp(ext, ".ber"))
162 return 0;
163
164 switch(ext[-1]) {
165 case 'B': /* The file is intentionally broken */
166 expectation = EXP_BROKEN; break;
Lev Walkinc5364602004-10-05 06:38:38 +0000167 case 'D': /* Reconstructing should yield different data */
168 expectation = EXP_DIFFERENT; break;
Lev Walkin1e609cf2004-09-27 20:50:48 +0000169 case 'L': /* Extensions are present */
170 expectation = EXP_RECLESS; break;
171 default:
172 expectation = EXP_OK; break;
173 }
174
175 fprintf(stderr, "\nProcessing file [../%s]\n", fname);
176
Lev Walkine0d321a2014-09-11 01:28:57 -0700177 getcwd(prevdir, sizeof(prevdir));
178 ret = chdir(SRCDIR_S "/data-62");
Lev Walkin1e609cf2004-09-27 20:50:48 +0000179 assert(ret == 0);
180 fp = fopen(fname, "r");
Lev Walkine0d321a2014-09-11 01:28:57 -0700181 ret = chdir(prevdir);
Lev Walkin1e609cf2004-09-27 20:50:48 +0000182 assert(ret == 0);
183 assert(fp);
184
185 rd = fread(fbuf, 1, sizeof(fbuf), fp);
186 fclose(fp);
187
Lev Walkin5b63acf2014-01-14 01:48:37 -0800188 assert(rd < (ssize_t)sizeof(fbuf)); /* expect small files */
Lev Walkin1e609cf2004-09-27 20:50:48 +0000189
190 process_data(expectation, fbuf, rd);
191
192 return 1;
193}
194
195int
196main() {
197 DIR *dir;
198 struct dirent *dent;
199 int processed_files = 0;
Lev Walkinc5364602004-10-05 06:38:38 +0000200 char *str;
Lev Walkin1e609cf2004-09-27 20:50:48 +0000201
Lev Walkine0d321a2014-09-11 01:28:57 -0700202 dir = opendir(SRCDIR_S "/data-62");
Lev Walkin1e609cf2004-09-27 20:50:48 +0000203 assert(dir);
204
Lev Walkinc5364602004-10-05 06:38:38 +0000205 str = getenv("DATA_62_FILE");
206 if(str && strncmp(str, "data-62-", 8) == 0)
207 process(str);
Lev Walkin1e609cf2004-09-27 20:50:48 +0000208
Lev Walkinc5364602004-10-05 06:38:38 +0000209 while((dent = readdir(dir))) {
210 if(strncmp(dent->d_name, "data-62-", 8) == 0)
211 if(process(dent->d_name))
212 processed_files++;
Lev Walkin1e609cf2004-09-27 20:50:48 +0000213 }
214
215 assert(processed_files);
216 closedir(dir);
217
218 return 0;
219}
220