diff --git a/interfaces/inner_api/dlp_fuse/include/dlp_link_file.h b/interfaces/inner_api/dlp_fuse/include/dlp_link_file.h index 3a1040eaacd321ddd437ff8667441243c6dd8faa..73894ee2a12f24eb3227eb3b7408e88198bdcdd5 100644 --- a/interfaces/inner_api/dlp_fuse/include/dlp_link_file.h +++ b/interfaces/inner_api/dlp_fuse/include/dlp_link_file.h @@ -42,8 +42,8 @@ public: struct stat GetLinkStat(); void UpdateAtimeStat(); void UpdateMtimeStat(); - int32_t Write(uint32_t offset, void* buf, uint32_t size); - int32_t Read(uint32_t offset, void* buf, uint32_t size, uint32_t uid); + int32_t Write(off_t offset, void* buf, size_t size, size_t& resSize); + int32_t Read(off_t offset, void* buf, size_t size, size_t& resSize, int32_t uid); std::shared_ptr GetDlpFilePtr() { return dlpFile_; diff --git a/interfaces/inner_api/dlp_fuse/src/dlp_link_file.cpp b/interfaces/inner_api/dlp_fuse/src/dlp_link_file.cpp index e5b631317b42dbfd18caef5d382a1d7c0cab382f..35fcb3512c48166d7db3f8c7a5c4781a18cb865c 100644 --- a/interfaces/inner_api/dlp_fuse/src/dlp_link_file.cpp +++ b/interfaces/inner_api/dlp_fuse/src/dlp_link_file.cpp @@ -129,7 +129,7 @@ void DlpLinkFile::UpdateMtimeStat() UpdateCurrTimeStat(&fileStat_.st_mtim); } -int32_t DlpLinkFile::Write(uint32_t offset, void* buf, uint32_t size) +int32_t DlpLinkFile::Write(off_t offset, void* buf, size_t size, size_t& resSize) { if (stopLinkFlag_) { DLP_LOG_INFO(LABEL, "linkFile is stopping link"); @@ -140,7 +140,7 @@ int32_t DlpLinkFile::Write(uint32_t offset, void* buf, uint32_t size) DLP_LOG_ERROR(LABEL, "Write link file fail, dlp file is null"); return DLP_FUSE_ERROR_DLP_FILE_NULL; } - int32_t res = dlpFile_->DlpFileWrite(offset, buf, size); + int32_t res = dlpFile_->DlpFileWrite(offset, buf, size, resSize); if (res < 0) { DLP_LOG_ERROR(LABEL, "Write link file fail, err=%{public}d.", res); } @@ -148,7 +148,7 @@ int32_t DlpLinkFile::Write(uint32_t offset, void* buf, uint32_t size) return res; } -int32_t DlpLinkFile::Read(uint32_t offset, void* buf, uint32_t size, uint32_t uid) +int32_t DlpLinkFile::Read(off_t offset, void* buf, size_t size, size_t& resSize, int32_t uid) { if (stopLinkFlag_) { DLP_LOG_INFO(LABEL, "linkFile is stopping link"); @@ -160,7 +160,8 @@ int32_t DlpLinkFile::Read(uint32_t offset, void* buf, uint32_t size, uint32_t ui return DLP_FUSE_ERROR_DLP_FILE_NULL; } UpdateAtimeStat(); - int32_t res = dlpFile_->DlpFileRead(offset, buf, size, hasRead_, uid); + resSize = size; + int32_t res = dlpFile_->DlpFileRead(offset, buf, resSize, hasRead_, uid); if (res < 0) { DLP_LOG_ERROR(LABEL, "Read link file failed, res %{public}d.", res); } diff --git a/interfaces/inner_api/dlp_fuse/src/fuse_daemon.cpp b/interfaces/inner_api/dlp_fuse/src/fuse_daemon.cpp index 2537d754bef828041a70cc74a569c2730d3598de..e009d44c625064e2b45a25b2e1ec8fae7cf0083f 100755 --- a/interfaces/inner_api/dlp_fuse/src/fuse_daemon.cpp +++ b/interfaces/inner_api/dlp_fuse/src/fuse_daemon.cpp @@ -195,14 +195,15 @@ static void FuseDaemonRead(fuse_req_t req, fuse_ino_t ino, size_t size, off_t of return; } (void)memset_s(buf, size, 0, size); - int32_t res = dlp->Read(static_cast(offset), buf, static_cast(size), req->ctx.uid); + size_t resSize; + int32_t res = dlp->Read(offset, buf, size, resSize, req->ctx.uid); if (res < 0) { fuse_reply_err(req, EIO); } else { - fuse_reply_buf(req, buf, static_cast(res)); + fuse_reply_buf(req, buf, resSize); } - DLP_LOG_DEBUG(LABEL, "Read file name %{private}s offset %{public}u size %{public}u res %{public}d", - dlp->GetLinkName().c_str(), static_cast(offset), static_cast(size), res); + DLP_LOG_DEBUG(LABEL, "Read file name %{private}s offset %{public}zu size %{public}zu res %{public}d", + dlp->GetLinkName().c_str(), static_cast(offset), size, res); free(buf); } @@ -224,15 +225,15 @@ static void FuseDaemonWrite( DLP_LOG_ERROR(LABEL, "Write link file fail, wrong ino"); return; } - int32_t res = dlp->Write(static_cast(off), - const_cast(static_cast(buf)), static_cast(size)); + size_t resSize; + int32_t res = dlp->Write(off, const_cast(static_cast(buf)), size, resSize); if (res < 0) { fuse_reply_err(req, EIO); } else { - fuse_reply_write(req, static_cast(res)); + fuse_reply_write(req, static_cast(resSize)); } - DLP_LOG_DEBUG(LABEL, "Write file name %{private}s offset %{public}u size %{public}u res %{public}d", - dlp->GetLinkName().c_str(), static_cast(off), static_cast(size), res); + DLP_LOG_DEBUG(LABEL, "Write file name %{private}s offset %{public}zu size %{public}zu res %{public}zu", + dlp->GetLinkName().c_str(), static_cast(off), size, resSize); } static void FuseDaemonForget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) diff --git a/interfaces/inner_api/dlp_fuse/test/unittest/src/dlp_fuse_test.cpp b/interfaces/inner_api/dlp_fuse/test/unittest/src/dlp_fuse_test.cpp index e3e1113fd21698b23e27461bf5b5501a1dc77806..50a5504becbfa290aa853fb3c60cec62ea199c1f 100644 --- a/interfaces/inner_api/dlp_fuse/test/unittest/src/dlp_fuse_test.cpp +++ b/interfaces/inner_api/dlp_fuse/test/unittest/src/dlp_fuse_test.cpp @@ -969,15 +969,15 @@ HWTEST_F(DlpFuseTest, LinkFileWrite001, TestSize.Level1) DLP_LOG_INFO(LABEL, "LinkFileWrite001"); std::shared_ptr filePtr = nullptr; DlpLinkFile linkFile("linkfile", filePtr); - + size_t resSize; uint8_t buffer[16] = {0}; - EXPECT_EQ(linkFile.Write(0, buffer, 15), DLP_FUSE_ERROR_DLP_FILE_NULL); + EXPECT_EQ(linkFile.Write(0, buffer, 15, resSize), DLP_FUSE_ERROR_DLP_FILE_NULL); filePtr = std::make_shared(-1, DLP_TEST_DIR, 0, false); ASSERT_NE(filePtr, nullptr); DlpLinkFile linkFile1("linkfile1", filePtr); - EXPECT_EQ(linkFile1.Write(0, buffer, 15), DLP_PARSE_ERROR_FILE_READ_ONLY); + EXPECT_EQ(linkFile1.Write(0, buffer, 15, resSize), DLP_PARSE_ERROR_FILE_READ_ONLY); } /** @@ -993,13 +993,14 @@ HWTEST_F(DlpFuseTest, LinkFileRead001, TestSize.Level1) DlpLinkFile linkFile("linkfile", filePtr); linkFile.hasRead_ = true; uint8_t buffer[16] = {0}; - EXPECT_EQ(linkFile.Read(0, buffer, 15, 0), DLP_FUSE_ERROR_DLP_FILE_NULL); + size_t resSize; + EXPECT_EQ(linkFile.Read(0, buffer, 15, resSize, 0), DLP_FUSE_ERROR_DLP_FILE_NULL); filePtr = std::make_shared(-1, DLP_TEST_DIR, 0, false); ASSERT_NE(filePtr, nullptr); DlpLinkFile linkFile1("linkfile1", filePtr); - EXPECT_EQ(linkFile1.Read(0, buffer, 15, 0), DLP_PARSE_ERROR_VALUE_INVALID); + EXPECT_EQ(linkFile1.Read(0, buffer, 15, resSize, 0), DLP_PARSE_ERROR_VALUE_INVALID); } /** @@ -1016,8 +1017,9 @@ HWTEST_F(DlpFuseTest, Truncate001, TestSize.Level1) EXPECT_EQ(linkFile.Truncate(-1), DLP_LINK_FILE_NOT_ALLOW_OPERATE); uint8_t buffer[16] = {0}; linkFile.hasRead_ = true; - EXPECT_EQ(linkFile.Write(0, buffer, 15), DLP_LINK_FILE_NOT_ALLOW_OPERATE); - EXPECT_EQ(linkFile.Read(0, buffer, 15, 0), DLP_LINK_FILE_NOT_ALLOW_OPERATE); + size_t resSize; + EXPECT_EQ(linkFile.Write(0, buffer, 15, resSize), DLP_LINK_FILE_NOT_ALLOW_OPERATE); + EXPECT_EQ(linkFile.Read(0, buffer, 15, resSize, 0), DLP_LINK_FILE_NOT_ALLOW_OPERATE); } /** diff --git a/interfaces/inner_api/dlp_parse/include/dlp_crypt.h b/interfaces/inner_api/dlp_parse/include/dlp_crypt.h index c91ad683744456825d325ccdffb286b4016078ca..82773c96ed2a8dd5e5bd0e8a0afcbe29bac27766 100644 --- a/interfaces/inner_api/dlp_parse/include/dlp_crypt.h +++ b/interfaces/inner_api/dlp_parse/include/dlp_crypt.h @@ -17,7 +17,7 @@ #define DLP_CRYPT_H #include - +#include #ifdef __cplusplus extern "C" { #endif @@ -60,7 +60,7 @@ enum DlpKeySize { }; struct DlpBlob { - uint32_t size = 0; + size_t size = 0; uint8_t* data = nullptr; }; @@ -118,16 +118,6 @@ int32_t DlpOpensslAesDecryptFinal(void** cryptoCtx, const struct DlpBlob* messag void DlpOpensslAesHalFreeCtx(void** cryptoCtx); -int32_t DlpOpensslHash(uint32_t alg, const struct DlpBlob* msg, struct DlpBlob* hash); - -int32_t DlpOpensslHashInit(void** cryptoCtx, uint32_t alg); - -int32_t DlpOpensslHashUpdate(void* cryptoCtx, const struct DlpBlob* msg); - -int32_t DlpOpensslHashFinal(void** cryptoCtx, const struct DlpBlob* msg, struct DlpBlob* hash); - -int32_t DlpOpensslHashFreeCtx(void** cryptoCtx); - int32_t DlpCtrModeIncreaeIvCounter(struct DlpBlob& iv, uint32_t count); int32_t DlpHmacEncode(const DlpBlob& key, int32_t fd, DlpBlob& out); diff --git a/interfaces/inner_api/dlp_parse/include/dlp_file.h b/interfaces/inner_api/dlp_parse/include/dlp_file.h index 894fa0cf94ef00bd68a847b23da4b1e2f5b22767..3f49c06ecd65762eeb9dae8541876835cf420b0c 100755 --- a/interfaces/inner_api/dlp_parse/include/dlp_file.h +++ b/interfaces/inner_api/dlp_parse/include/dlp_file.h @@ -156,10 +156,10 @@ public: void SetOfflineAccess(bool flag); bool GetOfflineAccess(); int32_t GenFile(int32_t inPlainFileFd); - int32_t RemoveDlpPermission(int outPlainFileFd); - int32_t DlpFileRead(uint32_t offset, void* buf, uint32_t size, bool& hasRead, int32_t uid); - int32_t DlpFileWrite(uint32_t offset, void* buf, uint32_t size); - uint32_t GetFsContentSize() const; + int32_t RemoveDlpPermission(int32_t outPlainFileFd); + int32_t DlpFileRead(off_t offset, void* buf, size_t& size, bool& hasRead, int32_t uid); + int32_t DlpFileWrite(off_t offset, void* buf, size_t size, size_t& resSize); + size_t GetFsContentSize() const; bool UpdateDlpFilePermission(); int32_t CheckDlpFile(); bool NeedAdapter(); @@ -193,7 +193,7 @@ public: return authPerm_; }; - int32_t Truncate(uint32_t size); + int32_t Truncate(size_t size); int32_t dlpFd_; private: @@ -203,23 +203,22 @@ private: const struct DlpBlob& hmacKey) const; int32_t CopyBlobParam(const struct DlpBlob& src, struct DlpBlob& dst) const; int32_t CleanBlobParam(struct DlpBlob& blob) const; - int32_t UpdateFileCertData(); int32_t PrepareBuff(struct DlpBlob& message1, struct DlpBlob& message2) const; int32_t GetLocalAccountName(std::string& account) const; int32_t GetDomainAccountName(std::string& account) const; - int32_t DoDlpContentCryptyOperation(int32_t inFd, int32_t outFd, uint32_t inOffset, - uint32_t inFileLen, bool isEncrypt); - int32_t DoDlpContentCopyOperation(int32_t inFd, int32_t outFd, uint32_t inOffset, uint32_t inFileLen); - int32_t WriteHeadAndCert(int tmpFile, const std::vector& offlineCert); + int32_t DoDlpContentCryptyOperation(int32_t inFd, int32_t outFd, off_t inOffset, + off_t inFileLen, bool isEncrypt); + int32_t DoDlpContentCopyOperation(int32_t inFd, int32_t outFd, off_t inOffset, off_t inFileLen); + int32_t WriteHeadAndCert(int32_t tmpFile, const std::vector& offlineCert); int32_t DupUsageSpec(struct DlpUsageSpec& spec); int32_t DoDlpBlockCryptOperation(struct DlpBlob& message1, - struct DlpBlob& message2, uint32_t offset, bool isEncrypt); - int32_t WriteFirstBlockData(uint32_t offset, void* buf, uint32_t size); - int32_t FillHoleData(uint32_t holeStart, uint32_t holeSize); - int32_t DoDlpFileWrite(uint32_t offset, void* buf, uint32_t size); + struct DlpBlob& message2, off_t offset, bool isEncrypt); + int32_t WriteFirstBlockData(off_t offset, void* buf, size_t size, size_t& resSize); + int32_t FillHoleData(size_t holeStart, size_t holeSize); + int32_t DoDlpFileWrite(off_t offset, void* buf, size_t size, size_t& resSize); int32_t UpdateDlpFileContentSize(); - int32_t UpdateFile(int tmpFile, const std::vector& cert, uint32_t oldTxtOffset); - int32_t GetTempFile(const std::string& workDir, int& tempFile, std::string& path); + int32_t UpdateFile(int32_t tmpFile, const std::vector& cert, uint32_t oldTxtOffset); + int32_t GetTempFile(const std::string& workDir, int32_t& tempFile, std::string& path); bool ParseDlpInfo(); bool ParseCert(); bool ParseEncData(); diff --git a/interfaces/inner_api/dlp_parse/src/dlp_crypt.cpp b/interfaces/inner_api/dlp_parse/src/dlp_crypt.cpp index a9332ed869d091107eb5e5cd2f657a1dd463010a..e252c839d6826d4db80a652d2995f27648592742 100644 --- a/interfaces/inner_api/dlp_parse/src/dlp_crypt.cpp +++ b/interfaces/inner_api/dlp_parse/src/dlp_crypt.cpp @@ -728,192 +728,6 @@ int32_t DlpOpensslAesDecrypt(const struct DlpBlob* key, const struct DlpUsageSpe return ret; } -static bool CheckDigestAlg(uint32_t alg) -{ - switch (alg) { - case DLP_DIGEST_SHA256: - case DLP_DIGEST_SHA384: - case DLP_DIGEST_SHA512: - return true; - default: - return false; - } -} - -const EVP_MD* GetOpensslAlg(uint32_t alg) -{ - switch (alg) { - case DLP_DIGEST_SHA256: - return EVP_sha256(); - case DLP_DIGEST_SHA384: - return EVP_sha384(); - case DLP_DIGEST_SHA512: - return EVP_sha512(); - default: - return nullptr; - } -} - -static uint32_t GetHashLen(uint32_t alg) -{ - if (alg == DLP_DIGEST_SHA256) { - return SHA256_LEN; - } else if (alg == DLP_DIGEST_SHA384) { - return SHA384_LEN; - } else { - return SHA512_LEN; - } -} - -static bool HashCheckParam(uint32_t alg, const struct DlpBlob* msg, struct DlpBlob* hash) -{ - if (!CheckDigestAlg(alg)) { - DLP_LOG_ERROR(LABEL, "Check hash param fail, alg type is unsupported"); - return false; - } - - if (!DlpOpensslCheckBlob(hash)) { - DLP_LOG_ERROR(LABEL, "Check hash param fail, hash is invalid"); - return false; - } - - uint32_t hashLen = GetHashLen(alg); - if (hash->size < hashLen) { - DLP_LOG_ERROR(LABEL, "Check hash param fail, hash buff too short"); - return false; - } - - if (!DlpOpensslCheckBlob(msg)) { - DLP_LOG_ERROR(LABEL, "Check hash param fail, msg is invalid"); - return false; - } - - return true; -} - -int32_t DlpOpensslHash(uint32_t alg, const struct DlpBlob* msg, struct DlpBlob* hash) -{ - if (!HashCheckParam(alg, msg, hash)) { - DLP_LOG_ERROR(LABEL, "Openssl hash fail, param is invalid"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - - const EVP_MD* opensslAlg = GetOpensslAlg(alg); - if (opensslAlg == nullptr) { - DLP_LOG_ERROR(LABEL, "Openssl hash fail, get alg fail"); - return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; - } - - int32_t ret = EVP_Digest(msg->data, msg->size, hash->data, &hash->size, opensslAlg, nullptr); - if (ret != DLP_OPENSSL_SUCCESS) { - DlpLogOpensslError(); - return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; - } - return DLP_OK; -} - -int32_t DlpOpensslHashInit(void** cryptoCtx, uint32_t alg) -{ - if (cryptoCtx == nullptr) { - DLP_LOG_ERROR(LABEL, "Openssl hash init fail, ctx is null"); - return DLP_PARSE_ERROR_DIGEST_INVALID; - } - if (!CheckDigestAlg(alg)) { - DLP_LOG_ERROR(LABEL, "Openssl hash init fail, alg is invalid"); - return DLP_PARSE_ERROR_DIGEST_INVALID; - } - const EVP_MD* opensslAlg = GetOpensslAlg(alg); - if (opensslAlg == nullptr) { - DLP_LOG_ERROR(LABEL, "Openssl hash init fail, get alg fail"); - return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; - } - EVP_MD_CTX* tmpctx = EVP_MD_CTX_new(); - if (tmpctx == nullptr) { - DLP_LOG_ERROR(LABEL, "Openssl hash init fail, alloc ctx fail"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - - EVP_MD_CTX_set_flags(tmpctx, EVP_MD_CTX_FLAG_ONESHOT); - int32_t ret = EVP_DigestInit_ex(tmpctx, opensslAlg, nullptr); - if (ret != DLP_OPENSSL_SUCCESS) { - DlpLogOpensslError(); - EVP_MD_CTX_free(tmpctx); - return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; - } - *cryptoCtx = static_cast(tmpctx); - return DLP_OK; -} - -int32_t DlpOpensslHashUpdate(void* cryptoCtx, const struct DlpBlob* msg) -{ - if (cryptoCtx == nullptr) { - DLP_LOG_ERROR(LABEL, "Openssl hash update fail, ctx is null"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - if (!DlpOpensslCheckBlob(msg)) { - DLP_LOG_ERROR(LABEL, "Openssl hash update fail, msg is invalid"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - - int32_t ret = EVP_DigestUpdate( - reinterpret_cast(cryptoCtx), reinterpret_cast(msg->data), msg->size); - if (ret != DLP_OPENSSL_SUCCESS) { - DlpLogOpensslError(); - return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; - } - return DLP_OK; -} - -int32_t DlpOpensslHashFinal(void** cryptoCtx, const struct DlpBlob* msg, struct DlpBlob* hash) -{ - if (cryptoCtx == nullptr || *cryptoCtx == nullptr) { - DLP_LOG_ERROR(LABEL, "Openssl hash final fail, ctx is null"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - if (!DlpOpensslCheckBlobZero(msg)) { - DLP_LOG_ERROR(LABEL, "Openssl hash final fail, msg is invalid"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - if (!DlpOpensslCheckBlob(hash)) { - DLP_LOG_ERROR(LABEL, "Openssl hash final fail, hash is invalid"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - - int32_t ret; - if (msg->size != 0) { - ret = EVP_DigestUpdate((EVP_MD_CTX*)*cryptoCtx, msg->data, msg->size); - if (ret != DLP_OPENSSL_SUCCESS) { - DlpLogOpensslError(); - EVP_MD_CTX_free((EVP_MD_CTX*)*cryptoCtx); - *cryptoCtx = nullptr; - return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; - } - } - - ret = EVP_DigestFinal_ex((EVP_MD_CTX*)*cryptoCtx, hash->data, &hash->size); - if (ret != DLP_OPENSSL_SUCCESS) { - DlpLogOpensslError(); - EVP_MD_CTX_free((EVP_MD_CTX*)*cryptoCtx); - *cryptoCtx = nullptr; - return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; - } - - EVP_MD_CTX_free((EVP_MD_CTX*)*cryptoCtx); - *cryptoCtx = nullptr; - return DLP_OK; -} - -int32_t DlpOpensslHashFreeCtx(void** cryptoCtx) -{ - if (cryptoCtx == nullptr || *cryptoCtx == nullptr) { - DLP_LOG_ERROR(LABEL, "Openssl hash free ctx fail, param is invalid"); - return DLP_PARSE_ERROR_VALUE_INVALID; - } - EVP_MD_CTX_free((EVP_MD_CTX*)*cryptoCtx); - *cryptoCtx = nullptr; - return DLP_OK; -} - static void IncIvCounterLitteEndian(struct DlpBlob& iv, uint32_t count) { uint8_t* data = iv.data; @@ -942,11 +756,11 @@ int32_t DlpCtrModeIncreaeIvCounter(struct DlpBlob& iv, uint32_t count) int32_t DlpHmacEncode(const DlpBlob& key, int32_t fd, DlpBlob& out) { if ((key.data == nullptr) || (key.size != SHA256_KEY_LEN)) { - DLP_LOG_ERROR(LABEL, "Key blob invalid, size %{public}u", key.size); + DLP_LOG_ERROR(LABEL, "Key blob invalid, size %{public}zu", key.size); return DLP_PARSE_ERROR_DIGEST_INVALID; } if ((out.data == nullptr) || (out.size < HMAC_SIZE)) { - DLP_LOG_ERROR(LABEL, "Output blob invalid, size %{public}u", out.size); + DLP_LOG_ERROR(LABEL, "Output blob invalid, size %{public}zu", out.size); return DLP_PARSE_ERROR_DIGEST_INVALID; } @@ -970,11 +784,13 @@ int32_t DlpHmacEncode(const DlpBlob& key, int32_t fd, DlpBlob& out) return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; } } - if (HMAC_Final(ctx, out.data, &out.size) != 1) { + unsigned int len; + if (HMAC_Final(ctx, out.data, &len) != 1) { DLP_LOG_ERROR(LABEL, "HMAC_Final failed"); HMAC_CTX_free(ctx); return DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR; } + out.size = len; HMAC_CTX_free(ctx); return DLP_OK; } diff --git a/interfaces/inner_api/dlp_parse/src/dlp_file.cpp b/interfaces/inner_api/dlp_parse/src/dlp_file.cpp index 04d689ba312c128daff2911c4771b5004cde7a93..a0e11617fc9695d3845ed33b9edd2a05a824110b 100755 --- a/interfaces/inner_api/dlp_parse/src/dlp_file.cpp +++ b/interfaces/inner_api/dlp_parse/src/dlp_file.cpp @@ -717,7 +717,7 @@ int32_t DlpFile::UpdateFile(int32_t tmpFile, const std::vector& cert, u if (ret != DLP_OK) { return ret; } - int32_t fileSize = lseek(tmpFile, 0, SEEK_CUR); + off_t fileSize = lseek(tmpFile, 0, SEEK_CUR); (void)lseek(tmpFile, 0, SEEK_SET); (void)lseek(dlpFd_, 0, SEEK_SET); ret = DoDlpContentCopyOperation(tmpFile, dlpFd_, 0, fileSize); @@ -875,7 +875,7 @@ int32_t DlpFile::DupUsageSpec(struct DlpUsageSpec& spec) } int32_t DlpFile::DoDlpBlockCryptOperation(struct DlpBlob& message1, struct DlpBlob& message2, - uint32_t offset, bool isEncrypt) + off_t offset, bool isEncrypt) { if (offset % DLP_BLOCK_SIZE != 0 || message1.data == nullptr || message1.size == 0 || message2.data == nullptr || message2.size == 0) { @@ -883,7 +883,7 @@ int32_t DlpFile::DoDlpBlockCryptOperation(struct DlpBlob& message1, struct DlpBl return DLP_PARSE_ERROR_VALUE_INVALID; } - uint32_t counterIndex = offset / DLP_BLOCK_SIZE; + off_t counterIndex = offset / DLP_BLOCK_SIZE; struct DlpUsageSpec spec; if (DupUsageSpec(spec) != DLP_OK) { DLP_LOG_ERROR(LABEL, "spec dup failed"); @@ -902,8 +902,8 @@ int32_t DlpFile::DoDlpBlockCryptOperation(struct DlpBlob& message1, struct DlpBl return DLP_OK; } -int32_t DlpFile::DoDlpContentCryptyOperation(int32_t inFd, int32_t outFd, uint32_t inOffset, - uint32_t inFileLen, bool isEncrypt) +int32_t DlpFile::DoDlpContentCryptyOperation(int32_t inFd, int32_t outFd, off_t inOffset, + off_t inFileLen, bool isEncrypt) { struct DlpBlob message, outMessage; if (PrepareBuff(message, outMessage) != DLP_OK) { @@ -911,10 +911,10 @@ int32_t DlpFile::DoDlpContentCryptyOperation(int32_t inFd, int32_t outFd, uint32 return DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL; } - uint32_t dlpContentOffset = inOffset; + off_t dlpContentOffset = inOffset; int32_t ret = DLP_OK; while (inOffset < inFileLen) { - uint32_t readLen = ((inFileLen - inOffset) < DLP_BUFF_LEN) ? (inFileLen - inOffset) : DLP_BUFF_LEN; + off_t readLen = ((inFileLen - inOffset) < DLP_BUFF_LEN) ? (inFileLen - inOffset) : DLP_BUFF_LEN; (void)memset_s(message.data, DLP_BUFF_LEN, 0, DLP_BUFF_LEN); (void)memset_s(outMessage.data, DLP_BUFF_LEN, 0, DLP_BUFF_LEN); if (read(inFd, message.data, readLen) != (ssize_t)readLen) { @@ -945,7 +945,7 @@ int32_t DlpFile::DoDlpContentCryptyOperation(int32_t inFd, int32_t outFd, uint32 return ret; } -int32_t DlpFile::DoDlpContentCopyOperation(int32_t inFd, int32_t outFd, uint32_t inOffset, uint32_t inFileLen) +int32_t DlpFile::DoDlpContentCopyOperation(int32_t inFd, int32_t outFd, off_t inOffset, off_t inFileLen) { if (inOffset > inFileLen) { return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; @@ -958,7 +958,7 @@ int32_t DlpFile::DoDlpContentCopyOperation(int32_t inFd, int32_t outFd, uint32_t int32_t ret = DLP_OK; while (inOffset < inFileLen) { - uint32_t readLen = ((inFileLen - inOffset) < DLP_BUFF_LEN) ? (inFileLen - inOffset) : DLP_BUFF_LEN; + off_t readLen = ((inFileLen - inOffset) < DLP_BUFF_LEN) ? (inFileLen - inOffset) : DLP_BUFF_LEN; (void)memset_s(data, DLP_BUFF_LEN, 0, DLP_BUFF_LEN); if (read(inFd, data, readLen) != (ssize_t)readLen) { @@ -978,9 +978,9 @@ int32_t DlpFile::DoDlpContentCopyOperation(int32_t inFd, int32_t outFd, uint32_t return ret; } -static int32_t GetFileSize(int32_t fd) +static off_t GetFileSize(int32_t fd) { - int32_t fileLen = lseek(fd, 0, SEEK_END); + off_t fileLen = lseek(fd, 0, SEEK_END); (void)lseek(fd, 0, SEEK_SET); return fileLen; } @@ -1004,7 +1004,7 @@ int32_t DlpFile::GenEncData(int32_t inPlainFileFd) if (inPlainFileFd == -1) { encFile = open(DLP_OPENING_ENC_DATA.c_str(), O_RDWR); } else { - int32_t fileLen = GetFileSize(inPlainFileFd); + off_t fileLen = GetFileSize(inPlainFileFd); OPEN_AND_CHECK(encFile, DLP_OPENING_ENC_DATA.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR, DLP_PARSE_ERROR_FILE_OPERATE_FAIL, LABEL); encDataFd_ = encFile; @@ -1019,7 +1019,7 @@ int32_t DlpFile::GenerateHmacVal(int32_t encFile, struct DlpBlob& out) { lseek(encFile, 0, SEEK_SET); int32_t fd = dup(encFile); - int32_t fileLen = GetFileSize(fd); + off_t fileLen = GetFileSize(fd); if (fileLen == 0) { (void)close(fd); CleanBlobParam(out); @@ -1134,7 +1134,7 @@ int32_t DlpFile::GenFileInZip(int32_t inPlainFileFd) ret = AddGeneralInfoToBuff(encFile); CHECK_RET(ret, 0, DLP_PARSE_ERROR_FILE_OPERATE_FAIL, LABEL); - int32_t zipSize = GetFileSize(tmpFile); + off_t zipSize = GetFileSize(tmpFile); LSEEK_AND_CHECK(dlpFd_, 0, SEEK_SET, DLP_PARSE_ERROR_FILE_OPERATE_FAIL, LABEL); ret = DoDlpContentCopyOperation(tmpFile, dlpFd_, 0, zipSize); CHECK_RET(ret, 0, DLP_PARSE_ERROR_FILE_OPERATE_FAIL, LABEL); @@ -1232,7 +1232,7 @@ int32_t DlpFile::RemoveDlpPermissionInZip(int32_t outPlainFileFd) } }); - int32_t fileSize = GetFileSize(encFd); + off_t fileSize = GetFileSize(encFd); int32_t ret = DoDlpContentCryptyOperation(encFd, outPlainFileFd, 0, fileSize, false); CHECK_RET(ret, 0, DLP_PARSE_ERROR_FILE_OPERATE_FAIL, LABEL); @@ -1300,19 +1300,18 @@ int32_t DlpFile::RemoveDlpPermission(int32_t outPlainFileFd) } } -int32_t DlpFile::DlpFileRead(uint32_t offset, void* buf, uint32_t size, bool& hasRead, int32_t uid) +int32_t DlpFile::DlpFileRead(off_t offset, void* buf, size_t& size, bool& hasRead, int32_t uid) { int32_t opFd = isZip_ ? encDataFd_ : dlpFd_; if (buf == nullptr || size == 0 || size > DLP_FUSE_MAX_BUFFLEN || - (offset >= DLP_MAX_CONTENT_SIZE - size) || opFd < 0 || !IsValidCipher(cipher_.encKey, cipher_.usageSpec, cipher_.hmacKey)) { DLP_LOG_ERROR(LABEL, "params is error"); return DLP_PARSE_ERROR_VALUE_INVALID; } - uint32_t alignOffset = (offset / DLP_BLOCK_SIZE) * DLP_BLOCK_SIZE; - uint32_t prefixingSize = offset - alignOffset; - uint32_t alignSize = size + prefixingSize; + off_t alignOffset = (offset / DLP_BLOCK_SIZE) * DLP_BLOCK_SIZE; + size_t prefixingSize = offset - alignOffset; + size_t alignSize = size + prefixingSize; if (lseek(opFd, head_.txtOffset + alignOffset, SEEK_SET) == -1) { DLP_LOG_ERROR(LABEL, "lseek dlp file failed. %{public}s", strerror(errno)); @@ -1322,12 +1321,12 @@ int32_t DlpFile::DlpFileRead(uint32_t offset, void* buf, uint32_t size, bool& ha auto encBuff = std::make_unique(alignSize); auto outBuff = std::make_unique(alignSize); - int32_t readLen = read(opFd, encBuff.get(), alignSize); + ssize_t readLen = read(opFd, encBuff.get(), alignSize); if (readLen == -1) { DLP_LOG_ERROR(LABEL, "read buff fail, %{public}s", strerror(errno)); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; } - if (readLen <= static_cast(prefixingSize)) { + if (readLen <= static_cast(prefixingSize)) { return 0; } @@ -1350,15 +1349,16 @@ int32_t DlpFile::DlpFileRead(uint32_t offset, void* buf, uint32_t size, bool& ha return res; } hasRead = true; - return message2.size - prefixingSize; + size = message2.size - prefixingSize; + return DLP_OK; } -int32_t DlpFile::WriteFirstBlockData(uint32_t offset, void* buf, uint32_t size) +int32_t DlpFile::WriteFirstBlockData(off_t offset, void* buf, size_t size, size_t& resSize) { - uint32_t alignOffset = (offset / DLP_BLOCK_SIZE) * DLP_BLOCK_SIZE; - uint32_t prefixingSize = offset % DLP_BLOCK_SIZE; - uint32_t requestSize = (size < (DLP_BLOCK_SIZE - prefixingSize)) ? size : (DLP_BLOCK_SIZE - prefixingSize); - uint32_t writtenSize = prefixingSize + requestSize; + off_t alignOffset = (offset / DLP_BLOCK_SIZE) * DLP_BLOCK_SIZE; + size_t prefixingSize = offset % DLP_BLOCK_SIZE; + size_t requestSize = (size < (DLP_BLOCK_SIZE - prefixingSize)) ? size : (DLP_BLOCK_SIZE - prefixingSize); + size_t writtenSize = prefixingSize + requestSize; uint8_t enBuf[DLP_BLOCK_SIZE] = {0}; uint8_t deBuf[DLP_BLOCK_SIZE] = {0}; int32_t opFd = isZip_ ? encDataFd_ : dlpFd_; @@ -1367,7 +1367,7 @@ int32_t DlpFile::WriteFirstBlockData(uint32_t offset, void* buf, uint32_t size) if (prefixingSize == 0) { break; } - int32_t readLen = read(opFd, enBuf, prefixingSize); + ssize_t readLen = read(opFd, enBuf, prefixingSize); if (readLen == -1) { DLP_LOG_ERROR(LABEL, "read first block prefixing fail, %{public}s", strerror(errno)); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; @@ -1405,31 +1405,32 @@ int32_t DlpFile::WriteFirstBlockData(uint32_t offset, void* buf, uint32_t size) DLP_LOG_ERROR(LABEL, "write failed, %{public}s", strerror(errno)); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; } - return requestSize; + resSize = requestSize; + return DLP_OK; } -int32_t DlpFile::DoDlpFileWrite(uint32_t offset, void* buf, uint32_t size) +int32_t DlpFile::DoDlpFileWrite(off_t offset, void* buf, size_t size, size_t& resSize) { int32_t opFd = isZip_ ? encDataFd_ : dlpFd_; - uint32_t alignOffset = (offset / DLP_BLOCK_SIZE * DLP_BLOCK_SIZE); + off_t alignOffset = (offset / DLP_BLOCK_SIZE * DLP_BLOCK_SIZE); if (lseek(opFd, head_.txtOffset + alignOffset, SEEK_SET) == static_cast(-1)) { - DLP_LOG_ERROR(LABEL, "lseek dlp file offset %{public}d failed, %{public}s", - head_.txtOffset + offset, strerror(errno)); + DLP_LOG_ERROR(LABEL, "lseek dlp file offset %{public}zu failed, %{public}s", + static_cast(head_.txtOffset + offset), strerror(errno)); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; } /* write first block data, if it may be not aligned */ - int32_t writenSize = WriteFirstBlockData(offset, static_cast(buf), size); - if (writenSize < 0) { + int32_t res = WriteFirstBlockData(offset, static_cast(buf), size, resSize); + if (res < 0) { DLP_LOG_ERROR(LABEL, "encrypt prefix data failed"); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; } - if (static_cast(writenSize) >= size) { - return writenSize; + if (resSize >= size) { + return DLP_OK; } - uint8_t *restBlocksPtr = static_cast(buf) + writenSize; - uint32_t restBlocksSize = size - static_cast(writenSize); + uint8_t *restBlocksPtr = static_cast(buf) + resSize; + size_t restBlocksSize = size - static_cast(resSize); uint8_t* writeBuff = new (std::nothrow) uint8_t[restBlocksSize](); if (writeBuff == nullptr) { DLP_LOG_ERROR(LABEL, "alloc write buffer fail"); @@ -1447,17 +1448,17 @@ int32_t DlpFile::DoDlpFileWrite(uint32_t offset, void* buf, uint32_t size) return ret; } - ret = write(opFd, writeBuff, restBlocksSize); + ssize_t writeRet = write(opFd, writeBuff, restBlocksSize); delete[] writeBuff; - if (ret <= 0) { + if (writeRet <= 0) { DLP_LOG_ERROR(LABEL, "write buff failed, %{public}s", strerror(errno)); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; } - - return ret + static_cast(writenSize); + resSize = writeRet + resSize; + return DLP_OK; } -uint32_t DlpFile::GetFsContentSize() const +size_t DlpFile::GetFsContentSize() const { struct stat fileStat; int32_t opFd = isZip_ ? encDataFd_ : dlpFd_; @@ -1503,21 +1504,22 @@ int32_t DlpFile::UpdateDlpFileContentSize() return DLP_OK; } -int32_t DlpFile::FillHoleData(uint32_t holeStart, uint32_t holeSize) +int32_t DlpFile::FillHoleData(size_t holeStart, size_t holeSize) { - DLP_LOG_INFO(LABEL, "Need create a hole filled with 0s, hole start %{public}x size %{public}x", + DLP_LOG_INFO(LABEL, "Need create a hole filled with 0s, hole start %{public}zu size %{public}zu", holeStart, holeSize); - uint32_t holeBufSize = (holeSize < HOLE_BUFF_SMALL_SIZE) ? HOLE_BUFF_SMALL_SIZE : HOLE_BUFF_SIZE; + size_t holeBufSize = (holeSize < HOLE_BUFF_SMALL_SIZE) ? HOLE_BUFF_SMALL_SIZE : HOLE_BUFF_SIZE; std::unique_ptr holeBuff(new (std::nothrow) uint8_t[holeBufSize]()); if (holeBuff == nullptr) { DLP_LOG_ERROR(LABEL, "New buf failed."); return DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL; } - uint32_t fillLen = 0; + size_t fillLen = 0; + size_t resSize = 0; while (fillLen < holeSize) { - uint32_t writeSize = ((holeSize - fillLen) < holeBufSize) ? (holeSize - fillLen) : holeBufSize; - int32_t res = DoDlpFileWrite(holeStart + fillLen, holeBuff.get(), writeSize); + size_t writeSize = ((holeSize - fillLen) < holeBufSize) ? (holeSize - fillLen) : holeBufSize; + int32_t res = DoDlpFileWrite(holeStart + fillLen, holeBuff.get(), writeSize, resSize); if (res < 0) { DLP_LOG_ERROR(LABEL, "Write failed, error %{public}d.", res); return res; @@ -1527,7 +1529,7 @@ int32_t DlpFile::FillHoleData(uint32_t holeStart, uint32_t holeSize) return DLP_OK; } -int32_t DlpFile::DlpFileWrite(uint32_t offset, void* buf, uint32_t size) +int32_t DlpFile::DlpFileWrite(off_t offset, void* buf, size_t size, size_t& resSize) { if (authPerm_ == READ_ONLY) { DLP_LOG_ERROR(LABEL, "Dlp file is readonly, write failed"); @@ -1535,19 +1537,18 @@ int32_t DlpFile::DlpFileWrite(uint32_t offset, void* buf, uint32_t size) } int32_t opFd = isZip_ ? encDataFd_ : dlpFd_; if (buf == nullptr || size == 0 || size > DLP_FUSE_MAX_BUFFLEN || - (offset >= DLP_MAX_CONTENT_SIZE - size) || opFd < 0 || !IsValidCipher(cipher_.encKey, cipher_.usageSpec, cipher_.hmacKey)) { DLP_LOG_ERROR(LABEL, "Dlp file param invalid"); return DLP_PARSE_ERROR_VALUE_INVALID; } - uint32_t curSize = GetFsContentSize(); - if (curSize != INVALID_FILE_SIZE && curSize < offset && + size_t curSize = GetFsContentSize(); + if (curSize != INVALID_FILE_SIZE && static_cast(curSize) < offset && (FillHoleData(curSize, offset - curSize) != DLP_OK)) { DLP_LOG_ERROR(LABEL, "Fill hole data failed"); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; } - int32_t res = DoDlpFileWrite(offset, buf, size); + int32_t res = DoDlpFileWrite(offset, buf, size, resSize); UpdateDlpFileContentSize(); // modify dlp file, clear old hmac value and will generate new @@ -1558,9 +1559,9 @@ int32_t DlpFile::DlpFileWrite(uint32_t offset, void* buf, uint32_t size) return res; } -int32_t DlpFile::Truncate(uint32_t size) +int32_t DlpFile::Truncate(size_t size) { - DLP_LOG_INFO(LABEL, "Truncate file size %{public}u", size); + DLP_LOG_INFO(LABEL, "Truncate file size %{public}zu", size); if (authPerm_ == READ_ONLY) { DLP_LOG_ERROR(LABEL, "Dlp file is readonly, truncate failed"); @@ -1572,7 +1573,7 @@ int32_t DlpFile::Truncate(uint32_t size) return DLP_PARSE_ERROR_VALUE_INVALID; } - uint32_t curSize = GetFsContentSize(); + size_t curSize = GetFsContentSize(); int32_t res = DLP_OK; if (size < curSize) { res = ftruncate(opFd, head_.txtOffset + size); @@ -1587,7 +1588,7 @@ int32_t DlpFile::Truncate(uint32_t size) } if (res != DLP_OK) { - DLP_LOG_ERROR(LABEL, "Truncate file size %{public}u failed, %{public}s", size, strerror(errno)); + DLP_LOG_ERROR(LABEL, "Truncate file size %{public}zu failed, %{public}s", size, strerror(errno)); return DLP_PARSE_ERROR_FILE_OPERATE_FAIL; } return DLP_OK; diff --git a/interfaces/inner_api/dlp_parse/src/dlp_zip.cpp b/interfaces/inner_api/dlp_parse/src/dlp_zip.cpp index 295fc71e18194a26734e84f7320996b90ff202e3..ee4dcda03dea7d60876c293c4b467a8ae009d9ff 100644 --- a/interfaces/inner_api/dlp_parse/src/dlp_zip.cpp +++ b/interfaces/inner_api/dlp_parse/src/dlp_zip.cpp @@ -64,7 +64,7 @@ int32_t AddBuffToZip(const void *buf, uint32_t size, const char *nameInZip, cons compressLevel, 0, /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, - NULL, 0, 0); + NULL, 0, 1); if (err != ZIP_OK) { DLP_LOG_ERROR(LABEL, "AddBuffToZip fail err %{public}d, nameInZip %{public}s", err, nameInZip); (void)zipClose(zf, NULL); @@ -107,7 +107,7 @@ int32_t AddFileContextToZip(int32_t fd, const char *nameInZip, const char *zipNa compressLevel, 0, /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, - NULL, 0, 0); + NULL, 0, 1); if (err != ZIP_OK) { DLP_LOG_ERROR(LABEL, "create zip file fail err %{public}d, nameInZip %{public}s", err, nameInZip); zipClose(zf, NULL); diff --git a/interfaces/inner_api/dlp_parse/test/dlp_crypt_test.cpp b/interfaces/inner_api/dlp_parse/test/dlp_crypt_test.cpp index 445cf2c5cea1dc40f5067135c5f33c8c88e5ee08..219e5770e63f313e64b7c929ccf8573702539667 100644 --- a/interfaces/inner_api/dlp_parse/test/dlp_crypt_test.cpp +++ b/interfaces/inner_api/dlp_parse/test/dlp_crypt_test.cpp @@ -33,10 +33,6 @@ using namespace OHOS::Security::DlpPermission; using namespace std; using namespace OHOS::Security::AccessToken; -extern "C" { -extern const EVP_MD* GetOpensslAlg(uint32_t alg); -} - namespace { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpCryptTest"}; static const int32_t DEFAULT_USERID = 100; @@ -1441,552 +1437,6 @@ HWTEST_F(DlpCryptTest, DlpOpensslAesHalFreeCtx002, TestSize.Level1) ASSERT_EQ(ctx, nullptr); } -/** - * @tc.name: GetOpensslAlg001 - * @tc.desc: get openssl invalid alg - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, GetOpensslAlg001, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "GetOpensslAlg001"); - ASSERT_EQ(GetOpensslAlg(1000), nullptr); -} - -/** - * @tc.name: DlpOpensslHash001 - * @tc.desc: HASH test - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHash001, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHash001"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[64] = {0}; - struct DlpBlob mIn = { - .data = nullptr, - .size = 15 - }; - mIn.data = input; - struct DlpBlob mOut = { - .data = nullptr, - .size = 64 - }; - mOut.data = out; - int ret; - - ret = DlpOpensslHash(DLP_DIGEST_SHA256, &mIn, &mOut); - cout << "sha256:"; - Dumpptr(out, 16); - ASSERT_EQ(0, ret); - mOut.size = 64; - ret = DlpOpensslHash(DLP_DIGEST_SHA384, &mIn, &mOut); - cout << "sha384:"; - Dumpptr(out, 16); - ASSERT_EQ(0, ret); - mOut.size = 64; - ret = DlpOpensslHash(DLP_DIGEST_SHA512, &mIn, &mOut); - cout << "sha512:"; - Dumpptr(out, 16); - ASSERT_EQ(0, ret); -} - -/** - * @tc.name: DlpOpensslHash002 - * @tc.desc: DlpOpensslHash with invalid alg - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHash002, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHash002"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[32] = {0}; - struct DlpBlob message = {15, input}; - struct DlpBlob hash = {32, out}; - - // alg = 0 - int32_t ret = DlpOpensslHash(DLP_DIGEST_NONE, &message, &hash); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); - - // alg != DLP_DIGEST_SHA256 | DLP_DIGEST_SHA384 | DLP_DIGEST_SHA512 - ret = DlpOpensslHash(100, &message, &hash); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); -} - -/** - * @tc.name: DlpOpensslHash003 - * @tc.desc: DlpOpensslHash with invalid message - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHash003, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHash003"); - uint8_t out[64] = {0}; - struct DlpBlob hash = {64, out}; - - // message = nullptr - int32_t ret = DlpOpensslHash(DLP_DIGEST_SHA512, nullptr, &hash); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); -} - -/** - * @tc.name: DlpOpensslHash004 - * @tc.desc: DlpOpensslHash with invalid hash - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHash004, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHash004"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - struct DlpBlob message = {15, input}; - - // hash = nullptr - int32_t ret = DlpOpensslHash(DLP_DIGEST_SHA512, &message, nullptr); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); -} - -/** - * @tc.name: DlpOpensslHash005 - * @tc.desc: DlpOpensslHash with hash len < alg len - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHash005, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHash005"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - struct DlpBlob message = {15, input}; - uint8_t output[16] = {}; - struct DlpBlob hash = {16, output}; - - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, DlpOpensslHash(DLP_DIGEST_SHA512, &message, &hash)); -} - -/** - * @tc.name: DlpOpensslHash006 - * @tc.desc: DlpOpensslHash with openssl abnormal branch - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHash006, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHash006"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - struct DlpBlob message = {15, input}; - uint8_t output[16] = {}; - struct DlpBlob hash = {64, output}; - - // EVP_sha512 failed - DlpCMockCondition condition; - condition.mockSequence = { true }; - SetMockConditions("EVP_sha512", condition); - ASSERT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR, DlpOpensslHash(DLP_DIGEST_SHA512, &message, &hash)); - CleanMockConditions(); - - // EVP_Digest failed - condition.mockSequence = { true }; - SetMockConditions("EVP_Digest", condition); - ASSERT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR, DlpOpensslHash(DLP_DIGEST_SHA512, &message, &hash)); - CleanMockConditions(); -} - -/** - * @tc.name: DlpOpensslHashInit001 - * @tc.desc: DlpOpensslHashInit with invalid cryptoCtx - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashInit001, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashInit001"); - - // cryptoCtx = nullptr - int32_t ret = DlpOpensslHashInit(nullptr, DLP_DIGEST_SHA256); - EXPECT_EQ(DLP_PARSE_ERROR_DIGEST_INVALID, ret); -} - -/** - * @tc.name: DlpOpensslHashInit002 - * @tc.desc: DlpOpensslHashInit with invalid alg - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashInit002, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashInit002"); - - // alg = DLP_DIGEST_NONE - void* ctx = nullptr; - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_NONE); - EXPECT_EQ(DLP_PARSE_ERROR_DIGEST_INVALID, ret); - - // alg = 100 - ctx = nullptr; - ret = DlpOpensslHashInit(&ctx, 100); - EXPECT_EQ(DLP_PARSE_ERROR_DIGEST_INVALID, ret); -} - -/** - * @tc.name: DlpOpensslHashInit003 - * @tc.desc: DlpOpensslHashInit with openssl abnormal branch - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashInit003, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashInit003"); - void* ctx = nullptr; - - // EVP_sha512 fail - DlpCMockCondition condition; - condition.mockSequence = { true }; - SetMockConditions("EVP_sha512", condition); - ASSERT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR, DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA512)); - CleanMockConditions(); - - // EVP_MD_CTX_new fail - condition.mockSequence = { true }; - SetMockConditions("EVP_MD_CTX_new", condition); - ASSERT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA512)); - CleanMockConditions(); - - // EVP_DigestInit_ex fail - condition.mockSequence = { true }; - SetMockConditions("EVP_DigestInit_ex", condition); - ASSERT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR, DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA512)); - CleanMockConditions(); -} - -/** - * @tc.name: DlpOpensslHashUpdate001 - * @tc.desc: DlpOpensslHashUpdate with invalid cryptoCtx - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashUpdate001, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashUpdate001"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - struct DlpBlob message = {15, input}; - - // cryptoCtx = nullptr - int32_t ret = DlpOpensslHashUpdate(nullptr, &message); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); -} - -/** - * @tc.name: DlpOpensslHashUpdate002 - * @tc.desc: DlpOpensslHashUpdate with invalid message - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashUpdate002, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashUpdate002"); - void* ctx = nullptr; - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - ASSERT_EQ(0, ret); - - // message = nullptr - ret = DlpOpensslHashUpdate(ctx, nullptr); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); - EVP_MD_CTX_free(reinterpret_cast(ctx)); -} - -/** - * @tc.name: DlpOpensslHashUpdate003 - * @tc.desc: DlpOpensslHashUpdate with openssl abnormal barnch - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashUpdate003, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashUpdate003"); - void* ctx = nullptr; - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - ASSERT_EQ(0, ret); - - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - struct DlpBlob msg1 = {15, input}; - - // EVP_DigestUpdate failed - DlpCMockCondition condition; - condition.mockSequence = { true }; - SetMockConditions("EVP_DigestUpdate", condition); - ASSERT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR, DlpOpensslHashUpdate(ctx, &msg1)); - CleanMockConditions(); - EVP_MD_CTX_free(reinterpret_cast(ctx)); -} - -/** - * @tc.name: DlpOpensslHashFinal001 - * @tc.desc: DlpOpensslHashFinal with invalid cryptoCtx - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFinal001, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFinal001"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[64] = {0}; - struct DlpBlob message = {15, input}; - struct DlpBlob hash = {64, out}; - - // cryptoCtx = nullptr - int32_t ret = DlpOpensslHashFinal(nullptr, &message, &hash); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); -} - -/** - * @tc.name: DlpOpensslHashFinal002 - * @tc.desc: DlpOpensslHashFinal with invalid message - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFinal002, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFinal002"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[64] = {0}; - struct DlpBlob hash = {64, out}; - struct DlpBlob msg1 = {15, input}; - void* ctx = nullptr; - - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - EXPECT_EQ(0, ret); - - msg1.size = 1; - int i = 0; - while (i < 15) { - ret = DlpOpensslHashUpdate(ctx, &msg1); - EXPECT_EQ(0, ret); - msg1.data = msg1.data + 1; - i++; - } - - // message = nullptr - ret = DlpOpensslHashFinal(&ctx, nullptr, &hash); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); - EVP_MD_CTX_free(reinterpret_cast(ctx)); -} - -/** - * @tc.name: DlpOpensslHashFinal003 - * @tc.desc: DlpOpensslHashFinal ok - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFinal003, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFinal003"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[64] = {0}; - struct DlpBlob hash = {64, out}; - struct DlpBlob msg1 = {15, input}; - void* ctx = nullptr; - - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - EXPECT_EQ(0, ret); - - msg1.size = 1; - int i = 0; - while (i < 15) { - ret = DlpOpensslHashUpdate(ctx, &msg1); - EXPECT_EQ(0, ret); - msg1.data = msg1.data + 1; - i++; - } - - ret = DlpOpensslHashFinal(&ctx, &msg1, &hash); - EXPECT_EQ(DLP_OK, ret); - EVP_MD_CTX_free(reinterpret_cast(ctx)); -} - -/** - * @tc.name: DlpOpensslHashFinal004 - * @tc.desc: DlpOpensslHashFinal with invalid hash - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFinal004, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFinal004"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - struct DlpBlob message = {15, input}; - struct DlpBlob msg1 = {15, input}; - void* ctx = nullptr; - - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - EXPECT_EQ(0, ret); - - msg1.size = 1; - int i = 0; - while (i < 15) { - ret = DlpOpensslHashUpdate(ctx, &msg1); - EXPECT_EQ(0, ret); - msg1.data = msg1.data + 1; - i++; - } - - // hash = nullptr - ret = DlpOpensslHashFinal(&ctx, &message, nullptr); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, ret); - EVP_MD_CTX_free(reinterpret_cast(ctx)); -} - -/** - * @tc.name: DlpOpensslHashFinal005 - * @tc.desc: DlpOpensslHashFinal with openssl EVP_DigestUpdate fail - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFinal005, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFinal005"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[64] = {0}; - struct DlpBlob hash = {64, out}; - struct DlpBlob msg1 = {15, input}; - void* ctx = nullptr; - - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - EXPECT_EQ(0, ret); - - msg1.size = 1; - int i = 0; - while (i < 15) { - ret = DlpOpensslHashUpdate(ctx, &msg1); - EXPECT_EQ(0, ret); - msg1.data = msg1.data + 1; - i++; - } - - DlpCMockCondition condition; - condition.mockSequence = { true }; - SetMockConditions("EVP_DigestUpdate", condition); - ASSERT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR, DlpOpensslHashFinal(&ctx, &msg1, &hash)); - CleanMockConditions(); - - EVP_MD_CTX_free(reinterpret_cast(ctx)); -} - -/** - * @tc.name: DlpOpensslHashFinal006 - * @tc.desc: DlpOpensslHashFinal with openssl EVP_DigestFinal_ex fail - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFinal006, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFinal006"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[64] = {0}; - struct DlpBlob hash = {64, out}; - struct DlpBlob msg1 = {15, input}; - void* ctx = nullptr; - - int32_t ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - EXPECT_EQ(0, ret); - - msg1.size = 1; - int i = 0; - while (i < 15) { - ret = DlpOpensslHashUpdate(ctx, &msg1); - EXPECT_EQ(0, ret); - msg1.data = msg1.data + 1; - i++; - } - - DlpCMockCondition condition; - condition.mockSequence = { true }; - SetMockConditions("EVP_DigestFinal_ex", condition); - ASSERT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR, DlpOpensslHashFinal(&ctx, &msg1, &hash)); - CleanMockConditions(); - - EVP_MD_CTX_free(reinterpret_cast(ctx)); -} - -/** - * @tc.name: DlpOpensslHashFreeCtx001 - * @tc.desc: DlpOpensslHashFreeCtx with null - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFreeCtx001, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFreeCtx001"); - ASSERT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, DlpOpensslHashFreeCtx(nullptr)); - - void *ctx = nullptr; - ASSERT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, DlpOpensslHashFreeCtx(&ctx)); -} - -/** - * @tc.name: DlpOpensslHashFreeCtx002 - * @tc.desc: DlpOpensslHashFreeCtx with null - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashFreeCtx002, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashFreeCtx002"); - void* ctx = nullptr; - ASSERT_EQ(0, DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256)); - ASSERT_EQ(DLP_OK, DlpOpensslHashFreeCtx(&ctx)); -} - -/** - * @tc.name: DlpOpensslHashTest001 - * @tc.desc: split hash test - * @tc.type: FUNC - * @tc.require:SR000GVIG3 - */ -HWTEST_F(DlpCryptTest, DlpOpensslHashTest001, TestSize.Level1) -{ - DLP_LOG_INFO(LABEL, "DlpOpensslHashTest001"); - uint8_t input[16] = "aaaaaaaaaaaaaaa"; - uint8_t out[64] = {0}; - struct DlpBlob mIn = { - .data = nullptr, - .size = 15 - }; - mIn.data = input; - struct DlpBlob mOut = { - .data = nullptr, - .size = 15 - }; - mOut.data = out; - struct DlpBlob mNull = { - .data = nullptr, - .size = 0 - }; - int i = 0; - int ret; - void *ctx; - - ret = DlpOpensslHashInit(&ctx, DLP_DIGEST_SHA256); - ASSERT_EQ(0, ret); - - mIn.size = 1; - while (i < 15) { - ret = DlpOpensslHashUpdate(ctx, &mIn); - ASSERT_EQ(0, ret); - mIn.data = mIn.data + 1; - i++; - } - ret = DlpOpensslHashFinal(&ctx, &mNull, &mOut); - ASSERT_EQ(0, ret); - DlpOpensslHashFreeCtx(&ctx); - - cout << "sha256sum:"; - Dumpptr(out, 16); - ASSERT_EQ(0, ret); -} - /** * @tc.name: DlpOpensslGenerateRandomKey001 * @tc.desc: random generate test diff --git a/interfaces/inner_api/dlp_parse/test/dlp_file_test.cpp b/interfaces/inner_api/dlp_parse/test/dlp_file_test.cpp index 8e2dafbe4cfea25aab8f0123c469435756143777..0133c706afa59e27cfe911425c9410371fb7e692 100644 --- a/interfaces/inner_api/dlp_parse/test/dlp_file_test.cpp +++ b/interfaces/inner_api/dlp_parse/test/dlp_file_test.cpp @@ -1372,49 +1372,43 @@ HWTEST_F(DlpFileTest, DlpFileRead001, TestSize.Level1) ASSERT_NE(fdPlain, -1); int fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); ASSERT_NE(fdDlp, -1); - DlpFile testFile(fdDlp, DLP_TEST_DIR, 0, false); initDlpFileCiper(testFile); int32_t uid = getuid(); bool hasRead = true; // isFuseLink_ true - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, nullptr, 10, hasRead, uid)); - + size_t size = 10; + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, nullptr, size, hasRead, uid)); uint8_t buffer[16] = {}; - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, 0, hasRead, uid)); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(DLP_MAX_CONTENT_SIZE, buffer, 1, hasRead, uid)); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, DLP_FUSE_MAX_BUFFLEN + 1, hasRead, uid)); - + size = 0; + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); + size = DLP_FUSE_MAX_BUFFLEN + 1; + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); + size = 16; testFile.dlpFd_ = -1; - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, 16, hasRead, uid)); + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); testFile.dlpFd_ = fdDlp; - testFile.cipher_.encKey.size = 0; - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, 16, hasRead, uid)); + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); testFile.cipher_.encKey.size = 16; - DlpCMockCondition condition; condition.mockSequence = { true }; SetMockConditions("lseek", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DlpFileRead(0, buffer, 16, hasRead, uid)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); CleanMockConditions(); - // read size 0 - EXPECT_EQ(0, testFile.DlpFileRead(0, buffer, 16, hasRead, uid)); - + EXPECT_EQ(0, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); // do crypt failed write(fdDlp, "1111", 4); lseek(fdDlp, 0, SEEK_SET); condition.mockSequence = { true }; SetMockConditions("EVP_CIPHER_CTX_new", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DlpFileRead(0, buffer, 16, hasRead, uid)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); CleanMockConditions(); - condition.mockSequence = { false, true }; SetMockConditions("memcpy_s", condition); - EXPECT_EQ(DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL, testFile.DlpFileRead(0, buffer, 16, hasRead, uid)); + EXPECT_EQ(DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL, testFile.DlpFileRead(0, buffer, size, hasRead, uid)); CleanMockConditions(); - close(fdPlain); close(fdDlp); unlink("/data/fuse_test_plain.txt"); @@ -1437,15 +1431,15 @@ HWTEST_F(DlpFileTest, WriteFirstBlockData001, TestSize.Level1) DlpFile testFile(fdDlp, DLP_TEST_DIR, 0, false); initDlpFileCiper(testFile); uint8_t writeBuffer[16] = {0x1}; - + size_t resSize; testFile.dlpFd_ = -1; - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16, resSize)); testFile.dlpFd_ = fdDlp; DlpCMockCondition condition; condition.mockSequence = { true }; SetMockConditions("memcpy_s", condition); - EXPECT_EQ(DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16, resSize)); CleanMockConditions(); // decrypt fail @@ -1453,28 +1447,28 @@ HWTEST_F(DlpFileTest, WriteFirstBlockData001, TestSize.Level1) lseek(fdDlp, 0, SEEK_SET); condition.mockSequence = { true }; SetMockConditions("EVP_CIPHER_CTX_new", condition); - EXPECT_EQ(DLP_PARSE_ERROR_CRYPT_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_CRYPT_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16, resSize)); CleanMockConditions(); // encrypt fail lseek(fdDlp, 0, SEEK_SET); condition.mockSequence = { false, true }; SetMockConditions("EVP_CIPHER_CTX_new", condition); - EXPECT_EQ(DLP_PARSE_ERROR_CRYPT_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_CRYPT_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16, resSize)); CleanMockConditions(); // lseek fail lseek(fdDlp, 0, SEEK_SET); condition.mockSequence = { true }; SetMockConditions("lseek", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16, resSize)); CleanMockConditions(); // write fail lseek(fdDlp, 0, SEEK_SET); condition.mockSequence = { true }; SetMockConditions("write", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.WriteFirstBlockData(4, writeBuffer, 16, resSize)); CleanMockConditions(); close(fdPlain); @@ -1499,35 +1493,35 @@ HWTEST_F(DlpFileTest, DoDlpFileWrite001, TestSize.Level1) DlpFile testFile(fdDlp, DLP_TEST_DIR, 0, false); initDlpFileCiper(testFile); uint8_t writeBuffer[18] = {0x1}; - + size_t resSize; DlpCMockCondition condition; condition.mockSequence = { true }; SetMockConditions("lseek", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18, resSize)); CleanMockConditions(); condition.mockSequence = { true }; lseek(fdDlp, 0, SEEK_SET); SetMockConditions("memcpy_s", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18, resSize)); CleanMockConditions(); condition.mockSequence = { true }; lseek(fdDlp, 0, SEEK_SET); SetMockConditions("EVP_CIPHER_CTX_new", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18, resSize)); CleanMockConditions(); condition.mockSequence = { false, true }; lseek(fdDlp, 0, SEEK_SET); SetMockConditions("EVP_CIPHER_CTX_new", condition); - EXPECT_EQ(DLP_PARSE_ERROR_CRYPT_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18)); + EXPECT_EQ(DLP_PARSE_ERROR_CRYPT_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18, resSize)); CleanMockConditions(); condition.mockSequence = { false, true }; lseek(fdDlp, 0, SEEK_SET); SetMockConditions("write", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DoDlpFileWrite(0, writeBuffer, 18, resSize)); CleanMockConditions(); close(fdPlain); @@ -1627,31 +1621,30 @@ HWTEST_F(DlpFileTest, DlpFileWrite001, TestSize.Level1) DlpFile testFile(fdDlp, DLP_TEST_DIR, 0, false); initDlpFileCiper(testFile); uint8_t writeBuffer[16] = {0x1}; - + size_t resSize; testFile.head_.txtOffset = 0; testFile.authPerm_ = READ_ONLY; - EXPECT_EQ(DLP_PARSE_ERROR_FILE_READ_ONLY, testFile.DlpFileWrite(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_READ_ONLY, testFile.DlpFileWrite(4, writeBuffer, 16, resSize)); testFile.authPerm_ = FULL_CONTROL; - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, nullptr, 16)); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, 0)); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(DLP_MAX_CONTENT_SIZE, writeBuffer, 1)); - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, DLP_FUSE_MAX_BUFFLEN + 1)); + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, nullptr, 16, resSize)); + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, 0, resSize)); + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, DLP_FUSE_MAX_BUFFLEN + 1, resSize)); testFile.dlpFd_ = -1; - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, 16, resSize)); testFile.dlpFd_ = fdDlp; testFile.cipher_.encKey.size = 0; - EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, testFile.DlpFileWrite(4, writeBuffer, 16, resSize)); testFile.cipher_.encKey.size = 16; // fill hole data fail DlpCMockCondition condition; condition.mockSequence = { true }; SetMockConditions("lseek", condition); - EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DlpFileWrite(16, writeBuffer, 16)); + EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL, testFile.DlpFileWrite(16, writeBuffer, 16, resSize)); CleanMockConditions(); close(fdPlain); diff --git a/test/fuzztest/dlp_permission/fuzzer/dlpfile_fuzzer/dlpfile_fuzzer.cpp b/test/fuzztest/dlp_permission/fuzzer/dlpfile_fuzzer/dlpfile_fuzzer.cpp index 81fb9fc669e59a420f2739f3cc1ead25e67bc2e2..b9afca17573b6abf0a48fc2f6484b7fe84d5a4c0 100644 --- a/test/fuzztest/dlp_permission/fuzzer/dlpfile_fuzzer/dlpfile_fuzzer.cpp +++ b/test/fuzztest/dlp_permission/fuzzer/dlpfile_fuzzer/dlpfile_fuzzer.cpp @@ -137,11 +137,13 @@ static void FuzzTest(const uint8_t* data, size_t size) DlpFileManager::GetInstance().CloseDlpFile(g_Dlpfile); res = DlpFileManager::GetInstance().OpenDlpFile(dlpFileFd, g_Dlpfile, DLP_TEST_DIR, appId); DLP_LOG_INFO(LABEL, "OpenDlpFile res=%{public}d", res); - g_Dlpfile->DlpFileWrite(0, const_cast(text.c_str()), text.length()); + size_t resSize; + g_Dlpfile->DlpFileWrite(0, const_cast(text.c_str()), text.length(), resSize); uint8_t writeBuffer[ARRRY_SIZE] = {0x1}; bool hasRead = true; - g_Dlpfile->DlpFileRead(0, writeBuffer, ARRRY_SIZE, hasRead, 0); - g_Dlpfile->Truncate(ARRRY_SIZE); + resSize = ARRRY_SIZE; + g_Dlpfile->DlpFileRead(0, writeBuffer, resSize, hasRead, 0); + g_Dlpfile->Truncate(resSize); } bool DlpFileFuzzTest(const uint8_t* data, size_t size)