1 /*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "context_test.h"
17 #include <securec.h>
18 #include "common.h"
19
20 using namespace testing::ext;
21
22 namespace OHOS {
23 namespace Wifi {
CalculateLeft(const Context * ctx)24 static int CalculateLeft(const Context *ctx)
25 {
26 int left =
27 (ctx->wBegin <= ctx->wEnd) ? (ctx->wCapacity - 1 - ctx->wEnd + ctx->wBegin) : (ctx->wBegin - ctx->wEnd - 1);
28 return left;
29 }
30
31 HWTEST_F(ContextTest, CreateContextTest, TestSize.Level1)
32 {
33 ctx = CreateContext(100);
34 ASSERT_TRUE(ctx != nullptr);
35 ASSERT_TRUE(ctx->wCapacity == CONTEXT_BUFFER_MIN_SIZE);
36 ReleaseContext(ctx);
37 ctx = CreateContext(102400000);
38 ASSERT_TRUE(ctx != nullptr);
39 ASSERT_TRUE(ctx->wCapacity == CONTEXT_BUFFER_MAX_SIZE);
40 }
41
42 HWTEST_F(ContextTest, AppendCacheTest, TestSize.Level1)
43 {
44 ctx = CreateContext(1024);
45 ASSERT_TRUE(ctx != nullptr) << "capacity :" << ctx->wCapacity;
46 /* capacity size 1024 */
47 ASSERT_TRUE(ctx->wCapacity == 1024);
48 ASSERT_TRUE(ctx->wEnd == 0);
49
50 /* calculate left capacity test */
51 ctx->wBegin = ctx->wEnd = 0;
52 ASSERT_TRUE(CalculateLeft(ctx) == 1023); /* capacity, use a space define endpos */
53 ctx->wEnd = 1023; /* pos [0,1023], max pos 1023 */
54 ASSERT_TRUE(CalculateLeft(ctx) == 0);
55 ctx->wBegin = 1023;
56 ctx->wEnd = 0;
57 ASSERT_TRUE(CalculateLeft(ctx) == 1022); /* wEnd max num can be 1022, so from 0 - 1021 = 1022 */
58 ctx->wEnd = 1022;
59 ASSERT_TRUE(CalculateLeft(ctx) == 0);
60 ctx->wBegin = ctx->wEnd = 0;
61
62 char buff[] = "123456789 123456789 123456789 12";
63 for (unsigned i = 0; i < 1024 / 32 - 1; ++i) {
64 ContextAppendWrite(ctx, buff, strlen(buff));
65 ASSERT_TRUE(ctx->wEnd == (i + 1) * 32);
66 }
67 ContextAppendWrite(test, buff, strlen(buff));
68 ASSERT_TRUE(strcpy_s(buff, sizeof(buff), "123456789 123456789 123456789 1") == EOK);
69 ContextAppendWrite(ctx, buff, strlen(buff));
70 ASSERT_TRUE(ctx->wEnd == 1023);
71 ASSERT_TRUE(CalculateLeft(ctx) == 0);
72 ctx->wBegin = 1023;
73 ASSERT_TRUE(strcpy_s(buff, sizeof(buff), "123456789 123456789 123456789 12") == EOK);
74 for (unsigned i = 0; i < 1024 / 32 - 1; ++i) {
75 ContextAppendWrite(ctx, buff, strlen(buff));
76 ASSERT_TRUE(ctx->wEnd == (i + 1) * 32 - 1);
77 }
78 ASSERT_TRUE(CalculateLeft(ctx) == 31);
79 ContextAppendWrite(ctx, buff, strlen(buff));
80 ASSERT_TRUE(ctx->wCapacity == 2048);
81 ASSERT_TRUE(ctx->wEnd == (1023 + 1024));
82 ctx->wCapacity = -1;
83 ContextAppendWrite(ctx, buff, strlen(buff));
84 }
85
ExpandReadCache(Context * context,int len)86 static int ExpandReadCache(Context *context, int len)
87 {
88 int left = (context->rBegin <= context->rEnd) ? (context->rCapacity - 1 - context->rEnd + context->rBegin)
89 : (context->rBegin - context->rEnd - 1);
90 if (left < len) {
91 int capacity = context->rCapacity;
92 while (left < len) {
93 capacity += context->rCapacity;
94 left += context->rCapacity;
95 }
96 char *p = (char *)calloc(capacity, sizeof(char));
97 if (p == nullptr) {
98 return -1;
99 }
100 if (memmove_s(p, capacity, context->szRead, context->rCapacity) != EOK) {
101 free(p);
102 return -1;
103 }
104 if (context->rBegin > context->rEnd &&
105 memmove_s(p + context->rCapacity, context->rCapacity, p, context->rEnd) != EOK) {
106 free(p);
107 return -1;
108 }
109 context->szRead = p;
110 if (context->rBegin > context->rEnd) {
111 context->rEnd += context->rCapacity;
112 }
113 context->rCapacity = capacity;
114 }
115 return 0;
116 }
117
ContextAppendRead(Context * context,const char * buf,int len)118 static int ContextAppendRead(Context *context, const char *buf, int len)
119 {
120 if (ExpandReadCache(context, len) < 0) {
121 return -1;
122 }
123 if (context->rEnd + len < context->rCapacity) {
124 if (memmove_s(context->szRead + context->rEnd, context->rCapacity - context->rEnd, buf, len) != EOK) {
125 return -1;
126 }
127 context->rEnd += len;
128 } else {
129 int tmp = context->rCapacity - context->rEnd;
130 if (tmp > 0 && memmove_s(context->szRead + context->rEnd, tmp, buf, tmp) != EOK) {
131 return -1;
132 }
133 if (tmp < len && memmove_s(context->szRead, len - tmp, buf + tmp, len - tmp) != EOK) {
134 return -1;
135 }
136 context->rEnd = len - tmp;
137 }
138 return 0;
139 }
140
141 HWTEST_F(ContextTest, ContextGetReadRecordTest, TestSize.Level1)
142 {
143 ctx = CreateContext(1024);
144 ASSERT_TRUE(ctx != nullptr);
145 ASSERT_TRUE(ctx->rCapacity == 1024);
146 ContextGetReadRecord(test);
147 char *p = ContextGetReadRecord(ctx);
148 ASSERT_TRUE(p == nullptr);
149
150 int ret = ContextAppendRead(ctx, "hello", strlen("hello"));
151 ASSERT_TRUE(ret == 0);
152 p = ContextGetReadRecord(ctx);
153 ASSERT_TRUE(p == nullptr);
154 ret = ContextAppendRead(ctx, ctx->cMsgEnd, strlen(ctx->cMsgEnd));
155 ASSERT_TRUE(ret == 0);
156 p = ContextGetReadRecord(ctx);
157 ASSERT_TRUE(p != nullptr);
158 EXPECT_TRUE(strcmp(p, "hello") == 0);
159 free(p);
160 ctx->rBegin = ctx->rEnd = 1024 - 5; // set end pos
161 ret = ContextAppendRead(ctx, "hello, welcome", strlen("hello, welcome"));
162 ASSERT_TRUE(ret == 0);
163 p = ContextGetReadRecord(ctx);
164 ASSERT_TRUE(p == nullptr);
165 ret = ContextAppendRead(ctx, ctx->cMsgEnd, strlen(ctx->cMsgEnd));
166 ASSERT_TRUE(ret == 0);
167 p = ContextGetReadRecord(ctx);
168 ASSERT_TRUE(p != nullptr);
169 EXPECT_TRUE(strcmp(p, "hello, welcome") == 0);
170 free(p);
171 }
172
173 HWTEST_F(ContextTest, ContextReadNetTest, TestSize.Level1)
174 {
175 ctx = CreateContext(1024);
176 ASSERT_TRUE(ctx != nullptr);
177 ContextReadNet(test);
178 int ret = ContextReadNet(ctx);
179 EXPECT_TRUE(ret == MAX_ONE_LINE_SIZE - 1);
180 ctx->rBegin = 1024 - 5;
181 ret = ContextReadNet(ctx);
182 EXPECT_TRUE(ret == MAX_ONE_LINE_SIZE - 1);
183 EXPECT_TRUE(ctx->rCapacity == 2048);
184 }
185
186 HWTEST_F(ContextTest, ContextWriteNetTest, TestSize.Level1)
187 {
188 ASSERT_TRUE(ContextWriteNet(nullptr) == -1);
189 ctx = CreateContext(1024);
190 ASSERT_TRUE(ctx != nullptr);
191 int ret = ContextWriteNet(ctx);
192 EXPECT_TRUE(ret == 0);
193 char buff[] = "123456789 123456789 123456789 12";
194 int size = strlen(buff);
195 ContextAppendWrite(ctx, buff, size);
196 ret = ContextWriteNet(ctx);
197 EXPECT_TRUE(ret == size);
198 ctx->wBegin = ctx->wEnd = 1023;
199 ContextAppendWrite(ctx, buff, size);
200 ret = ContextWriteNet(ctx);
201 EXPECT_TRUE(ret == size - 1);
202 }
203 } // namespace Wifi
204 } // namespace OHOS
205