• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ashmem_mock.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace DistributedHardware {
28 using namespace std;
29 class AvSyncUtilsTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 public:
36     std::shared_ptr<MockAshmem> ashmemMock_;
37 };
38 
SetUpTestCase()39 void AvSyncUtilsTest::SetUpTestCase()
40 {
41 }
42 
TearDownTestCase()43 void AvSyncUtilsTest::TearDownTestCase()
44 {
45 }
46 
SetUp()47 void AvSyncUtilsTest::SetUp()
48 {
49     auto ashmem_ = IAshmem::GetOrCreateInstance();
50     ashmemMock_ = std::static_pointer_cast<MockAshmem>(ashmem_);
51 }
52 
TearDown()53 void AvSyncUtilsTest::TearDown()
54 {
55     IAshmem::ReleaseInstance();
56     ashmemMock_ = nullptr;
57 }
58 
59 HWTEST_F(AvSyncUtilsTest, CreateAVTransSharedMemory_001, TestSize.Level0)
60 {
61     std::string name = "";
62     size_t size = 0;
63     auto ret = CreateAVTransSharedMemory(name, size);
64     EXPECT_EQ(0, ret.fd);
65 }
66 
67 HWTEST_F(AvSyncUtilsTest, CloseAVTransSharedMemory_001, TestSize.Level0)
68 {
69     AVTransSharedMemory memory = {
70         .fd = -1,
71         .size = 0,
72         .name = "",
73     };
74     CloseAVTransSharedMemory(memory);
75     EXPECT_EQ(0, memory.size);
76 }
77 
78 HWTEST_F(AvSyncUtilsTest, CloseAVTransSharedMemory_002, TestSize.Level1)
79 {
80     AVTransSharedMemory memory = {
81         .fd = -1,
82         .size = 0,
83         .name = "name_test",
84     };
85     CloseAVTransSharedMemory(memory);
86     EXPECT_EQ(0, memory.size);
87 
88     AVTransSharedMemory memory1 = {
89         .fd = 1,
90         .size = 0,
91         .name = "name_test",
92     };
93     CloseAVTransSharedMemory(memory1);
94     EXPECT_EQ(0, memory1.size);
95 }
96 
97 HWTEST_F(AvSyncUtilsTest, MarshalSharedMemory_001, TestSize.Level0)
98 {
99     AVTransSharedMemory memory = {
100         .fd = 1,
101         .size = 100,
102         .name = "name_test",
103     };
104     auto ret = MarshalSharedMemory(memory);
105     EXPECT_EQ(false, ret.empty());
106 }
107 
108 HWTEST_F(AvSyncUtilsTest, UnmarshalSharedMemory_001, TestSize.Level0)
109 {
110     std::string jsonStr = "jsonStr_test";
111     auto ret = UnmarshalSharedMemory(jsonStr);
112     EXPECT_EQ(0, ret.fd);
113 }
114 
115 HWTEST_F(AvSyncUtilsTest, UnmarshalSharedMemory_002, TestSize.Level1)
116 {
117     cJSON *cJsonObj = cJSON_CreateObject();
118     cJSON_AddStringToObject(cJsonObj, KEY_SHARED_MEM_FD.c_str(), "mem_fd_test");
119     char* cjson = cJSON_PrintUnformatted(cJsonObj);
120     std::string jsonStr(cjson);
121     auto ret = UnmarshalSharedMemory(jsonStr);
122     EXPECT_EQ(0, ret.fd);
123     cJSON_free(cjson);
124     cJSON_Delete(cJsonObj);
125 
126     cJSON *cJsonObj1 = cJSON_CreateObject();
127     cJSON_AddNumberToObject(cJsonObj1, KEY_SHARED_MEM_FD.c_str(), 1);
128     cJSON_AddStringToObject(cJsonObj1, KEY_SHARED_MEM_SIZE.c_str(), "mem_size_test");
129     char* cjson1 = cJSON_PrintUnformatted(cJsonObj1);
130     std::string jsonStr1(cjson);
131     ret = UnmarshalSharedMemory(jsonStr1);
132     EXPECT_EQ(0, ret.fd);
133     cJSON_free(cjson1);
134     cJSON_Delete(cJsonObj1);
135 
136     cJSON *cJsonObj2 = cJSON_CreateObject();
137     cJSON_AddNumberToObject(cJsonObj2, KEY_SHARED_MEM_FD.c_str(), 1);
138     cJSON_AddNumberToObject(cJsonObj2, KEY_SHARED_MEM_SIZE.c_str(), 100);
139     cJSON_AddNumberToObject(cJsonObj2, KEY_SHARED_MEM_NAME.c_str(), 1);
140     char* cjson2 = cJSON_PrintUnformatted(cJsonObj2);
141     std::string jsonStr2(cjson2);
142     ret = UnmarshalSharedMemory(jsonStr2);
143     EXPECT_EQ(0, ret.fd);
144     cJSON_free(cjson2);
145     cJSON_Delete(cJsonObj2);
146 
147     cJSON *cJsonObj3 = cJSON_CreateObject();
148     cJSON_AddNumberToObject(cJsonObj3, KEY_SHARED_MEM_FD.c_str(), 1);
149     cJSON_AddNumberToObject(cJsonObj3, KEY_SHARED_MEM_SIZE.c_str(), 100);
150     cJSON_AddStringToObject(cJsonObj3, KEY_SHARED_MEM_NAME.c_str(), "mem_name_test");
151     char* cjson3 = cJSON_PrintUnformatted(cJsonObj3);
152     std::string jsonStr3(cjson3);
153     ret = UnmarshalSharedMemory(jsonStr3);
154     EXPECT_EQ("mem_name_test", ret.name);
155     cJSON_free(cjson3);
156     cJSON_Delete(cJsonObj3);
157 }
158 
159 HWTEST_F(AvSyncUtilsTest, WriteClockUnitToMemory_001, TestSize.Level1)
160 {
161     AVTransSharedMemory memory = {
162         .fd = 1,
163         .size = 100,
164         .name = "name_test",
165     };
166     AVSyncClockUnit unit = {
167         .index = 1,
168         .frameNum = 1,
169         .pts = 1,
170     };
171     auto ret = WriteClockUnitToMemory(memory, unit);
172     EXPECT_EQ(false, ret == 0);
173 }
174 }
175 }