xref: /spdk/test/unit/lib/util/xor.c/xor_ut.c (revision 8afdeef3becfe9409cc9e7372bd0bc10e8b7d46d)
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 test_xor_gen(void)
19 {
20 	void *bufs[BUF_COUNT];
21 	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 	for (i = 0; i < BUF_COUNT; i++) {
29 		ret = posix_memalign(&bufs[i], spdk_xor_get_optimal_alignment(), BUF_SIZE);
30 		SPDK_CU_ASSERT_FATAL(ret == 0);
31 
32 		tmp = bufs[i];
33 		for (j = 0; j < BUF_SIZE / sizeof(*tmp); j++) {
34 			tmp[j] = (i << 16) + j;
35 		}
36 	}
37 	dest = bufs[SRC_BUF_COUNT];
38 
39 	/* prepare the reference buffer */
40 	ref = malloc(BUF_SIZE);
41 	SPDK_CU_ASSERT_FATAL(ref != NULL);
42 
43 	memset(ref, 0, BUF_SIZE);
44 	for (i = 0; i < SRC_BUF_COUNT; i++) {
45 		for (j = 0; j < BUF_SIZE; j++) {
46 			ref[j] ^= ((uint8_t *)bufs[i])[j];
47 		}
48 	}
49 
50 	/* generate xor, compare the dest and reference buffers */
51 	ret = spdk_xor_gen(dest, bufs, SRC_BUF_COUNT, BUF_SIZE);
52 	CU_ASSERT(ret == 0);
53 	ret = memcmp(ref, dest, BUF_SIZE);
54 	CU_ASSERT(ret == 0);
55 
56 	/* len not multiple of alignment */
57 	memset(dest, 0xba, BUF_SIZE);
58 	ret = spdk_xor_gen(dest, bufs, SRC_BUF_COUNT, BUF_SIZE - 1);
59 	CU_ASSERT(ret == 0);
60 	ret = memcmp(ref, dest, BUF_SIZE - 1);
61 	CU_ASSERT(ret == 0);
62 
63 	/* unaligned buffer */
64 	memcpy(bufs2, bufs, sizeof(bufs2));
65 	bufs2[1] += 1;
66 	bufs2[2] += 2;
67 	bufs2[3] += 3;
68 
69 	memset(ref, 0, BUF_SIZE);
70 	for (i = 0; i < SRC_BUF_COUNT; i++) {
71 		for (j = 0; j < BUF_SIZE - SRC_BUF_COUNT; j++) {
72 			ref[j] ^= ((uint8_t *)bufs2[i])[j];
73 		}
74 	}
75 
76 	memset(dest, 0xba, BUF_SIZE);
77 	ret = spdk_xor_gen(dest, bufs2, SRC_BUF_COUNT, BUF_SIZE - SRC_BUF_COUNT);
78 	CU_ASSERT(ret == 0);
79 	ret = memcmp(ref, dest, BUF_SIZE - SRC_BUF_COUNT);
80 	CU_ASSERT(ret == 0);
81 
82 	/* xoring a buffer with itself should result in all zeros */
83 	memset(ref, 0, BUF_SIZE);
84 	bufs2[0] = bufs[0];
85 	bufs2[1] = bufs[0];
86 	dest = bufs[0];
87 
88 	ret = spdk_xor_gen(dest, bufs2, 2, BUF_SIZE);
89 	CU_ASSERT(ret == 0);
90 	ret = memcmp(ref, dest, BUF_SIZE);
91 	CU_ASSERT(ret == 0);
92 
93 	/* cleanup */
94 	for (i = 0; i < BUF_COUNT; i++) {
95 		free(bufs[i]);
96 	}
97 	free(ref);
98 }
99 
100 int
101 main(int argc, char **argv)
102 {
103 	CU_pSuite	suite = NULL;
104 	unsigned int	num_failures;
105 
106 	CU_initialize_registry();
107 
108 	suite = CU_add_suite("xor", NULL, NULL);
109 
110 	CU_ADD_TEST(suite, test_xor_gen);
111 
112 
113 	num_failures = spdk_ut_run_tests(argc, argv, NULL);
114 
115 	CU_cleanup_registry();
116 
117 	return num_failures;
118 }
119