Skip to content

Commit

Permalink
Giving errors for expiresAt in the past
Browse files Browse the repository at this point in the history
  • Loading branch information
kzangeli committed Sep 1, 2023
1 parent 476ce36 commit c9c4561
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Fixed issues:
* #280 - Added extensive logging for downloading of contexts
* #280 - Fixed two erroneous type values in the response payload body (EntityTypeInformation => EntityTypeInfo and EntityAttributeList => AttributeList)
* #280 - Changed all error codes from InvalidRequest to BadRequestData, except for JSON Parse Error that is still an InvalidRequest
* #280 - Giving errors for expiresAt in the past
1 change: 1 addition & 0 deletions src/lib/orionld/kjTree/kjTreeToRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ bool kjTreeToRegistration(ngsiv2::Registration* regP, char** regIdPP)
DUPLICATE_CHECK(expiresP, "Registration::expiresAt", kNodeP);
STRING_CHECK(kNodeP, "Registration::expiresAt");
DATETIME_CHECK(expiresP->value.s, regP->expires, "Registration::expiresAt");
PCHECK_EXPIRESAT_IN_FUTURE(0, "Invalid Registration", "/expiresAt/ in the past", 400, regP->expires, orionldState.requestTime);
}
else if (strcmp(kNodeP->name, "endpoint") == 0)
{
Expand Down
7 changes: 6 additions & 1 deletion src/lib/orionld/kjTree/kjTreeToSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern "C"
#include "orionld/common/CHECK.h" // CHECKx()
#include "orionld/common/SCOMPARE.h" // SCOMPAREx
#include "orionld/common/uuidGenerate.h" // uuidGenerate
#include "orionld/payloadCheck/PCHECK.h" // PCHECK_URI
#include "orionld/payloadCheck/PCHECK.h" // PCHECK_URI, PCHECK_EXPIRESAT_IN_FUTURE
#include "orionld/payloadCheck/fieldPaths.h" // Paths to fields in the payload
#include "orionld/q/qRender.h" // qRender
#include "orionld/kjTree/kjTreeToEntIdVector.h" // kjTreeToEntIdVector
Expand All @@ -48,6 +48,10 @@ extern "C"



// -----------------------------------------------------------------------------
//
// oldTreatmentForQ -
//
bool oldTreatmentForQ(ngsiv2::Subscription* subP, char* q)
{
bool qWithOr = false;
Expand Down Expand Up @@ -306,6 +310,7 @@ bool kjTreeToSubscription(ngsiv2::Subscription* subP, char** subIdPP, KjNode** e
DUPLICATE_CHECK(expiresP, "Subscription::expiresAt", kNodeP->value.s);
STRING_CHECK(kNodeP, "Subscription::expiresAt");
DATETIME_CHECK(expiresP, subP->expires, "Subscription::expiresAt");
PCHECK_EXPIRESAT_IN_FUTURE(0, "Invalid Subscription", "/expiresAt/ in the past", 400, subP->expires, orionldState.requestTime);
}
else if (strcmp(kNodeP->name, "throttling") == 0)
{
Expand Down
19 changes: 19 additions & 0 deletions src/lib/orionld/payloadCheck/PCHECK.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,25 @@ do



// -----------------------------------------------------------------------------
//
// PCHECK_EXPIRESAT_IN_FUTURE
//
#define PCHECK_EXPIRESAT_IN_FUTURE(_type, _title, detail, status, expiresAt, now) \
do \
{ \
int type = (_type == 0)? OrionldBadRequestData : _type; \
const char* title = (_title == NULL)? "expiresAt in the past" : _title; \
\
if (expiresAt < now) \
{ \
orionldError((OrionldResponseErrorType) type, title, detail, status); \
return false; \
} \
} while (0)



// -----------------------------------------------------------------------------
//
// PCHECK_SPECIAL_ATTRIBUTE
Expand Down
1 change: 1 addition & 0 deletions src/lib/orionld/payloadCheck/pCheckSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ bool pCheckSubscription
PCHECK_STRING(subItemP, 0, NULL, SubscriptionExpiresAtPath, 400);
PCHECK_DUPLICATE(expiresAtP, subItemP, 0, NULL, SubscriptionExpiresAtPath, 400);
PCHECK_ISO8601(expiresAt, expiresAtP->value.s, 0, NULL, SubscriptionExpiresAtPath, 400);
PCHECK_EXPIRESAT_IN_FUTURE(0, "Invalid Subscription", "/expiresAt/ in the past", 400, expiresAt, orionldState.requestTime);
}
else if (strcmp(subItemP->name, "throttling") == 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/lib/orionld/payloadCheck/pcheckRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ bool pcheckRegistration(const char* regModeString, KjNode* registrationP, const
STRING_CHECK(nodeP, nodeP->name);
EMPTY_STRING_CHECK(nodeP, nodeP->name);
DATETIME_CHECK(expiresP->value.s, dateTime, nodeP->name);
PCHECK_EXPIRESAT_IN_FUTURE(0, "Invalid Registration", "/expiresAt/ in the past", 400, dateTime, orionldState.requestTime);
nodeP->name = (char*) "expiresAt";
}
else if (strcmp(nodeP->name, "endpoint") == 0)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/orionld/payloadCheck/pcheckSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C"
#include "orionld/common/CHECK.h" // STRING_CHECK, ...
#include "orionld/q/qAliasCompact.h" // qAliasCompact
#include "orionld/context/orionldAttributeExpand.h" // orionldAttributeExpand
#include "orionld/payloadCheck/PCHECK.h" // PCHECK_EXPIRESAT_IN_FUTURE
#include "orionld/payloadCheck/fieldPaths.h" // Paths to fields in the payload, e.g. subscriptionNotification = Subscription::notification"
#include "orionld/payloadCheck/pcheckGeoQ.h" // pcheckGeoQ
#include "orionld/payloadCheck/pcheckEntityInfoArray.h" // pcheckEntityInfoArray
Expand Down Expand Up @@ -189,6 +190,7 @@ bool pcheckSubscription
STRING_CHECK(nodeP, nodeP->name);
EMPTY_STRING_CHECK(nodeP, nodeP->name);
DATETIME_CHECK(expiresP->value.s, dateTime, nodeP->name);
PCHECK_EXPIRESAT_IN_FUTURE(0, "Invalid Subscription", "/expiresAt/ in the past", 400, dateTime, orionldState.requestTime);
}
else if (strcmp(nodeP->name, "lang") == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ echo
echo "21. PATCH the registration with a new expires"
echo "============================================="
payload='{
"expiresAt": "2021-12-31T18:19:20.210Z"
"expiresAt": "2035-12-31T18:19:20.210Z"
}'
orionCurl --url /ngsi-ld/v1/csourceRegistrations/urn:ngsi-ld:ContextSourceRegistration:R1 -X PATCH --payload "$payload"
echo
Expand Down Expand Up @@ -1314,7 +1314,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
2
]
},
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2035-12-31T18:19:20.210Z",
"status": "active",
"endpoint": "http://my.csource.org:1026",
"P1": 1,
Expand Down Expand Up @@ -1388,7 +1388,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
2
]
},
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2035-12-31T18:19:20.210Z",
"status": "active",
"endpoint": "http://localhost:1026/go",
"P1": 1,
Expand Down Expand Up @@ -1462,7 +1462,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
2
]
},
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2035-12-31T18:19:20.210Z",
"status": "active",
"endpoint": "http://localhost:1026/go",
"P1": "New P1",
Expand Down Expand Up @@ -1536,7 +1536,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
2
]
},
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2035-12-31T18:19:20.210Z",
"status": "active",
"endpoint": "http://localhost:1026/go",
"P1": "New P1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ orionldStart CB -experimental
# 116. Attempt to create a registration with an 'expiresAt' that is not a string - see failure
# 117. Attempt to create a registration with an 'expiresAt' that is an empty string - see failure
# 118. Attempt to create a registration with an 'expiresAt' that is not a valid DateTime - see failure
# 118. Attempt to create a registration with an 'expiresAt' that is in the past - see failure
# 119. Attempt to create a registration with a duplicated 'expiresAt' - see failure
#
# 120. Attempt to create a registration without 'endpoint' - see failure
Expand Down Expand Up @@ -1878,14 +1879,27 @@ echo
echo


