1 /*
2  * Copyright (c) 2024 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 <gtest/gtest.h>
17 
18 #include "av_sync_utils.h"
19 
20 #include "av_trans_constants.h"
21 #include "cJSON.h"
22 
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace DistributedHardware {
26 using namespace std;
27 class AvSyncUtilsTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase()35 void AvSyncUtilsTest::SetUpTestCase()
36 {
37 }
38 
TearDownTestCase()39 void AvSyncUtilsTest::TearDownTestCase()
40 {
41 }
42 
SetUp()43 void AvSyncUtilsTest::SetUp()
44 {
45 }
46 
TearDown()47 void AvSyncUtilsTest::TearDown()
48 {
49 }
50 
51 HWTEST_F(AvSyncUtilsTest, CreateAVTransSharedMemory_001, TestSize.Level0)
52 {
53     std::string name = "";
54     size_t size = 0;
55     auto ret = CreateAVTransSharedMemory(name, size);
56     EXPECT_EQ(0, ret.fd);
57 }
58 
59 HWTEST_F(AvSyncUtilsTest, CloseAVTransSharedMemory_001, TestSize.Level0)
60 {
61     AVTransSharedMemory memory = {
62         .fd = -1,
63         .size = 0,
64         .name = "",
65     };
66     CloseAVTransSharedMemory(memory);
67     EXPECT_EQ(0, memory.size);
68 }
69 
70 HWTEST_F(AvSyncUtilsTest, CloseAVTransSharedMemory_002, TestSize.Level0)
71 {
72     AVTransSharedMemory memory = {
73         .fd = -1,
74         .size = 0,
75         .name = "name_test",
76     };
77     CloseAVTransSharedMemory(memory);
78     EXPECT_EQ(0, memory.size);
79 
80     AVTransSharedMemory memory1 = {
81         .fd = 1,
82         .size = 0,
83         .name = "name_test",
84     };
85     CloseAVTransSharedMemory(memory1);
86     EXPECT_EQ(0, memory1.size);
87 }
88 
89 HWTEST_F(AvSyncUtilsTest, MarshalSharedMemory_001, TestSize.Level0)
90 {
91     AVTransSharedMemory memory = {
92         .fd = 1,
93         .size = 100,
94         .name = "name_test",
95     };
96     auto ret = MarshalSharedMemory(memory);
97     EXPECT_EQ(false, ret.empty());
98 }
99 
100 HWTEST_F(AvSyncUtilsTest, UnmarshalSharedMemory_001, TestSize.Level0)
101 {
102     std::string jsonStr = "jsonStr_test";
103     auto ret = UnmarshalSharedMemory(jsonStr);
104     EXPECT_EQ(0, ret.fd);
105 }
106 
107 HWTEST_F(AvSyncUtilsTest, UnmarshalSharedMemory_002, TestSize.Level0)
108 {
109     cJSON *cJsonObj = cJSON_CreateObject();
110     cJSON_AddStringToObject(cJsonObj, KEY_SHARED_MEM_FD.c_str(), "mem_fd_test");
111     char* cjson = cJSON_PrintUnformatted(cJsonObj);
112     std::string jsonStr(cjson);
113     auto ret = UnmarshalSharedMemory(jsonStr);
114     EXPECT_EQ(0, ret.fd);
115     cJSON_free(cjson);
116     cJSON_Delete(cJsonObj);
117 
118     cJSON *cJsonObj1 = cJSON_CreateObject();
119     cJSON_AddNumberToObject(cJsonObj1, KEY_SHARED_MEM_FD.c_str(), 1);
120     cJSON_AddStringToObject(cJsonObj1, KEY_SHARED_MEM_SIZE.c_str(), "mem_size_test");
121     char* cjson1 = cJSON_PrintUnformatted(cJsonObj1);
122     std::string jsonStr1(cjson);
123     ret = UnmarshalSharedMemory(jsonStr1);
124     EXPECT_EQ(0, ret.fd);
125     cJSON_free(cjson1);
126     cJSON_Delete(cJsonObj1);
127 
128     cJSON *cJsonObj2 = cJSON_CreateObject();
129     cJSON_AddNumberToObject(cJsonObj2, KEY_SHARED_MEM_FD.c_str(), 1);
130     cJSON_AddNumberToObject(cJsonObj2, KEY_SHARED_MEM_SIZE.c_str(), 100);
131     cJSON_AddNumberToObject(cJsonObj2, KEY_SHARED_MEM_NAME.c_str(), 1);
132     char* cjson2 = cJSON_PrintUnformatted(cJsonObj2);
133     std::string jsonStr2(cjson2);
134     ret = UnmarshalSharedMemory(jsonStr2);
135     EXPECT_EQ(0, ret.fd);
136     cJSON_free(cjson2);
137     cJSON_Delete(cJsonObj2);
138 
139     cJSON *cJsonObj3 = cJSON_CreateObject();
140     cJSON_AddNumberToObject(cJsonObj3, KEY_SHARED_MEM_FD.c_str(), 1);
141     cJSON_AddNumberToObject(cJsonObj3, KEY_SHARED_MEM_SIZE.c_str(), 100);
142     cJSON_AddStringToObject(cJsonObj3, KEY_SHARED_MEM_NAME.c_str(), "mem_name_test");
143     char* cjson3 = cJSON_PrintUnformatted(cJsonObj3);
144     std::string jsonStr3(cjson3);
145     ret = UnmarshalSharedMemory(jsonStr3);
146     EXPECT_EQ("mem_name_test", ret.name);
147     cJSON_free(cjson3);
148     cJSON_Delete(cJsonObj3);
149 }
150 }
151 }