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 #define private public
17 #define protected public
18 #include <gtest/gtest.h>
19 #include "item_data_box.h"
20 #include "item_property_transform_box.h"
21 #include "item_property_color_box.h"
22 #include "item_property_box.h"
23 #include "item_ref_box.h"
24 #include "item_info_box.h"
25 #include "heif_box.h"
26 #include "item_property_hvcc_box.h"
27 
28 using namespace testing::ext;
29 namespace OHOS {
30 namespace ImagePlugin {
31 class HeifParserBoxTest : public testing::Test {
32 public:
HeifParserBoxTest()33     HeifParserBoxTest() {}
~HeifParserBoxTest()34     ~HeifParserBoxTest() {}
35 };
36 
37 /**
38  * @tc.name: ParseContentTest001
39  * @tc.desc: HeifImirBox
40  * @tc.type: FUNC
41  */
42 HWTEST_F(HeifParserBoxTest, ParseContentTest001, TestSize.Level3)
43 {
44     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseContentTest001 start";
45     HeifImirBox heifImirBox;
46     auto stream = std::make_shared<HeifBufferInputStream>(nullptr, 0, true);
47     HeifStreamReader reader(stream, 0, 0);
48     heifImirBox.ParseContent(reader);
49     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseContentTest001 end";
50 }
51 
52 /**
53  * @tc.name: WriteTest001
54  * @tc.desc: HeifImirBox
55  * @tc.type: FUNC
56  */
57 HWTEST_F(HeifParserBoxTest, WriteTest001, TestSize.Level3)
58 {
59     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest001 start";
60     HeifImirBox heifImirBox;
61     heifImirBox.direction_ = HeifTransformMirrorDirection::INVALID;
62     HeifStreamWriter write;
63     heif_error ret = heifImirBox.Write(write);
64     ASSERT_EQ(ret, heif_invalid_mirror_direction);
65     heifImirBox.direction_ = HeifTransformMirrorDirection::HORIZONTAL;
66     ret = heifImirBox.Write(write);
67     ASSERT_EQ(ret, heif_error_ok);
68     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest001 end";
69 }
70 
71 /**
72  * @tc.name: WriteTest002
73  * @tc.desc: HeifNclxColorProfile
74  * @tc.type: FUNC
75  */
76 HWTEST_F(HeifParserBoxTest, WriteTest002, TestSize.Level3)
77 {
78     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest002 start";
79     HeifNclxColorProfile heifNclx(0, 0, 0, 0);
80     HeifStreamWriter write;
81     heif_error ret = heifNclx.Write(write);
82     ASSERT_EQ(ret, heif_error_ok);
83     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest002 end";
84 }
85 
86 /**
87  * @tc.name: GetPropertyTest001
88  * @tc.desc: HeifIpcoBox
89  * @tc.type: FUNC
90  */
91 HWTEST_F(HeifParserBoxTest, GetPropertyTest001, TestSize.Level3)
92 {
93     GTEST_LOG_(INFO) << "HeifParserBoxTest: GetPropertyTest001 start";
94     auto heifIpcoBox = std::make_shared<HeifIpcoBox>();
95     std::shared_ptr<HeifIpmaBox> heifIpmaBox = std::make_shared<HeifIpmaBox>();
96     heif_item_id itemId = 1;
97     uint32_t boxType = 0;
98     struct PropertyAssociation rec {
99         .essential = false,
100         .propertyIndex = 0,
101     };
102     std::vector<PropertyAssociation> proPerty;
103     proPerty.push_back(rec);
104     struct PropertyEntry ref {
105         .itemId = 0,
106         .associations = proPerty,
107     };
108     heifIpmaBox->entries_.push_back(ref);
109     EXPECT_EQ(heifIpcoBox->GetProperty(itemId, heifIpmaBox, boxType), nullptr);
110     itemId = 0;
111     EXPECT_EQ(heifIpcoBox->GetProperty(itemId, heifIpmaBox, boxType), nullptr);
112     heifIpmaBox->entries_.clear();
113     rec.propertyIndex = 1;
114     proPerty.push_back(rec);
115     ref.associations = proPerty;
116     heifIpmaBox->entries_.push_back(ref);
117     std::shared_ptr<HeifBox> heifBox = std::make_shared<HeifBox>(0);
118     heifIpcoBox->children_.push_back(heifBox);
119     heifIpcoBox->GetProperty(itemId, heifIpmaBox, boxType);
120     boxType = 1;
121     EXPECT_EQ(heifIpcoBox->GetProperty(itemId, heifIpmaBox, boxType), nullptr);
122     GTEST_LOG_(INFO) << "HeifParserBoxTest: GetPropertyTest001 end";
123 }
124 
125 /**
126  * @tc.name: GetPropertiesTest001
127  * @tc.desc: HeifIpcoBox
128  * @tc.type: FUNC
129  */
130 HWTEST_F(HeifParserBoxTest, GetPropertiesTest001, TestSize.Level3)
131 {
132     GTEST_LOG_(INFO) << "HeifParserBoxTest: GetPropertiesTest001 start";
133     auto heifIpcoBox = std::make_shared<HeifIpcoBox>();
134     uint32_t itemId = 1;
135     std::shared_ptr<HeifIpmaBox> ipma = std::make_shared<HeifIpmaBox>();
136     std::vector<std::shared_ptr<HeifBox>> outProperties;
137     struct PropertyAssociation rec {
138         .essential = false,
139         .propertyIndex = 16,
140     };
141     std::vector<PropertyAssociation> proPerty;
142     proPerty.push_back(rec);
143     struct PropertyEntry ref {
144         .itemId = 0,
145         .associations = proPerty,
146     };
147     ipma->entries_.push_back(ref);
148     EXPECT_EQ(heifIpcoBox->GetProperties(itemId, ipma, outProperties), heif_error_property_not_found);
149     itemId = 0;
150     EXPECT_EQ(heifIpcoBox->GetProperties(itemId, ipma, outProperties), heif_error_invalid_property_index);
151     GTEST_LOG_(INFO) << "HeifParserBoxTest: GetPropertiesTest001 end";
152 }
153 
154 /**
155  * @tc.name: GetPropertiesTest002
156  * @tc.desc: HeifIpmaBox
157  * @tc.type: FUNC
158  */
159 HWTEST_F(HeifParserBoxTest, GetPropertiesTest002, TestSize.Level3)
160 {
161     GTEST_LOG_(INFO) << "HeifParserBoxTest: GetPropertiesTest002 start";
162     auto heifIpmaBox = std::make_shared<HeifIpmaBox>();
163     uint32_t itemId = 1;
164     struct PropertyEntry ref {.itemId = 0};
165     heifIpmaBox->entries_.push_back(ref);
166     EXPECT_EQ(heifIpmaBox->GetProperties(itemId), nullptr);
167     GTEST_LOG_(INFO) << "HeifParserBoxTest: GetPropertiesTest002 end";
168 }
169 
170 /**
171  * @tc.name: InferFullBoxVersionTest001
172  * @tc.desc: HeifIpmaBox
173  * @tc.type: FUNC
174  */
175 HWTEST_F(HeifParserBoxTest, InferFullBoxVersionTest001, TestSize.Level3)
176 {
177     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferFullBoxVersionTest001 start";
178     auto heifIpmaBox = std::make_shared<HeifIpmaBox>();
179     struct PropertyEntry ref {.itemId = 0xFFFFFFFF};
180     heifIpmaBox->entries_.push_back(ref);
181     heifIpmaBox->InferFullBoxVersion();
182     heifIpmaBox->entries_.clear();
183     struct PropertyAssociation rec {
184         .essential = false,
185         .propertyIndex = 0x8F,
186     };
187     std::vector<PropertyAssociation> proPerty;
188     proPerty.push_back(rec);
189     ref.itemId = 0xFF00;
190     ref.associations = proPerty;
191     heifIpmaBox->entries_.push_back(ref);
192     heifIpmaBox->InferFullBoxVersion();
193     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferFullBoxVersionTest001 end";
194 }
195 
196 /**
197  * @tc.name: ParseItemRefTest001
198  * @tc.desc: HeifIrefBox
199  * @tc.type: FUNC
200  */
201 HWTEST_F(HeifParserBoxTest, ParseItemRefTest001, TestSize.Level3)
202 {
203     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseItemRefTest001 start";
204     auto heifIrefBox = std::make_shared<HeifIrefBox>();
205     auto inputStream = std::make_shared<HeifBufferInputStream>(nullptr, 0, true);
206     HeifStreamReader reader(inputStream, 0, 0);
207     HeifIrefBox::Reference ref;
208     heifIrefBox->version_ = 1;
209     heifIrefBox->ParseItemRef(reader, ref);
210     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseItemRefTest001 end";
211 }
212 
213 /**
214  * @tc.name: InferFullBoxVersionTest002
215  * @tc.desc: HeifIrefBox
216  * @tc.type: FUNC
217  */
218 HWTEST_F(HeifParserBoxTest, InferFullBoxVersionTest002, TestSize.Level3)
219 {
220     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferFullBoxVersionTest002 start";
221     auto heifIrefBox = std::make_shared<HeifIrefBox>();
222     struct HeifIrefBox::Reference ref {.fromItemId = 0xFFFFFFFF};
223     heifIrefBox->references_.push_back(ref);
224     EXPECT_TRUE((0xFFFFFFFF >> TWO_BYTES_SHIFT) > 0);
225     heifIrefBox->InferFullBoxVersion();
226     heifIrefBox->references_.clear();
227     ref.fromItemId = 0x00000000;
228     ref.toItemIds.push_back(0xFFFFFFFF);
229     heifIrefBox->references_.push_back(ref);
230     EXPECT_TRUE((0x00000000 >> TWO_BYTES_SHIFT) == 0);
231     heifIrefBox->InferFullBoxVersion();
232     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferFullBoxVersionTest002 end";
233 }
234 
235 /**
236  * @tc.name: HasReferencesTest001
237  * @tc.desc: HeifIrefBox
238  * @tc.type: FUNC
239  */
240 HWTEST_F(HeifParserBoxTest, HasReferencesTest001, TestSize.Level3)
241 {
242     GTEST_LOG_(INFO) << "HeifParserBoxTest: HasReferencesTest001 start";
243     auto heifIrefBox = std::make_shared<HeifIrefBox>();
244     heif_item_id itemId = 0;
245     struct HeifIrefBox::Reference ref {.fromItemId = 0};
246     heifIrefBox->references_.push_back(ref);
247     EXPECT_EQ(heifIrefBox->HasReferences(itemId), true);
248     heifIrefBox->references_.clear();
249     ref.fromItemId = 1;
250     heifIrefBox->references_.push_back(ref);
251     EXPECT_EQ(heifIrefBox->HasReferences(itemId), false);
252     GTEST_LOG_(INFO) << "HeifParserBoxTest: HasReferencesTest001 end";
253 }
254 
255 /**
256  * @tc.name: ParseContentTest002
257  * @tc.desc: HeifIinfBox
258  * @tc.type: FUNC
259  */
260 HWTEST_F(HeifParserBoxTest, ParseContentTest002, TestSize.Level3)
261 {
262     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseContentTest002 start";
263     HeifIinfBox heifIinfBox;
264     HeifInfeBox heifInfeBox;
265     auto stream = std::make_shared<HeifBufferInputStream>(nullptr, 0, true);
266     HeifStreamReader reader(stream, 0, 0);
267     reader.hasError_ = false;
268     ASSERT_EQ(heifIinfBox.ParseContent(reader), heif_error_ok);
269     heifInfeBox.version_ = 0;
270     heifInfeBox.ParseContent(reader);
271     heifInfeBox.version_ = 3;
272     heifInfeBox.ParseContent(reader);
273     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseContentTest002 end";
274 }
275 
276 /**
277  * @tc.name: InferFullBoxVersionTest003
278  * @tc.desc: HeifInfeBox
279  * @tc.type: FUNC
280  */
281 HWTEST_F(HeifParserBoxTest, InferFullBoxVersionTest003, TestSize.Level3)
282 {
283     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferFullBoxVersionTest003 start";
284     HeifInfeBox heifInfeBox;
285     heifInfeBox.isHidden_ = false;
286     heifInfeBox.itemType_ = "";
287     ASSERT_EQ(heifInfeBox.itemType_.empty(), true);
288     heifInfeBox.itemId_ = 0xFFFFFFFF;
289     heifInfeBox.InferFullBoxVersion();
290     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferFullBoxVersionTest003 end";
291 }
292 
293 /**
294  * @tc.name: WriteTest003
295  * @tc.desc: HeifInfeBox
296  * @tc.type: FUNC
297  */
298 HWTEST_F(HeifParserBoxTest, WriteTest003, TestSize.Level3)
299 {
300     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest003 start";
301     HeifInfeBox heifInfeBox;
302     HeifStreamWriter write;
303     heifInfeBox.version_ = 0;
304     ASSERT_EQ(heifInfeBox.Write(write), heif_error_ok);
305     heifInfeBox.version_ = HEIF_BOX_VERSION_THREE;
306     heifInfeBox.itemType_ = "";
307     ASSERT_EQ(heifInfeBox.itemType_.empty(), true);
308     ASSERT_EQ(heifInfeBox.Write(write), heif_error_ok);
309     heifInfeBox.itemType_ = "uri";
310     ASSERT_EQ(heifInfeBox.Write(write), heif_error_ok);
311     HeifPtimBox heifPtimBox;
312     heifPtimBox.version_ = HEIF_BOX_VERSION_ONE;
313     ASSERT_EQ(heifPtimBox.Write(write), heif_error_ok);
314     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest003 end";
315 }
316 
317 /**
318  * @tc.name: InferHeaderSizeTest001
319  * @tc.desc: HeifBox
320  * @tc.type: FUNC
321  */
322 HWTEST_F(HeifParserBoxTest, InferHeaderSizeTest001, TestSize.Level3)
323 {
324     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferHeaderSizeTest001 start";
325     HeifBox heifBox;
326     heifBox. boxType_ = BOX_TYPE_UUID;
327     ASSERT_EQ(heifBox.InferHeaderSize(), 24);
328     GTEST_LOG_(INFO) << "HeifParserBoxTest: InferHeaderSizeTest001 end";
329 }
330 
331 /**
332  * @tc.name: AppendNalDataTest001
333  * @tc.desc: HeifHvccBox
334  * @tc.type: FUNC
335  */
336 HWTEST_F(HeifParserBoxTest, AppendNalDataTest001, TestSize.Level3)
337 {
338     GTEST_LOG_(INFO) << "HeifParserBoxTest: AppendNalDataTest001 start";
339     HeifHvccBox heifHvccBox;
340     std::vector<uint8_t> nalData = {0x01, 0x02, 0x03, 0x04};
341     heifHvccBox.AppendNalData(nalData);
342     GTEST_LOG_(INFO) << "HeifParserBoxTest: AppendNalDataTest001 end";
343 }
344 
345 /**
346  * @tc.name: ParseExtentsTest001
347  * @tc.desc: HeifIlocBox
348  * @tc.type: FUNC
349  */
350 HWTEST_F(HeifParserBoxTest, ParseExtentsTest001, TestSize.Level3)
351 {
352     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseExtentsTest001 start";
353     HeifIlocBox heifIlocBox;
354     HeifIlocBox::Item item;
355     auto stream = std::make_shared<HeifBufferInputStream>(nullptr, 0, true);
356     HeifStreamReader reader(stream, 0, 0);
357     heifIlocBox.version_ = HEIF_BOX_VERSION_ONE;
358     heifIlocBox.ParseExtents(item, reader, 4, 4, 4);
359     heifIlocBox.ParseExtents(item, reader, 8, 8, 8);
360     GTEST_LOG_(INFO) << "HeifParserBoxTest: ParseExtentsTest001 end";
361 }
362 
363 /**
364  * @tc.name: AppendDataTest001
365  * @tc.desc: HeifIlocBox
366  * @tc.type: FUNC
367  */
368 HWTEST_F(HeifParserBoxTest, AppendDataTest001, TestSize.Level3)
369 {
370     GTEST_LOG_(INFO) << "HeifParserBoxTest: AppendDataTest001 start";
371     HeifIlocBox heifIlocBox;
372     heif_item_id itemId = 0;
373     std::vector<uint8_t> data;
374     uint8_t constructionMethod = 1;
375     ASSERT_EQ(heifIlocBox.UpdateData(itemId, data, constructionMethod), heif_invalid_exif_data);
376     ASSERT_EQ(heifIlocBox.items_.size(), 0);
377     constructionMethod = 0;
378     ASSERT_EQ(heifIlocBox.UpdateData(itemId, data, constructionMethod), heif_error_item_not_found);
379     heifIlocBox.items_.resize(2);
380     ASSERT_EQ(heifIlocBox.items_.size(), 2);
381     ASSERT_EQ(heifIlocBox.UpdateData(itemId, data, constructionMethod), heif_error_ok);
382     GTEST_LOG_(INFO) << "HeifParserBoxTest: AppendDataTest001 end";
383 }
384 
385 /**
386  * @tc.name: ReadToExtentDataTest001
387  * @tc.desc: HeifIlocBox
388  * @tc.type: FUNC
389  */
390 HWTEST_F(HeifParserBoxTest, ReadToExtentDataTest001, TestSize.Level3)
391 {
392     GTEST_LOG_(INFO) << "HeifParserBoxTest: ReadToExtentDataTest001 start";
393     HeifIlocBox heifIlocBox;
394     HeifIlocBox::Item item;
395     struct HeifIlocBox::Extent ref {.offset = 1};
396     item.extents.push_back(ref);
397     item.baseOffset = 1;
398     std::shared_ptr<HeifBufferInputStream> stream = std::make_shared<HeifBufferInputStream>(nullptr, 0, true);
399     stream->length_ = 0;
400     std::shared_ptr<HeifIdatBox> idatBox = std::make_shared<HeifIdatBox>();
401     item.constructionMethod = 0;
402     ASSERT_EQ(heifIlocBox.ReadToExtentData(item, stream, idatBox), heif_error_eof);
403     GTEST_LOG_(INFO) << "HeifParserBoxTest: ReadToExtentDataTest001 end";
404 }
405 
406 /**
407  * @tc.name: WriteTest004
408  * @tc.desc: HeifIdatBox
409  * @tc.type: FUNC
410  */
411 HWTEST_F(HeifParserBoxTest, WriteTest004, TestSize.Level3)
412 {
413     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest004 start";
414     HeifIdatBox heifIdatBox;
415     HeifStreamWriter writer;
416     heifIdatBox.dataForWriting_ = {0xff};
417     ASSERT_EQ(heifIdatBox.dataForWriting_.empty(), false);
418     ASSERT_EQ(heifIdatBox.Write(writer), heif_error_ok);
419     GTEST_LOG_(INFO) << "HeifParserBoxTest: WriteTest004 end";
420 }
421 
422 /**
423  * @tc.name: ReadDataTest001
424  * @tc.desc: HeifIdatBox
425  * @tc.type: FUNC
426  */
427 HWTEST_F(HeifParserBoxTest, ReadDataTest001, TestSize.Level3)
428 {
429     GTEST_LOG_(INFO) << "HeifParserBoxTest: ReadDataTest001 start";
430     HeifIdatBox heifIdatBox;
431     std::shared_ptr<HeifInputStream> stream;
432     std::vector<uint8_t> outData;
433     heifIdatBox.startPos_ = 1;
434     ASSERT_EQ(heifIdatBox.ReadData(stream, 16, 0, outData), heif_error_eof);
435     ASSERT_EQ(heifIdatBox.ReadData(stream, 0, 16, outData), heif_error_eof);
436     GTEST_LOG_(INFO) << "HeifParserBoxTest: ReadDataTest001 end";
437 }
438 }
439 }