Skip to content

Commit 58b74eb

Browse files
committed
add signed integer tests for test 134
1 parent 6c52784 commit 58b74eb

File tree

4 files changed

+162
-31
lines changed

4 files changed

+162
-31
lines changed

asn1c/tests/check-134.-gen-PER.c

+63-23
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,33 @@ static void
3535
verify(int testNo, T_t *ti) {
3636
asn_enc_rval_t er;
3737
asn_dec_rval_t rv;
38-
unsigned char buf[16];
38+
unsigned char buf[20];
3939
T_t *to = 0;
4040

41-
fprintf(stderr, "%d IN: { %lu, %lu }\n", testNo,
42-
i2ul(&ti->unsigned33), i2ul(&ti->unsigned42));
41+
fprintf(stderr, "%d IN: { %lu, %lu, %ld, %ld }\n", testNo,
42+
i2ul(&ti->unsigned33), i2ul(&ti->unsigned42),
43+
i2ul(&ti->signed33), ti->signed33ext
44+
);
4345

4446
er = uper_encode_to_buffer(&asn_DEF_T, ti, buf, sizeof buf);
45-
assert(er.encoded == 33 + 42);
47+
assert(er.encoded >= 33 + 42 + 33 + 1 + 33);
4648

4749
rv = uper_decode(0, &asn_DEF_T, (void *)&to, buf, sizeof buf, 0, 0);
4850
assert(rv.code == RC_OK);
4951

5052
fprintf(stderr, "%d ENC: %2x%2x%2x%2x %2x%2x%2x%2x\n", testNo,
5153
buf[0], buf[1], buf[2], buf[3],
5254
buf[4], buf[5], buf[6], buf[7]);
53-
fprintf(stderr, "%d OUT: { %lu, %lu } vs { %lu, %lu }\n",
55+
fprintf(stderr, "%d OUT: { %lu, %lu, %ld, %ld } vs { %lu, %lu, %ld, %ld }\n",
5456
testNo,
5557
i2ul(&ti->unsigned33), i2ul(&ti->unsigned42),
56-
i2ul(&to->unsigned33), i2ul(&to->unsigned42));
58+
i2ul(&ti->signed33), ti->signed33ext,
59+
i2ul(&to->unsigned33), i2ul(&to->unsigned42),
60+
i2ul(&to->signed33), to->signed33ext);
5761
assert(i2ul(&ti->unsigned33) == i2ul(&to->unsigned33));
5862
assert(i2ul(&ti->unsigned42) == i2ul(&to->unsigned42));
63+
assert(i2ul(&ti->signed33) == i2ul(&to->signed33));
64+
assert(ti->signed33ext == to->signed33ext);
5965

6066
xer_fprint(stderr, &asn_DEF_T, ti);
6167
xer_fprint(stderr, &asn_DEF_T, to);
@@ -77,37 +83,71 @@ int main() {
7783
T_t ti;
7884

7985
memset(&ti, 0, sizeof(ti));
80-
ul2i(&ti.unsigned33, 0);
81-
ul2i(&ti.unsigned42, 0);
86+
ul2i(&ti.unsigned33, 0);
87+
ul2i(&ti.unsigned42, 0);
88+
ul2i(&ti.signed33, 0);
89+
ti.signed33ext = 0;
8290
verify(1, &ti);
8391

84-
ul2i(&ti.unsigned33, 1);
85-
ul2i(&ti.unsigned42, 1);
92+
ul2i(&ti.unsigned33, 1);
93+
ul2i(&ti.unsigned42, 1);
94+
ul2i(&ti.signed33, 1);
95+
ti.signed33ext = 1;
8696
verify(2, &ti);
8797

88-
ul2i(&ti.unsigned33, 5000000000);
89-
ul2i(&ti.unsigned42, 3153600000000);
98+
ul2i(&ti.unsigned33, 5000000000);
99+
ul2i(&ti.unsigned42, 3153600000000);
100+
ul2i(&ti.signed33, 4000000000);
101+
ti.signed33ext = 4000000000;
90102
verify(3, &ti);
91103

92104
ul2i(&ti.unsigned33, -1);
93-
ul2i(&ti.unsigned42, 0);
105+
ul2i(&ti.unsigned42, 0);
106+
ul2i(&ti.signed33, 0);
107+
ti.signed33ext = 0;
94108
NO_encode(4, &ti);
95109

96-
ul2i(&ti.unsigned33, 0);
110+
ul2i(&ti.unsigned33, 0);
97111
ul2i(&ti.unsigned42, -1);
112+
ul2i(&ti.signed33, 0);
113+
ti.signed33ext = 0;
98114
NO_encode(5, &ti);
99115

100-
ul2i(&ti.unsigned33, 5000000000 + 1);
101-
ul2i(&ti.unsigned42, 0);
116+
ul2i(&ti.unsigned33, 0);
117+
ul2i(&ti.unsigned42, 0);
118+
ul2i(&ti.signed33, -4000000000-1);
119+
ti.signed33ext = 0;
102120
NO_encode(6, &ti);
103121

104-
ul2i(&ti.unsigned33, 0);
105-
ul2i(&ti.unsigned42, 3153600000000 + 1);
106-
NO_encode(7, &ti);
107-
108-
ul2i(&ti.unsigned33, 5000000000 - 1);
109-
ul2i(&ti.unsigned42, 3153600000000 - 1);
110-
verify(8, &ti);
122+
ul2i(&ti.unsigned33, 0);
123+
ul2i(&ti.unsigned42, 0);
124+
ul2i(&ti.signed33, 0);
125+
ti.signed33ext = -4000000000-1;
126+
verify(7, &ti); /* signed33ext is extensible */
127+
128+
ul2i(&ti.unsigned33, 5000000000 + 1);
129+
ul2i(&ti.unsigned42, 0);
130+
ul2i(&ti.signed33, 0);
131+
ti.signed33ext = 0;
132+
NO_encode(8, &ti);
133+
134+
ul2i(&ti.unsigned33, 0);
135+
ul2i(&ti.unsigned42, 3153600000000 + 1);
136+
ul2i(&ti.signed33, 0);
137+
ti.signed33ext = 0;
138+
NO_encode(9, &ti);
139+
140+
ul2i(&ti.unsigned33, 5000000000 - 1);
141+
ul2i(&ti.unsigned42, 3153600000000 - 1);
142+
ul2i(&ti.signed33, 4000000000 - 1);
143+
ti.signed33ext = 4000000000 - 1;
144+
verify(10, &ti);
145+
146+
ul2i(&ti.unsigned33, 0);
147+
ul2i(&ti.unsigned42, 0);
148+
ul2i(&ti.signed33, 0);
149+
ti.signed33ext = 4000000000 + 1;
150+
verify(11, &ti);
111151

112152
return 0;
113153
}

skeletons/INTEGER.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,10 @@ INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
733733
/* X.691-11/2008, #13.2.2, test if constrained whole number */
734734
if(ct && ct->range_bits >= 0) {
735735
/* #11.5.6 -> #11.3 */
736-
ASN_DEBUG("Encoding integer with range %d bits",
737-
ct->range_bits);
738-
long v = value - ct->lower_bound;
739-
if(uper_put_constrained_whole_number_s(po, v, ct->range_bits))
736+
ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
737+
value, value - ct->lower_bound, ct->range_bits);
738+
unsigned long v = value - ct->lower_bound;
739+
if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
740740
_ASN_ENCODE_FAILED;
741741
_ASN_ENCODED_OK(er);
742742
}

