diff --git a/framework/tools/hdi-gen/ast/ast_enum_type.h b/framework/tools/hdi-gen/ast/ast_enum_type.h index e371055b5260147b86b82e984330a7fa4066b4dc..0a37586bd06444a17b8091679432b6eb31ead104 100644 --- a/framework/tools/hdi-gen/ast/ast_enum_type.h +++ b/framework/tools/hdi-gen/ast/ast_enum_type.h @@ -110,6 +110,16 @@ public: return members_[index]; } + inline bool HasMember(std::string memberName) + { + for (size_t i = 0; i < members_.size(); i++) { + if (members_[i]->GetName() == memberName) { + return true; + } + } + return false; + } + bool IsEnumType() override; std::string Dump(const std::string &prefix) override; diff --git a/framework/tools/hdi-gen/ast/ast_expr.cpp b/framework/tools/hdi-gen/ast/ast_expr.cpp index 81da7d0b1428db4b93fe821750ec16591967c441..e5f6d806f1becebf17a70b68a91cc3992899d8ab 100644 --- a/framework/tools/hdi-gen/ast/ast_expr.cpp +++ b/framework/tools/hdi-gen/ast/ast_expr.cpp @@ -103,5 +103,22 @@ std::string ASTNumExpr::Dump(const std::string &prefix) return sb.ToString(); } + +std::string ASTEnumExpr::Dump(const std::string &prefix) +{ + StringBuilder sb; + sb.Append(prefix); + if (isParenExpr) { + sb.Append("("); + } + + sb.AppendFormat("%s", value_.c_str()); + + if (isParenExpr) { + sb.Append("("); + } + + return sb.ToString(); +} } // namespace HDI } // namespace OHOS \ No newline at end of file diff --git a/framework/tools/hdi-gen/ast/ast_expr.h b/framework/tools/hdi-gen/ast/ast_expr.h index a50e3003e4fc401fdcec74892a63e722e84c9b8d..767bab6cdd81fdbc81df7d53422e9ff369574cab 100644 --- a/framework/tools/hdi-gen/ast/ast_expr.h +++ b/framework/tools/hdi-gen/ast/ast_expr.h @@ -69,6 +69,19 @@ public: std::string Dump(const std::string &prefix) override; std::string value_; }; + +/** + * @brief Defines the enumeration object structure in expression. + * + * This structure includes the enumeration information when using enum nesting identify in idl. + * + * @since 5.0 + */ +class ASTEnumExpr : public ASTExpr { +public: + std::string Dump(const std::string &prefix) override; + std::string value_; +}; } // namespace HDI } // namespace OHOS diff --git a/framework/tools/hdi-gen/ast/ast_method.cpp b/framework/tools/hdi-gen/ast/ast_method.cpp index 0dc89a36781bd5af122ca155109aac910bba8f63..6b0e94f7ab4b15c6550375afc34a682aee4418ac 100644 --- a/framework/tools/hdi-gen/ast/ast_method.cpp +++ b/framework/tools/hdi-gen/ast/ast_method.cpp @@ -7,10 +7,29 @@ */ #include "ast/ast_method.h" + +#include "ast/ast_interface_type.h" #include "util/string_builder.h" namespace OHOS { namespace HDI { +bool ASTMethod::IsOverload(AutoPtr interface) +{ + if (interface == nullptr) { + return false; + } + interface = interface->GetExtendsInterface(); + while (interface != nullptr) { + for (const auto &method : interface->GetMethodsBySystem(Options::GetInstance().GetSystemLevel())) { + if (name_ == method->GetName()) { + return true; + } + } + interface = interface->GetExtendsInterface(); + } + return false; +} + void ASTMethod::AddParameter(const AutoPtr ¶meter) { if (parameter == nullptr) { diff --git a/framework/tools/hdi-gen/ast/ast_method.h b/framework/tools/hdi-gen/ast/ast_method.h index 89e44ce4a5e17de743dbdfb6c3b62be300421369..d7e4ab67412214df7cd5d0e9530ca08246a9e5fa 100644 --- a/framework/tools/hdi-gen/ast/ast_method.h +++ b/framework/tools/hdi-gen/ast/ast_method.h @@ -61,6 +61,8 @@ public: return attr_->HasValue(ASTAttr::MINI); } + bool IsOverload(AutoPtr interface); + void AddParameter(const AutoPtr ¶meter); AutoPtr GetParameter(size_t index); @@ -70,12 +72,23 @@ public: return parameters_.size(); } + inline void SetCmdId(size_t cmdId) + { + cmdId_ = cmdId; + } + + inline size_t GetCmdId() + { + return cmdId_; + } + std::string Dump(const std::string &prefix) override; private: std::string name_; AutoPtr attr_ = new ASTAttr(); std::vector> parameters_; + size_t cmdId_; // used to identify same name method }; } // namespace HDI } // namespace OHOS diff --git a/framework/tools/hdi-gen/ast/ast_struct_type.cpp b/framework/tools/hdi-gen/ast/ast_struct_type.cpp index ff7f14bdc01496bb737a6c3c83683d1811980764..7b8f277c31bd7ece0db49e521174a1dcb4e1e4a9 100644 --- a/framework/tools/hdi-gen/ast/ast_struct_type.cpp +++ b/framework/tools/hdi-gen/ast/ast_struct_type.cpp @@ -11,6 +11,19 @@ namespace OHOS { namespace HDI { +void ASTStructType::SetParentType(const AutoPtr &parentType) +{ + if (parentType == nullptr) { + return; + } + + std::vector>> parentMembers = parentType->GetMembers(); + for (auto member : parentMembers) { + AddMember(std::get<1>(member), std::get<0>(member)); + } + parentType_ = parentType; +} + void ASTStructType::AddMember(const AutoPtr &typeName, std::string name) { for (auto it : members_) { @@ -34,7 +47,11 @@ std::string ASTStructType::Dump(const std::string &prefix) { StringBuilder sb; sb.Append(prefix).Append(attr_->Dump(prefix)).Append(" "); - sb.AppendFormat("struct %s {\n", name_.c_str()); + if (parentType_ == nullptr) { + sb.AppendFormat("struct %s {\n", name_.c_str()); + } else { + sb.AppendFormat("struct %s : %s {\n", name_.c_str(), parentType_->ToString().c_str()); + } if (members_.size() > 0) { for (auto it : members_) { sb.Append(prefix + " "); diff --git a/framework/tools/hdi-gen/ast/ast_struct_type.h b/framework/tools/hdi-gen/ast/ast_struct_type.h index 81ad65be287894166d61f8918ebf5c6530751ddb..e2a6d80ade4bfc2b1a7ea92fce0eb920c8c523c8 100644 --- a/framework/tools/hdi-gen/ast/ast_struct_type.h +++ b/framework/tools/hdi-gen/ast/ast_struct_type.h @@ -49,8 +49,15 @@ public: return attr_ != nullptr ? attr_->HasValue(ASTAttr::LITE) : false; } + void SetParentType(const AutoPtr &parentType); + void AddMember(const AutoPtr &typeName, std::string name); + inline std::vector>> GetMembers() + { + return members_; + } + inline size_t GetMemberNumber() { return members_.size(); @@ -123,6 +130,7 @@ public: private: AutoPtr attr_; std::vector>> members_; + AutoPtr parentType_; // used to dump parent type when using struct extension identify in idl }; } // namespace HDI } // namespace OHOS diff --git a/framework/tools/hdi-gen/codegen/code_emitter.cpp b/framework/tools/hdi-gen/codegen/code_emitter.cpp index d5645eb2475f49a1833966c2d956f61af1fe49e7..2898061396091d3998de7167a0bb4eb8f6a75c62 100644 --- a/framework/tools/hdi-gen/codegen/code_emitter.cpp +++ b/framework/tools/hdi-gen/codegen/code_emitter.cpp @@ -153,7 +153,8 @@ std::string CodeEmitter::InterfaceToFilePath(const std::string &interfaceName) c std::string CodeEmitter::EmitMethodCmdID(const AutoPtr &method) { - return StringHelper::Format("CMD_%s_%s", ConstantName(baseName_).c_str(), ConstantName(method->GetName()).c_str()); + return StringHelper::Format("CMD_%s_%s_%d", + ConstantName(baseName_).c_str(), ConstantName(method->GetName()).c_str(), method->GetCmdId()); } void CodeEmitter::EmitInterfaceMethodCommands(StringBuilder &sb, const std::string &prefix) diff --git a/framework/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp b/framework/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp index 91982cd02cc9b226e8c1f3b061de49944bb01a4c..16224d00908c8b2ced32d90d6efc5a3a135296eb 100644 --- a/framework/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp +++ b/framework/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp @@ -196,6 +196,12 @@ void CppInterfaceCodeEmitter::EmitInterfaceMethodsDecl(StringBuilder &sb, const void CppInterfaceCodeEmitter::EmitInterfaceMethodDecl( const AutoPtr &method, StringBuilder &sb, const std::string &prefix) const { + if (interface_->GetExtendsInterface() != nullptr && method->IsOverload(interface_)) { + sb.Append(prefix).AppendFormat("using %s::%s;\n", + EmitDefinitionByInterface(interface_->GetExtendsInterface(), interfaceName_).c_str(), + method->GetName().c_str()); + } + if (method->GetParameterNumber() == 0) { sb.Append(prefix).AppendFormat("virtual int32_t %s() = 0;\n", method->GetName().c_str()); } else { diff --git a/framework/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp b/framework/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp index e48f11c22207d5f0ae66d0a87f0fd0f4957eaf96..2bba7d98d571576423c41b9b53b12609a83261e3 100644 --- a/framework/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp +++ b/framework/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp @@ -150,17 +150,18 @@ void CppServiceStubCodeEmitter::EmitStubMethodDecls(StringBuilder &sb, const std void CppServiceStubCodeEmitter::EmitStubMethodDecl( const AutoPtr &method, StringBuilder &sb, const std::string &prefix) const { - sb.Append(prefix).AppendFormat("int32_t %s%s(MessageParcel& %s, MessageParcel& %s, MessageOption& %s);\n", - stubName_.c_str(), method->GetName().c_str(), dataParcelName_.c_str(), replyParcelName_.c_str(), - optionName_.c_str()); + sb.Append(prefix).AppendFormat("int32_t %s%s_%d(MessageParcel& %s, MessageParcel& %s, MessageOption& %s);\n", + stubName_.c_str(), method->GetName().c_str(), method->GetCmdId(), + dataParcelName_.c_str(), replyParcelName_.c_str(), optionName_.c_str()); } void CppServiceStubCodeEmitter::EmitStubStaticMethodDecl( const AutoPtr &method, StringBuilder &sb, const std::string &prefix) const { sb.Append(prefix).AppendFormat( - "static int32_t %s%s_(MessageParcel& %s, MessageParcel& %s, MessageOption& %s, sptr<%s> impl);\n", - stubName_.c_str(), method->GetName().c_str(), dataParcelName_.c_str(), replyParcelName_.c_str(), + "static int32_t %s%s_%d_(MessageParcel& %s, MessageParcel& %s, MessageOption& %s, sptr<%s> impl);\n", + stubName_.c_str(), method->GetName().c_str(), method->GetCmdId(), + dataParcelName_.c_str(), replyParcelName_.c_str(), optionName_.c_str(), EmitDefinitionByInterface(interface_, interfaceName_).c_str()); } @@ -344,13 +345,15 @@ void CppServiceStubCodeEmitter::EmitStubOnRequestMethodImpl(StringBuilder &sb, c AutoPtr getVerMethod = interface_->GetVersionMethod(); sb.Append(prefix + TAB + TAB).AppendFormat("case %s:\n", EmitMethodCmdID(getVerMethod).c_str()); sb.Append(prefix + TAB + TAB + TAB) - .AppendFormat("return %sStub%s(data, reply, option);\n", baseName_.c_str(), getVerMethod->GetName().c_str()); + .AppendFormat("return %sStub%s_%d(data, reply, option);\n", + baseName_.c_str(), getVerMethod->GetName().c_str(), getVerMethod->GetCmdId()); AutoPtr interface = interface_; while (interface != nullptr) { for (const auto &method : interface->GetMethodsBySystem(Options::GetInstance().GetSystemLevel())) { sb.Append(prefix + TAB + TAB).AppendFormat("case %s:\n", EmitMethodCmdID(method).c_str()); sb.Append(prefix + TAB + TAB + TAB) - .AppendFormat("return %sStub%s(data, reply, option);\n", baseName_.c_str(), method->GetName().c_str()); + .AppendFormat("return %sStub%s_%d(data, reply, option);\n", + baseName_.c_str(), method->GetName().c_str(), method->GetCmdId()); } interface = interface->GetExtendsInterface(); } @@ -393,13 +396,14 @@ void CppServiceStubCodeEmitter::EmitStubMethodImpls(StringBuilder &sb, const std void CppServiceStubCodeEmitter::EmitStubMethodImpl(AutoPtr interface, const AutoPtr &method, StringBuilder &sb, const std::string &prefix) const { - sb.Append(prefix).AppendFormat("int32_t %s::%s%s(MessageParcel& %s, MessageParcel& %s, MessageOption& %s)\n", - EmitDefinitionByInterface(interface_, stubName_).c_str(), stubName_.c_str(), method->GetName().c_str(), + sb.Append(prefix).AppendFormat("int32_t %s::%s%s_%d(MessageParcel& %s, MessageParcel& %s, MessageOption& %s)\n", + EmitDefinitionByInterface(interface_, stubName_).c_str(), + stubName_.c_str(), method->GetName().c_str(), method->GetCmdId(), dataParcelName_.c_str(), replyParcelName_.c_str(), optionName_.c_str()); sb.Append(prefix).Append("{\n"); - sb.Append(prefix + TAB).AppendFormat("return %s::%s%s_(%s, %s, %s, impl_);\n", + sb.Append(prefix + TAB).AppendFormat("return %s::%s%s_%d_(%s, %s, %s, impl_);\n", EmitDefinitionByInterface(interface, stubName_).c_str(), - stubName_.c_str(), method->GetName().c_str(), + stubName_.c_str(), method->GetName().c_str(), method->GetCmdId(), dataParcelName_.c_str(), replyParcelName_.c_str(), optionName_.c_str()); sb.Append("}\n"); @@ -409,8 +413,9 @@ void CppServiceStubCodeEmitter::EmitStubStaticMethodImpl( const AutoPtr &method, StringBuilder &sb, const std::string &prefix) const { sb.Append(prefix).AppendFormat( - "int32_t %s::%s%s_(MessageParcel& %s, MessageParcel& %s, MessageOption& %s, sptr<%s> impl)\n", - EmitDefinitionByInterface(interface_, stubName_).c_str(), stubName_.c_str(), method->GetName().c_str(), + "int32_t %s::%s%s_%d_(MessageParcel& %s, MessageParcel& %s, MessageOption& %s, sptr<%s> impl)\n", + EmitDefinitionByInterface(interface_, stubName_).c_str(), + stubName_.c_str(), method->GetName().c_str(), method->GetCmdId(), dataParcelName_.c_str(), replyParcelName_.c_str(), optionName_.c_str(), EmitDefinitionByInterface(interface_, interfaceName_).c_str()); sb.Append(prefix).Append("{\n"); diff --git a/framework/tools/hdi-gen/parser/parser.cpp b/framework/tools/hdi-gen/parser/parser.cpp index 7f95b33b1f4f836ec7e259d4e60d1d124ed0234a..063065244adca1c2e5176a094dd1e52b3d7ce238 100644 --- a/framework/tools/hdi-gen/parser/parser.cpp +++ b/framework/tools/hdi-gen/parser/parser.cpp @@ -43,6 +43,7 @@ static std::regex g_binaryNumRe(RE_BIN_DIGIT RE_DIGIT_SUFFIX, std::regex_constan static std::regex g_octNumRe(RE_OCT_DIGIT RE_DIGIT_SUFFIX, std::regex_constants::icase); static std::regex g_decNumRe(RE_DEC_DIGIT RE_DIGIT_SUFFIX, std::regex_constants::icase); static std::regex g_hexNumRe(RE_HEX_DIFIT RE_DIGIT_SUFFIX, std::regex_constants::icase); +AutoPtr g_currentEnum = nullptr; bool Parser::Parse(const std::vector &fileDetails) { @@ -546,6 +547,14 @@ AutoPtr Parser::ParseMethod(const AutoPtr &interfac lexer_.GetToken(); } + size_t methodsCount = interface->GetMethodNumber() + 1; + AutoPtr extInterface = interface->GetExtendsInterface(); + while (extInterface != nullptr) { + methodsCount += extInterface->GetMethodNumber(); + extInterface = extInterface->GetExtendsInterface(); + } + method->SetCmdId(methodsCount); + return method; } @@ -994,6 +1003,7 @@ AutoPtr Parser::ParseUserDefType() void Parser::ParseEnumDeclaration(const AttrSet &attrs) { AutoPtr enumType = new ASTEnumType; + g_currentEnum = enumType; enumType->SetAttribute(ParseUserDefTypeAttr(attrs)); lexer_.GetToken(); @@ -1030,6 +1040,7 @@ void Parser::ParseEnumDeclaration(const AttrSet &attrs) enumType->SetNamespace(ast_->ParseNamespace(ast_->GetFullName())); ast_->AddTypeDefinition(enumType.Get()); + g_currentEnum = nullptr; } AutoPtr Parser::ParseEnumBaseType() @@ -1116,7 +1127,10 @@ void Parser::ParseStructDeclaration(const AttrSet &attrs) } token = lexer_.PeekToken(); - if (token.kind != TokenType::BRACES_LEFT) { + if (token.kind == TokenType::COLON) { + AutoPtr parentType = ParseStructParentType(); + structType->SetParentType(parentType); + } else if (token.kind != TokenType::BRACES_LEFT) { LogError(token, StringHelper::Format("expected '{' before '%s' token", token.value.c_str())); } else { lexer_.GetToken(); @@ -1142,6 +1156,34 @@ void Parser::ParseStructDeclaration(const AttrSet &attrs) ast_->AddTypeDefinition(structType.Get()); } +AutoPtr Parser::ParseStructParentType() +{ + lexer_.GetToken(); + Token token = lexer_.PeekToken(); + AutoPtr baseType = ParseType(); + if (baseType == nullptr) { + LogError(token, StringHelper::Format("expected base type name before '{' token")); + return nullptr; + } + + switch (baseType->GetTypeKind()) { + case TypeKind::TYPE_STRUCT: + break; + default: { + LogError(token, StringHelper::Format("illegal base type of struct: '%s'", baseType->ToString().c_str())); + lexer_.SkipUntilToken(TokenType::BRACES_LEFT); + } + } + + AutoPtr parentType = dynamic_cast(baseType.Get()); + token = lexer_.PeekToken(); + if (token.kind != TokenType::BRACES_LEFT) { + LogError(token, StringHelper::Format("expected '{' before '%s' token", token.value.c_str())); + } + lexer_.GetToken(); + return parentType; +} + void Parser::ParseStructMember(const AutoPtr &structType) { Token token = lexer_.PeekToken(); @@ -1480,6 +1522,13 @@ AutoPtr Parser::ParsePrimaryExpr() } case TokenType::NUM: return ParseNumExpr(); + case TokenType::ID: + if (g_currentEnum == nullptr) { + LogError(token, StringHelper::Format("this expression is not supported")); + lexer_.SkipUntilToken(TokenType::COMMA); + return nullptr; + } + return ParseEnumExpr(); default: LogError(token, StringHelper::Format("this expression is not supported")); lexer_.SkipUntilToken(TokenType::COMMA); @@ -1500,6 +1549,19 @@ AutoPtr Parser::ParseNumExpr() return expr.Get(); } +AutoPtr Parser::ParseEnumExpr() +{ + Token token = lexer_.GetToken(); + if (!g_currentEnum->HasMember(token.value)) { + LogError(token, StringHelper::Format("unknown enum member: '%s'", token.value.c_str())); + return nullptr; + } + + AutoPtr expr = new ASTEnumExpr; + expr->value_ = token.value; + return expr.Get(); +} + bool Parser::CheckNumber(const std::string& integerVal) const { if (std::regex_match(integerVal, g_binaryNumRe)|| diff --git a/framework/tools/hdi-gen/parser/parser.h b/framework/tools/hdi-gen/parser/parser.h index 9b307f6ee9b23877e6263010bf2713b27d8394a3..0d570bb765912d5c0c685c23f194fd9d6155fd59 100644 --- a/framework/tools/hdi-gen/parser/parser.h +++ b/framework/tools/hdi-gen/parser/parser.h @@ -134,6 +134,8 @@ private: // parse declaration of struct void ParseStructDeclaration(const AttrSet &attrs = {}); + AutoPtr ParseStructParentType(); + void ParseStructMember(const AutoPtr &structType); // parse declaration of union @@ -167,6 +169,8 @@ private: AutoPtr ParseNumExpr(); + AutoPtr ParseEnumExpr(); + bool CheckNumber(const std::string& integerVal) const; bool CheckType(const Token &token, const AutoPtr &type); diff --git a/framework/tools/hdi-gen/test/unittest/01_empty_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/01_empty_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..96d02c8e15a866a5f5d896f3c1a3a6c407e5a79d --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/01_empty_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/01_empty_idl/target/fail_output.txt b/framework/tools/hdi-gen/test/unittest/01_empty_idl/target/fail_output.txt new file mode 100644 index 0000000000000000000000000000000000000000..3c62fc974e63fa4413af097f220d284a654c4441 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/01_empty_idl/target/fail_output.txt @@ -0,0 +1,2 @@ +[HDI-GEN]: IFoo.idl:1:1: expected 'package' before '' token +[MAIN]: failed to preprocess \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..301e8791ecd91fe7adf6e99d5de606ca7d5f9e88 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] String info); + + InfoTest([in] int inParam, [out] double outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_proxy.cpp.txt b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_proxy.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..9c69b0d39e2ab2eb01d3289678d5fb478666edfa --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_proxy.cpp.txt @@ -0,0 +1,120 @@ +#include "v1_0/foo_proxy.h" +#include +#include +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG foo_proxy + +sptr OHOS::HDI::Foo::V1_0::IFoo::Get(bool isStub) +{ + return IFoo::Get("foo_service", isStub); +} + +sptr OHOS::HDI::Foo::V1_0::IFoo::Get(const std::string& serviceName, bool isStub) +{ + if (isStub) { + std::string desc = Str16ToStr8(OHOS::HDI::Foo::V1_0::IFoo::GetDescriptor()); + void *impl = LoadHdiImpl(desc.c_str(), serviceName == "foo_service" ? "service" : serviceName.c_str()); + if (impl == nullptr) { + HDF_LOGE("failed to load hdi impl %{public}s", desc.data()); + return nullptr; + } + return reinterpret_cast(impl); + } + + using namespace OHOS::HDI::ServiceManager::V1_0; + auto servMgr = IServiceManager::Get(); + if (servMgr == nullptr) { + HDF_LOGE("%{public}s:get IServiceManager failed!", __func__); + return nullptr; + } + + sptr remote = servMgr->GetService(serviceName.c_str()); + if (remote == nullptr) { + HDF_LOGE("%{public}s:get remote object failed!", __func__); + return nullptr; + } + + sptr proxy = new FooProxy(remote); + if (proxy == nullptr) { + HDF_LOGE("%{public}s:iface_cast failed!", __func__); + return nullptr; + } + + uint32_t serMajorVer = 0; + uint32_t serMinorVer = 0; + int32_t fooRet = proxy->GetVersion(serMajorVer, serMinorVer); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s:get version failed!", __func__); + return nullptr; + } + + if (serMajorVer != 1) { + HDF_LOGE("%{public}s:check version failed! version of service:%u.%u, version of client:1.0", __func__, serMajorVer, serMinorVer); + return nullptr; + } + + return proxy; +} + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::Ping(const std::string& sendMsg, std::string& recvMsg) + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::GetData(std::string& info) + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::InfoTest(int32_t inParam, double& outParam) + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::GetVersion(uint32_t& majorVer, uint32_t& minorVer) + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::Ping_(const std::string& sendMsg, std::string& recvMsg, + const sptr remote) +{ + MessageParcel fooData; + MessageParcel fooReply; + MessageOption fooOption(MessageOption::TF_SYNC); + + if (!fooData.WriteInterfaceToken(OHOS::HDI::Foo::V1_0::IFoo::GetDescriptor())) { + HDF_LOGE("%{public}s: failed to write interface descriptor!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooData.WriteBool(false)) { + HDF_LOGE("%{public}s:failed to write flag of memory setting!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooData.WriteCString(sendMsg.c_str())) { + HDF_LOGE("%{public}s: write sendMsg failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (remote == nullptr) { + HDF_LOGE("%{public}s: invalid remote object!", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + int32_t fooRet = remote->SendRequest(CMD_FOO_PING_1, fooData, fooReply, fooOption); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooRet); + return fooRet; + } + + const char* recvMsgCp = fooReply.ReadCString(); + if (recvMsgCp == nullptr) { + HDF_LOGE("%{public}s: read recvMsg failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + recvMsg = recvMsgCp; + + return fooRet; +} + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::GetData_(std::string& info, const sptr remote) + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::InfoTest_(int32_t inParam, double& outParam, const sptr remote) + +int32_t OHOS::HDI::Foo::V1_0::FooProxy::GetVersion_(uint32_t& majorVer, uint32_t& minorVer, + const sptr remote) diff --git a/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_service.h.txt b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_service.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..7fa27784c135dd8d92a288ee7b9ec2611d949ac2 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_service.h.txt @@ -0,0 +1,9 @@ +#include "v1_0/ifoo.h" + +class FooService : public OHOS::HDI::Foo::V1_0::IFoo { + int32_t Ping(const std::string& sendMsg, std::string& recvMsg) override; + + int32_t GetData(std::string& info) override; + + int32_t InfoTest(int32_t inParam, double& outParam) override; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_stub.cpp.txt b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_stub.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..f5a240ff28616a1a52edb23002e699c15c068dd2 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/foo_stub.cpp.txt @@ -0,0 +1,108 @@ +#include "v1_0/foo_stub.h" +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG foo_stub + +sptr OHOS::HDI::Foo::V1_0::IFoo::Get(bool isStub) +{ + return OHOS::HDI::Foo::V1_0::IFoo::Get("foo_service", isStub); +} + +sptr OHOS::HDI::Foo::V1_0::IFoo::Get(const std::string& serviceName, bool isStub) +{ + if (!isStub) { + return nullptr; + } + std::string desc = Str16ToStr8(OHOS::HDI::Foo::V1_0::IFoo::GetDescriptor()); + void *impl = LoadHdiImpl(desc.c_str(), serviceName == "foo_service" ? "service" : serviceName.c_str()); + if (impl == nullptr) { + HDF_LOGE("failed to load hdi impl %{public}s", desc.c_str()); + return nullptr; + } + return reinterpret_cast(impl); +} + +int32_t OHOS::HDI::Foo::V1_0::FooStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + switch (code) { + case CMD_FOO_GET_VERSION_0: + return FooStubGetVersion_0(data, reply, option); + case CMD_FOO_PING_1: + return FooStubPing_1(data, reply, option); + case CMD_FOO_GET_DATA_2: + return FooStubGetData_2(data, reply, option); + case CMD_FOO_INFO_TEST_3: + return FooStubInfoTest_3(data, reply, option); + default: { + HDF_LOGE("%{public}s: cmd %{public}d is not supported", __func__, code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } +} + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubPing_1(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubGetData_2(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubInfoTest_3(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubGetVersion_0(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubPing_1_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) +{ + if (fooData.ReadInterfaceToken() != OHOS::HDI::Foo::V1_0::IFoo::GetDescriptor()) { + HDF_LOGE("%{public}s: interface token check failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + bool fooMemSet = false; + if (!fooData.ReadBool(fooMemSet)) { + HDF_LOGE("%{public}s: failed to read fooMemSet", __func__); + return HDF_ERR_INVALID_PARAM; + } + const char* sendMsgCp = fooData.ReadCString(); + if (sendMsgCp == nullptr) { + HDF_LOGE("%{public}s: read sendMsg failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + std::string sendMsg = sendMsgCp; + + std::string recvMsg; + if (fooMemSet) { + uint32_t capacity = 0; + if (!fooData.ReadUint32(capacity)) { + HDF_LOGE("%{public}s: failed to read capacity", __func__); + return HDF_ERR_INVALID_PARAM; + } + HDI_CHECK_VALUE_RETURN(capacity, >, HDI_BUFF_MAX_SIZE / sizeof(char), HDF_ERR_INVALID_PARAM); + recvMsg.reserve(capacity); + } + + if (impl == nullptr) { + HDF_LOGE("%{public}s: impl is nullptr!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t fooRet = impl->Ping(sendMsg, recvMsg); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooRet); + return fooRet; + } + + if (!fooReply.WriteCString(recvMsg.c_str())) { + HDF_LOGE("%{public}s: write recvMsg failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return fooRet; +} + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubGetData_2_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubInfoTest_3_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) + +int32_t OHOS::HDI::Foo::V1_0::FooStub::FooStubGetVersion_0_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/ifoo.h.txt b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/ifoo.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..b72a58638372d2ebbc419e975132970b9a36bac9 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/02_standard_interface_idl/target/foo/v1_0/ifoo.h.txt @@ -0,0 +1,46 @@ +#ifndef HDI_BUFF_MAX_SIZE +#define HDI_BUFF_MAX_SIZE (1024 * 200) +#endif + +#ifndef HDI_CHECK_VALUE_RETURN +#define HDI_CHECK_VALUE_RETURN(lv, compare, rv, ret) do { \ + if ((lv) compare (rv)) { \ + return ret; \ + } \ +} while (false) +#endif + +#ifndef HDI_CHECK_VALUE_RET_GOTO +#define HDI_CHECK_VALUE_RET_GOTO(lv, compare, rv, ret, value, table) do { \ + if ((lv) compare (rv)) { \ + ret = value; \ + goto table; \ + } \ +} while (false) +#endif + +namespace OHOS { +namespace HDI { +namespace Foo { +namespace V1_0 { +using namespace OHOS; +using namespace OHOS::HDI; + +enum { + CMD_FOO_GET_VERSION_0 = 0, + CMD_FOO_PING_1 = 1, + CMD_FOO_GET_DATA_2 = 2, + CMD_FOO_INFO_TEST_3 = 3, +}; + +class IFoo : public HdiBase { + virtual int32_t Ping(const std::string& sendMsg, std::string& recvMsg) = 0; + + virtual int32_t GetData(std::string& info) = 0; + + virtual int32_t InfoTest(int32_t inParam, double& outParam) = 0; +}; +} // V1_0 +} // Foo +} // HDI +} // OHOS \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..301e8791ecd91fe7adf6e99d5de606ca7d5f9e88 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] String info); + + InfoTest([in] int inParam, [out] double outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/foo/v1_0/IFooCallback.idl b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/foo/v1_0/IFooCallback.idl new file mode 100644 index 0000000000000000000000000000000000000000..2e991fcc764016c1c0c80e0cbf35c403f6cb5d70 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/foo/v1_0/IFooCallback.idl @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +[callback] interface IFooCallback { + PushData([in] String message); +} \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_proxy.cpp.txt b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_proxy.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..3219dbf47f5e349bfeb6aab3ea435595716ad356 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_proxy.cpp.txt @@ -0,0 +1,46 @@ +#include "v1_0/foo_callback_proxy.h" +#include +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG foo_callback_proxy + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackProxy::PushData(const std::string& message) + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackProxy::GetVersion(uint32_t& majorVer, uint32_t& minorVer) + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackProxy::PushData_(const std::string& message, const sptr remote) +{ + MessageParcel fooCallbackData; + MessageParcel fooCallbackReply; + MessageOption fooCallbackOption(MessageOption::TF_SYNC); + + if (!fooCallbackData.WriteInterfaceToken(OHOS::HDI::Foo::V1_0::IFooCallback::GetDescriptor())) { + HDF_LOGE("%{public}s: failed to write interface descriptor!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooCallbackData.WriteCString(message.c_str())) { + HDF_LOGE("%{public}s: write message failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (remote == nullptr) { + HDF_LOGE("%{public}s: invalid remote object!", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + int32_t fooCallbackRet = remote->SendRequest(CMD_FOO_CALLBACK_PUSH_DATA_1, fooCallbackData, fooCallbackReply, fooCallbackOption); + if (fooCallbackRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooCallbackRet); + return fooCallbackRet; + } + + return fooCallbackRet; +} + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackProxy::GetVersion_(uint32_t& majorVer, uint32_t& minorVer, + const sptr remote) diff --git a/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_service.h.txt b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_service.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..135aa1a0f838a29b3ce7728bd82bf62c1ba27bfc --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_service.h.txt @@ -0,0 +1,18 @@ +#ifndef OHOS_HDI_FOO_V1_0_FOOCALLBACKSERVICE_H +#define OHOS_HDI_FOO_V1_0_FOOCALLBACKSERVICE_H + +#include "v1_0/ifoo_callback.h" + +namespace OHOS { +namespace HDI { +namespace Foo { +namespace V1_0 { +class FooCallbackService : public OHOS::HDI::Foo::V1_0::IFooCallback { + int32_t PushData(const std::string& message) override; +}; +} // V1_0 +} // Foo +} // HDI +} // OHOS + +#endif // OHOS_HDI_FOO_V1_0_FOOCALLBACKSERVICE_H \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_stub.cpp.txt b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_stub.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..d5444c6df763d43ffb505616063b9e63b3058a37 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/foo_callback_stub.cpp.txt @@ -0,0 +1,53 @@ +#include "v1_0/foo_callback_stub.h" +#include +#include + +#define HDF_LOG_TAG foo_callback_stub + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + switch (code) { + case CMD_FOO_CALLBACK_GET_VERSION_0: + return FooCallbackStubGetVersion_0(data, reply, option); + case CMD_FOO_CALLBACK_PUSH_DATA_1: + return FooCallbackStubPushData_1(data, reply, option); + default: { + HDF_LOGE("%{public}s: cmd %{public}d is not supported", __func__, code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } +} + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackStub::FooCallbackStubPushData_1(MessageParcel& fooCallbackData, MessageParcel& fooCallbackReply, MessageOption& fooCallbackOption) + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackStub::FooCallbackStubGetVersion_0(MessageParcel& fooCallbackData, MessageParcel& fooCallbackReply, MessageOption& fooCallbackOption) + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackStub::FooCallbackStubPushData_1_(MessageParcel& fooCallbackData, MessageParcel& fooCallbackReply, MessageOption& fooCallbackOption, sptr impl) +{ + if (fooCallbackData.ReadInterfaceToken() != OHOS::HDI::Foo::V1_0::IFooCallback::GetDescriptor()) { + HDF_LOGE("%{public}s: interface token check failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + const char* messageCp = fooCallbackData.ReadCString(); + if (messageCp == nullptr) { + HDF_LOGE("%{public}s: read message failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + std::string message = messageCp; + + if (impl == nullptr) { + HDF_LOGE("%{public}s: impl is nullptr!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t fooCallbackRet = impl->PushData(message); + if (fooCallbackRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooCallbackRet); + return fooCallbackRet; + } + + return fooCallbackRet; +} + +int32_t OHOS::HDI::Foo::V1_0::FooCallbackStub::FooCallbackStubGetVersion_0_(MessageParcel& fooCallbackData, MessageParcel& fooCallbackReply, MessageOption& fooCallbackOption, sptr impl) \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/ifoo_callback.h.txt b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/ifoo_callback.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..aabfea55fb08dc5fcb1a39c558bd22096383b3e7 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/03_standard_callback_idl/target/foo/v1_0/ifoo_callback.h.txt @@ -0,0 +1,11 @@ +using namespace OHOS; +using namespace OHOS::HDI; + +enum { + CMD_FOO_CALLBACK_GET_VERSION_0 = 0, + CMD_FOO_CALLBACK_PUSH_DATA_1 = 1, +}; + +class IFooCallback : public HdiBase { + virtual int32_t PushData(const std::string& message) = 0; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..301e8791ecd91fe7adf6e99d5de606ca7d5f9e88 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] String info); + + InfoTest([in] int inParam, [out] double outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/foo/v1_1/IFoo.idl b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/foo/v1_1/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..b58ae9415f25b322e7eb01b956a5f265111d0813 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/foo/v1_1/IFoo.idl @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_1; + +import ohos.hdi.foo.v1_0.IFoo; + +interface IFoo extends ohos.hdi.foo.v1_0.IFoo { + TestPingV1_1([in] String sendMsg,[out] String recvMsg); + + TestGetData([out] String info); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_proxy.cpp.txt b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_proxy.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..6e3dabf56ef478a884c4336f9c24a0bdf9a2c550 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_proxy.cpp.txt @@ -0,0 +1,162 @@ +#include "v1_1/foo_proxy.h" +#include +#include +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG foo_proxy + +template +static sptr CastFromTemplate(const sptr &parent) +{ + if (parent == nullptr) { + HDF_LOGE("%{public}s:parent is nullptr!", __func__); + return nullptr; + } + + if (!parent->IsProxy()) { + HDF_LOGE("%{public}s:not proxy, not support castfrom!", __func__); + return nullptr; + } + + sptr remote = OHOS::HDI::hdi_objcast(parent); + if (remote == nullptr) { + HDF_LOGE("%{public}s:hdi_objcast failed!", __func__); + return nullptr; + } + + sptr proxy = OHOS::HDI::hdi_facecast(remote); + if (proxy == nullptr) { + HDF_LOGE("%{public}s:hdi_facecast failed!", __func__); + return nullptr; + } + + uint32_t serMajorVer = 0; + uint32_t serMinorVer = 0; + int32_t fooRet = proxy->GetVersion(serMajorVer, serMinorVer); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s:get version failed!", __func__); + return nullptr; + } + + if (serMajorVer != 1) { + HDF_LOGE("%{public}s:check version failed! version of service:%u.%u, version of client:1.1", __func__, serMajorVer, serMinorVer); + return nullptr; + } + + return proxy; +} + +sptr OHOS::HDI::Foo::V1_1::IFoo::Get(bool isStub) +{ + return IFoo::Get("foo_service", isStub); +} + +sptr OHOS::HDI::Foo::V1_1::IFoo::Get(const std::string& serviceName, bool isStub) +{ + if (isStub) { + std::string desc = Str16ToStr8(OHOS::HDI::Foo::V1_1::IFoo::GetDescriptor()); + void *impl = LoadHdiImpl(desc.c_str(), serviceName == "foo_service" ? "service" : serviceName.c_str()); + if (impl == nullptr) { + HDF_LOGE("failed to load hdi impl %{public}s", desc.data()); + return nullptr; + } + return reinterpret_cast(impl); + } + + using namespace OHOS::HDI::ServiceManager::V1_0; + auto servMgr = IServiceManager::Get(); + if (servMgr == nullptr) { + HDF_LOGE("%{public}s:get IServiceManager failed!", __func__); + return nullptr; + } + + sptr remote = servMgr->GetService(serviceName.c_str()); + if (remote == nullptr) { + HDF_LOGE("%{public}s:get remote object failed!", __func__); + return nullptr; + } + + sptr proxy = new FooProxy(remote); + if (proxy == nullptr) { + HDF_LOGE("%{public}s:iface_cast failed!", __func__); + return nullptr; + } + + uint32_t serMajorVer = 0; + uint32_t serMinorVer = 0; + int32_t fooRet = proxy->GetVersion(serMajorVer, serMinorVer); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s:get version failed!", __func__); + return nullptr; + } + + if (serMajorVer != 1) { + HDF_LOGE("%{public}s:check version failed! version of service:%u.%u, version of client:1.1", __func__, serMajorVer, serMinorVer); + return nullptr; + } + + return proxy; +} + +sptr OHOS::HDI::Foo::V1_1::IFoo::CastFrom(const sptr &parent) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::TestPingV1_1(const std::string& sendMsg, std::string& recvMsg) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::TestGetData(std::string& info) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::Ping(const std::string& sendMsg, std::string& recvMsg) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::GetData(std::string& info) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::InfoTest(int32_t inParam, double& outParam) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::GetVersion(uint32_t& majorVer, uint32_t& minorVer) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::TestPingV1_1_(const std::string& sendMsg, std::string& recvMsg, + const sptr remote) +{ + MessageParcel fooData; + MessageParcel fooReply; + MessageOption fooOption(MessageOption::TF_SYNC); + + if (!fooData.WriteInterfaceToken(OHOS::HDI::Foo::V1_1::IFoo::GetDescriptor())) { + HDF_LOGE("%{public}s: failed to write interface descriptor!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooData.WriteBool(false)) { + HDF_LOGE("%{public}s:failed to write flag of memory setting!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooData.WriteCString(sendMsg.c_str())) { + HDF_LOGE("%{public}s: write sendMsg failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (remote == nullptr) { + HDF_LOGE("%{public}s: invalid remote object!", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + int32_t fooRet = remote->SendRequest(CMD_FOO_TEST_PING_V1_1_4, fooData, fooReply, fooOption); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooRet); + return fooRet; + } + + const char* recvMsgCp = fooReply.ReadCString(); + if (recvMsgCp == nullptr) { + HDF_LOGE("%{public}s: read recvMsg failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + recvMsg = recvMsgCp; + + return fooRet; +} + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::TestGetData_(std::string& info, const sptr remote) \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_service.h.txt b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_service.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..b9623d6d053b6f1ca5add45eb4e2553e098a1548 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_service.h.txt @@ -0,0 +1,18 @@ +#ifndef OHOS_HDI_FOO_V1_1_FOOSERVICE_H +#define OHOS_HDI_FOO_V1_1_FOOSERVICE_H + +#include "v1_1/ifoo.h" + +class FooService : public OHOS::HDI::Foo::V1_1::IFoo { + int32_t TestPingV1_1(const std::string& sendMsg, std::string& recvMsg) override; + + int32_t TestGetData(std::string& info) override; + + int32_t Ping(const std::string& sendMsg, std::string& recvMsg) override; + + int32_t GetData(std::string& info) override; + + int32_t InfoTest(int32_t inParam, double& outParam) override; +}; + +#endif // OHOS_HDI_FOO_V1_1_FOOSERVICE_H \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_stub.cpp.txt b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_stub.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..af7aebf1d81434ebebd2f2e239367d3d2377d825 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/foo_stub.cpp.txt @@ -0,0 +1,112 @@ +#include "v1_1/foo_stub.h" +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG foo_stub + +sptr OHOS::HDI::Foo::V1_1::IFoo::Get(bool isStub) +{ + return OHOS::HDI::Foo::V1_1::IFoo::Get("foo_service", isStub); +} + +sptr OHOS::HDI::Foo::V1_1::IFoo::Get(const std::string& serviceName, bool isStub) +{ + if (!isStub) { + return nullptr; + } + std::string desc = Str16ToStr8(OHOS::HDI::Foo::V1_1::IFoo::GetDescriptor()); + void *impl = LoadHdiImpl(desc.c_str(), serviceName == "foo_service" ? "service" : serviceName.c_str()); + if (impl == nullptr) { + HDF_LOGE("failed to load hdi impl %{public}s", desc.c_str()); + return nullptr; + } + return reinterpret_cast(impl); +} + +int32_t OHOS::HDI::Foo::V1_1::FooStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + switch (code) { + case CMD_FOO_GET_VERSION_0: + return FooStubGetVersion_0(data, reply, option); + case CMD_FOO_TEST_PING_V1_1_4: + return FooStubTestPingV1_1_4(data, reply, option); + case CMD_FOO_TEST_GET_DATA_5: + return FooStubTestGetData_5(data, reply, option); + case CMD_FOO_PING_1: + return FooStubPing_1(data, reply, option); + case CMD_FOO_GET_DATA_2: + return FooStubGetData_2(data, reply, option); + case CMD_FOO_INFO_TEST_3: + return FooStubInfoTest_3(data, reply, option); + default: { + HDF_LOGE("%{public}s: cmd %{public}d is not supported", __func__, code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } +} + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubTestPingV1_1_4(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubTestGetData_5(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubPing_1(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubGetData_2(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubInfoTest_3(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubGetVersion_0(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubTestPingV1_1_4_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) +{ + if (fooData.ReadInterfaceToken() != OHOS::HDI::Foo::V1_1::IFoo::GetDescriptor()) { + HDF_LOGE("%{public}s: interface token check failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + bool fooMemSet = false; + if (!fooData.ReadBool(fooMemSet)) { + HDF_LOGE("%{public}s: failed to read fooMemSet", __func__); + return HDF_ERR_INVALID_PARAM; + } + const char* sendMsgCp = fooData.ReadCString(); + if (sendMsgCp == nullptr) { + HDF_LOGE("%{public}s: read sendMsg failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + std::string sendMsg = sendMsgCp; + + std::string recvMsg; + if (fooMemSet) { + uint32_t capacity = 0; + if (!fooData.ReadUint32(capacity)) { + HDF_LOGE("%{public}s: failed to read capacity", __func__); + return HDF_ERR_INVALID_PARAM; + } + HDI_CHECK_VALUE_RETURN(capacity, >, HDI_BUFF_MAX_SIZE / sizeof(char), HDF_ERR_INVALID_PARAM); + recvMsg.reserve(capacity); + } + + if (impl == nullptr) { + HDF_LOGE("%{public}s: impl is nullptr!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t fooRet = impl->TestPingV1_1(sendMsg, recvMsg); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooRet); + return fooRet; + } + + if (!fooReply.WriteCString(recvMsg.c_str())) { + HDF_LOGE("%{public}s: write recvMsg failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return fooRet; +} + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubTestGetData_5_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/ifoo.h.txt b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/ifoo.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..989d4d478768ced4b4a0ab93d55a84321a2d9721 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/04_extended_interface_idl/target/foo/v1_1/ifoo.h.txt @@ -0,0 +1,25 @@ +#ifndef OHOS_HDI_FOO_V1_1_IFOO_H +#define OHOS_HDI_FOO_V1_1_IFOO_H + +#include +#include +#include +#include +#include "foo/v1_0/ifoo.h" + +using namespace OHOS; +using namespace OHOS::HDI; +using namespace OHOS::HDI::Foo::V1_0; + +enum { + CMD_FOO_TEST_PING_V1_1_4 = 4, + CMD_FOO_TEST_GET_DATA_5 = 5, +}; + +class IFoo : public OHOS::HDI::Foo::V1_0::IFoo { + virtual int32_t TestPingV1_1(const std::string& sendMsg, std::string& recvMsg) = 0; + + virtual int32_t TestGetData(std::string& info) = 0; +}; + +#endif // OHOS_HDI_FOO_V1_1_IFOO_H \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/05_types_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/05_types_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..6ed1975b352f7589e3557db05f6172bbe12f2a20 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/05_types_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +import ohos.hdi.foo.v1_0.Types; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] struct FooInfo info); + + InfoTest([in] struct FooInfo inParam, [out] struct FooInfo outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/05_types_idl/foo/v1_0/Types.idl b/framework/tools/hdi-gen/test/unittest/05_types_idl/foo/v1_0/Types.idl new file mode 100644 index 0000000000000000000000000000000000000000..ed068503d2a82864bc5108238b25446016a6643e --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/05_types_idl/foo/v1_0/Types.idl @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +enum FooType { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, +}; + +struct FooInfo { + unsigned int id; + String name; + enum FooType type; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/05_types_idl/target/foo/v1_0/types.cpp.txt b/framework/tools/hdi-gen/test/unittest/05_types_idl/target/foo/v1_0/types.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..d86b2abc8503215cd2419cc04f554a4006781c84 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/05_types_idl/target/foo/v1_0/types.cpp.txt @@ -0,0 +1,48 @@ +#include "v1_0/types.h" +#include +#include +#include + +bool FooInfoBlockMarshalling(OHOS::MessageParcel& data, const OHOS::HDI::Foo::V1_0::FooInfo& dataBlock) +{ + if (!data.WriteUint32(dataBlock.id)) { + HDF_LOGE("%{public}s: write dataBlock.id failed!", __func__); + return false; + } + + if (!data.WriteCString(dataBlock.name.c_str())) { + HDF_LOGE("%{public}s: write dataBlock.name failed!", __func__); + return false; + } + + if (!data.WriteUint64(static_cast(dataBlock.type))) { + HDF_LOGE("%{public}s: write dataBlock.type failed!", __func__); + return false; + } + return true; +} + +bool FooInfoBlockUnmarshalling(OHOS::MessageParcel& data, OHOS::HDI::Foo::V1_0::FooInfo& dataBlock) +{ + if (!data.ReadUint32(dataBlock.id)) { + HDF_LOGE("%{public}s: read dataBlock.id failed!", __func__); + return false; + } + + const char* nameCp = data.ReadCString(); + if (nameCp == nullptr) { + HDF_LOGE("%{public}s: read nameCp failed", __func__); + return false; + } + dataBlock.name = nameCp; + + { + uint64_t enumTmp = 0; + if (!data.ReadUint64(enumTmp)) { + HDF_LOGE("%{public}s: write dataBlock.type failed!", __func__); + return false; + } + dataBlock.type = static_cast(enumTmp); + } + return true; +} \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/05_types_idl/target/foo/v1_0/types.h.txt b/framework/tools/hdi-gen/test/unittest/05_types_idl/target/foo/v1_0/types.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..958d52784c20d4ad4f132d4669854a1a6fb2e3c8 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/05_types_idl/target/foo/v1_0/types.h.txt @@ -0,0 +1,29 @@ +#ifndef OHOS_HDI_FOO_V1_0_TYPES_H +#define OHOS_HDI_FOO_V1_0_TYPES_H + +#include +#include +#include + +namespace OHOS { +class MessageParcel; +} + +using namespace OHOS; + +enum FooType : int32_t { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, +}; + +struct FooInfo { + uint32_t id; + std::string name; + OHOS::HDI::Foo::V1_0::FooType type; +}; + +bool FooInfoBlockMarshalling(OHOS::MessageParcel &data, const OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); + +bool FooInfoBlockUnmarshalling(OHOS::MessageParcel &data, OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); + +#endif // OHOS_HDI_FOO_V1_0_TYPES_H \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..6ed1975b352f7589e3557db05f6172bbe12f2a20 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +import ohos.hdi.foo.v1_0.Types; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] struct FooInfo info); + + InfoTest([in] struct FooInfo inParam, [out] struct FooInfo outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/foo/v1_0/Types.idl b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/foo/v1_0/Types.idl new file mode 100644 index 0000000000000000000000000000000000000000..0486a857202b382468cd44be22be6584c11185d1 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/foo/v1_0/Types.idl @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +enum FooType { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, +}; + +enum FooType2 : FooType{ + FOO_TYPE_THREE = 3, + FOO_TYPE_FOUR = 4, +}; + +struct FooInfo { + unsigned int id; + String name; + enum FooType2 type; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/target/foo/v1_0/types.cpp.txt b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/target/foo/v1_0/types.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..e287186b461acb1f72e45fc0934deffb2b9d8c81 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/target/foo/v1_0/types.cpp.txt @@ -0,0 +1,8 @@ +#include "v1_0/types.h" +#include +#include +#include + +bool FooInfoBlockMarshalling(OHOS::MessageParcel& data, const OHOS::HDI::Foo::V1_0::FooInfo& dataBlock) + +bool FooInfoBlockUnmarshalling(OHOS::MessageParcel& data, OHOS::HDI::Foo::V1_0::FooInfo& dataBlock) \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/target/foo/v1_0/types.h.txt b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/target/foo/v1_0/types.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..9b475b7b62928d96bb18c3bc6efa578b52b3ed3c --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/06_extended_enum_idl/target/foo/v1_0/types.h.txt @@ -0,0 +1,27 @@ +namespace OHOS { +class MessageParcel; +} + +using namespace OHOS; + +enum FooType : int32_t { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, +}; + +enum FooType2 : int32_t { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, + FOO_TYPE_THREE = 3, + FOO_TYPE_FOUR = 4, +}; + +struct FooInfo { + uint32_t id; + std::string name; + OHOS::HDI::Foo::V1_0::FooType2 type; +}; + +bool FooInfoBlockMarshalling(OHOS::MessageParcel &data, const OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); + +bool FooInfoBlockUnmarshalling(OHOS::MessageParcel &data, OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..884fa5a04d8348d4746d047a2b452bb9bfa435fe --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +import ohos.hdi.foo.v1_0.Types; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] struct ExtendedFooInfo info); + + InfoTest([in] struct ExtendedFooInfo inParam, [out] struct ExtendedFooInfo outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/foo/v1_0/Types.idl b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/foo/v1_0/Types.idl new file mode 100644 index 0000000000000000000000000000000000000000..eb20b8cd1070244d78c16549cd8860545fc1a0d1 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/foo/v1_0/Types.idl @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +enum FooType { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, +}; + +struct FooInfo { + unsigned int id; + String name; + enum FooType type; +}; + +struct ExtendedFooInfo : FooInfo { + unsigned int extendedId; + String extendedInfo; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/target/foo/v1_0/types.cpp.txt b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/target/foo/v1_0/types.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..9bea95eab47eb64f687670696532876af6d7644f --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/target/foo/v1_0/types.cpp.txt @@ -0,0 +1,74 @@ +#include "v1_0/types.h" +#include +#include +#include + +bool FooInfoBlockMarshalling(OHOS::MessageParcel& data, const OHOS::HDI::Foo::V1_0::FooInfo& dataBlock) + +bool FooInfoBlockUnmarshalling(OHOS::MessageParcel& data, OHOS::HDI::Foo::V1_0::FooInfo& dataBlock) + +bool ExtendedFooInfoBlockMarshalling(OHOS::MessageParcel& data, const OHOS::HDI::Foo::V1_0::ExtendedFooInfo& dataBlock) +{ + if (!data.WriteUint32(dataBlock.id)) { + HDF_LOGE("%{public}s: write dataBlock.id failed!", __func__); + return false; + } + + if (!data.WriteCString(dataBlock.name.c_str())) { + HDF_LOGE("%{public}s: write dataBlock.name failed!", __func__); + return false; + } + + if (!data.WriteUint64(static_cast(dataBlock.type))) { + HDF_LOGE("%{public}s: write dataBlock.type failed!", __func__); + return false; + } + + if (!data.WriteUint32(dataBlock.extendedId)) { + HDF_LOGE("%{public}s: write dataBlock.extendedId failed!", __func__); + return false; + } + + if (!data.WriteCString(dataBlock.extendedInfo.c_str())) { + HDF_LOGE("%{public}s: write dataBlock.extendedInfo failed!", __func__); + return false; + } + return true; +} + +bool ExtendedFooInfoBlockUnmarshalling(OHOS::MessageParcel& data, OHOS::HDI::Foo::V1_0::ExtendedFooInfo& dataBlock) +{ + if (!data.ReadUint32(dataBlock.id)) { + HDF_LOGE("%{public}s: read dataBlock.id failed!", __func__); + return false; + } + + const char* nameCp = data.ReadCString(); + if (nameCp == nullptr) { + HDF_LOGE("%{public}s: read nameCp failed", __func__); + return false; + } + dataBlock.name = nameCp; + + { + uint64_t enumTmp = 0; + if (!data.ReadUint64(enumTmp)) { + HDF_LOGE("%{public}s: write dataBlock.type failed!", __func__); + return false; + } + dataBlock.type = static_cast(enumTmp); + } + + if (!data.ReadUint32(dataBlock.extendedId)) { + HDF_LOGE("%{public}s: read dataBlock.extendedId failed!", __func__); + return false; + } + + const char* extendedInfoCp = data.ReadCString(); + if (extendedInfoCp == nullptr) { + HDF_LOGE("%{public}s: read extendedInfoCp failed", __func__); + return false; + } + dataBlock.extendedInfo = extendedInfoCp; + return true; +} \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/target/foo/v1_0/types.h.txt b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/target/foo/v1_0/types.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..bf4ee00cdc5cc3e5817f13230f3f8f1746b1a68c --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/07_extended_struct_idl/target/foo/v1_0/types.h.txt @@ -0,0 +1,32 @@ +namespace OHOS { +class MessageParcel; +} + +using namespace OHOS; + +enum FooType : int32_t { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, +}; + +struct FooInfo { + uint32_t id; + std::string name; + OHOS::HDI::Foo::V1_0::FooType type; +}; + +struct ExtendedFooInfo { + uint32_t id; + std::string name; + OHOS::HDI::Foo::V1_0::FooType type; + uint32_t extendedId; + std::string extendedInfo; +}; + +bool FooInfoBlockMarshalling(OHOS::MessageParcel &data, const OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); + +bool FooInfoBlockUnmarshalling(OHOS::MessageParcel &data, OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); + +bool ExtendedFooInfoBlockMarshalling(OHOS::MessageParcel &data, const OHOS::HDI::Foo::V1_0::ExtendedFooInfo& dataBlock); + +bool ExtendedFooInfoBlockUnmarshalling(OHOS::MessageParcel &data, OHOS::HDI::Foo::V1_0::ExtendedFooInfo& dataBlock); \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..301e8791ecd91fe7adf6e99d5de606ca7d5f9e88 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] String info); + + InfoTest([in] int inParam, [out] double outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/foo/v1_1/IFoo.idl b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/foo/v1_1/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..ff3783f022204d9f988697978c8d67f76fd7f412 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/foo/v1_1/IFoo.idl @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_1; + +import ohos.hdi.foo.v1_0.IFoo; + +interface IFoo extends ohos.hdi.foo.v1_0.IFoo { + Ping([in] String sendMsg,[out] String recvMsg, [in] int code); + + GetData([out] String info, [out] String ver); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_proxy.cpp.txt b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_proxy.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..9e9b296d8422cccad54ca4c0b7e32e29825d2485 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_proxy.cpp.txt @@ -0,0 +1,74 @@ +#include "v1_1/foo_proxy.h" +#include +#include +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG foo_proxy + +sptr OHOS::HDI::Foo::V1_1::IFoo::CastFrom(const sptr &parent) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::Ping(const std::string& sendMsg, std::string& recvMsg, int32_t code) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::GetData(std::string& info, std::string& ver) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::Ping(const std::string& sendMsg, std::string& recvMsg) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::GetData(std::string& info) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::InfoTest(int32_t inParam, double& outParam) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::GetVersion(uint32_t& majorVer, uint32_t& minorVer) + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::Ping_(const std::string& sendMsg, std::string& recvMsg, int32_t code, + const sptr remote) +{ + MessageParcel fooData; + MessageParcel fooReply; + MessageOption fooOption(MessageOption::TF_SYNC); + + if (!fooData.WriteInterfaceToken(OHOS::HDI::Foo::V1_1::IFoo::GetDescriptor())) { + HDF_LOGE("%{public}s: failed to write interface descriptor!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooData.WriteBool(false)) { + HDF_LOGE("%{public}s:failed to write flag of memory setting!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooData.WriteCString(sendMsg.c_str())) { + HDF_LOGE("%{public}s: write sendMsg failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!fooData.WriteInt32(code)) { + HDF_LOGE("%{public}s: write code failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (remote == nullptr) { + HDF_LOGE("%{public}s: invalid remote object!", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + int32_t fooRet = remote->SendRequest(CMD_FOO_PING_4, fooData, fooReply, fooOption); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooRet); + return fooRet; + } + + const char* recvMsgCp = fooReply.ReadCString(); + if (recvMsgCp == nullptr) { + HDF_LOGE("%{public}s: read recvMsg failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + recvMsg = recvMsgCp; + + return fooRet; +} + +int32_t OHOS::HDI::Foo::V1_1::FooProxy::GetData_(std::string& info, std::string& ver, const sptr remote) \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_service.h.txt b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_service.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..de316e01dc1c5604e408d56aeaf26ef34fb32728 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_service.h.txt @@ -0,0 +1,16 @@ +#ifndef OHOS_HDI_FOO_V1_1_FOOSERVICE_H +#define OHOS_HDI_FOO_V1_1_FOOSERVICE_H + +#include "v1_1/ifoo.h" + +class FooService : public OHOS::HDI::Foo::V1_1::IFoo { + int32_t Ping(const std::string& sendMsg, std::string& recvMsg, int32_t code) override; + + int32_t GetData(std::string& info, std::string& ver) override; + + int32_t Ping(const std::string& sendMsg, std::string& recvMsg) override; + + int32_t GetData(std::string& info) override; + + int32_t InfoTest(int32_t inParam, double& outParam) override; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_stub.cpp.txt b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_stub.cpp.txt new file mode 100644 index 0000000000000000000000000000000000000000..8d8989fb667bd8e8ae90a5c489f6d0df36ef7034 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/foo_stub.cpp.txt @@ -0,0 +1,99 @@ +#include "v1_1/foo_stub.h" +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG foo_stub + +int32_t OHOS::HDI::Foo::V1_1::FooStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + switch (code) { + case CMD_FOO_GET_VERSION_0: + return FooStubGetVersion_0(data, reply, option); + case CMD_FOO_PING_4: + return FooStubPing_4(data, reply, option); + case CMD_FOO_GET_DATA_5: + return FooStubGetData_5(data, reply, option); + case CMD_FOO_PING_1: + return FooStubPing_1(data, reply, option); + case CMD_FOO_GET_DATA_2: + return FooStubGetData_2(data, reply, option); + case CMD_FOO_INFO_TEST_3: + return FooStubInfoTest_3(data, reply, option); + default: { + HDF_LOGE("%{public}s: cmd %{public}d is not supported", __func__, code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } +} + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubPing_4(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubGetData_5(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubPing_1(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubGetData_2(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubInfoTest_3(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubGetVersion_0(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption) + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubPing_4_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) +{ + if (fooData.ReadInterfaceToken() != OHOS::HDI::Foo::V1_1::IFoo::GetDescriptor()) { + HDF_LOGE("%{public}s: interface token check failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + bool fooMemSet = false; + if (!fooData.ReadBool(fooMemSet)) { + HDF_LOGE("%{public}s: failed to read fooMemSet", __func__); + return HDF_ERR_INVALID_PARAM; + } + const char* sendMsgCp = fooData.ReadCString(); + if (sendMsgCp == nullptr) { + HDF_LOGE("%{public}s: read sendMsg failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + std::string sendMsg = sendMsgCp; + + std::string recvMsg; + if (fooMemSet) { + uint32_t capacity = 0; + if (!fooData.ReadUint32(capacity)) { + HDF_LOGE("%{public}s: failed to read capacity", __func__); + return HDF_ERR_INVALID_PARAM; + } + HDI_CHECK_VALUE_RETURN(capacity, >, HDI_BUFF_MAX_SIZE / sizeof(char), HDF_ERR_INVALID_PARAM); + recvMsg.reserve(capacity); + } + + int32_t code = 0; + if (!fooData.ReadInt32(code)) { + HDF_LOGE("%{public}s: read code failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (impl == nullptr) { + HDF_LOGE("%{public}s: impl is nullptr!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t fooRet = impl->Ping(sendMsg, recvMsg, code); + if (fooRet != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, fooRet); + return fooRet; + } + + if (!fooReply.WriteCString(recvMsg.c_str())) { + HDF_LOGE("%{public}s: write recvMsg failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return fooRet; +} + +int32_t OHOS::HDI::Foo::V1_1::FooStub::FooStubGetData_5_(MessageParcel& fooData, MessageParcel& fooReply, MessageOption& fooOption, sptr impl) \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/ifoo.h.txt b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/ifoo.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..dd3f2f23a32dc057ae0ebe02d0bdb189f5e9e061 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/08_overload_method_idl/target/foo/v1_1/ifoo.h.txt @@ -0,0 +1,12 @@ +enum { + CMD_FOO_PING_4 = 4, + CMD_FOO_GET_DATA_5 = 5, +}; + +class IFoo : public OHOS::HDI::Foo::V1_0::IFoo { + using OHOS::HDI::Foo::V1_0::IFoo::Ping; + virtual int32_t Ping(const std::string& sendMsg, std::string& recvMsg, int32_t code) = 0; + + using OHOS::HDI::Foo::V1_0::IFoo::GetData; + virtual int32_t GetData(std::string& info, std::string& ver) = 0; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/foo/v1_0/IFoo.idl b/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/foo/v1_0/IFoo.idl new file mode 100644 index 0000000000000000000000000000000000000000..6ed1975b352f7589e3557db05f6172bbe12f2a20 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/foo/v1_0/IFoo.idl @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +import ohos.hdi.foo.v1_0.Types; + +interface IFoo { + Ping([in] String sendMsg,[out] String recvMsg); + + GetData([out] struct FooInfo info); + + InfoTest([in] struct FooInfo inParam, [out] struct FooInfo outParam); +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/foo/v1_0/Types.idl b/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/foo/v1_0/Types.idl new file mode 100644 index 0000000000000000000000000000000000000000..b554fedb841e55f0f7de7a04ab98207d76b0789c --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/foo/v1_0/Types.idl @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +package ohos.hdi.foo.v1_0; + +enum FooType { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, + FOO_TYPE_NESTING = (FOO_TYPE_ONE << FOO_TYPE_TWO), +}; + +struct FooInfo { + unsigned int id; + String name; + enum FooType type; +}; \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/target/foo/v1_0/types.h.txt b/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/target/foo/v1_0/types.h.txt new file mode 100644 index 0000000000000000000000000000000000000000..3b51b25f5c60f18b699c6ddfe3df51a272247874 --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/09_enum_nesting_idl/target/foo/v1_0/types.h.txt @@ -0,0 +1,23 @@ +#include +#include +#include + +namespace OHOS { +class MessageParcel; +} + +enum FooType : int32_t { + FOO_TYPE_ONE = 1, + FOO_TYPE_TWO = 2, + FOO_TYPE_NESTING = (FOO_TYPE_ONE << FOO_TYPE_TWO), +}; + +struct FooInfo { + uint32_t id; + std::string name; + OHOS::HDI::Foo::V1_0::FooType type; +}; + +bool FooInfoBlockMarshalling(OHOS::MessageParcel &data, const OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); + +bool FooInfoBlockUnmarshalling(OHOS::MessageParcel &data, OHOS::HDI::Foo::V1_0::FooInfo& dataBlock); \ No newline at end of file diff --git a/framework/tools/hdi-gen/test/unittest/unit_test.py b/framework/tools/hdi-gen/test/unittest/unit_test.py new file mode 100644 index 0000000000000000000000000000000000000000..c72c251aca9431523675729ab3974bde36338b2a --- /dev/null +++ b/framework/tools/hdi-gen/test/unittest/unit_test.py @@ -0,0 +1,258 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright (c) 2024 Huawei Device Co., Ltd. +# +# HDF is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. + +import os +import subprocess +import time + + +def get_time_stamp(): + return int(round(time.time() * 1000)) + + +def print_success(info): + print("\033[32m{}\033[0m".format(info)) + + +def print_failure(info): + print("\033[31m{}\033[0m".format(info)) + + +def is_subsequence(first_file, second_file): + first_info = first_file.read() + second_info = second_file.readline() + while second_info: + if first_info.find(second_info) == -1: + print("line\n", second_info, "is not in output file", first_file_path) + return False + second_info = second_file.readline() + return True + + +def compare_file(first_file_path, second_file_path): + with open(first_file_path, 'r') as first_file: + with open(second_file_path, 'r') as second_file: + return is_subsequence(first_file, second_file) + + +def compare_target_files(first_file_path, second_file_path): + first_files_list = get_all_files(first_file_path) + second_files_list = get_all_files(second_file_path) + + first_files = set([file[len(first_file_path):] for file in first_files_list]) + second_files = set([file[len(second_file_path):-4] for file in second_files_list]) + + common_files = first_files & second_files + + for files in common_files: + if not compare_file(first_file_path + files, second_file_path + files + ".txt"): + print("file ", first_file_path + files, second_file_path + files + ".txt", "is different") + return False + return True + + +def exec_command(command): + return subprocess.getstatusoutput(command) + + +def file_exists(file_path): + return os.path.isfile(file_path) + + +def make_binary_file(file_path): + print("making hdi-gen...") + return exec_command("make --directory={} --jobs=4".format(file_path)) + + +def clean_binary_file(file_path): + return exec_command("make --directory={} clean".format(file_path)) + + +def get_all_files(path): + file_list = [] + items = os.listdir(path) + for item in items: + item_path = os.path.join(path, item) + if not os.path.isdir(item_path): + file_list.append(item_path) + else: + file_list += get_all_files(item_path) + return file_list + + +def get_all_idl_files(idl_path): + file_list = get_all_files(idl_path) + idl_file_list = [] + for file in file_list: + if os.path.splitext(file)[-1] == ".idl": + idl_file_list.append(file) + return idl_file_list + + + +class Test: + def __init__(self, name, working_dir): + self.name = name + self.working_dir = working_dir + self.idl_dir = os.path.join(self.working_dir, "foo") + self.output_dir = os.path.join(working_dir, "out") + self.target_dir = os.path.join(working_dir, "target") + self.command = "../../hdi-gen -s full -m ipc -l cpp -r ohos.hdi:{} -d {}".format(working_dir, self.output_dir) + + def run(self): + # please add test code here + return False + + def run_success(self): + self.add_idl_files() + status, _ = exec_command(self.command) + if status == 0 and compare_target_files(self.output_dir, self.target_dir): + return True + return False + + def run_fail(self): + self.add_idl_files() + status, _ = exec_command(self.command) + + expected_fail_output = "" + with open(os.path.join(self.target_dir, "fail_output.txt"), 'r') as target_output: + expected_fail_output = target_output.read() + + if status != 0 and expected_fail_output == _: + return True + return False + + def remove_output(self): + exec_command("rm -rf {}".format(self.output_dir)) + return True + + def add_idl_files(self): + idl_list = get_all_idl_files(self.idl_dir) + for idl in idl_list: + self.command += " -c {}".format(idl) + + def test(self): + print_success("[ RUN ] {}".format(self.name)) + start_time = get_time_stamp() + result = self.run() + end_time = get_time_stamp() + + if result: + print_success("[ OK ] {} ({}ms)".format(self.name, end_time - start_time)) + else: + print_failure("[ FAILED ] {} ({}ms)".format(self.name, end_time - start_time)) + return result + + +# compile empty idl file +class UnitTest01(Test): + def run(self): + return self.run_fail() + + +# standard interface idl file +class UnitTest02(Test): + def run(self): + return self.run_success() + + +# standard callback idl file +class UnitTest03(Test): + def run(self): + return self.run_success() + + +# extended interface idl file +class UnitTest04(Test): + def run(self): + return self.run_success() + + +# interface with types idl file +class UnitTest05(Test): + def run(self): + return self.run_success() + + +# extended enum idl file +class UnitTest06(Test): + def run(self): + return self.run_success() + + +# extended struct idl file +class UnitTest07(Test): + def run(self): + return self.run_success() + + +# overload method idl file +class UnitTest08(Test): + def run(self): + return self.run_success() + + +# enum nesting idl file +class UnitTest09(Test): + def run(self): + return self.run_success() + + +class Tests: + test_cases = [ + UnitTest01("UnitTestEmptyIdl", "01_empty_idl"), + UnitTest02("UnitTestStandardInterface", "02_standard_interface_idl"), + UnitTest03("UnitTestStandardCallback", "03_standard_callback_idl"), + UnitTest04("UnitTestExtendedInterface", "04_extended_interface_idl"), + UnitTest05("UnitTestTypesIdl", "05_types_idl"), + UnitTest06("UnitTestEnumExtension", "06_extended_enum_idl"), + UnitTest07("UnitTestStructExtension", "07_extended_struct_idl"), + UnitTest08("UnitTestOverloadMethod", "08_overload_method_idl"), + UnitTest09("UnitTestEnumNesting", "09_enum_nesting_idl"), + ] + + @staticmethod + def set_up_test_case(): + hdi_gen_file = "../../hdi-gen" + ret = file_exists(hdi_gen_file) + if not ret: + hdi_gen_path = "../../" + if make_binary_file(hdi_gen_path)[0] == 0: + ret = True + if not ret: + print_failure("[===========] failed to make hdi-gen") + return ret + + @staticmethod + def tear_down_test_case(): + for case in Tests.test_cases: + case.remove_output() + hdi_gen_path = "../../" + clean_binary_file(hdi_gen_path) + + @staticmethod + def test(): + test_case_num = len(Tests.test_cases) + success_case_num = 0 + print_success("[===========] start {} test".format(test_case_num)) + for test_case in Tests.test_cases: + if test_case.test(): + success_case_num += 1 + print_success("[ PASSED ] {} test".format(success_case_num)) + failure_case_num = test_case_num - success_case_num + if failure_case_num > 0: + print_failure("[ FAILED ] {} test".format(failure_case_num)) + + +if __name__ == "__main__": + if not Tests.set_up_test_case(): + print_failure("test case set up failed!") + exit(-1) + Tests.test() + Tests.tear_down_test_case()