1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_NDEBUG 0
18 #define LOG_TAG "VendorTagDescriptorTests"
19
20 #include <binder/Parcel.h>
21 #include <camera/VendorTagDescriptor.h>
22 #include <camera_metadata_tests_fake_vendor.h>
23 #include <camera_metadata_hidden.h>
24 #include <system/camera_vendor_tags.h>
25 #include <utils/Errors.h>
26 #include <utils/Log.h>
27 #include <utils/RefBase.h>
28
29 #include <gtest/gtest.h>
30 #include <stdint.h>
31
32 using namespace android;
33
34 enum {
35 BAD_TAG_ARRAY = 0xDEADBEEFu,
36 BAD_TAG = 0x8DEADBADu,
37 };
38
39 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
40
ContainsTag(uint32_t * tagArray,size_t size,uint32_t tag)41 static bool ContainsTag(uint32_t* tagArray, size_t size, uint32_t tag) {
42 for (size_t i = 0; i < size; ++i) {
43 if (tag == tagArray[i]) return true;
44 }
45 return false;
46 }
47
48 #define EXPECT_CONTAINS_TAG(t, a) \
49 EXPECT_TRUE(ContainsTag(a, ARRAY_SIZE(a), t))
50
51 #define ASSERT_NOT_NULL(x) \
52 ASSERT_TRUE((x) != NULL)
53
54 extern "C" {
55
zero_get_tag_count(const vendor_tag_ops_t *)56 static int zero_get_tag_count(const vendor_tag_ops_t*) {
57 return 0;
58 }
59
default_get_tag_count(const vendor_tag_ops_t *)60 static int default_get_tag_count(const vendor_tag_ops_t*) {
61 return VENDOR_TAG_COUNT_ERR;
62 }
63
default_get_all_tags(const vendor_tag_ops_t *,uint32_t *)64 static void default_get_all_tags(const vendor_tag_ops_t*, uint32_t*) {
65 //Noop
66 }
67
default_get_section_name(const vendor_tag_ops_t *,uint32_t)68 static const char* default_get_section_name(const vendor_tag_ops_t*, uint32_t) {
69 return VENDOR_SECTION_NAME_ERR;
70 }
71
default_get_tag_name(const vendor_tag_ops_t *,uint32_t)72 static const char* default_get_tag_name(const vendor_tag_ops_t*, uint32_t) {
73 return VENDOR_TAG_NAME_ERR;
74 }
75
default_get_tag_type(const vendor_tag_ops_t *,uint32_t)76 static int default_get_tag_type(const vendor_tag_ops_t*, uint32_t) {
77 return VENDOR_TAG_TYPE_ERR;
78 }
79
80 } /*extern "C"*/
81
82 // Set default vendor operations for a vendor_tag_ops struct
FillWithDefaults(vendor_tag_ops_t * vOps)83 static void FillWithDefaults(vendor_tag_ops_t* vOps) {
84 ASSERT_NOT_NULL(vOps);
85 vOps->get_tag_count = default_get_tag_count;
86 vOps->get_all_tags = default_get_all_tags;
87 vOps->get_section_name = default_get_section_name;
88 vOps->get_tag_name = default_get_tag_name;
89 vOps->get_tag_type = default_get_tag_type;
90 }
91
92 /**
93 * Test if values from VendorTagDescriptor methods match corresponding values
94 * from vendor_tag_ops functions.
95 */
TEST(VendorTagDescriptorTest,ConsistentWithVendorTags)96 TEST(VendorTagDescriptorTest, ConsistentWithVendorTags) {
97 sp<VendorTagDescriptor> vDesc;
98 const vendor_tag_ops_t *vOps = &fakevendor_ops;
99 EXPECT_EQ(OK, VendorTagDescriptor::createDescriptorFromOps(vOps, /*out*/vDesc));
100
101 ASSERT_NOT_NULL(vDesc);
102
103 // Ensure reasonable tag count
104 int tagCount = vDesc->getTagCount();
105 EXPECT_EQ(tagCount, vOps->get_tag_count(vOps));
106
107 uint32_t descTagArray[tagCount];
108 uint32_t opsTagArray[tagCount];
109
110 // Get all tag ids
111 vDesc->getTagArray(descTagArray);
112 vOps->get_all_tags(vOps, opsTagArray);
113
114 ASSERT_NOT_NULL(descTagArray);
115 ASSERT_NOT_NULL(opsTagArray);
116
117 uint32_t tag;
118 for (int i = 0; i < tagCount; ++i) {
119 // For each tag id, check whether type, section name, tag name match
120 tag = descTagArray[i];
121 EXPECT_CONTAINS_TAG(tag, opsTagArray);
122 EXPECT_EQ(vDesc->getTagType(tag), vOps->get_tag_type(vOps, tag));
123 EXPECT_STREQ(vDesc->getSectionName(tag), vOps->get_section_name(vOps, tag));
124 EXPECT_STREQ(vDesc->getTagName(tag), vOps->get_tag_name(vOps, tag));
125 }
126 }
127
128 /**
129 * Test if values from VendorTagDescriptor methods stay consistent after being
130 * parcelled/unparcelled.
131 */
TEST(VendorTagDescriptorTest,ConsistentAcrossParcel)132 TEST(VendorTagDescriptorTest, ConsistentAcrossParcel) {
133 sp<VendorTagDescriptor> vDescOriginal, vDescParceled;
134 const vendor_tag_ops_t *vOps = &fakevendor_ops;
135 EXPECT_EQ(OK, VendorTagDescriptor::createDescriptorFromOps(vOps, /*out*/vDescOriginal));
136
137 ASSERT_TRUE(vDescOriginal != NULL);
138
139 Parcel p;
140
141 // Check whether parcel read/write succeed
142 EXPECT_EQ(OK, vDescOriginal->writeToParcel(&p));
143 p.setDataPosition(0);
144
145 vDescParceled = new VendorTagDescriptor();
146 ASSERT_EQ(OK, vDescParceled->readFromParcel(&p));
147
148 // Ensure consistent tag count
149 int tagCount = vDescOriginal->getTagCount();
150 ASSERT_EQ(tagCount, vDescParceled->getTagCount());
151
152 uint32_t descTagArray[tagCount];
153 uint32_t desc2TagArray[tagCount];
154
155 // Get all tag ids
156 vDescOriginal->getTagArray(descTagArray);
157 vDescParceled->getTagArray(desc2TagArray);
158
159 ASSERT_NOT_NULL(descTagArray);
160 ASSERT_NOT_NULL(desc2TagArray);
161
162 uint32_t tag;
163 for (int i = 0; i < tagCount; ++i) {
164 // For each tag id, check consistency between the two vendor tag
165 // descriptors for each type, section name, tag name
166 tag = descTagArray[i];
167 EXPECT_CONTAINS_TAG(tag, desc2TagArray);
168 EXPECT_EQ(vDescOriginal->getTagType(tag), vDescParceled->getTagType(tag));
169 EXPECT_STREQ(vDescOriginal->getSectionName(tag), vDescParceled->getSectionName(tag));
170 EXPECT_STREQ(vDescOriginal->getTagName(tag), vDescParceled->getTagName(tag));
171 }
172 }
173
174 /**
175 * Test defaults and error conditions.
176 */
TEST(VendorTagDescriptorTest,ErrorConditions)177 TEST(VendorTagDescriptorTest, ErrorConditions) {
178 sp<VendorTagDescriptor> vDesc;
179 vendor_tag_ops_t vOps;
180 FillWithDefaults(&vOps);
181
182 // Make empty tag count
183 vOps.get_tag_count = zero_get_tag_count;
184
185 // Ensure create fails when using null vOps
186 EXPECT_EQ(BAD_VALUE, VendorTagDescriptor::createDescriptorFromOps(/*vOps*/NULL, vDesc));
187
188 // Ensure creat succeeds for empty vendor tag ops
189 ASSERT_EQ(OK, VendorTagDescriptor::createDescriptorFromOps(&vOps, vDesc));
190
191 // Ensure defaults are returned when no vtags are defined, or tag is unknown
192 EXPECT_EQ(VENDOR_TAG_COUNT_ERR, vDesc->getTagCount());
193 uint32_t* tagArray = reinterpret_cast<uint32_t*>(BAD_TAG_ARRAY);
194 uint32_t* testArray = tagArray;
195 vDesc->getTagArray(tagArray);
196 EXPECT_EQ(testArray, tagArray);
197 EXPECT_EQ(VENDOR_SECTION_NAME_ERR, vDesc->getSectionName(BAD_TAG));
198 EXPECT_EQ(VENDOR_TAG_NAME_ERR, vDesc->getTagName(BAD_TAG));
199 EXPECT_EQ(VENDOR_TAG_TYPE_ERR, vDesc->getTagType(BAD_TAG));
200
201 // Make sure global can be set/cleared
202 sp<VendorTagDescriptor> prevGlobal = VendorTagDescriptor::getGlobalVendorTagDescriptor();
203 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
204
205 EXPECT_TRUE(VendorTagDescriptor::getGlobalVendorTagDescriptor() == NULL);
206 EXPECT_EQ(OK, VendorTagDescriptor::setAsGlobalVendorTagDescriptor(vDesc));
207 EXPECT_TRUE(VendorTagDescriptor::getGlobalVendorTagDescriptor() != NULL);
208 EXPECT_EQ(VENDOR_SECTION_NAME_ERR, vDesc->getSectionName(BAD_TAG));
209 EXPECT_EQ(OK, VendorTagDescriptor::setAsGlobalVendorTagDescriptor(prevGlobal));
210 EXPECT_EQ(prevGlobal, VendorTagDescriptor::getGlobalVendorTagDescriptor());
211 }
212