1/*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 *    conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 *    of conditions and the following disclaimer in the documentation and/or other materials
12 *    provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 *    to endorse or promote products derived from this software without specific prior written
16 *    permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "xts_mem.h"
32
33LITE_TEST_SUIT(MEM, MemApi, MemApiTestSuite);
34
35static BOOL MemApiTestSuiteSetUp(void)
36{
37    return TRUE;
38}
39
40static BOOL MemApiTestSuiteTearDown(void)
41{
42    return TRUE;
43}
44
45/**
46 * @tc.number SUB_KERNEL_MEM_MEMSET_0100
47 * @tc.name   memset function set buffer value test
48 * @tc.desc   [C-L*-311]
49 */
50LITE_TEST_CASE(MemApiTestSuite, testMemset, Function | MediumTest | Level1)
51{
52    char chr = 'A';
53    int i, len, failure;
54    len = GetRandom(1024); /* 1024, common data for test, no special meaning */
55    errno_t err = EOK;
56
57    char buf[1024]; /* 1024, common data for test, no special meaning */
58    err = memset_s(buf, sizeof(buf), chr, len);
59    ICUNIT_ASSERT_EQUAL(err, 0, err);
60    failure = 0;
61    for (i = 0; i < len; i++) {
62        if (buf[i] != chr) {
63            failure = 1; /* 1, common data for test, no special meaning */
64            break;
65        }
66    }
67    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
68    return 0;
69}
70
71/**
72 * @tc.number SUB_KERNEL_MEM_MEMCPY_0100
73 * @tc.name   memcpy function copy buffer test
74 * @tc.desc   [C-L*-311]
75 */
76LITE_TEST_CASE(MemApiTestSuite, testMemcpy, Function | MediumTest | Level2)
77{
78    char chr = 'A';
79    int i, len, failure;
80    char src[1024]; /* 1024, common data for test, no special meaning */
81    char dst[1024]; /* 1024, common data for test, no special meaning */
82
83    len = GetRandom(1024); /* 1024, common data for test, no special meaning */
84
85    for (i = 0; i < len; i++) {
86        src[i] = chr + i % 26; /* 26, common data for test, no special meaning */
87    }
88
89    errno_t err = memcpy_s(dst, sizeof(dst) / sizeof(dst[0]), src, len);
90    ICUNIT_ASSERT_EQUAL(err, 0, err);
91    failure = 0;
92    for (i = 0; i < len; i++) {
93        if (dst[i] != src[i]) {
94            failure = 1; /* 1, common data for test, no special meaning */
95            break;
96        }
97    }
98    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
99    return 0;
100}
101
102/**
103 * @tc.number SUB_KERNEL_MEM_MEMMOVE_0100
104 * @tc.name   memmove function move buffer test
105 * @tc.desc   [C-L*-311]
106 */
107LITE_TEST_CASE(MemApiTestSuite, testMemmove, Function | MediumTest | Level2)
108{
109    char chr = 'A';
110    char buf[1024]; /* 1024, common data for test, no special meaning */
111    int i, len, failure;
112
113    len = sizeof(buf);
114    for (i = 0; i < len; i++) {
115        buf[i] = chr + GetRandom(26); /* 26, common data for test, no special meaning */
116    }
117    errno_t err = memmove_s(&buf[0], sizeof(buf) / sizeof(buf[0]), &buf[len / 2], len / 2); /* 2, common data for test, no special meaning */
118    ICUNIT_ASSERT_EQUAL(err, 0, err);
119
120    failure = 0;
121    for (i = 0; i < len / 2; i++) { /* 2, common data for test, no special meaning */
122        if (buf[i] != buf[len / 2 + i]) { /* 2, common data for test, no special meaning */
123            failure = 1; /* 1, common data for test, no special meaning */
124            break;
125        }
126    }
127    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
128    return 0;
129}
130
131/**
132 * @tc.number SUB_KERNEL_MEM_MEMMOVE_0200
133 * @tc.name   memmove function overlay move buffer test
134 * @tc.desc   [C-L*-311]
135 */
136LITE_TEST_CASE(MemApiTestSuite, testMemmoveOverlay, Function | MediumTest | Level3)
137{
138    char chr = 'A';
139    char buf[1024]; /* 1024, common data for test, no special meaning */
140    char backup[1024]; /* 1024, common data for test, no special meaning */
141    int i, len, failure;
142
143    len = sizeof(buf);
144    for (i = 0; i < len; i++) {
145        buf[i] = chr + GetRandom(26); /* 26, common data for test, no special meaning */
146        backup[i] = buf[i];
147    }
148    errno_t err = memmove_s(&buf[16], sizeof(buf) / sizeof(buf[0]) - 16, &buf[0], len / 2); /* 16, 2, common data for test, no special meaning */
149    ICUNIT_ASSERT_EQUAL(err, 0, err);
150
151    failure = 0;
152    for (i = 0; i < len / 2; i++) { /* 2, common data for test, no special meaning */
153        if (buf[i + 16] != backup[i]) { /* 16, common data for test, no special meaning */
154            failure = 1; /* 1, common data for test, no special meaning */
155            break;
156        }
157    }
158    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
159    return 0;
160}
161
162
163/**
164 * @tc.number SUB_KERNEL_MEM_MEMCMP_0100
165 * @tc.name   memmove function move buffer test
166 * @tc.desc   [C-L*-311]
167 */
168LITE_TEST_CASE(MemApiTestSuite, testMemcmp, Function | MediumTest | Level2)
169{
170    char orign[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; /* 8, common data for test, no special meaning */
171    char lt[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x77}; /* 8, common data for test, no special meaning */
172    char eq[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; /* 8, common data for test, no special meaning */
173    char gt[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x99}; /* 8, common data for test, no special meaning */
174
175    int ret;
176    int len = sizeof(orign);
177
178    ret = memcmp(lt, orign, len);
179    ICUNIT_ASSERT_WITHIN_EQUAL(ret, INT_MIN, 0, ret);
180
181    ret = memcmp(eq, orign, len);
182    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
183
184    ret = memcmp(gt, orign, len);
185    ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, INT_MAX, ret);
186
187    ret = memcmp(gt, orign, 0);
188    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
189    return 0;
190}
191
192RUN_TEST_SUITE(MemApiTestSuite);
193
194void MemApiTest(void)
195{
196    RUN_ONE_TESTCASE(testMemset);
197    RUN_ONE_TESTCASE(testMemcpy);
198    RUN_ONE_TESTCASE(testMemmove);
199    RUN_ONE_TESTCASE(testMemmoveOverlay);
200    RUN_ONE_TESTCASE(testMemcmp);
201}
202