echo "118. Attempt to create a registration with an 'expiresAt' that is in the past - see failure"
echo "==========================================================================================="
payload='{
"type": "ContextSourceRegistration",
"endpoint": "http://a.b.c:1234/abc",
"information": [ { "propertyNames": [ "P1" ] } ],
"expiresAt": "2023-09-01T10:00:00.000Z"
}'
orionCurl --url /ngsi-ld/v1/csourceRegistrations --payload "$payload"
echo
echo


echo "119. Attempt to create a registration with a duplicated 'expiresAt' - see failure"
echo "================================================================================="
payload='{
"type": "ContextSourceRegistration",
"endpoint": "http://a.b.c:1234/abc",
"information": [ { "propertyNames": [ "P1" ] } ],
"expiresAt": "2022-10-02T18:00:00Z",
"expiresAt": "2022-10-02T18:00:00Z"
"expiresAt": "2032-10-02T18:00:00Z",
"expiresAt": "2032-10-02T18:00:00Z"
}'
orionCurl --url /ngsi-ld/v1/csourceRegistrations --payload "$payload"
echo
Expand Down Expand Up @@ -4466,6 +4480,20 @@ Date: REGEX(.*)
}


118. Attempt to create a registration with an 'expiresAt' that is in the past - see failure
===========================================================================================
HTTP/1.1 400 Bad Request
Content-Length: 127
Content-Type: application/json
Date: REGEX(.*)

