1 /* 2 * Copyright (C) 2021 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 "serial_test.h" 17 #include "serial.h" 18 19 using namespace testing::ext; 20 21 namespace OHOS { 22 namespace Wifi { 23 24 Context *SerialTest::ctx = nullptr; 25 26 HWTEST_F(SerialTest, SerialOneTest, TestSize.Level1) 27 { 28 WriteBegin(ctx, 0); 29 ASSERT_EQ(WriteBegin(test, 0), -1); 30 31 WriteFunc(ctx, "SerialTest"); 32 ASSERT_EQ(WriteFunc(test, "SerialTest"), -1); 33 34 WriteInt(ctx, 100); 35 ASSERT_EQ(WriteInt(test, 100), -1); 36 37 WriteLong(ctx, 1234567890L); 38 ASSERT_EQ(WriteLong(test, 1234567890L), -1); 39 40 WriteInt64(ctx, 12345678909832323LL); 41 ASSERT_EQ(WriteInt64(test, 12345678909832323LL), -1); 42 43 WriteDouble(ctx, 3.14159); 44 ASSERT_EQ(WriteDouble(test, 3.14159), -1); 45 46 WriteChar(ctx, 'a'); 47 ASSERT_EQ(WriteChar(test, 'a'), -1); 48 49 WriteStr(ctx, "Hello, world"); 50 ASSERT_EQ(WriteStr(test, "Hello, world"), -1); 51 52 int count = strlen("2c:f0:xx:xx:xx:be"); 53 WriteUStr(ctx, (const unsigned char *)"2c:f0:xx:xx:xx:be", count); 54 ASSERT_EQ(WriteUStr(test, (const unsigned char *)"2c:f0:xx:xx:xx:be", count), -1); 55 56 WriteEnd(ctx); 57 ASSERT_EQ(WriteEnd(test), -1); 58 } 59 60 HWTEST_F(SerialTest, SerialTwoTest, TestSize.Level1) 61 { 62 ctx->oneProcess = ctx->szWrite; 63 ctx->nSize = ctx->wEnd; 64 65 EXPECT_TRUE(strncmp(ctx->oneProcess, "N\t", 2) == 0); 66 ctx->nPos = 2; 67 char str[1024] = {0}; 68 ASSERT_EQ(ReadFunc(ctx, str, 1024), 0); 69 ASSERT_EQ(ReadFunc(test, str, 1024), -1); 70 71 ctx->nSize = ctx->nPos; 72 ASSERT_EQ(ReadFunc(ctx, str, 1024), -1); 73 74 EXPECT_TRUE(strcmp(str, "SerialTest") == 0); 75 int i = 0; 76 ctx->nSize = ctx->wEnd; 77 ASSERT_EQ(ReadInt(test, &i), -1); 78 ASSERT_EQ(ReadInt(ctx, &i), 0); 79 80 EXPECT_TRUE(i == 100); 81 long l = 0; 82 ASSERT_EQ(ReadLong(test, &l), -1); 83 ASSERT_EQ(ReadLong(ctx, &l), 0); 84 EXPECT_TRUE(l == 1234567890L); 85 int64_t t = 0; 86 ASSERT_EQ(ReadInt64(test, &t), -1); 87 ASSERT_EQ(ReadInt64(ctx, &t), 0); 88 EXPECT_TRUE(t == 12345678909832323LL); 89 double d = 0.0; 90 ASSERT_EQ(ReadDouble(test, &d), -1); 91 ASSERT_EQ(ReadDouble(ctx, &d), 0); 92 EXPECT_TRUE(d - 3.14159 < 0.000001 && d - 3.14159 > -0.000001); 93 char c = ' '; 94 ASSERT_EQ(ReadChar(test, &c), -1); 95 ASSERT_EQ(ReadChar(ctx, &c), 0); 96 EXPECT_TRUE(c == 'a'); 97 ASSERT_EQ(ReadStr(test, str, 1024), -1); 98 ASSERT_EQ(ReadStr(ctx, str, 1024), 0); 99 EXPECT_TRUE(strcmp(str, "Hello, world") == 0); 100 int count = strlen("2c:f0:xx:xx:xx:be"); 101 ASSERT_EQ(ReadUStr(test, (unsigned char *)str, count + 1), -1); 102 ASSERT_EQ(ReadUStr(ctx, (unsigned char *)str, count + 1), 0); 103 EXPECT_TRUE(strcmp(str, "2c:f0:xx:xx:xx:be") == 0); 104 EXPECT_TRUE(strncmp(ctx->oneProcess + ctx->nPos, "$$$$$$", 6) == 0); 105 } 106 } // namespace Wifi 107 } // namespace OHOS