Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2022 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : :
8 : : #include "spdk_internal/cunit.h"
9 : :
10 : : #include "util/xor.c"
11 : : #include "common/lib/test_env.c"
12 : :
13 : : #define BUF_COUNT 8
14 : : #define SRC_BUF_COUNT (BUF_COUNT - 1)
15 : : #define BUF_SIZE 4096
16 : :
17 : : static void
18 : 5 : test_xor_gen(void)
19 : : {
20 : 4 : void *bufs[BUF_COUNT];
21 : 4 : void *bufs2[SRC_BUF_COUNT];
22 : : uint8_t *ref, *dest;
23 : : int ret;
24 : : size_t i, j;
25 : : uint32_t *tmp;
26 : :
27 : : /* alloc and fill the buffers with a pattern */
28 [ + + ]: 45 : for (i = 0; i < BUF_COUNT; i++) {
29 [ - + ]: 40 : ret = posix_memalign(&bufs[i], spdk_xor_get_optimal_alignment(), BUF_SIZE);
30 [ - + ]: 40 : SPDK_CU_ASSERT_FATAL(ret == 0);
31 : :
32 : 40 : tmp = bufs[i];
33 [ + + ]: 41000 : for (j = 0; j < BUF_SIZE / sizeof(*tmp); j++) {
34 : 40960 : tmp[j] = (i << 16) + j;
35 : : }
36 : : }
37 : 5 : dest = bufs[SRC_BUF_COUNT];
38 : :
39 : : /* prepare the reference buffer */
40 : 5 : ref = malloc(BUF_SIZE);
41 [ - + ]: 5 : SPDK_CU_ASSERT_FATAL(ref != NULL);
42 : :
43 [ - + ]: 5 : memset(ref, 0, BUF_SIZE);
44 [ + + ]: 40 : for (i = 0; i < SRC_BUF_COUNT; i++) {
45 [ + + ]: 143395 : for (j = 0; j < BUF_SIZE; j++) {
46 : 143360 : ref[j] ^= ((uint8_t *)bufs[i])[j];
47 : : }
48 : : }
49 : :
50 : : /* generate xor, compare the dest and reference buffers */
51 : 5 : ret = spdk_xor_gen(dest, bufs, SRC_BUF_COUNT, BUF_SIZE);
52 : 5 : CU_ASSERT(ret == 0);
53 [ - + - + ]: 5 : ret = memcmp(ref, dest, BUF_SIZE);
54 : 5 : CU_ASSERT(ret == 0);
55 : :
56 : : /* len not multiple of alignment */
57 [ - + ]: 5 : memset(dest, 0xba, BUF_SIZE);
58 : 5 : ret = spdk_xor_gen(dest, bufs, SRC_BUF_COUNT, BUF_SIZE - 1);
59 : 5 : CU_ASSERT(ret == 0);
60 [ - + - + ]: 5 : ret = memcmp(ref, dest, BUF_SIZE - 1);
61 : 5 : CU_ASSERT(ret == 0);
62 : :
63 : : /* unaligned buffer */
64 : 5 : memcpy(bufs2, bufs, sizeof(bufs2));
65 : 5 : bufs2[1] += 1;
66 : 5 : bufs2[2] += 2;
67 : 5 : bufs2[3] += 3;
68 : :
69 [ - + ]: 5 : memset(ref, 0, BUF_SIZE);
70 [ + + ]: 40 : for (i = 0; i < SRC_BUF_COUNT; i++) {
71 [ + + ]: 143150 : for (j = 0; j < BUF_SIZE - SRC_BUF_COUNT; j++) {
72 : 143115 : ref[j] ^= ((uint8_t *)bufs2[i])[j];
73 : : }
74 : : }
75 : :
76 [ - + ]: 5 : memset(dest, 0xba, BUF_SIZE);
77 : 5 : ret = spdk_xor_gen(dest, bufs2, SRC_BUF_COUNT, BUF_SIZE - SRC_BUF_COUNT);
78 : 5 : CU_ASSERT(ret == 0);
79 [ - + - + ]: 5 : ret = memcmp(ref, dest, BUF_SIZE - SRC_BUF_COUNT);
80 : 5 : CU_ASSERT(ret == 0);
81 : :
82 : : /* xoring a buffer with itself should result in all zeros */
83 [ - + ]: 5 : memset(ref, 0, BUF_SIZE);
84 : 5 : bufs2[0] = bufs[0];
85 : 5 : bufs2[1] = bufs[0];
86 : 5 : dest = bufs[0];
87 : :
88 : 5 : ret = spdk_xor_gen(dest, bufs2, 2, BUF_SIZE);
89 : 5 : CU_ASSERT(ret == 0);
90 [ - + - + ]: 5 : ret = memcmp(ref, dest, BUF_SIZE);
91 : 5 : CU_ASSERT(ret == 0);
92 : :
93 : : /* cleanup */
94 [ + + ]: 45 : for (i = 0; i < BUF_COUNT; i++) {
95 : 40 : free(bufs[i]);
96 : : }
97 : 5 : free(ref);
98 : 5 : }
99 : :
100 : : int
101 : 5 : main(int argc, char **argv)
102 : : {
103 : 5 : CU_pSuite suite = NULL;
104 : : unsigned int num_failures;
105 : :
106 : 5 : CU_initialize_registry();
107 : :
108 : 5 : suite = CU_add_suite("xor", NULL, NULL);
109 : :
110 : 5 : CU_ADD_TEST(suite, test_xor_gen);
111 : :
112 : :
113 : 5 : num_failures = spdk_ut_run_tests(argc, argv, NULL);
114 : :
115 : 5 : CU_cleanup_registry();
116 : :
117 : 5 : return num_failures;
118 : : }
|