tests/134-per-long-OK.asn1

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ BEGIN
1515
-- Supported only on 64-bit platforms.
1616
T ::= SEQUENCE {
1717
unsigned33 INTEGER (0..5000000000), -- range 33 bits
18-
unsigned42 INTEGER (0..3153600000000) -- range 42 bits
18+
unsigned42 INTEGER (0..3153600000000), -- range 42 bits
19+
signed33 INTEGER (-4000000000..4000000000), -- 33 bits
20+
signed33ext INTEGER (-4000000000..4000000000,...)
1921
}
2022

2123
END

tests/134-per-long-OK.asn1.-Pgen-PER

+92-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
/*** <<< INCLUDES [T] >>> ***/
33

44
#include <INTEGER.h>
5+
#include <NativeInteger.h>
56
#include <constr_SEQUENCE.h>
67

78
/*** <<< TYPE-DECLS [T] >>> ***/
89

910
typedef struct T {
1011
INTEGER_t unsigned33;
1112
INTEGER_t unsigned42;
13+
INTEGER_t signed33;
14+
long signed33ext;
1215

1316
/* Context for parsing across buffer boundaries */
1417
asn_struct_ctx_t _asn_ctx;
@@ -82,6 +85,62 @@ memb_unsigned42_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
8285
}
8386
}
8487

88+
static int
89+
memb_signed33_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
90+
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
91+
const INTEGER_t *st = (const INTEGER_t *)sptr;
92+
long value;
93+
94+
if(!sptr) {
95+
_ASN_CTFAIL(app_key, td, sptr,
96+
"%s: value not given (%s:%d)",
97+
td->name, __FILE__, __LINE__);
98+
return -1;
99+
}
100+
101+
if(asn_INTEGER2long(st, &value)) {
102+
_ASN_CTFAIL(app_key, td, sptr,
103+
"%s: value too large (%s:%d)",
104+
td->name, __FILE__, __LINE__);
105+
return -1;
106+
}
107+
108+
if((value >= -4000000000 && value <= 4000000000)) {
109+
/* Constraint check succeeded */
110+
return 0;
111+
} else {
112+
_ASN_CTFAIL(app_key, td, sptr,
113+
"%s: constraint failed (%s:%d)",
114+
td->name, __FILE__, __LINE__);
115+
return -1;
116+
}
117+
}
118+
119+
static int
120+
memb_signed33ext_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
121+
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
122+
long value;
123+
124+
if(!sptr) {
125+
_ASN_CTFAIL(app_key, td, sptr,
126+
"%s: value not given (%s:%d)",
127+
td->name, __FILE__, __LINE__);
128+
return -1;
129+
}
130+
131+
value = *(const long *)sptr;
132+
133+
if((value >= -4000000000 && value <= 4000000000)) {
134+
/* Constraint check succeeded */
135+
return 0;
136+
} else {
137+
_ASN_CTFAIL(app_key, td, sptr,
138+
"%s: constraint failed (%s:%d)",
139+
td->name, __FILE__, __LINE__);
140+
return -1;
141+
}
142+
}
143+
85144

