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/byteorder.h"
29 static void torture_pull_le_u8(void **state
)
31 uint8_t data
[2] = {0};
36 result
= CVAL(data
, 0);
37 assert_int_equal(result
, 0);
40 result
= CVAL(data
, 0);
41 assert_int_equal(result
, 42);
45 result
= CVAL(data
, 0);
46 assert_int_equal(result
, 0xf);
49 result
= CVAL(data
, 0);
50 assert_int_equal(result
, 0xff);
53 result
= CVAL(data
, 1);
54 assert_int_equal(result
, 42);
57 static void torture_pull_le_u16(void **state
)
59 uint8_t data
[2] = {0, 0};
64 result
= SVAL(data
, 0);
65 assert_int_equal(result
, 0);
69 result
= SVAL(data
, 0);
70 assert_int_equal(result
, 42);
74 result
= SVAL(data
, 0);
75 assert_int_equal(result
, 0x00ff);
79 result
= SVAL(data
, 0);
80 assert_int_equal(result
, 0xff00);
84 result
= SVAL(data
, 0);
85 assert_int_equal(result
, 0xffff);
88 static void torture_pull_le_u32(void **state
)
90 uint8_t data
[4] = {0, 0, 0, 0};
95 result
= IVAL(data
, 0);
96 assert_int_equal(result
, 0);
102 result
= IVAL(data
, 0);
103 assert_int_equal(result
, 42);
109 result
= IVAL(data
, 0);
110 assert_int_equal(result
, 0x00ff);
116 result
= IVAL(data
, 0);
117 assert_int_equal(result
, 0xff00);
123 result
= IVAL(data
, 0);
124 assert_int_equal(result
, 0xff0000);
130 result
= IVAL(data
, 0);
131 assert_int_equal(result
, 0xff000000);
137 result
= IVAL(data
, 0);
138 assert_int_equal(result
, 0xffffffff);
141 static void torture_push_le_u8(void **state
)
143 uint8_t data
[4] = {0, 0, 0, 0};
144 uint8_t data2
[4] = {42, 42, 42, 42};
152 assert_memory_equal(data
, data2
, sizeof(data
));
155 static void torture_push_le_u16(void **state
)
157 uint8_t data
[4] = {0, 0, 0, 0};
158 uint8_t data2
[4] = {0xa6, 0x7f, 0x2a, 0x00};
163 SSVALX(data
, 0, 32678);
165 assert_memory_equal(data
, data2
, sizeof(data
));
167 result
= SVAL(data
, 2);
168 assert_int_equal(result
, 42);
170 result
= SVAL(data
, 0);
171 assert_int_equal(result
, 32678);
174 static void torture_push_le_u32(void **state
)
176 uint8_t data
[8] = {0};
177 uint8_t data2
[8] = {0xa6, 0x7f, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00};
182 SIVALX(data
, 0, 32678);
184 assert_memory_equal(data
, data2
, sizeof(data
));
186 result
= IVAL(data
, 4);
187 assert_int_equal(result
, 42);
189 result
= IVAL(data
, 0);
190 assert_int_equal(result
, 32678);
192 SIVALX(data
, 0, 0xfffefffe);
193 result
= IVAL(data
, 0);
194 assert_int_equal(result
, 0xfffefffe);
197 static void torture_push_le_u64(void **state
)
199 uint8_t data
[16] = {0};
204 SBVAL(data
, 0, 32678);
206 result
= BVAL(data
, 0);
207 assert_int_equal(result
, 32678);
209 SBVAL(data
, 0, 0xfffefffefffefffeUL
);
211 result
= BVAL(data
, 0);
212 assert_int_equal(result
, 0xfffefffefffefffeUL
);
215 /****************** BIG ENDIAN ********************/
217 static void torture_pull_be_u8(void **state
)
219 uint8_t data
[2] = {0};
224 result
= CVAL(data
, 0);
225 assert_int_equal(result
, 0);
228 result
= CVAL(data
, 0);
229 assert_int_equal(result
, 42);
233 result
= CVAL(data
, 0);
234 assert_int_equal(result
, 0xf);
237 result
= CVAL(data
, 0);
238 assert_int_equal(result
, 0xff);
241 result
= CVAL(data
, 1);
242 assert_int_equal(result
, 42);
245 static void torture_pull_be_u16(void **state
)
247 uint8_t data
[2] = {0, 0};
252 result
= RSVAL(data
, 0);
253 assert_int_equal(result
, 0);
257 result
= RSVAL(data
, 0);
258 assert_int_equal(result
, 42);
262 result
= RSVAL(data
, 0);
263 assert_int_equal(result
, 0x00ff);
267 result
= RSVAL(data
, 0);
268 assert_int_equal(result
, 0xff00);
272 result
= RSVAL(data
, 0);
273 assert_int_equal(result
, 0xffff);
276 static void torture_pull_be_u32(void **state
)
278 uint8_t data
[4] = {0, 0, 0, 0};
283 result
= RIVAL(data
, 0);
284 assert_int_equal(result
, 0);
290 result
= RIVAL(data
, 0);
291 assert_int_equal(result
, 42);
297 result
= RIVAL(data
, 0);
298 assert_int_equal(result
, 0x00ff);
304 result
= RIVAL(data
, 0);
305 assert_int_equal(result
, 0xff00);
311 result
= RIVAL(data
, 0);
312 assert_int_equal(result
, 0xff0000);
318 result
= RIVAL(data
, 0);
319 assert_int_equal(result
, 0xff000000);
325 result
= RIVAL(data
, 0);
326 assert_int_equal(result
, 0xffffffff);
329 static void torture_push_be_u8(void **state
)
331 uint8_t data
[4] = {0, 0, 0, 0};
332 uint8_t data2
[4] = {42, 42, 42, 42};
340 assert_memory_equal(data
, data2
, sizeof(data
));
343 static void torture_push_be_u16(void **state
)
345 uint8_t data
[4] = {0, 0, 0, 0};
346 uint8_t data2
[4] = {0x7f, 0xa6, 0x00, 0x2a};
351 RSSVALS(data
, 0, 32678);
352 RSSVALS(data
, 2, 42);
353 assert_memory_equal(data
, data2
, sizeof(data
));
355 result
= RSVAL(data
, 2);
356 assert_int_equal(result
, 42);
358 result
= RSVAL(data
, 0);
359 assert_int_equal(result
, 32678);
362 static void torture_push_be_u32(void **state
)
364 uint8_t data
[8] = {0};
365 uint8_t data2
[8] = {0x00, 0x00, 0x7f, 0xa6, 0x00, 0x00, 0x00, 0x2a};
370 RSIVALS(data
, 0, 32678);
371 RSIVALS(data
, 4, 42);
372 assert_memory_equal(data
, data2
, sizeof(data
));
374 result
= RIVAL(data
, 4);
375 assert_int_equal(result
, 42);
377 result
= RIVAL(data
, 0);
378 assert_int_equal(result
, 32678);
380 RSIVALS(data
, 0, 0xfffefffe);
381 result
= RIVAL(data
, 0);
382 assert_int_equal(result
, 0xfffefffe);
385 static void torture_push_be_u64(void **state
)
387 uint8_t data
[16] = {0};
392 RSBVALS(data
, 0, 32678);
394 result
= RBVAL(data
, 0);
395 assert_int_equal(result
, 32678);
397 SBVAL(data
, 8, 0xfffefffe);
399 result
= BVAL(data
, 8);
400 assert_int_equal(result
, 0xfffefffe);
403 int main(int argc
, char *argv
[])
406 const struct CMUnitTest tests
[] = {
407 cmocka_unit_test(torture_pull_le_u8
),
408 cmocka_unit_test(torture_pull_le_u16
),
409 cmocka_unit_test(torture_pull_le_u32
),
411 cmocka_unit_test(torture_push_le_u8
),
412 cmocka_unit_test(torture_push_le_u16
),
413 cmocka_unit_test(torture_push_le_u32
),
414 cmocka_unit_test(torture_push_le_u64
),
417 cmocka_unit_test(torture_pull_be_u8
),
418 cmocka_unit_test(torture_pull_be_u16
),
419 cmocka_unit_test(torture_pull_be_u32
),
421 cmocka_unit_test(torture_push_be_u8
),
422 cmocka_unit_test(torture_push_be_u16
),
423 cmocka_unit_test(torture_push_be_u32
),
424 cmocka_unit_test(torture_push_be_u64
),
428 cmocka_set_test_filter(argv
[1]);
430 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
432 rc
= cmocka_run_group_tests(tests
, NULL
, NULL
);