083493d1b7f96ed96d13c7f2ee5db38b43b69905
[com/asn1c.git] / tests / tests-skeletons / check-PER-UniversalString.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <sys/time.h>
4
5 #include <UniversalString.h>
6 #include <per_support.h>
7
8 static void
9 check_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;
12     char error_buf[128];
13     size_t error_buf_len = sizeof(error_buf);
14
15     st_in = OCTET_STRING_new_fromBuf(td, buf, buflen);
16     assert(st_in);
17     assert(st_in->size == buflen);
18
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 */
25     asn_enc_rval_t enc = uper_encode_to_buffer(td, 0, st_in, uper_output_buffer,
26                                                sizeof(uper_output_buffer));
27     assert(enc.encoded == -1);
28
29     ASN_STRUCT_FREE(*td, st_in);
30 }
31
32 static void
33 check_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
42     int st_in_ct = asn_check_constraints(td, st_in, NULL, NULL);
43     assert(st_in_ct == 0);
44     asn_enc_rval_t enc =
45         uper_encode_to_buffer(td, 0, st_in,
46                               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);
52     int st_out_ct = asn_check_constraints(td, st_out, NULL, NULL);
53     assert(st_out_ct == 0);
54     assert(dec.consumed == (size_t)enc.encoded);
55     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
64 int
65 main() {
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