blob: 083493d1b7f96ed96d13c7f2ee5db38b43b69905 [file] [log] [blame]
Lev Walkin4ef01a22017-09-06 22:54:39 -07001#include <stdio.h>
2#include <assert.h>
3#include <sys/time.h>
4
5#include <UniversalString.h>
6#include <per_support.h>
7
8static void
9check_encode_failed(asn_TYPE_descriptor_t *td, const char *buf, size_t buflen) {
10 uint8_t uper_output_buffer[32];
11 UniversalString_t *st_in;
Lev Walkinb7a202e2017-09-06 23:19:48 -070012 char error_buf[128];
13 size_t error_buf_len = sizeof(error_buf);
Lev Walkin4ef01a22017-09-06 22:54:39 -070014
15 st_in = OCTET_STRING_new_fromBuf(td, buf, buflen);
16 assert(st_in);
17 assert(st_in->size == buflen);
Lev Walkinb7a202e2017-09-06 23:19:48 -070018
19 /* First signal that something is wrong with the length */
20 int st_in_ct = asn_check_constraints(td, st_in, error_buf, &error_buf_len);
21 assert(st_in_ct != 0);
22 fprintf(stderr, "%s\n", error_buf);
23
24 /* Second signal that something is wrong with the length */
Lev Walkin56153042017-10-24 00:47:03 -070025 asn_enc_rval_t enc = uper_encode_to_buffer(td, 0, st_in, uper_output_buffer,
Lev Walkinb7a202e2017-09-06 23:19:48 -070026 sizeof(uper_output_buffer));
Lev Walkin4ef01a22017-09-06 22:54:39 -070027 assert(enc.encoded == -1);
28
29 ASN_STRUCT_FREE(*td, st_in);
30}
31
32static void
33check_round_trip_OK(asn_TYPE_descriptor_t *td, const char *buf, size_t buflen) {
34 uint8_t uper_output_buffer[32];
35 UniversalString_t *st_in;
36 UniversalString_t *st_out = 0;
37
38 st_in = OCTET_STRING_new_fromBuf(td, buf, buflen);
39 assert(st_in);
40 assert(st_in->size == buflen);
41
Lev Walkinb7a202e2017-09-06 23:19:48 -070042 int st_in_ct = asn_check_constraints(td, st_in, NULL, NULL);
43 assert(st_in_ct == 0);
Lev Walkin4ef01a22017-09-06 22:54:39 -070044 asn_enc_rval_t enc =
Lev Walkin56153042017-10-24 00:47:03 -070045 uper_encode_to_buffer(td, 0, st_in,
Lev Walkin4ef01a22017-09-06 22:54:39 -070046 uper_output_buffer, sizeof(uper_output_buffer));
47 assert(enc.encoded > 0);
48
49 asn_dec_rval_t dec =
50 uper_decode(0, &asn_DEF_UniversalString, (void **)&st_out,
51 uper_output_buffer, (enc.encoded + 7) / 8, 0, 0);
Lev Walkinb7a202e2017-09-06 23:19:48 -070052 int st_out_ct = asn_check_constraints(td, st_out, NULL, NULL);
53 assert(st_out_ct == 0);
Vasil Velichkov72b10442017-10-19 04:38:38 +030054 assert(dec.consumed == (size_t)enc.encoded);
Lev Walkin4ef01a22017-09-06 22:54:39 -070055 assert(st_in->size == st_out->size);
56 assert(memcmp(st_in->buf, st_out->buf, st_in->size) == 0);
57 assert(st_out->size == buflen);
58 assert(memcmp(st_out->buf, buf, buflen) == 0);
59
60 ASN_STRUCT_FREE(*td, st_in);
61 ASN_STRUCT_FREE(*td, st_out);
62}
63
64int
65main() {
66 static char UniversalString_data[] = { 0, 0, 0, 65, 0, 0, 0, 65 };
67
68 check_round_trip_OK(&asn_DEF_UniversalString, UniversalString_data, 0);
69 check_encode_failed(&asn_DEF_UniversalString, UniversalString_data, 1);
70 check_encode_failed(&asn_DEF_UniversalString, UniversalString_data, 2);
71 check_encode_failed(&asn_DEF_UniversalString, UniversalString_data, 3);
72 check_round_trip_OK(&asn_DEF_UniversalString, UniversalString_data, 4);
73 check_encode_failed(&asn_DEF_UniversalString, UniversalString_data, 5);
74 check_encode_failed(&asn_DEF_UniversalString, UniversalString_data, 6);
75 check_encode_failed(&asn_DEF_UniversalString, UniversalString_data, 7);
76 check_round_trip_OK(&asn_DEF_UniversalString, UniversalString_data, 8);
77
78 return 0;
79}
80