2 * Unix SMB/CIFS implementation.
4 * Copyright (C) 2018-2019 Andreas Schneider <asn@samba.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "lib/replace/replace.h"
27 #include "lib/util/bytearray.h"
28 #include "lib/util/byteorder.h"
30 static void torture_le_u8(void **state
)
32 uint8_t data
[2] = {0};
37 /* Test CVAL and SCVAL */
38 PUSH_LE_U8(data
, 0, 23);
39 PUSH_LE_U8(data
, 1, 42);
41 result
= CVAL(data
, 0);
42 assert_int_equal(result
, 23);
44 result
= CVAL(data
, 1);
45 assert_int_equal(result
, 42);
47 /* Test CVAL_NC and PVAL */
48 PUSH_LE_U8(data
, 0, 23);
49 PUSH_LE_U8(data
, 1, 42);
51 result
= CVAL_NC(data
, 0);
52 assert_int_equal(result
, 23);
54 result
= PVAL(data
, 1);
55 assert_int_equal(result
, 42);
61 result
= PULL_LE_U8(data
, 0);
62 assert_int_equal(result
, 42);
64 result
= PULL_LE_U8(data
, 1);
65 assert_int_equal(result
, 23);
68 static void torture_le_u16(void **state
)
70 uint8_t data
[2] = {0};
76 PUSH_LE_U16(data
, 0, 0xff00);
77 result
= SVAL(data
, 0);
78 assert_int_equal(result
, 0xff00);
81 SSVAL(data
, 0, 0x00ff);
82 result
= PULL_LE_U16(data
, 0);
83 assert_int_equal(result
, 0x00ff);
86 SSVALX(data
, 0, 0x00fa);
87 result
= PULL_LE_U16(data
, 0);
88 assert_int_equal(result
, 0x00fa);
91 SSVALS(data
, 0, 0x00fb);
92 result
= PULL_LE_U16(data
, 0);
93 assert_int_equal(result
, 0x00fb);
96 static void torture_le_u32(void **state
)
98 uint8_t data
[4] = {0};
104 PUSH_LE_U32(data
, 0, 0xff000000);
105 result
= IVAL(data
, 0);
106 assert_int_equal(result
, 0xff000000);
109 SIVAL(data
, 0, 0xffaabbcc);
110 result
= PULL_LE_U32(data
, 0);
111 assert_int_equal(result
, 0xffaabbcc);
114 SIVALX(data
, 0, 0xffbbccdd);
115 result
= PULL_LE_U32(data
, 0);
116 assert_int_equal(result
, 0xffbbccdd);
119 SIVALS(data
, 0, 0xffccddee);
120 result
= PULL_LE_U32(data
, 0);
121 assert_int_equal(result
, 0xffccddee);
124 static void torture_le_u64(void **state
)
126 uint8_t data
[8] = {0};
131 PUSH_LE_U64(data
, 0, 0xfffefffefffefffeUL
);
132 result
= BVAL(data
, 0);
133 assert_int_equal(result
, 0xfffefffefffefffeUL
);
135 SBVAL(data
, 0, 0xfffafffafffafffaUL
);
136 result
= PULL_LE_U64(data
, 0);
137 assert_int_equal(result
, 0xfffafffafffafffaUL
);
140 static void torture_be_u8(void **state
)
142 uint8_t data
[2] = {0};
147 PUSH_BE_U8(data
, 0, 23);
148 PUSH_BE_U8(data
, 1, 42);
150 result
= CVAL(data
, 0);
151 assert_int_equal(result
, 23);
153 result
= CVAL(data
, 1);
154 assert_int_equal(result
, 42);
159 result
= PULL_BE_U8(data
, 0);
160 assert_int_equal(result
, 42);
162 result
= PULL_BE_U8(data
, 1);
163 assert_int_equal(result
, 23);
166 static void torture_be_u16(void **state
)
168 uint8_t data
[2] = {0};
174 PUSH_BE_U16(data
, 0, 0xff00);
175 result
= RSVAL(data
, 0);
176 assert_int_equal(result
, 0xff00);
179 PUSH_BE_U16(data
, 0, 0xffaa);
180 result
= RSVALS(data
, 0);
181 assert_int_equal(result
, 0xffaa);
184 RSSVAL(data
, 0, 0x00ff);
185 result
= PULL_BE_U16(data
, 0);
186 assert_int_equal(result
, 0x00ff);
189 RSSVALS(data
, 0, 0x00fa);
190 result
= PULL_BE_U16(data
, 0);
191 assert_int_equal(result
, 0x00fa);
194 static void torture_be_u32(void **state
)
196 uint8_t data
[4] = {0};
202 PUSH_BE_U32(data
, 0, 0xff000000);
203 result
= RIVAL(data
, 0);
204 assert_int_equal(result
, 0xff000000);
207 PUSH_BE_U32(data
, 0, 0xff0000aa);
208 result
= RIVALS(data
, 0);
209 assert_int_equal(result
, 0xff0000aa);
212 RSIVAL(data
, 0, 0xffeeddcc);
213 result
= PULL_BE_U32(data
, 0);
214 assert_int_equal(result
, 0xffeeddcc);
217 RSIVALS(data
, 0, 0xffaaddcc);
218 result
= PULL_BE_U32(data
, 0);
219 assert_int_equal(result
, 0xffaaddcc);
222 static void torture_be_u64(void **state
)
224 uint8_t data
[8] = {0};
230 PUSH_BE_U64(data
, 0, 0xfffefffefffefffeUL
);
231 result
= RBVAL(data
, 0);
232 assert_int_equal(result
, 0xfffefffefffefffeUL
);
235 PUSH_BE_U64(data
, 0, 0xfffafffafffafffaUL
);
236 result
= RBVALS(data
, 0);
237 assert_int_equal(result
, 0xfffafffafffafffaUL
);
240 RSBVAL(data
, 0, 0xfffbfffbfffbfffbUL
);
241 result
= PULL_BE_U64(data
, 0);
242 assert_int_equal(result
, 0xfffbfffbfffbfffbUL
);
245 RSBVALS(data
, 0, 0xfffcfffcfffcfffcUL
);
246 result
= PULL_BE_U64(data
, 0);
247 assert_int_equal(result
, 0xfffcfffcfffcfffcUL
);
250 int main(int argc
, char *argv
[])
253 const struct CMUnitTest tests
[] = {
254 cmocka_unit_test(torture_le_u8
),
255 cmocka_unit_test(torture_le_u16
),
256 cmocka_unit_test(torture_le_u32
),
257 cmocka_unit_test(torture_le_u64
),
259 cmocka_unit_test(torture_be_u8
),
260 cmocka_unit_test(torture_be_u16
),
261 cmocka_unit_test(torture_be_u32
),
262 cmocka_unit_test(torture_be_u64
),
266 cmocka_set_test_filter(argv
[1]);
268 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
270 rc
= cmocka_run_group_tests(tests
, NULL
, NULL
);