86145
/*** <<< CTDEFS [T] >>> ***/
87146

@@ -95,6 +154,16 @@ static asn_per_constraints_t asn_PER_memb_unsigned42_constr_3 GCC_NOTUSED = {
95154
{ APC_UNCONSTRAINED, -1, -1, 0, 0 },
96155
0, 0 /* No PER value map */
97156
};
157+
static asn_per_constraints_t asn_PER_memb_signed33_constr_4 GCC_NOTUSED = {
158+
{ APC_CONSTRAINED, 33, -1, -4000000000, 4000000000 } /* (-4000000000..4000000000) */,
159+
{ APC_UNCONSTRAINED, -1, -1, 0, 0 },
160+
0, 0 /* No PER value map */
161+
};
162+
static asn_per_constraints_t asn_PER_memb_signed33ext_constr_5 GCC_NOTUSED = {
163+
{ APC_CONSTRAINED | APC_EXTENSIBLE, 33, -1, -4000000000, 4000000000 } /* (-4000000000..4000000000,...) */,
164+
{ APC_UNCONSTRAINED, -1, -1, 0, 0 },
165+
0, 0 /* No PER value map */
166+
};
98167

99168
/*** <<< STAT-DEFS [T] >>> ***/
100169

@@ -117,19 +186,39 @@ static asn_TYPE_member_t asn_MBR_T_1[] = {
117186
.default_value = 0,
118187
.name = "unsigned42"
119188
},
189+
{ ATF_NOFLAGS, 0, offsetof(struct T, signed33),
190+
.tag = (ASN_TAG_CLASS_CONTEXT | (2 << 2)),
191+
.tag_mode = -1, /* IMPLICIT tag at current level */
192+
.type = &asn_DEF_INTEGER,
193+
.memb_constraints = memb_signed33_constraint_1,
194+
.per_constraints = &asn_PER_memb_signed33_constr_4,
195+
.default_value = 0,
196+
.name = "signed33"
197+
},
198+
{ ATF_NOFLAGS, 0, offsetof(struct T, signed33ext),
199+
.tag = (ASN_TAG_CLASS_CONTEXT | (3 << 2)),
200+
.tag_mode = -1, /* IMPLICIT tag at current level */
201+
.type = &asn_DEF_NativeInteger,
202+
.memb_constraints = memb_signed33ext_constraint_1,
203+
.per_constraints = &asn_PER_memb_signed33ext_constr_5,
204+
.default_value = 0,
205+
.name = "signed33ext"
206+
},
120207
};
121208
static ber_tlv_tag_t asn_DEF_T_tags_1[] = {
122209
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
123210
};
124211
static asn_TYPE_tag2member_t asn_MAP_T_tag2el_1[] = {
125212
{ (ASN_TAG_CLASS_CONTEXT | (0 << 2)), 0, 0, 0 }, /* unsigned33 */
126-
{ (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 } /* unsigned42 */
213+
{ (ASN_TAG_CLASS_CONTEXT | (1 << 2)), 1, 0, 0 }, /* unsigned42 */
214+
{ (ASN_TAG_CLASS_CONTEXT | (2 << 2)), 2, 0, 0 }, /* signed33 */
215+
{ (ASN_TAG_CLASS_CONTEXT | (3 << 2)), 3, 0, 0 } /* signed33ext */
127216
};
128217
static asn_SEQUENCE_specifics_t asn_SPC_T_specs_1 = {
129218
sizeof(struct T),
130219
offsetof(struct T, _asn_ctx),
131220
asn_MAP_T_tag2el_1,
132-
2, /* Count of tags in the map */
221+
4, /* Count of tags in the map */
133222
0, 0, 0, /* Optional elements (not needed) */
134223
-1, /* Start extensions */
135224
-1 /* Stop extensions */
@@ -155,7 +244,7 @@ asn_TYPE_descriptor_t asn_DEF_T = {
155244
/sizeof(asn_DEF_T_tags_1[0]), /* 1 */
156245
0, /* No PER visible constraints */
157246
asn_MBR_T_1,
158-
2, /* Elements count */
247+
4, /* Elements count */
159248
&asn_SPC_T_specs_1 /* Additional specs */
160249
};
161250

0 commit comments

Comments
 (0)