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
32using namespace android;
33
34enum {
35    BAD_TAG_ARRAY = 0xDEADBEEFu,
36    BAD_TAG = 0x8DEADBADu,
37};
38
39#define ARRAY_SIZE(a)      (sizeof(a) / sizeof((a)[0]))
40
41static 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
54extern "C" {
55
56static int zero_get_tag_count(const vendor_tag_ops_t*) {
57    return 0;
58}
59
60static int default_get_tag_count(const vendor_tag_ops_t*) {
61    return VENDOR_TAG_COUNT_ERR;
62}
63
64static void default_get_all_tags(const vendor_tag_ops_t*, uint32_t*) {
65    //Noop
66}
67
68static const char* default_get_section_name(const vendor_tag_ops_t*, uint32_t) {
69    return VENDOR_SECTION_NAME_ERR;
70}
71
72static const char* default_get_tag_name(const vendor_tag_ops_t*, uint32_t) {
73    return VENDOR_TAG_NAME_ERR;
74}
75
76static 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
83static 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 */
96TEST(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 */
132TEST(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    ASSERT_EQ(OK, vDescParceled->readFromParcel(&p));
146
147    // Ensure consistent tag count
148    int tagCount = vDescOriginal->getTagCount();
149    ASSERT_EQ(tagCount, vDescParceled->getTagCount());
150
151    uint32_t descTagArray[tagCount];
152    uint32_t desc2TagArray[tagCount];
153
154    // Get all tag ids
155    vDescOriginal->getTagArray(descTagArray);
156    vDescParceled->getTagArray(desc2TagArray);
157
158    ASSERT_NOT_NULL(descTagArray);
159    ASSERT_NOT_NULL(desc2TagArray);
160
161    uint32_t tag;
162    for (int i = 0; i < tagCount; ++i) {
163        // For each tag id, check consistency between the two vendor tag
164        // descriptors for each type, section name, tag name
165        tag = descTagArray[i];
166        EXPECT_CONTAINS_TAG(tag, desc2TagArray);
167        EXPECT_EQ(vDescOriginal->getTagType(tag), vDescParceled->getTagType(tag));
168        EXPECT_STREQ(vDescOriginal->getSectionName(tag), vDescParceled->getSectionName(tag));
169        EXPECT_STREQ(vDescOriginal->getTagName(tag), vDescParceled->getTagName(tag));
170    }
171}
172
173/**
174 * Test defaults and error conditions.
175 */
176TEST(VendorTagDescriptorTest, ErrorConditions) {
177    sp<VendorTagDescriptor> vDesc;
178    vendor_tag_ops_t vOps;
179    FillWithDefaults(&vOps);
180
181    // Make empty tag count
182    vOps.get_tag_count = zero_get_tag_count;
183
184    // Ensure create fails when using null vOps
185    EXPECT_EQ(BAD_VALUE, VendorTagDescriptor::createDescriptorFromOps(/*vOps*/NULL, vDesc));
186
187    // Ensure creat succeeds for empty vendor tag ops
188    ASSERT_EQ(OK, VendorTagDescriptor::createDescriptorFromOps(&vOps, vDesc));
189
190    // Ensure defaults are returned when no vtags are defined, or tag is unknown
191    EXPECT_EQ(VENDOR_TAG_COUNT_ERR, vDesc->getTagCount());
192    uint32_t* tagArray = reinterpret_cast<uint32_t*>(BAD_TAG_ARRAY);
193    uint32_t* testArray = tagArray;
194    vDesc->getTagArray(tagArray);
195    EXPECT_EQ(testArray, tagArray);
196    EXPECT_EQ(VENDOR_SECTION_NAME_ERR, vDesc->getSectionName(BAD_TAG));
197    EXPECT_EQ(VENDOR_TAG_NAME_ERR, vDesc->getTagName(BAD_TAG));
198    EXPECT_EQ(VENDOR_TAG_TYPE_ERR, vDesc->getTagType(BAD_TAG));
199
200    // Make sure global can be set/cleared
201    sp<VendorTagDescriptor> prevGlobal = VendorTagDescriptor::getGlobalVendorTagDescriptor();
202    VendorTagDescriptor::clearGlobalVendorTagDescriptor();
203
204    EXPECT_TRUE(VendorTagDescriptor::getGlobalVendorTagDescriptor() == NULL);
205    EXPECT_EQ(OK, VendorTagDescriptor::setAsGlobalVendorTagDescriptor(vDesc));
206    EXPECT_TRUE(VendorTagDescriptor::getGlobalVendorTagDescriptor() != NULL);
207    EXPECT_EQ(VENDOR_SECTION_NAME_ERR, vDesc->getSectionName(BAD_TAG));
208    EXPECT_EQ(OK, VendorTagDescriptor::setAsGlobalVendorTagDescriptor(prevGlobal));
209    EXPECT_EQ(prevGlobal, VendorTagDescriptor::getGlobalVendorTagDescriptor());
210}
211