| 1 |
/* HEADER Testing ASN.1 encoding and decoding */ |
| 2 |
|
| 3 |
int i; |
| 4 |
|
| 5 |
#if 0 |
| 6 |
snmp_set_do_debugging(TRUE); |
| 7 |
debug_register_tokens("dumpv_recv,dumpv_send,asn"); |
| 8 |
#endif |
| 9 |
|
| 10 |
{ |
| 11 |
static struct { |
| 12 |
unsigned char encoded[8]; |
| 13 |
unsigned encoded_len; |
| 14 |
unsigned long decoded; |
| 15 |
} data[] = { |
| 16 |
{ { ASN_INTEGER, 4, 0x80, 0x00, 0x00, 0x00 }, 6, -0x80000000L }, |
| 17 |
{ { ASN_INTEGER, 4, 0x80, 0x00, 0x00, 0x01 }, 6, -0x7fffffffL }, |
| 18 |
{ { ASN_INTEGER, 3, 0xff, 0x00, 0x01 }, 5, -0xffffL }, |
| 19 |
{ { ASN_INTEGER, 1, 0xfd }, 3, -3 }, |
| 20 |
{ { ASN_INTEGER, 1, 0xff }, 3, -1 }, |
| 21 |
{ { ASN_INTEGER, 1, 0x00 }, 3, 0 }, |
| 22 |
{ { ASN_INTEGER, 1, 0x01 }, 3, 1 }, |
| 23 |
{ { ASN_INTEGER, 1, 0x03 }, 3, 3 }, |
| 24 |
{ { ASN_INTEGER, 3, 0x00, 0xff, 0xff, }, 5, 0xffff }, |
| 25 |
{ { ASN_INTEGER, 4, 0x7f, 0xff, 0xff, 0xff }, 6, 0x7fffffff }, |
| 26 |
}; |
| 27 |
for (i = 0; i < sizeof(data)/sizeof(data[0]); ++i) { |
| 28 |
u_char encoded[256]; |
| 29 |
size_t encoded_length; |
| 30 |
u_char *build_result; |
| 31 |
size_t decoded_length; |
| 32 |
u_char decoded_type; |
| 33 |
long decoded_value = 0; |
| 34 |
u_char *parse_result; |
| 35 |
u_char *rbuild_result = NULL; |
| 36 |
size_t rbuild_len = 0; |
| 37 |
size_t offset = 0; |
| 38 |
const long input = data[i].decoded; |
| 39 |
|
| 40 |
encoded_length = sizeof(encoded); |
| 41 |
build_result = asn_build_int(encoded, &encoded_length, ASN_INTEGER, |
| 42 |
&input, sizeof(input)); |
| 43 |
OKF(build_result + encoded_length == encoded + sizeof(encoded), |
| 44 |
("asn_build_int(%ld)", input)); |
| 45 |
OKF(build_result - encoded == data[i].encoded_len, |
| 46 |
("input %ld: encoded len %" NETSNMP_PRIz "d <> %d\n", |
| 47 |
input, build_result - encoded, data[i].encoded_len)); |
| 48 |
OKF(memcmp(encoded, data[i].encoded, data[i].encoded_len) == 0, |
| 49 |
("encoded data for input %ld\n", input)); |
| 50 |
decoded_length = sizeof(encoded) - encoded_length; |
| 51 |
parse_result = asn_parse_int(encoded, &decoded_length, &decoded_type, |
| 52 |
&decoded_value, sizeof(decoded_value)); |
| 53 |
OKF(parse_result == build_result && decoded_type == ASN_INTEGER |
| 54 |
&& decoded_value == input, |
| 55 |
("asn_parse_int(asn_build_int(%ld)) %s; decoded type %d <> %d;" |
| 56 |
" decoded value %ld", |
| 57 |
input, parse_result == build_result ? "succeeded" : "failed", |
| 58 |
decoded_type, ASN_INTEGER, decoded_value)); |
| 59 |
|
| 60 |
OKF(asn_realloc_rbuild_int(&rbuild_result, &rbuild_len, &offset, 1, |
| 61 |
ASN_INTEGER, &input, sizeof(input)), |
| 62 |
("asn_realloc_rbuild_int(%ld)", input)); |
| 63 |
OKF(sizeof(encoded) - encoded_length == offset && |
| 64 |
memcmp(encoded, rbuild_result + rbuild_len - offset, |
| 65 |
offset) == 0, |
| 66 |
("asn_build_int(%ld) != asn_realloc_rbuild_int(%ld)", |
| 67 |
input, input)); |
| 68 |
free(rbuild_result); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
{ |
| 73 |
const unsigned long intval[] = { |
| 74 |
0, 1, 3, 0xffff, 0x7fffffff, 0x80000000U, 0xffffffffU |
| 75 |
}; |
| 76 |
for (i = 0; i < sizeof(intval)/sizeof(intval[0]); ++i) { |
| 77 |
u_char encoded[256]; |
| 78 |
size_t encoded_length; |
| 79 |
u_char *build_result; |
| 80 |
size_t decoded_length; |
| 81 |
u_char decoded_type; |
| 82 |
unsigned long decoded_value = 0; |
| 83 |
u_char *parse_result; |
| 84 |
u_char *rbuild_result = NULL; |
| 85 |
size_t rbuild_len = 0; |
| 86 |
size_t offset = 0; |
| 87 |
|
| 88 |
encoded_length = sizeof(encoded); |
| 89 |
build_result = asn_build_unsigned_int(encoded, &encoded_length, |
| 90 |
ASN_UINTEGER, |
| 91 |
&intval[i], sizeof(intval[i])); |
| 92 |
OKF(build_result + encoded_length == encoded + sizeof(encoded), |
| 93 |
("asn_build_unsigned_int(%lu)", intval[i])); |
| 94 |
decoded_length = sizeof(encoded) - encoded_length; |
| 95 |
parse_result = asn_parse_unsigned_int(encoded, &decoded_length, |
| 96 |
&decoded_type, &decoded_value, |
| 97 |
sizeof(decoded_value)); |
| 98 |
OKF(parse_result && decoded_type == ASN_UINTEGER |
| 99 |
&& decoded_value == intval[i], |
| 100 |
("asn_parse_unsigned_int(asn_build_unsigned_int(%lu)) %s;" |
| 101 |
" decoded type %d <> %d; decoded value %lu", |
| 102 |
intval[i], parse_result == build_result ? "succeeded" : "failed", |
| 103 |
decoded_type, ASN_UINTEGER, decoded_value)); |
| 104 |
|
| 105 |
OKF(asn_realloc_rbuild_unsigned_int(&rbuild_result, &rbuild_len, |
| 106 |
&offset, 1, ASN_UINTEGER, |
| 107 |
&intval[i], sizeof(intval[i])), |
| 108 |
("asn_realloc_rbuild_unsigned_int(%lud)", intval[i])); |
| 109 |
OKF(sizeof(encoded) - encoded_length == offset && |
| 110 |
memcmp(encoded, rbuild_result + rbuild_len - offset, |
| 111 |
offset) == 0, |
| 112 |
("asn_build_unsigned_int(%ld) != " |
| 113 |
"asn_realloc_rbuild_unsigned_int(%ld)", |
| 114 |
intval[i], intval[i])); |
| 115 |
free (rbuild_result); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
{ |
| 120 |
static struct { |
| 121 |
unsigned char encoded[8]; |
| 122 |
unsigned encoded_len; |
| 123 |
unsigned long decoded; |
| 124 |
} data[] = { |
| 125 |
{ { ASN_UINTEGER, 1, 0x80 }, 3, 0x80 }, |
| 126 |
{ { ASN_UINTEGER, 2, 0x80, 0x00 }, 4, 0x8000 }, |
| 127 |
{ { ASN_UINTEGER, 3, 0x80, 0x00, 0x00 }, 5, 0x800000 }, |
| 128 |
{ { ASN_UINTEGER, 4, 0x80, 0x00, 0x00, 0x00 }, 6, 0x80000000 }, |
| 129 |
}; |
| 130 |
for (i = 0; i < sizeof(data)/sizeof(data[0]); i++) { |
| 131 |
size_t decoded_length; |
| 132 |
u_char decoded_type; |
| 133 |
unsigned long decoded_value = 0; |
| 134 |
u_char *parse_result; |
| 135 |
|
| 136 |
decoded_length = data[i].encoded_len; |
| 137 |
parse_result = asn_parse_unsigned_int(data[i].encoded, |
| 138 |
&decoded_length, |
| 139 |
&decoded_type, &decoded_value, |
| 140 |
sizeof(decoded_value)); |
| 141 |
OKF(parse_result && decoded_type == ASN_UINTEGER |
| 142 |
&& decoded_value == data[i].decoded, |
| 143 |
("asn_parse_unsigned_int(%02x %02x %02x %02x %02x %02x %02x %02x, %d) %s;" |
| 144 |
" decoded type %d <> %d; decoded length %d; decoded value %lu", |
| 145 |
data[i].encoded[0], data[i].encoded[1], data[i].encoded[2], |
| 146 |
data[i].encoded[3], data[i].encoded[4], data[i].encoded[5], |
| 147 |
data[i].encoded[6], data[i].encoded[7], data[i].encoded_len, |
| 148 |
parse_result ? "succeeded" : "failed", |
| 149 |
decoded_type, ASN_UINTEGER, (int)decoded_length, decoded_value)); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
| 154 |
|
| 155 |
#define TOINT64(c) ((int64_t)(long)(c).high << 32 | (c).low) |
| 156 |
|
| 157 |
{ |
| 158 |
const struct counter64 intval[] = { |
| 159 |
{ 0x80000000U, 0U }, |
| 160 |
{ 0x80000000U, 0xffffffffU }, |
| 161 |
{ 0xffffffffU, 0 }, |
| 162 |
{ 0xffffffffU, 0xffff0000U }, |
| 163 |
{ 0xffffffffU, 0xfffffffcU }, |
| 164 |
{ 0xffffffffU, 0xffffffffU }, |
| 165 |
{ 0, 0 }, |
| 166 |
{ 0, 1 }, |
| 167 |
{ 0, 3 }, |
| 168 |
{ 0, 0xffff }, |
| 169 |
{ 0, 0x7fffffff }, |
| 170 |
{ 1, 0 }, |
| 171 |
{ 1, 0xffffffff }, |
| 172 |
{ 0x7fffffff, 0 }, |
| 173 |
{ 0x7fffffff, 0xdeadbeef }, |
| 174 |
{ 0x7fffffff, 0xffffffff }, |
| 175 |
}; |
| 176 |
for (i = 0; i < sizeof(intval)/sizeof(intval[0]); ++i) { |
| 177 |
u_char encoded[256]; |
| 178 |
size_t encoded_length; |
| 179 |
u_char *build_result; |
| 180 |
size_t decoded_length; |
| 181 |
u_char decoded_type; |
| 182 |
struct counter64 decoded_value = { 0 }; |
| 183 |
u_char *parse_result; |
| 184 |
u_char *rbuild_result = NULL; |
| 185 |
size_t rbuild_len = 0; |
| 186 |
size_t offset = 0; |
| 187 |
|
| 188 |
encoded_length = sizeof(encoded); |
| 189 |
build_result = asn_build_signed_int64(encoded, &encoded_length, |
| 190 |
ASN_OPAQUE_I64, |
| 191 |
&intval[i], sizeof(intval[i])); |
| 192 |
OKF(build_result + encoded_length == encoded + sizeof(encoded), |
| 193 |
("asn_build_signed_int64(%" PRIi64 ")", TOINT64(intval[i]))); |
| 194 |
decoded_length = sizeof(encoded) - encoded_length; |
| 195 |
parse_result = asn_parse_signed_int64(encoded, &decoded_length, |
| 196 |
&decoded_type, &decoded_value, |
| 197 |
sizeof(decoded_value)); |
| 198 |
OKF(parse_result == build_result && decoded_type == ASN_OPAQUE_I64 |
| 199 |
&& memcmp(&decoded_value, &intval[i], sizeof(decoded_value)) == 0, |
| 200 |
("asn_parse_signed_int64(asn_build_signed_int64(%" PRIi64 ")) %s;" |
| 201 |
" decoded type %d <> %d; decoded value %" PRIi64, |
| 202 |
TOINT64(intval[i]), |
| 203 |
parse_result == build_result ? "succeeded" : "failed", |
| 204 |
decoded_type, ASN_OPAQUE_I64, TOINT64(decoded_value))); |
| 205 |
|
| 206 |
OKF(asn_realloc_rbuild_signed_int64(&rbuild_result, &rbuild_len, |
| 207 |
&offset, 1, ASN_OPAQUE_I64, |
| 208 |
&intval[i], sizeof(intval[i])), |
| 209 |
("asn_realloc_rbuild_signed_int64(%" PRIi64 ")", TOINT64(intval[i]))); |
| 210 |
OKF(sizeof(encoded) - encoded_length == offset && |
| 211 |
memcmp(encoded, rbuild_result + rbuild_len - offset, |
| 212 |
offset) == 0, |
| 213 |
("asn_build_signed_int64(%" PRIi64 ") != " |
| 214 |
"asn_realloc_rbuild_signed_int64(%" PRIi64 ")", |
| 215 |
TOINT64(intval[i]), TOINT64(intval[i]))); |
| 216 |
free (rbuild_result); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
#endif |
| 221 |
|
| 222 |
#define TOUINT64(c) ((uint64_t)(c).high << 32 | (c).low) |
| 223 |
|
| 224 |
{ |
| 225 |
const struct counter64 intval[] = { |
| 226 |
{ 0, 0 }, |
| 227 |
{ 0, 1 }, |
| 228 |
{ 0, 3 }, |
| 229 |
{ 0, 0xffff }, |
| 230 |
{ 0, 0x7fffffff }, |
| 231 |
{ 0, 0x80000000 }, |
| 232 |
{ 0, 0xffffffff }, |
| 233 |
{ 1, 0 }, |
| 234 |
{ 1, 0xffffffff }, |
| 235 |
{ 0x7fffffff, 0 }, |
| 236 |
{ 0x7fffffff, 0xdeadbeef }, |
| 237 |
{ 0x7fffffff, 0xffffffff }, |
| 238 |
{ 0xffffffff, 0 }, |
| 239 |
{ 0xffffffff, 0xdeadbeef }, |
| 240 |
{ 0xffffffff, 0xffffffff }, |
| 241 |
}; |
| 242 |
for (i = 0; i < sizeof(intval)/sizeof(intval[0]); ++i) { |
| 243 |
u_char encoded[256]; |
| 244 |
size_t encoded_length; |
| 245 |
u_char *build_result; |
| 246 |
size_t decoded_length; |
| 247 |
u_char decoded_type; |
| 248 |
struct counter64 decoded_value = { 0 }; |
| 249 |
u_char *parse_result; |
| 250 |
u_char *rbuild_result = NULL; |
| 251 |
size_t rbuild_len = 0; |
| 252 |
size_t offset = 0; |
| 253 |
|
| 254 |
encoded_length = sizeof(encoded); |
| 255 |
build_result = asn_build_unsigned_int64(encoded, &encoded_length, |
| 256 |
ASN_COUNTER64, |
| 257 |
&intval[i], sizeof(intval[i])); |
| 258 |
OKF(build_result + encoded_length == encoded + sizeof(encoded), |
| 259 |
("asn_build_unsigned_int64(%" PRIu64 ")", TOUINT64(intval[i]))); |
| 260 |
decoded_length = sizeof(encoded) - encoded_length; |
| 261 |
parse_result = asn_parse_unsigned_int64(encoded, &decoded_length, |
| 262 |
&decoded_type, &decoded_value, |
| 263 |
sizeof(decoded_value)); |
| 264 |
OKF(parse_result && decoded_type == ASN_COUNTER64 |
| 265 |
&& memcmp(&decoded_value, &intval[i], sizeof(decoded_value)) == 0, |
| 266 |
("asn_parse_unsigned_int64(asn_build_unsigned_int64(%" PRIu64 ")) %s;" |
| 267 |
" decoded type %d <> %d; decoded value %" PRIu64, |
| 268 |
TOUINT64(intval[i]), |
| 269 |
parse_result == build_result ? "succeeded" : "failed", |
| 270 |
decoded_type, ASN_COUNTER64, TOUINT64(decoded_value))); |
| 271 |
|
| 272 |
OKF(asn_realloc_rbuild_unsigned_int64(&rbuild_result, &rbuild_len, |
| 273 |
&offset, 1, ASN_COUNTER64, |
| 274 |
&intval[i], sizeof(intval[i])), |
| 275 |
("asn_realloc_rbuild_unsigned_int64(%" PRIu64 ")", TOUINT64(intval[i]))); |
| 276 |
OKF(sizeof(encoded) - encoded_length == offset && |
| 277 |
memcmp(encoded, rbuild_result + rbuild_len - offset, |
| 278 |
offset) == 0, |
| 279 |
("asn_build_unsigned_int64(%" PRIi64 ") != " |
| 280 |
"asn_realloc_rbuild_unsigned_int64(%" PRIi64 ")", |
| 281 |
TOUINT64(intval[i]), TOUINT64(intval[i]))); |
| 282 |
free (rbuild_result); |
| 283 |
} |
| 284 |
|
| 285 |
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
| 286 |
|
| 287 |
for (i = 0; i < sizeof(intval)/sizeof(intval[0]); ++i) { |
| 288 |
u_char encoded[256]; |
| 289 |
size_t encoded_length; |
| 290 |
u_char *build_result; |
| 291 |
size_t decoded_length; |
| 292 |
u_char decoded_type; |
| 293 |
struct counter64 decoded_value = { 0 }; |
| 294 |
u_char *parse_result; |
| 295 |
u_char *rbuild_result = NULL; |
| 296 |
size_t rbuild_len = 0; |
| 297 |
size_t offset = 0; |
| 298 |
|
| 299 |
encoded_length = sizeof(encoded); |
| 300 |
build_result = asn_build_unsigned_int64(encoded, &encoded_length, |
| 301 |
ASN_OPAQUE_COUNTER64, |
| 302 |
&intval[i], sizeof(intval[i])); |
| 303 |
OKF(build_result + encoded_length == encoded + sizeof(encoded), |
| 304 |
("asn_build_unsigned_int64(%" PRIu64 ")", TOUINT64(intval[i]))); |
| 305 |
decoded_length = sizeof(encoded) - encoded_length; |
| 306 |
parse_result = asn_parse_unsigned_int64(encoded, &decoded_length, |
| 307 |
&decoded_type, &decoded_value, |
| 308 |
sizeof(decoded_value)); |
| 309 |
OKF(parse_result && decoded_type == ASN_OPAQUE_COUNTER64 |
| 310 |
&& memcmp(&decoded_value, &intval[i], sizeof(decoded_value)) == 0, |
| 311 |
("asn_parse_unsigned_int64(asn_build_unsigned_int64(%" PRIu64 ")) %s;" |
| 312 |
" decoded type %d <> %d; decoded value %" PRIu64, |
| 313 |
TOUINT64(intval[i]), |
| 314 |
parse_result == build_result ? "succeeded" : "failed", |
| 315 |
decoded_type, ASN_OPAQUE_COUNTER64, TOUINT64(decoded_value))); |
| 316 |
|
| 317 |
OKF(asn_realloc_rbuild_unsigned_int64(&rbuild_result, &rbuild_len, |
| 318 |
&offset, 1, ASN_OPAQUE_COUNTER64, |
| 319 |
&intval[i], sizeof(intval[i])), |
| 320 |
("asn_realloc_rbuild_unsigned_int64(%" PRIu64 ")", TOUINT64(intval[i]))); |
| 321 |
OKF(sizeof(encoded) - encoded_length == offset && |
| 322 |
memcmp(encoded, rbuild_result + rbuild_len - offset, |
| 323 |
offset) == 0, |
| 324 |
("asn_build_unsigned_int64(%" PRIi64 ") != " |
| 325 |
"asn_realloc_rbuild_unsigned_int64(%" PRIi64 ")", |
| 326 |
TOUINT64(intval[i]), TOUINT64(intval[i]))); |
| 327 |
free (rbuild_result); |
| 328 |
} |
| 329 |
|
| 330 |
for (i = 0; i < sizeof(intval)/sizeof(intval[0]); ++i) { |
| 331 |
u_char encoded[256]; |
| 332 |
size_t encoded_length; |
| 333 |
u_char *build_result; |
| 334 |
size_t decoded_length; |
| 335 |
u_char decoded_type; |
| 336 |
struct counter64 decoded_value = { 0 }; |
| 337 |
u_char *parse_result; |
| 338 |
u_char *rbuild_result = NULL; |
| 339 |
size_t rbuild_len = 0; |
| 340 |
size_t offset = 0; |
| 341 |
|
| 342 |
encoded_length = sizeof(encoded); |
| 343 |
build_result = asn_build_unsigned_int64(encoded, &encoded_length, |
| 344 |
ASN_OPAQUE_U64, |
| 345 |
&intval[i], sizeof(intval[i])); |
| 346 |
OKF(build_result + encoded_length == encoded + sizeof(encoded), |
| 347 |
("asn_build_unsigned_int64(%" PRIu64 ")", TOUINT64(intval[i]))); |
| 348 |
decoded_length = sizeof(encoded) - encoded_length; |
| 349 |
parse_result = asn_parse_unsigned_int64(encoded, &decoded_length, |
| 350 |
&decoded_type, &decoded_value, |
| 351 |
sizeof(decoded_value)); |
| 352 |
OKF(parse_result && decoded_type == ASN_OPAQUE_U64 |
| 353 |
&& memcmp(&decoded_value, &intval[i], sizeof(decoded_value)) == 0, |
| 354 |
("asn_parse_unsigned_int64(asn_build_unsigned_int64(%" PRIu64 ")) %s;" |
| 355 |
" decoded type %d <> %d; decoded value %" PRIu64, |
| 356 |
TOUINT64(intval[i]), |
| 357 |
parse_result == build_result ? "succeeded" : "failed", |
| 358 |
decoded_type, ASN_OPAQUE_U64, TOUINT64(decoded_value))); |
| 359 |
|
| 360 |
OKF(asn_realloc_rbuild_unsigned_int64(&rbuild_result, &rbuild_len, |
| 361 |
&offset, 1, ASN_OPAQUE_U64, |
| 362 |
&intval[i], sizeof(intval[i])), |
| 363 |
("asn_realloc_rbuild_unsigned_int64(%" PRIu64 ")", TOUINT64(intval[i]))); |
| 364 |
OKF(sizeof(encoded) - encoded_length == offset && |
| 365 |
memcmp(encoded, rbuild_result + rbuild_len - offset, |
| 366 |
offset) == 0, |
| 367 |
("asn_build_unsigned_int64(%" PRIi64 ") != " |
| 368 |
"asn_realloc_rbuild_unsigned_int64(%" PRIi64 ")", |
| 369 |
TOUINT64(intval[i]), TOUINT64(intval[i]))); |
| 370 |
free (rbuild_result); |
| 371 |
} |
| 372 |
|
| 373 |
#endif |
| 374 |
} |