{
"detail": "/expiresAt/ in the past",
"title": "Invalid Registration",
"type": "https://uri.etsi.org/ngsi-ld/errors/BadRequestData"
}


119. Attempt to create a registration with a duplicated 'expiresAt' - see failure
=================================================================================
HTTP/1.1 400 Bad Request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ echo
echo "21. PATCH the registration with a new expires"
echo "============================================="
payload='{
"expiresAt": "2021-12-31T18:19:20.210Z"
"expiresAt": "2032-12-31T18:19:20.210Z"
}'
orionCurl --url /ngsi-ld/v1/csourceRegistrations/urn:ngsi-ld:ContextSourceRegistration:R1 -X PATCH --payload "$payload"
echo
Expand Down Expand Up @@ -1255,7 +1255,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
"type": "ContextSourceRegistration",
"name": "New Name",
"description": "New Description",
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2032-12-31T18:19:20.210Z",
"endpoint": "http://my.csource.org:1026",
"information": [
{
Expand Down Expand Up @@ -1327,7 +1327,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
"type": "ContextSourceRegistration",
"name": "New Name",
"description": "New Description",
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2032-12-31T18:19:20.210Z",
"endpoint": "http://localhost:1026/go",
"information": [
{
Expand Down Expand Up @@ -1399,7 +1399,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
"type": "ContextSourceRegistration",
"name": "New Name",
"description": "New Description",
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2032-12-31T18:19:20.210Z",
"endpoint": "http://localhost:1026/go",
"information": [
{
Expand Down Expand Up @@ -1478,7 +1478,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
"type": "ContextSourceRegistration",
"name": "New Name",
"description": "New Description",
"expiresAt": "2021-12-31T18:19:20.210Z",
"expiresAt": "2032-12-31T18:19:20.210Z",
"endpoint": "http://localhost:1026/go",
"information": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ orionldStart CB
# 11. Create the subscription from https://github.com/FIWARE/context.Orion-LD/issues/895
# 12. See the subscription in mongo
#
# 13. Attempt to create a subscription with an expiresAt in the past
#


echo "01. POST /ngsi-ld/v1/subscriptions, with a sub-id"
Expand Down Expand Up @@ -401,6 +403,31 @@ echo
echo


echo "13. Attempt to create a subscription with an expiresAt in the past"
echo "=================================================================="
payload='{
"id": "urn:ngsi-ld:subs:13",
"description": "test0001",
"type": "Subscription",
"entities": [
{
"type": "http://XXXX/YYYY/ngsi-context.jsonld#Person"
}
],
"notification": {
"format": "normalized",
"endpoint": {
"uri": "http://tutorial:3000/subscription/test0001",
"accept": "application/json"
}
},
"expiresAt": "2023-09-01T10:00:00.000Z"
}'
orionCurl --url /ngsi-ld/v1/subscriptions --payload "$payload" -H "Content-Type: application/json"
echo
echo


--REGEXPECT--
01. POST /ngsi-ld/v1/subscriptions, with a sub-id
=================================================
Expand Down Expand Up @@ -747,6 +774,20 @@ MongoDB server REGEX(.*)
bye


13. Attempt to create a subscription with an expiresAt in the past
==================================================================
HTTP/1.1 400 Bad Request
Content-Length: 127
Content-Type: application/json
Date: REGEX(.*)

{
"detail": "/expiresAt/ in the past",
"title": "Invalid Subscription",
"type": "https://uri.etsi.org/ngsi-ld/errors/BadRequestData"
}


--TEARDOWN--
brokerStop CB
dbDrop CB

0 comments on commit c9c4561

Please sign in to comment.