diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index d8d1c02314..ab8066416d 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -15,6 +15,9 @@ - Fix: bug when tenants are over 44 bytes long (#2811) - Add: Support for null values in string filters (#2359) - Fix: Partial sanity check for custon url, only if the url contains no replacements using ${xxx} (#2280, #2279) +- Fix: if entity::type is given but as an empty string, in payload of POST /v2/subscriptions or PATCH /v2/subscriptions/{sub}, an error is returned (#1985) +- Fix: admin requests (such as GET /version) with errors now return '400 Bad Request', not '200 OK' +- Fix: attempt to create an entity with an error in service path (and probably more types of errors) now returns '400 Bad Request', not '200 OK' (#2763) - Fix: not calling regfree when regcomp fails. Potential fix for a crash (#2769) - Fix: wrongly overlogging metadata abscense in csub docs as Runtime Error (#2796) - Fix: if HTTP header Content-Length contains a value above the maximum payload size, an error is returned and the message is not read (#2761) diff --git a/doc/manuals/user/ngsiv2_implementation_notes.md b/doc/manuals/user/ngsiv2_implementation_notes.md index d2f49d9d10..b86fcf60dd 100644 --- a/doc/manuals/user/ngsiv2_implementation_notes.md +++ b/doc/manuals/user/ngsiv2_implementation_notes.md @@ -144,7 +144,7 @@ The particular validations that Orion implements on NGSIv2 subscription payloads * **id** or **idPattern**: one of them is mandatory (but both at the same time is not allowed). id must follow NGSIv2 restrictions for IDs. idPattern must be not empty and a valid regex. * **type** or **typePattern**: optional (but both at the same time is not allowed). type must - follow NGSIv2 restrictions for IDs. typePattern must be not empty and a valid regex. + follow NGSIv2 restrictions for IDs. type must not be empty. typePattern must be a valid regex, and non-empty. * **condition**: optional (but if present it must have a content, i.e. `{}` is not allowed) * **attrs**: optional (but if present it must be a list; empty list is allowed) * **expression**: optional (but if present it must have a content, i.e. `{}` is not allowed) diff --git a/src/lib/apiTypesV2/Entity.cpp b/src/lib/apiTypesV2/Entity.cpp index 5cf96b2efb..b870e0e07f 100644 --- a/src/lib/apiTypesV2/Entity.cpp +++ b/src/lib/apiTypesV2/Entity.cpp @@ -216,8 +216,8 @@ std::string Entity::check(ApiVersion apiVersion, RequestType requestType) { if (forbiddenIdChars(apiVersion, id.c_str())) { - alarmMgr.badInput(clientIp, "found a forbidden character in the id of an entity"); - return "Invalid characters in entity id"; + alarmMgr.badInput(clientIp, ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID); + return ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID; } } @@ -244,8 +244,8 @@ std::string Entity::check(ApiVersion apiVersion, RequestType requestType) { if (forbiddenIdChars(apiVersion, type.c_str())) { - alarmMgr.badInput(clientIp, "found a forbidden character in the type of an entity"); - return "Invalid characters in entity type"; + alarmMgr.badInput(clientIp, ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE); + return ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE; } } diff --git a/src/lib/common/errorMessages.h b/src/lib/common/errorMessages.h index 1fc703160b..73481069c3 100644 --- a/src/lib/common/errorMessages.h +++ b/src/lib/common/errorMessages.h @@ -56,6 +56,14 @@ #define ERROR_DESC_BAD_REQUEST_EMPTY_ENTITY_TYPE "entity type length: 0, min length supported: " STR(MIN_ID_LEN) #define ERROR_DESC_BAD_REQUEST_EMPTY_PAYLOAD "empty payload" #define ERROR_DESC_BAD_REQUEST_EMPTY_ENTITIES_VECTOR "empty entities vector" +#define ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID "Invalid characters in entity id" +#define ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE "Invalid characters in entity type" +#define ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTID "Invalid JSON type for entity id" +#define ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTTYPE "Invalid JSON type for entity type" +#define ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTIDPATTERN "Invalid JSON type for entity idPattern" +#define ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTTYPEPATTERN "Invalid JSON type for entity typePattern" +#define ERROR_DESC_BAD_REQUEST_INVALID_REGEX_ENTIDPATTERN "Invalid regex for entity idPattern" +#define ERROR_DESC_BAD_REQUEST_INVALID_REGEX_ENTTYPEPATTERN "Invalid regex for entity typePattern" #define ERROR_NOT_FOUND "NotFound" #define ERROR_DESC_NOT_FOUND_ENTITY "The requested entity has not been found. Check type and id" @@ -67,6 +75,6 @@ #define ERROR_TOO_MANY "TooManyResults" #define ERROR_DESC_TOO_MANY_ENTITIES "More than one matching entity. Please refine your query" -#define ERROR_DESC_BAD_VERB "method not allowed" +#define ERROR_DESC_BAD_VERB "method not allowed" #endif // SRC_LIB_COMMON_ERRORMESSAGES_H diff --git a/src/lib/jsonParseV2/parseEntity.cpp b/src/lib/jsonParseV2/parseEntity.cpp index aada26bec2..bd4099ffc2 100644 --- a/src/lib/jsonParseV2/parseEntity.cpp +++ b/src/lib/jsonParseV2/parseEntity.cpp @@ -32,6 +32,7 @@ #include "jsonParseV2/jsonParseTypeNames.h" #include "jsonParseV2/parseEntity.h" #include "jsonParseV2/parseContextAttribute.h" +#include "parse/forbiddenChars.h" #include "alarmMgr/alarmMgr.h" using namespace rapidjson; @@ -139,19 +140,32 @@ std::string parseEntity(ConnectionInfo* ciP, Entity* eP, bool eidInURL) { if (type != "String") { - alarmMgr.badInput(clientIp, "invalid JSON type for entity id"); - ciP->httpStatusCode = SccBadRequest;; - OrionError oe(SccBadRequest, "invalid JSON type for entity id", "BadRequest"); + alarmMgr.badInput(clientIp, ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTID); + ciP->httpStatusCode = SccBadRequest; + OrionError oe(SccBadRequest, ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTID, "BadRequest"); + return oe.toJson(); } eP->id = iter->value.GetString(); + + if (forbiddenIdChars(ciP->apiVersion, eP->id.c_str(), "")) + { + alarmMgr.badInput(clientIp, ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID); + ciP->httpStatusCode = SccBadRequest; + OrionError oe(SccBadRequest, ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID, "BadRequest"); + + return oe.toJson(); + } } else // "id" is present in payload for /v2/entities/ - not a valid payload { - alarmMgr.badInput(clientIp, "'id' is not a valid attribute"); - ciP->httpStatusCode = SccBadRequest;; - OrionError oe(SccBadRequest, "invalid input, 'id' as attribute", "BadRequest"); + const char* errorText = "invalid input, 'id' as attribute"; + + alarmMgr.badInput(clientIp, errorText); + ciP->httpStatusCode = SccBadRequest; + OrionError oe(SccBadRequest, errorText, "BadRequest"); + return oe.toJson(); } } @@ -159,14 +173,35 @@ std::string parseEntity(ConnectionInfo* ciP, Entity* eP, bool eidInURL) { if (type != "String") { - alarmMgr.badInput(clientIp, "invalid JSON type for entity type"); + alarmMgr.badInput(clientIp, ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTTYPE); ciP->httpStatusCode = SccBadRequest; - OrionError oe(SccBadRequest, "invalid JSON type for entity type", "BadRequest"); + OrionError oe(SccBadRequest, ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTTYPE, "BadRequest"); + return oe.toJson(); } eP->type = iter->value.GetString(); eP->typeGiven = true; + + if (eP->type.empty()) + { + const char* errorText = "entity type length: 0, min length supported: 1"; + + alarmMgr.badInput(clientIp, errorText); + ciP->httpStatusCode = SccBadRequest; + OrionError oe(SccBadRequest, errorText, "BadRequest"); + + return oe.toJson(); + } + + if (forbiddenIdChars(ciP->apiVersion, eP->type.c_str(), "")) + { + alarmMgr.badInput(clientIp, ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE); + ciP->httpStatusCode = SccBadRequest; + OrionError oe(SccBadRequest, ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE, "BadRequest"); + + return oe.toJson(); + } } else // attribute { diff --git a/src/lib/jsonParseV2/parseEntityObject.cpp b/src/lib/jsonParseV2/parseEntityObject.cpp index 6632bb066e..919778a5c3 100644 --- a/src/lib/jsonParseV2/parseEntityObject.cpp +++ b/src/lib/jsonParseV2/parseEntityObject.cpp @@ -24,6 +24,7 @@ */ #include "rapidjson/document.h" +#include "common/errorMessages.h" #include "rest/ConnectionInfo.h" #include "ngsi/ParseData.h" #include "ngsi/Request.h" @@ -68,27 +69,27 @@ std::string parseEntityObject(ConnectionInfo* ciP, Value::ConstValueIterator val { if (type != "String") { - return "invalid JSON type for entity id"; + return ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTID; } eP->id = iter->value.GetString(); - if (forbiddenChars(eP->id.c_str(), "")) + if (forbiddenIdChars(ciP->apiVersion, eP->id.c_str(), "")) { - return "Invalid characters in entity id"; + return ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID; } } else if (name == "idPattern") { if (type != "String") { - return "invalid JSON type for entity idPattern"; + return ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTIDPATTERN; } regex_t re; if (regcomp(&re, iter->value.GetString(), REG_EXTENDED) != 0) { - return "invalid regex for entity id pattern"; + return ERROR_DESC_BAD_REQUEST_INVALID_REGEX_ENTIDPATTERN; } regfree(&re); // If regcomp fails it frees up itself (see glibc sources for details) @@ -99,28 +100,33 @@ std::string parseEntityObject(ConnectionInfo* ciP, Value::ConstValueIterator val { if (type != "String") { - return "invalid JSON type for entity type"; + return ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTTYPE; } eP->type = iter->value.GetString(); eP->typeGiven = true; - if (forbiddenChars(eP->type.c_str(), "")) + if (eP->type.empty()) { - return "Invalid characters in entity type"; + return "entity type length: 0, min length supported: 1"; + } + + if (forbiddenIdChars(ciP->apiVersion, eP->type.c_str(), "")) + { + return ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE; } } else if (name == "typePattern") { if (type != "String") { - return "invalid JSON type for entity typePattern"; + return ERROR_DESC_BAD_REQUEST_INVALID_JTYPE_ENTTYPEPATTERN; } regex_t re; if (regcomp(&re, iter->value.GetString(), REG_EXTENDED) != 0) { - return "invalid regex for entity type pattern"; + return ERROR_DESC_BAD_REQUEST_INVALID_REGEX_ENTTYPEPATTERN; } regfree(&re); // If regcomp fails it frees up itself (see glibc sources for details) diff --git a/src/lib/jsonParseV2/parseSubscription.cpp b/src/lib/jsonParseV2/parseSubscription.cpp index 6131c850d3..477fafa84a 100644 --- a/src/lib/jsonParseV2/parseSubscription.cpp +++ b/src/lib/jsonParseV2/parseSubscription.cpp @@ -362,7 +362,7 @@ static std::string parseEntitiesVector(ConnectionInfo* ciP, std::vector* regex_t re; if (regcomp(&re, idPattern.c_str(), REG_EXTENDED) != 0) { - return badInput(ciP, "Invalid regex for entity id pattern"); + return badInput(ciP, ERROR_DESC_BAD_REQUEST_INVALID_REGEX_ENTIDPATTERN); } regfree(&re); // If regcomp fails it frees up itself } @@ -384,6 +384,10 @@ static std::string parseEntitiesVector(ConnectionInfo* ciP, std::vector* { return badInput(ciP, "max type length exceeded"); } + if (typeOpt.value.empty()) + { + return badInput(ciP, "entity type length: 0, min length supported: 1"); + } type = typeOpt.value; } } @@ -407,7 +411,7 @@ static std::string parseEntitiesVector(ConnectionInfo* ciP, std::vector* regex_t re; if (regcomp(&re, typePattern.c_str(), REG_EXTENDED) != 0) { - return badInput(ciP, "Invalid regex for entity id pattern"); + return badInput(ciP, ERROR_DESC_BAD_REQUEST_INVALID_REGEX_ENTTYPEPATTERN); } regfree(&re); // If regcomp fails it frees up itself } diff --git a/src/lib/rest/OrionError.cpp b/src/lib/rest/OrionError.cpp index 06899d6e78..cb0fa7ab31 100644 --- a/src/lib/rest/OrionError.cpp +++ b/src/lib/rest/OrionError.cpp @@ -109,7 +109,7 @@ std::string OrionError::smartRender(ApiVersion apiVersion) */ std::string OrionError::setStatusCodeAndSmartRender(ApiVersion apiVersion, HttpStatusCode* scP) { - if (apiVersion == V2) + if ((apiVersion == V2) || (apiVersion == ADMIN_API)) { *scP = code; } diff --git a/src/lib/serviceRoutinesV2/postEntities.cpp b/src/lib/serviceRoutinesV2/postEntities.cpp index 9812a8dada..4c8f8a8de6 100644 --- a/src/lib/serviceRoutinesV2/postEntities.cpp +++ b/src/lib/serviceRoutinesV2/postEntities.cpp @@ -100,13 +100,22 @@ std::string postEntities // 02. Call standard op postUpdateContext postUpdateContext(ciP, components, compV, parseDataP, NGSIV2_FLAVOUR_ONCREATE); - // 03. Check error + // + // 03. Check error - 3 different ways to get an error from postUpdateContext ... :-( + // FIXME P4: make postUpdateContext have ONE way to return errors. See github issue #2763 + // std::string answer = ""; - if (parseDataP->upcrs.res.oe.code != SccNone ) + if (parseDataP->upcrs.res.oe.code != SccNone) { TIMED_RENDER(answer = parseDataP->upcrs.res.oe.toJson()); ciP->httpStatusCode = parseDataP->upcrs.res.oe.code; } + else if (parseDataP->upcrs.res.errorCode.code != SccOk) + { + ciP->httpStatusCode = parseDataP->upcrs.res.errorCode.code; + TIMED_RENDER(answer = parseDataP->upcrs.res.errorCode.toJson(true)); + ciP->answer = answer; + } else { // Prepare HTTP headers diff --git a/test/functionalTest/cases/0970_create_entity/errors_at_creation.test b/test/functionalTest/cases/0970_create_entity/errors_at_creation.test index 28f904d8b7..4ea81ac87f 100644 --- a/test/functionalTest/cases/0970_create_entity/errors_at_creation.test +++ b/test/functionalTest/cases/0970_create_entity/errors_at_creation.test @@ -185,7 +185,7 @@ Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "invalid JSON type for entity id", + "description": "Invalid JSON type for entity id", "error": "BadRequest" } @@ -199,7 +199,7 @@ Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "invalid JSON type for entity id", + "description": "Invalid JSON type for entity id", "error": "BadRequest" } @@ -213,7 +213,7 @@ Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "invalid JSON type for entity type", + "description": "Invalid JSON type for entity type", "error": "BadRequest" } @@ -227,7 +227,7 @@ Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "invalid JSON type for entity type", + "description": "Invalid JSON type for entity type", "error": "BadRequest" } diff --git a/test/functionalTest/cases/1108_non_string_fields/non_string_fields.test b/test/functionalTest/cases/1108_non_string_fields/non_string_fields.test index b9e30a77d0..77cdde0046 100644 --- a/test/functionalTest/cases/1108_non_string_fields/non_string_fields.test +++ b/test/functionalTest/cases/1108_non_string_fields/non_string_fields.test @@ -120,7 +120,7 @@ Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "invalid JSON type for entity id", + "description": "Invalid JSON type for entity id", "error": "BadRequest" } @@ -134,7 +134,7 @@ Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "invalid JSON type for entity type", + "description": "Invalid JSON type for entity type", "error": "BadRequest" } diff --git a/test/functionalTest/cases/1728_bug_forbidden_chars/restrict_chars_entity.test b/test/functionalTest/cases/1728_bug_forbidden_chars/forbidden_chars_entity.test similarity index 98% rename from test/functionalTest/cases/1728_bug_forbidden_chars/restrict_chars_entity.test rename to test/functionalTest/cases/1728_bug_forbidden_chars/forbidden_chars_entity.test index dc46b8e4ca..10f9de691a 100644 --- a/test/functionalTest/cases/1728_bug_forbidden_chars/restrict_chars_entity.test +++ b/test/functionalTest/cases/1728_bug_forbidden_chars/forbidden_chars_entity.test @@ -21,7 +21,7 @@ # VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh --NAME-- -Restict chars in id (old set) +Forbidden chars in id (old set) --SHELL-INIT-- dbInit CB diff --git a/test/functionalTest/cases/1985_empty_entity_type/empty_entity_type_in_subscription.test b/test/functionalTest/cases/1985_empty_entity_type/empty_entity_type_in_subscription.test new file mode 100644 index 0000000000..59fdd34fd9 --- /dev/null +++ b/test/functionalTest/cases/1985_empty_entity_type/empty_entity_type_in_subscription.test @@ -0,0 +1,148 @@ +# Copyright 2016 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +Empty entity type in subscription + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +# +# 01. Attempt to create a subscription with an empty entity type, see error +# 02. Create a subscription +# 03. Attempt to modify a subscription with an empty entity type, see error +# + +echo "01. Attempt to create a subscription with an empty entity type, see error" +echo "=========================================================================" +payload='{ + "subject": { + "entities": [ + { + "id": "E", + "type": "" + } + ], + "condition": { + "attrs": [ ] + } + }, + "notification": { + "http": {"url": "http://localhost:'$LISTENER_PORT'/notify"}, + "metadata": [ "dateCreated", "dateModified" ] + }, + "expires": "2050-04-05T14:00:00.00Z" +}' +orionCurl --url /v2/subscriptions --payload "$payload" +echo +echo + + +echo "02. Create a subscription" +echo "=========================" +payload='{ + "subject": { + "entities": [ + { + "id": "E", + "type": "T" + } + ], + "condition": { + "attrs": [ ] + } + }, + "notification": { + "http": {"url": "http://localhost:'$LISTENER_PORT'/notify"}, + "metadata": [ "dateCreated", "dateModified" ] + }, + "expires": "2050-04-05T14:00:00.00Z" +}' +orionCurl --url /v2/subscriptions --payload "$payload" +SUB_ID=$(echo "$_responseHeaders" | grep Location | awk -F/ '{ print $4 }' | tr -d "\r\n") +echo +echo + + +echo "03. Attempt to modify a subscription with an empty entity type, see error" +echo "=========================================================================" +payload=' +{ + "subject": { + "entities": [ + { + "id": "E", + "type": "" + } + ] + } +}' +orionCurl -X PATCH --url /v2/subscriptions/$SUB_ID --payload "$payload" +echo +echo + + +--REGEXPECT-- +01. Attempt to create a subscription with an empty entity type, see error +========================================================================= +HTTP/1.1 400 Bad Request +Content-Length: 85 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +{ + "description": "entity type length: 0, min length supported: 1", + "error": "BadRequest" +} + + +02. Create a subscription +========================= +HTTP/1.1 201 Created +Content-Length: 0 +Location: /v2/subscriptions/REGEX([0-9a-f\-]{24}) +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + + + +03. Attempt to modify a subscription with an empty entity type, see error +========================================================================= +HTTP/1.1 400 Bad Request +Content-Length: 85 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +{ + "description": "entity type length: 0, min length supported: 1", + "error": "BadRequest" +} + + +--TEARDOWN-- +brokerStop CB +dbDrop CB diff --git a/test/functionalTest/cases/2257_crash_with_invalid_subscription_when_matching/crash_with_invalid_subscription_when_matching.test b/test/functionalTest/cases/2257_crash_with_invalid_subscription_when_matching/crash_with_invalid_subscription_when_matching.test index df4060464c..5fc4e3a7cc 100644 --- a/test/functionalTest/cases/2257_crash_with_invalid_subscription_when_matching/crash_with_invalid_subscription_when_matching.test +++ b/test/functionalTest/cases/2257_crash_with_invalid_subscription_when_matching/crash_with_invalid_subscription_when_matching.test @@ -70,13 +70,13 @@ echo 01. Create subscription with invalid regex in idPattern ======================================================= HTTP/1.1 400 Bad Request -Content-Length: 74 +Content-Length: 73 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "Invalid regex for entity id pattern", + "description": "Invalid regex for entity idPattern", "error": "BadRequest" } diff --git a/test/functionalTest/cases/2763_postUpdateContext_error_return/invalid_service_path_gives_400_bad_request.test b/test/functionalTest/cases/2763_postUpdateContext_error_return/invalid_service_path_gives_400_bad_request.test new file mode 100644 index 0000000000..4bd6bccbb1 --- /dev/null +++ b/test/functionalTest/cases/2763_postUpdateContext_error_return/invalid_service_path_gives_400_bad_request.test @@ -0,0 +1,56 @@ +# Copyright 2016 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +invalid_service_path_gives_400_bad_request + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +echo "01. Send a /version request with an Invalid Service Path, see 400 Bad Request" +echo "=============================================================================" +orionCurl --url /version --servicePath "notStartingWithSlash" +echo +echo + + +--REGEXPECT-- +01. Send a /version request with an Invalid Service Path, see 400 Bad Request +============================================================================= +HTTP/1.1 400 Bad Request +Content-Length: 111 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +{ + "description": "Only /absolute/ Service Paths allowed [a service path must begin with /]", + "error": "BadRequest" +} + + +--TEARDOWN-- +brokerStop CB +dbDrop CB diff --git a/test/functionalTest/cases/2763_postUpdateContext_error_return/postEntities_with_errors_in_service_path.test b/test/functionalTest/cases/2763_postUpdateContext_error_return/postEntities_with_errors_in_service_path.test new file mode 100644 index 0000000000..f062ee605e --- /dev/null +++ b/test/functionalTest/cases/2763_postUpdateContext_error_return/postEntities_with_errors_in_service_path.test @@ -0,0 +1,80 @@ +# Copyright 2016 Telefonica Investigacion y Desarrollo, S.A.U +# +# This file is part of Orion Context Broker. +# +# Orion Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# iot_support at tid dot es + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +postEntities with errors in service-path + +--SHELL-INIT-- +dbInit CB +brokerStart CB + +--SHELL-- + +# +# 01. Try to create an entity with more than one service path - see error +# 02. Make sure the entity hasn't been created +# + +echo "01. Try to create an entity with more than one service path - see error" +echo "=======================================================================" +payload='{ "id": "E1", "type": "T1" }' +orionCurl --url /v2/entities --payload "$payload" --servicePath /E1,/E2 +echo +echo + + +echo "02. Make sure the entity hasn't been created" +echo "============================================" +orionCurl --url /v2/entities +echo +echo + + +--REGEXPECT-- +01. Try to create an entity with more than one service path - see error +======================================================================= +HTTP/1.1 400 Bad Request +Content-Length: 79 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +{ + "code": "400", + "details": "more than one service path in context update request" +} + + +02. Make sure the entity hasn't been created +============================================ +HTTP/1.1 200 OK +Content-Length: 2 +Content-Type: application/json +Fiware-Correlator: REGEX([0-9a-f\-]{36}) +Date: REGEX(.*) + +[] + + +--TEARDOWN-- +brokerStop CB +dbDrop CB diff --git a/test/functionalTest/cases/2819_forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern/forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern.test b/test/functionalTest/cases/2819_forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern/forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern.test index bc69bb8710..d7eed8437a 100644 --- a/test/functionalTest/cases/2819_forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern/forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern.test +++ b/test/functionalTest/cases/2819_forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern/forbiddenChars_in_batch-query_and_NOT_in_URI_param_typePattern.test @@ -117,13 +117,13 @@ Date: REGEX(.*) 02. POST /v2/op/query with forbidden chars '<>=;' in entity::id, see it fail due to forbidden chars =================================================================================================== HTTP/1.1 400 Bad Request -Content-Length: 67 +Content-Length: 70 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "forbidden chars in entity id", + "description": "Invalid characters in entity id", "error": "BadRequest" } @@ -131,13 +131,13 @@ Date: REGEX(.*) 03. POST /v2/op/query with forbidden chars '<>=;' in entity::type, see it fail due to forbidden chars ===================================================================================================== HTTP/1.1 400 Bad Request -Content-Length: 69 +Content-Length: 72 Content-Type: application/json Fiware-Correlator: REGEX([0-9a-f\-]{36}) Date: REGEX(.*) { - "description": "forbidden chars in entity type", + "description": "Invalid characters in entity type", "error": "BadRequest" } diff --git a/test/unittests/apiTypesV2/Entity_test.cpp b/test/unittests/apiTypesV2/Entity_test.cpp index 346647d889..1c3b4eac83 100644 --- a/test/unittests/apiTypesV2/Entity_test.cpp +++ b/test/unittests/apiTypesV2/Entity_test.cpp @@ -22,10 +22,12 @@ * * Author: Fermin Galan */ - #include "apiTypesV2/Entity.h" +#include "common/errorMessages.h" #include "unittest.h" + + /* **************************************************************************** * * present - no output expected, just exercising the code @@ -72,14 +74,14 @@ TEST(Entity, check) EXPECT_EQ("No Entity ID", enP->check(V1, EntitiesRequest)); enP->id = "E<1>"; - EXPECT_EQ("Invalid characters in entity id", enP->check(V1, EntitiesRequest)); + EXPECT_EQ(ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID, enP->check(V1, EntitiesRequest)); enP->isPattern = "true"; EXPECT_EQ("OK", enP->check(V1, EntitiesRequest)); enP->id = "E"; enP->isPattern = "false"; enP->type = "T<1>"; - EXPECT_EQ("Invalid characters in entity type", enP->check(V1, EntitiesRequest)); + EXPECT_EQ(ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE, enP->check(V1, EntitiesRequest)); enP->isTypePattern = true; EXPECT_EQ("OK", enP->check(V1, EntitiesRequest)); enP->type = "T";