/* * Copyright 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define __C2_GENERATE_GLOBAL_VARS__ #include #include #include #include #include #include namespace android { namespace { enum { kParamIndexTestStart = 0x1000, kParamIndexInt, kParamIndexString, kParamIndexComposite, kParamIndexFlexString, kParamIndexLong = C2Param::TYPE_INDEX_VENDOR_START, }; typedef C2GlobalParam C2IntInfo; typedef C2GlobalParam C2LongInfo; struct C2FixedSizeStringStruct { char value[12]; DEFINE_AND_DESCRIBE_BASE_C2STRUCT(FixedSizeString) C2FIELD(value, "value") }; typedef C2GlobalParam C2StringInfo; struct C2CompositeStruct { int32_t i32; uint64_t u64; char str[12]; uint8_t blob[8]; uint8_t flexBlob[]; C2CompositeStruct() = default; DEFINE_AND_DESCRIBE_BASE_FLEX_C2STRUCT(Composite, flexBlob) C2FIELD(i32, "i32") C2FIELD(u64, "u64") C2FIELD(str, "str") C2FIELD(blob, "blob") C2FIELD(flexBlob, "flex-blob") }; static_assert(C2CompositeStruct::FLEX_SIZE == 1, ""); static_assert(_C2FlexHelper::FLEX_SIZE == 1, ""); typedef C2GlobalParam C2CompositeInfo; typedef C2GlobalParam C2FlexStringInfo; #define SUPPORTED_TYPES \ C2IntInfo, \ C2LongInfo, \ C2StringInfo, \ C2CompositeInfo, \ C2FlexStringInfo template struct describe_impl; template struct describe_impl { static std::unique_ptr describe(C2Param::CoreIndex index) { if (index == T::CORE_INDEX) { return std::make_unique(T::CORE_INDEX, T::FieldList()); } else { return describe_impl::describe(index); } } }; template<> struct describe_impl<> { static std::unique_ptr describe(C2Param::CoreIndex) { return nullptr; } }; template const char *GetName() { return nullptr; } template<> const char *GetName() { return "int"; } template<> const char *GetName() { return "long"; } template<> const char *GetName() { return "string"; } template<> const char *GetName() { return "composite"; } template<> const char *GetName() { return "flex-string"; } template struct fill_descriptors_impl; template struct fill_descriptors_impl { static void fill(std::vector> *vec) { fill_descriptors_impl::fill(vec); vec->push_back(std::make_shared( T::PARAM_TYPE, C2ParamDescriptor::IS_PERSISTENT, GetName())); } }; template<> struct fill_descriptors_impl<> { static void fill(std::vector> *) {} }; template T *CastParam(const std::unique_ptr ¶m) { return (T *)param.get(); } class ParamReflector : public C2ParamReflector { public: ParamReflector() = default; ~ParamReflector() override = default; std::unique_ptr describe(C2Param::CoreIndex paramIndex) const override { return describe_impl::describe(paramIndex); } }; } // namespace class ReflectedParamUpdaterTest : public ::testing::Test { public: ReflectedParamUpdaterTest() : mReflector(new ParamReflector) { fill_descriptors_impl::fill(&mDescriptors); } std::shared_ptr mReflector; std::vector> mDescriptors; }; TEST_F(ReflectedParamUpdaterTest, SingleValueTest) { ReflectedParamUpdater updater; ReflectedParamUpdater::Dict msg; msg.emplace("int.value", int32_t(12)); msg.emplace("vendor.long.value", int64_t(34)); updater.addParamDesc(mReflector, mDescriptors); std::vector indices; updater.getParamIndicesFromMessage(msg, &indices); EXPECT_EQ(1, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2IntInfo::PARAM_TYPE; })); EXPECT_EQ(1, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2LongInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2StringInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2CompositeInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2FlexStringInfo::PARAM_TYPE; })); std::vector> params; params.emplace_back(new C2IntInfo); params.emplace_back(new C2LongInfo); EXPECT_EQ(0, CastParam(params[0])->value); EXPECT_EQ(0, CastParam(params[1])->value); updater.updateParamsFromMessage(msg, ¶ms); EXPECT_EQ(12, CastParam(params[0])->value); EXPECT_EQ(34, CastParam(params[1])->value); C2Value c2Value; int32_t int32Value = 0; int64_t int64Value = 0; msg = updater.getParams(params); ASSERT_EQ(1u, msg.count("int.value")); EXPECT_EQ(true, msg["int.value"].find(&c2Value)); EXPECT_EQ(true, c2Value.get(&int32Value)); EXPECT_EQ(12, int32Value); ASSERT_EQ(1u, msg.count("vendor.long.value")); EXPECT_EQ(true, msg["vendor.long.value"].find(&c2Value)); EXPECT_EQ(true, c2Value.get(&int64Value)); EXPECT_EQ(34, int64Value); } TEST_F(ReflectedParamUpdaterTest, StringTest) { ReflectedParamUpdater updater; ReflectedParamUpdater::Dict msg; msg.emplace("string.value", AString("56")); msg.emplace("flex-string.value", AString("Some string")); updater.addParamDesc(mReflector, mDescriptors); std::vector indices; updater.getParamIndicesFromMessage(msg, &indices); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2IntInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2LongInfo::PARAM_TYPE; })); EXPECT_EQ(1, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2StringInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2CompositeInfo::PARAM_TYPE; })); EXPECT_EQ(1, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2FlexStringInfo::PARAM_TYPE; })); std::vector> params; params.emplace_back(new C2StringInfo); EXPECT_EQ(0, CastParam(params[0])->value[0]); params.emplace_back(C2FlexStringInfo::AllocUnique(0)); EXPECT_EQ(0u, CastParam(params[1])->flexCount()); char *flexStringData = &CastParam(params[1])->m.value[0]; updater.updateParamsFromMessage(msg, ¶ms); EXPECT_STREQ("56", CastParam(params[0])->value); EXPECT_EQ(12u, CastParam(params[0])->flexCount()); EXPECT_STREQ("Some string", CastParam(params[1])->m.value); EXPECT_NE(flexStringData, &CastParam(params[1])->m.value[0]); flexStringData = &CastParam(params[1])->m.value[0]; // verify truncation and in-place update msg["string.value"] = ReflectedParamUpdater::Value(AString("1234567890ABCDE")); msg["flex-string.value"] = ReflectedParamUpdater::Value(AString("abc")); updater.updateParamsFromMessage(msg, ¶ms); EXPECT_STREQ("1234567890A", CastParam(params[0])->value); EXPECT_EQ(4u, CastParam(params[1])->flexCount()); EXPECT_STREQ("abc", CastParam(params[1])->m.value); EXPECT_EQ(flexStringData, &CastParam(params[1])->m.value[0]); AString strValue; msg = updater.getParams(params); ASSERT_EQ(1u, msg.count("string.value")); EXPECT_EQ(true, msg["string.value"].find(&strValue)); EXPECT_STREQ("1234567890A", strValue.c_str()); ASSERT_EQ(1u, msg.count("flex-string.value")); EXPECT_EQ(true, msg["flex-string.value"].find(&strValue)); EXPECT_STREQ("abc", strValue.c_str()); } TEST_F(ReflectedParamUpdaterTest, CompositeTest) { ReflectedParamUpdater updater; ReflectedParamUpdater::Dict msg; msg.emplace("composite.i32", int32_t(78)); msg.emplace("composite.u64", int64_t(910)); msg.emplace("composite.str", AString("1112")); msg.emplace("composite.blob", ABuffer::CreateAsCopy("buffer08", 8)); msg.emplace("composite.flex-blob", ABuffer::CreateAsCopy("flex-buffer-14", 14)); updater.addParamDesc(mReflector, mDescriptors); std::vector indices; updater.getParamIndicesFromMessage(msg, &indices); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2IntInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2LongInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2StringInfo::PARAM_TYPE; })); EXPECT_EQ(1, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2CompositeInfo::PARAM_TYPE; })); std::vector> params; params.emplace_back(C2CompositeInfo::AllocUnique(0)); EXPECT_EQ(0, CastParam(params[0])->m.i32); EXPECT_EQ(0u, CastParam(params[0])->m.u64); EXPECT_EQ(0, CastParam(params[0])->m.str[0]); EXPECT_EQ(0, memcmp("\0\0\0\0\0\0\0\0", CastParam(params[0])->m.blob, 8)); EXPECT_EQ(0u, CastParam(params[0])->flexCount()); uint8_t *flexBlobData = &CastParam(params[0])->m.flexBlob[0]; updater.updateParamsFromMessage(msg, ¶ms); EXPECT_EQ(78, CastParam(params[0])->m.i32); EXPECT_EQ(910u, CastParam(params[0])->m.u64); EXPECT_STREQ("1112", CastParam(params[0])->m.str); EXPECT_EQ(0, memcmp("buffer08", CastParam(params[0])->m.blob, 8)); AString hex; hexdump(CastParam(params[0])->m.blob, 8, 0, &hex); printf("%s\n", hex.c_str()); ASSERT_EQ(14u, CastParam(params[0])->flexCount()); EXPECT_EQ(0, memcmp("flex-buffer-14", CastParam(params[0])->m.flexBlob, 14)); EXPECT_NE(flexBlobData, &CastParam(params[0])->m.flexBlob[0]); flexBlobData = &CastParam(params[0])->m.flexBlob[0]; // test setting and zero extending shorter blob than allowed msg.clear(); msg.emplace("composite.blob", ABuffer::CreateAsCopy("buf05", 5)); updater.updateParamsFromMessage(msg, ¶ms); EXPECT_EQ(0, memcmp("buf05\0\0\0", CastParam(params[0])->m.blob, 8)); ASSERT_EQ(14u, CastParam(params[0])->flexCount()); EXPECT_EQ(0, memcmp("flex-buffer-14", CastParam(params[0])->m.flexBlob, 14)); EXPECT_EQ(flexBlobData, &CastParam(params[0])->m.flexBlob[0]); // test setting and trimming larger blob than allowed msg.clear(); msg.emplace("composite.blob", ABuffer::CreateAsCopy("ReallyLongBuffer", 16)); updater.updateParamsFromMessage(msg, ¶ms); EXPECT_EQ(0, memcmp("ReallyLo", CastParam(params[0])->m.blob, 8)); ASSERT_EQ(14u, CastParam(params[0])->flexCount()); EXPECT_EQ(0, memcmp("flex-buffer-14", CastParam(params[0])->m.flexBlob, 14)); EXPECT_EQ(flexBlobData, &CastParam(params[0])->m.flexBlob[0]); // test trimming flex blob in-place msg.clear(); msg.emplace("composite.flex-blob", ABuffer::CreateAsCopy("buf05", 5)); updater.updateParamsFromMessage(msg, ¶ms); ASSERT_EQ(5u, CastParam(params[0])->flexCount()); EXPECT_EQ(0, memcmp("buf05", CastParam(params[0])->m.flexBlob, 5)); EXPECT_EQ(flexBlobData, &CastParam(params[0])->m.flexBlob[0]); } TEST_F(ReflectedParamUpdaterTest, CompositePartialTest) { ReflectedParamUpdater updater; ReflectedParamUpdater::Dict msg; msg.emplace("composite.i32", C2Value(1314)); msg.emplace("composite.str", AString("1516")); updater.addParamDesc(mReflector, mDescriptors); std::vector indices; updater.getParamIndicesFromMessage(msg, &indices); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2IntInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2LongInfo::PARAM_TYPE; })); EXPECT_EQ(0, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2StringInfo::PARAM_TYPE; })); EXPECT_EQ(1, std::count_if(indices.begin(), indices.end(), [](const auto &value) { return (uint32_t)value == C2CompositeInfo::PARAM_TYPE; })); std::vector> params; params.emplace_back(C2CompositeInfo::AllocUnique(12u)); EXPECT_EQ(0, CastParam(params[0])->m.i32); EXPECT_EQ(0u, CastParam(params[0])->m.u64); EXPECT_EQ(0, CastParam(params[0])->m.str[0]); updater.updateParamsFromMessage(msg, ¶ms); EXPECT_EQ(1314, CastParam(params[0])->m.i32); EXPECT_EQ(0u, CastParam(params[0])->m.u64); EXPECT_STREQ("1516", CastParam(params[0])->m.str); } } // namespace android