diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h index 6f88630c7eeee11a5353afdc15ddbe583a02d13c..fec8d218f6b531d1d4398b6a10f4bfab5a42228e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h @@ -32,7 +32,7 @@ public: static ValueObject GetValueByFiled(JsonObject &node, const std::string &filed); static ValueObject GetValueByFiled(JsonObject &node, const std::string &filed, bool &isFiledExist); static bool CheckJsonField(JsonObject &node); - static bool CheckProjectionField(JsonObject &node); + static bool CheckProjectionField(JsonObject &node, int &errCode); static int ParseNode(JsonObject &Node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor); static std::vector> ParsePath(const JsonObject &node, int &errCode); @@ -45,7 +45,8 @@ private: static bool JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag); static bool CheckNode(JsonObject &Node, std::set filedSet, bool &errFlag); - static bool CheckProjectionNode(JsonObject &Node, std::set filedSet, bool &errFlag, bool isFirstFloor); + static bool CheckProjectionNode(JsonObject &Node, std::set filedSet, bool &errFlag, + bool isFirstFloor, int &errCode); static void CheckLeafNode(const JsonObject &Node, std::vector &leafValue); static bool IsArrayMatch(const JsonObject &src, const JsonObject &target, int &isAlreadyMatched); }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp index bf8c9e652e40277ae60f0d76dab7368929231912..cfd77cea3c015503ea667f94adbb0c12eae67ddd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp @@ -135,7 +135,8 @@ bool JsonCommon::CheckJsonField(JsonObject &jsonObj) return CheckNode(jsonObj, filedSet, errFlag); } -bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set filedSet, bool &errFlag, bool isFirstFloor) +bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set filedSet, bool &errFlag, + bool isFirstFloor, int &errCode) { if (!errFlag) { return false; @@ -144,23 +145,27 @@ bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set fil if (!node.IsNull()) { int ret = 0; fieldName = node.GetItemFiled(ret); + if (fieldName.empty()) { + errCode = -E_INVALID_ARGS; + errFlag = false; + return false; + } if (filedSet.find(fieldName) == filedSet.end() && ret == E_OK) { filedSet.insert(fieldName); - if (fieldName.empty()) { - errFlag = false; - return false; - } } else { + errCode = -E_INVALID_JSON_FORMAT; errFlag = false; return false; } for (size_t i = 0; i < fieldName.size(); i++) { if (!((isalpha(fieldName[i])) || (isdigit(fieldName[i])) || ('_' == fieldName[i]) || (isFirstFloor && '.' == fieldName[i]))) { + errCode = -E_INVALID_ARGS; errFlag = false; return false; } if (i == 0 && (isdigit(fieldName[i]))) { + errCode = -E_INVALID_ARGS; errFlag = false; return false; } @@ -169,21 +174,21 @@ bool JsonCommon::CheckProjectionNode(JsonObject &node, std::set fil if (!node.GetChild().IsNull()) { auto nodeNew = node.GetChild(); std::set newFiledSet; - CheckProjectionNode(nodeNew, newFiledSet, errFlag, false); + CheckProjectionNode(nodeNew, newFiledSet, errFlag, false, errCode); } if (!node.GetNext().IsNull()) { auto nodeNew = node.GetNext(); - CheckProjectionNode(nodeNew, filedSet, errFlag, isFirstFloor); + CheckProjectionNode(nodeNew, filedSet, errFlag, isFirstFloor, errCode); } return errFlag; } -bool JsonCommon::CheckProjectionField(JsonObject &jsonObj) +bool JsonCommon::CheckProjectionField(JsonObject &jsonObj, int &errCode) { std::set filedSet; bool errFlag = true; bool isFirstFloor = true; - return CheckProjectionNode(jsonObj, filedSet, errFlag, isFirstFloor); + return CheckProjectionNode(jsonObj, filedSet, errFlag, isFirstFloor, errCode); } int JsonCommon::ParseNode(JsonObject &node, std::vector singlePath, diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp index ed128f1f0f8dbf7629f5c5deff94ef7cefddeed2..5cf096fbfc75c04141f689b5810a2e5fa9fb9ca2 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp @@ -241,11 +241,21 @@ int CheckCommon::CheckUpdata(JsonObject &updataObj, std::vector filedSet; + int errCode = E_OK; + if (!updataObj.GetChild().IsNull()) { + auto updataObjChild = updataObj.GetChild(); + if (!JsonCommon::CheckProjectionField(updataObjChild, errCode)) { + GLOGE("updataObj json field format is illegal"); + return errCode; + } + } for (int i = 0; i < path.size(); i++) { for (int j = 0; j < path[i].size(); j++) { for (auto oneChar : path[i][j]) { if (!((isalpha(oneChar)) || (isdigit(oneChar)) || ('_' == oneChar))) { - return -E_INVALID_ARGS;; + return -E_INVALID_ARGS; } } } @@ -266,17 +276,18 @@ int CheckCommon::CheckUpdata(JsonObject &updataObj, std::vector> &path) +int CheckCommon::CheckProjection(JsonObject &projectionObj, std::vector> &path) { if (projectionObj.GetDeep() > JSON_DEEP_MAX) { GLOGE("projectionObj's json deep is deeper than JSON_DEEP_MAX"); return -E_INVALID_ARGS; } + int errCode = E_OK; if (!projectionObj.GetChild().IsNull()) { auto projectionObjChild = projectionObj.GetChild(); - if (!JsonCommon::CheckProjectionField(projectionObjChild)) { + if (!JsonCommon::CheckProjectionField(projectionObjChild, errCode)) { GLOGE("projection json field format is illegal"); - return false; + return errCode; } } for (int i = 0; i < path.size(); i++) { @@ -286,14 +297,14 @@ bool CheckCommon::CheckProjection(JsonObject &projectionObj, std::vector> &path); static bool CheckDocument(const std::string &updateStr, int &errCode); - static bool CheckProjection(JsonObject &projectionObj, std::vector> &path); + static int CheckProjection(JsonObject &projectionObj, std::vector> &path); }; using Key = std::vector; using Value = std::vector; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp index c00a3d0a597ee9676423f12513ffc143f2d24efa..0cc6fb5ce96a675e55643a4eb8912d9eca9902f4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp @@ -492,14 +492,15 @@ int DocumentStore::FindDocument(const std::string &collection, const std::string if (errCode != E_OK) { return errCode; } - if (!CheckCommon::CheckProjection(projectionObj, allPath)) { - GLOGE("projection format unvalid"); - return -E_INVALID_ARGS; - } if (GetViewType(projectionObj, viewType) != E_OK) { GLOGE("GetViewType failed"); return -E_INVALID_ARGS; } + errCode = CheckCommon::CheckProjection(projectionObj, allPath); + if (errCode != E_OK) { + GLOGE("projection format unvalid"); + return errCode; + } } bool ifShowId = false; if (flags == GRD_DOC_ID_DISPLAY) { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp index f87342c7977219d0c299f09fa7ad774b4fbe9c53..e17b58be5ae824ebc794b91f11566638a8c56bb9 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp @@ -171,14 +171,10 @@ HWTEST_F(DocumentDBDataTest, UpsertDataTest008, TestSize.Level0) EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), updateDoc.c_str(), GRD_DOC_APPEND), 1); } -HWTEST_F(DocumentDBDataTest, UpsertDataTest009, TestSize.Level0) +HWTEST_F(DocumentDBDataTest, UpsertDataTest010, TestSize.Level0) { - std::string filter = R""({"_id":"1234", "aaa" : "bbb"})""; - std::string document = R""({"name":"Tmn","age":18,"addr":{"city":"shanghai","postal":200001}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_APPEND), 1); - std::string filter2 = R""({"_id":"1234", "aaa" : "ccc"})""; - std::string updateDoc = R""({"name":"Xue","case":2,"age":28,"addr":{"city":"shenzhen","postal":518000}})""; - EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, filter.c_str(), document.c_str(), GRD_DOC_APPEND), GRD_DATA_CONFLICT); + int result = GRD_UpsertDoc(g_db, g_coll, R"({"_id" : "abcde"})", R"({"a00001":1, "a00001":2})", 0); + ASSERT_EQ(result, GRD_INVALID_FORMAT); } /** @@ -272,18 +268,9 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) HWTEST_F(DocumentDBDataTest, UpdateDataTest007, TestSize.Level0) { - int result = GRD_OK; - const char *doc = R"({"_id":"007", "field1":{"c_field":{"cc_field":{"ccc_field":1}}}, "field2":2})"; - result = GRD_InsertDoc(g_db,g_coll, doc, 0); - cJSON *updata = cJSON_CreateObject(); - for (int i = 0; i <= 40000; i++) { - string temp = "f" + string(5 - std::to_string(i).size(), '0') + std::to_string(i); - cJSON_AddStringToObject(updata, temp.c_str(), "a"); - } - char *updateStr = cJSON_PrintUnformatted(updata); - result = GRD_UpdateDoc(g_db, g_coll, R""({"_id":"007"})"", updateStr, 0); - EXPECT_EQ(result, 1); - cJSON_Delete(updata); - cJSON_free; - + const char *updateStr = R""({"field2":{"c_field":{"cc_field":{"ccc_field":{"ccc_field":[1,false,1.234e2,["hello world!"]]}}}}})""; + int result = GRD_UpdateDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); + int result2 = GRD_UpsertDoc(g_db, g_coll, "{\"field\" : 2}", updateStr, 0); + EXPECT_EQ(result, GRD_INVALID_ARGS); + EXPECT_EQ(result2, GRD_INVALID_ARGS); } \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp index 60c8ffb5e4016e01bd61afa0c186dcfa7eb52220..807cc4bfa3c9e1d983c97a2c39cb24ee461681c1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp @@ -1522,3 +1522,24 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest057, TestSize.Level1) // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } +/** + * @tc.name: DocumentFindApiTest058 + * @tc.desc: Test findDoc with no _id. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest058, TestSize.Level1) {} + +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest059, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. + */ + const char *filter = "{\"personInfo\" : {\"school\":\"B\"}}"; + GRD_ResultSet *resultSet = nullptr; + const char *projection = R"({"a00001":1, "a00001":1})"; + Query query = { filter, projection }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_FORMAT); +}