From d0453855187c12cbea0223e2d86ae51c3ff9fb2f Mon Sep 17 00:00:00 2001 From: lwx1155083 Date: Fri, 10 Mar 2023 15:18:51 +0800 Subject: [PATCH 1/5] Add validation to parcel.cpp. Issue:I6LQJS Test:NA Signed-off-by: lwx1155083 --- base/src/parcel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/parcel.cpp b/base/src/parcel.cpp index 65b15f4..1dfe931 100644 --- a/base/src/parcel.cpp +++ b/base/src/parcel.cpp @@ -1044,7 +1044,7 @@ bool Parcel::ReadString16(std::u16string &value) } size_t readCapacity = (static_cast(dataLength) + 1) * sizeof(char16_t); - if (readCapacity <= GetReadableBytes()) { + if ((readCapacity > (static_cast(dataLength))) && (readCapacity <= GetReadableBytes())) { const uint8_t *str = ReadBuffer(readCapacity); if (str != nullptr) { const auto *u16Str = reinterpret_cast(str); -- Gitee From b3f6e679c3f1581b7f2f922e01a654acac70b061 Mon Sep 17 00:00:00 2001 From: huangyuchen Date: Fri, 10 Mar 2023 09:42:26 +0800 Subject: [PATCH 2/5] Fix thread-safe bugs in UT cases of thread pool. Issue: I6MMQJ Test: UT Change-Id: Ic63dfb283a795cc088aa3b0c0cb0c09f74886ca4 Signed-off-by: huangyuchen --- base/test/unittest/common/utils_thread_pool_test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/test/unittest/common/utils_thread_pool_test.cpp b/base/test/unittest/common/utils_thread_pool_test.cpp index 118bb0f..107d9e6 100644 --- a/base/test/unittest/common/utils_thread_pool_test.cpp +++ b/base/test/unittest/common/utils_thread_pool_test.cpp @@ -106,11 +106,13 @@ HWTEST_F(UtilsThreadPoolTest, test_04, TestSize.Level0) void TestFuncAddOneTime(int& i) { + std::lock_guard lock(g_mutex); ++g_times; } void TestFuncSubOneTime(int& i) { + std::lock_guard lock(g_mutex); --g_times; } @@ -175,18 +177,18 @@ HWTEST_F(UtilsThreadPoolTest, test_06, TestSize.Level0) void TestFuncAddWait(int& i) { + std::unique_lock lk(g_mutex); ++g_times; printf("after func:%s0%d called, :%d\n", __func__, i, g_times); - std::unique_lock lk(g_mutex); g_cv.wait(lk, [] {return g_ready;}); printf("func:%s0%d received ready signal!\n", __func__, i); } void TestFuncSubWait(int& i) { + std::unique_lock lk(g_mutex); --g_times; printf("after func:%s0%d called, :%d\n", __func__, i, g_times); - std::unique_lock lk(g_mutex); g_cv.wait(lk, [] {return g_ready;}); printf("func:%s0%d received ready signal!\n", __func__, i); } -- Gitee From 48c5f4cccf42b0dff06bd43e8dc20a5245e2eb2f Mon Sep 17 00:00:00 2001 From: lwx1155083 Date: Wed, 15 Mar 2023 14:20:28 +0800 Subject: [PATCH 3/5] Increase branch coverage of ashmem's testcases. Issue:I6NB2O Test:UT Signed-off-by: lwx1155083 --- .../unittest/common/utils_ashmem_test.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/base/test/unittest/common/utils_ashmem_test.cpp b/base/test/unittest/common/utils_ashmem_test.cpp index a2a011b..d809159 100644 --- a/base/test/unittest/common/utils_ashmem_test.cpp +++ b/base/test/unittest/common/utils_ashmem_test.cpp @@ -320,3 +320,50 @@ HWTEST_F(UtilsAshmemTest, test_ashmem_InvalidOperation_005, TestSize.Level0) ashmem->CloseAshmem(); } + +/** + * @tc.name: test_ashmem_InvalidOperation_006 + * @tc.desc: test invalid input or test invalid operation + * @tc.type: FUNC + */ +HWTEST_F(UtilsAshmemTest, test_ashmem_InvalidOperation_006, TestSize.Level0) +{ + sptr ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE); + ASSERT_TRUE(ashmem != nullptr); + + bool ret = ashmem->MapReadAndWriteAshmem(); + ASSERT_TRUE(ret); + + ret = ashmem->WriteToAshmem(nullptr, sizeof(MEMORY_CONTENT), 0); + EXPECT_FALSE(ret); + + ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), MEMORY_SIZE+1); + EXPECT_FALSE(ret); + + ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), -1); + EXPECT_FALSE(ret); + + ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), MEMORY_SIZE+1, 0); + EXPECT_FALSE(ret); + + ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), -1, 0); + EXPECT_FALSE(ret); + + ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), MEMORY_SIZE); + EXPECT_FALSE(ret); + + ashmem->UnmapAshmem(); + ashmem->CloseAshmem(); + + ashmem->GetAshmemSize(); + EXPECT_FALSE(ret); + + ashmem->GetProtection(); + EXPECT_FALSE(ret); + + ashmem->UnmapAshmem(); + EXPECT_FALSE(ret); + + ashmem->CloseAshmem(); + EXPECT_FALSE(ret); +} -- Gitee From ea7699c799a68f1bbf42ab3974ac29e25281c991 Mon Sep 17 00:00:00 2001 From: liyiming13 Date: Wed, 15 Mar 2023 19:17:02 +0800 Subject: [PATCH 4/5] Implement Parcel-WriteVector test case Issue: I6NNYJ Test: unittest Signed-off-by: liyiming13 --- .../unittest/common/utils_parcel_test.cpp | 124 +++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) diff --git a/base/test/unittest/common/utils_parcel_test.cpp b/base/test/unittest/common/utils_parcel_test.cpp index e787d22..6690417 100644 --- a/base/test/unittest/common/utils_parcel_test.cpp +++ b/base/test/unittest/common/utils_parcel_test.cpp @@ -1081,6 +1081,128 @@ HWTEST_F(UtilsParcelTest, test_parcel_WriteAndReadVector_004, TestSize.Level0) } } +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteBoolVector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteInt8Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteInt16Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteInt32Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteInt64Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteUInt8Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteUInt16Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteUInt32Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteUInt64Vector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteFloatVector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteDoubleVector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteStringVector(vectorTest); +} + +bool CallWriteVector(Parcel &parcel, const std::vector &vectorTest) +{ + return parcel.WriteString16Vector(vectorTest); +} + +template +void ParcelWriteVector(const std::vector &vectorTest) +{ + Parcel parcel1(nullptr); + Parcel parcel2(nullptr); + bool result; + result = CallWriteVector(parcel1, vectorTest); + EXPECT_EQ(result, true); + + void *buffer = nullptr; + size_t size = parcel1.GetDataSize(); + if (!SendData(buffer, size, reinterpret_cast(parcel1.GetData()))) { + ASSERT_FALSE(false); + } + result = parcel2.ParseFrom(reinterpret_cast(buffer), parcel1.GetDataSize()); + EXPECT_EQ(result, true); + + result = CallWriteVector(parcel2, vectorTest); + EXPECT_EQ(result, false); +} + +/** + * @tc.name: test_parcel_WriteAndReadVector_005 + * @tc.desc: test vector parcel write failed. + * @tc.type: FUNC + */ +HWTEST_F(UtilsParcelTest, test_parcel_WriteAndReadVector_005, TestSize.Level0) +{ + vector boolVectorTest { true, false }; + vector int8VectorTest { 1, 0 }; + vector int16VectorTest { 1, 0 }; + vector int32VectorTest { 1, 0 }; + vector int64VectorTest { 1, 0 }; + vector uint8VectorTest { 1, 0 }; + vector uint16VectorTest { 1, 0 }; + vector uint32VectorTest { 1, 0 }; + vector uint64VectorTest { 1, 0 }; + vector floatVectorTest { 1.1, 0 }; + vector doubleVectorTest { 1.1, 0 }; + vector stringVectorTest { "true", "false" }; + vector string16VectorTest { u"true", u"false" }; + + ParcelWriteVector(boolVectorTest); + ParcelWriteVector(int8VectorTest); + ParcelWriteVector(int16VectorTest); + ParcelWriteVector(int32VectorTest); + ParcelWriteVector(int64VectorTest); + ParcelWriteVector(uint8VectorTest); + ParcelWriteVector(uint16VectorTest); + ParcelWriteVector(uint32VectorTest); + ParcelWriteVector(uint64VectorTest); + ParcelWriteVector(floatVectorTest); + ParcelWriteVector(doubleVectorTest); + ParcelWriteVector(stringVectorTest); + ParcelWriteVector(string16VectorTest); +} + class TestParcelable : public virtual Parcelable { public: TestParcelable() = default; @@ -1224,4 +1346,4 @@ HWTEST_F(UtilsParcelTest, test_SetMaxCapacity_002, TestSize.Level0) EXPECT_EQ(false, ret); } } // namespace -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee From ec47c5bd46ed4c04bc4be07ec74ae7d347ce5c22 Mon Sep 17 00:00:00 2001 From: hzl Date: Tue, 21 Mar 2023 20:23:20 +0800 Subject: [PATCH 5/5] test Change-Id: I12086e8a622fad06609cbeff364dde20ffd1e30f test delete Change-Id: I8b447ba98805eae6484414c377b3c4938c9bf199 IOS_PLATFORM test Change-Id: Ic44ea4c290c3c514395570197acfc60f22b170e4 IOS_PLATFORM Change-Id: I3b569ecd1be7bcf11b6b6cf716bf37eda7a03afb IOS_PLATFORM test Change-Id: I33e75126f5b796b62e517aca05daeba96a2a1da5 IOS_PLATFORM Change-Id: I6891f84077824d9a38ffbd218ca4048b480bccc5 Signed-off-by: lothilic@163.com IOS_PLATFORM test Change-Id: I685466310ff5db35782c04b5a99dc9917e1b2f4d IOS_PLATFORM Change-Id: I54dab281807207bd0fa35d33eb62a200e87b0e54 Mock test Change-Id: Ib0f2a78050f14b1e61efea8ef75ae4b682ae3c1e Mock test Change-Id: Idce4ca6630cd079419649987602ce71c660b5ce1 Mock test Change-Id: Ia2ca4f92db338b0d8460f8180921efca37fd66f6 MOCK test Change-Id: I89bddb4d2cfd8b0f44529f054557279a983475fd Mock test Change-Id: Ie1909496b3aa035e8593c17f565207209aa861a6 _APPLE Change-Id: I9db24c680f7aaa79742692cbfe2d81441b0c7d2c _APPLE Change-Id: I6bcaab5e9e4a7254d1ec9742fa54f14bb190f096 --- base/BUILD.gn | 23 +++++++++++++++++++++++ base/include/ashmem.h | 7 +++++-- base/include/directory_ex.h | 6 ++++++ base/include/flat_obj.h | 8 +++++++- base/include/parcel.h | 1 - base/include/string_ex.h | 3 ++- base/src/ashmem.cpp | 12 ++++++++---- base/src/directory_ex.cpp | 27 +++++++++++++++++++++++++++ base/src/string_ex.cpp | 14 +++++++++++--- 9 files changed, 89 insertions(+), 12 deletions(-) diff --git a/base/BUILD.gn b/base/BUILD.gn index 46e668b..0ee50d7 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -94,6 +94,8 @@ ohos_static_library("utilsbase") { all_dependent_configs = [ ":utils_all_dependent_configs" ] if (current_os != "android" && current_os != "ios") { defines = [ "CONFIG_HILOG" ] + }else if(current_os == "ios") { + defines = ["_APPLE"] } external_deps = [ "hilog_native:libhilog_base" ] public_deps = [ "//third_party/bounds_checking_function:libsec_static" ] @@ -117,6 +119,25 @@ ohos_static_library("utilsbase_rtti") { part_name = "c_utils" } +if(current_os == "ios") { + ohos_static_library("utils_static_ios") { + sources = [ + "src/directory_ex.cpp", + "src/parcel.cpp", + "src/refbase.cpp", + "src/rwlock.cpp", + "src/string_ex.cpp", + ] + configs = [ ":utils_coverage_config" ] + all_dependent_configs = [ ":utils_all_dependent_configs" ] + defines = ["_APPLE"] + public_deps = [ "//third_party/bounds_checking_function:libsec_static" ] + + subsystem_name = "commonlibrary" + part_name = "c_utils" + } +} + ohos_shared_library("utils") { sources = sources_utils configs = [ ":utils_coverage_config" ] @@ -132,6 +153,8 @@ ohos_shared_library("utils") { all_dependent_configs = [ ":utils_all_dependent_configs" ] if (current_os != "android" && current_os != "ios") { defines = [ "CONFIG_HILOG" ] + }else if(current_os == "ios"){ + defines = ["_APPLE"] } external_deps = [ "hilog_native:libhilog_base" ] public_deps = [ "//third_party/bounds_checking_function:libsec_shared" ] diff --git a/base/include/ashmem.h b/base/include/ashmem.h index f7d855e..970a193 100644 --- a/base/include/ashmem.h +++ b/base/include/ashmem.h @@ -23,10 +23,13 @@ #ifndef UTILS_BASE_ASHMEM_H #define UTILS_BASE_ASHMEM_H +#ifndef _APPLE + #include +#endif + #include -#include -#include "refbase.h" #include "parcel.h" +#include "refbase.h" namespace OHOS { /** diff --git a/base/include/directory_ex.h b/base/include/directory_ex.h index 1686b24..ff31da1 100644 --- a/base/include/directory_ex.h +++ b/base/include/directory_ex.h @@ -142,5 +142,11 @@ bool ChangeModeDirectory(const std::string& path, const mode_t& mode); * @return Return true if get success, else false. */ bool PathToRealPath(const std::string& path, std::string& realPath); + +/** + * The TransformFileName function transform the input file name for windows or mac. + */ +std::string TransformFileName(const std::string& fileName); + } // OHOS #endif diff --git a/base/include/flat_obj.h b/base/include/flat_obj.h index be109d5..22ca8af 100644 --- a/base/include/flat_obj.h +++ b/base/include/flat_obj.h @@ -25,7 +25,13 @@ #define UTILS_BASE_FLAT_OBJ_H #include -#include + +#ifndef _APPLE + #include +#else + typedef u_int32_t __u32; + typedef u_int64_t __u64; +#endif #ifdef BINDER_IPC_32BIT typedef __u32 binder_size_t; diff --git a/base/include/parcel.h b/base/include/parcel.h index 3ce1b4d..c78f685 100644 --- a/base/include/parcel.h +++ b/base/include/parcel.h @@ -29,7 +29,6 @@ #include "nocopyable.h" #include "refbase.h" #include "flat_obj.h" - namespace OHOS { class Parcel; diff --git a/base/include/string_ex.h b/base/include/string_ex.h index 2d5b4b8..3e8a861 100644 --- a/base/include/string_ex.h +++ b/base/include/string_ex.h @@ -213,7 +213,7 @@ bool IsSameTextStr(const std::string& first, const std::string& second); * @return Return true if all are ASCII encoded, otherwise false. */ bool IsAsciiString(const std::string& str); - +#ifndef _APPLE /** * @ingroup StringOperation * @brief Convert a string from UTF-16 to UTF-8 encoded. @@ -231,6 +231,7 @@ std::string Str16ToStr8(const std::u16string& str16); * @return If converting failed, return an empty `std::u16string` object. */ std::u16string Str8ToStr16(const std::string& str); +#endif } // namespace OHOS #endif // STRING_EX_H diff --git a/base/src/ashmem.cpp b/base/src/ashmem.cpp index 6a85f02..0884a37 100644 --- a/base/src/ashmem.cpp +++ b/base/src/ashmem.cpp @@ -12,23 +12,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "ashmem.h" +#ifndef _APPLE #include #include #include + #include -#include #include +#include +#include + +#include #include #include #include #include #include #include -#include -#include + #include "securec.h" #include "utils_log.h" @@ -255,3 +258,4 @@ bool Ashmem::CheckValid(int32_t size, int32_t offset, int cmd) return true; } } +#endif diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index fd8a366..d5bfa7b 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -289,4 +289,31 @@ bool PathToRealPath(const string& path, string& realPath) return true; } +string TransformFileName(const string& fileName) +{ + string::size_type pos = fileName.find("."); + string transformfileName = ""; + if (pos == string::npos) { + transformfileName = fileName; + +#ifdef _WIN32 + transformfileName = transformfileName.append(".dll"); +#elif defined _APPLE + transformfileName = transformfileName.append(".dylib"); +#endif + + return transformfileName; + } else { + transformfileName = string(fileName).substr(0, pos + 1); + +#ifdef _WIN32 + transformfileName = transformfileName.append("dll"); +#elif defined _APPLE + transformfileName = transformfileName.append("dylib"); +#endif + + return transformfileName; + } +} + } // OHOS diff --git a/base/src/string_ex.cpp b/base/src/string_ex.cpp index 32340ae..a8b923c 100644 --- a/base/src/string_ex.cpp +++ b/base/src/string_ex.cpp @@ -106,14 +106,21 @@ bool StrToInt(const string& str, int& value) } char* end = nullptr; - errno = 0; +#ifndef _APPLE + errno = 0;//1 +#endif auto addr = str.c_str(); auto result = strtol(addr, &end, 10); /* 10 means decimal */ +#ifndef _APPLE if ((end == addr) || (end[0] != '\0') || (errno == ERANGE) || (result > INT_MAX) || (result < INT_MIN)) { return false; } - +#else + if ((end == addr) || (end[0] != '\0') || (result > INT_MAX) || (result < INT_MIN)) { + return false; + } +#endif value = static_cast(result); return true; } @@ -239,7 +246,7 @@ bool IsAsciiString(const string& str) return true; } - +#ifndef _APPLE u16string Str8ToStr16(const string& str) { u16string str16Value; @@ -259,4 +266,5 @@ string Str16ToStr8(const u16string& str16) return str8Value; } +#endif } // namespace OHOS -- Gitee