Skip to content

Commit

Permalink
fix for wrong date values
Browse files Browse the repository at this point in the history
  • Loading branch information
ArqamFarooqui110719 committed May 28, 2024
1 parent 3bb75ee commit fbccd91
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Fix: simplified GET /version operation, without including "libversions" field (add ?options=libVersions to get it)
- Fix: lighter operation to get databases list from MongoDB (#4517)
- Fix: wrong date values should not allowed in subscription's expires field (#4541)
19 changes: 19 additions & 0 deletions src/lib/common/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,25 @@ double parse8601Time(const std::string& ss)
time.tm_min = m; // 0-59
time.tm_sec = (int) s; // 0-61 (0-60 in C++11)

const int minYear = 0;
const int minMonth = 0;
const int maxMonth = 11;
const int minDay = 1;
const int maxDay = 31;
const int minHour = 0;
const int maxHour = 23;
const int minMinute = 0;
const int maxMinute = 59;
const int minSecond = 0;
const int maxSecond = 59;

if (time.tm_year < minYear || time.tm_mon < minMonth || time.tm_mon > maxMonth || time.tm_mday < minDay ||
time.tm_mday > maxDay || time.tm_hour < minHour || time.tm_hour > maxHour || time.tm_min < minMinute ||
time.tm_min > maxMinute || time.tm_sec < minSecond || time.tm_sec > maxSecond)
{
return -1;
}

int64_t totalSecs = timegm(&time) - offset;
float millis = s - (int) s;
double timestamp = totalSecs;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,358 @@
# Copyright 2024 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--
Wrong date values are not supported in 'expires' field of subscription

--SHELL-INIT--
dbInit CB
brokerStart CB

--SHELL--

#
# 01. Create a subscription with an wrong date value in the 'seconds' component of the 'expires' field, see error
# 02. Create a subscription with an wrong date value in the 'minutes' component of the 'expires' field, see error
# 03. Create a subscription with an wrong date value in the 'hours' component of the 'expires' field, see error
# 04. Create a subscription with an wrong date value in the 'days' component of the 'expires' field, see error
# 05. Create a subscription with an wrong date value in the 'months' component of the 'expires' field, see error
# 06. Create a subscription with an wrong date value in the 'years' component of the 'expires' field, see error
# 07. Create a subscription with an valid date value in 'expires' field, (success case)
#


echo "01. Create a subscription with an wrong date value in the 'seconds' component of the 'expires' field, see error"
echo "==============================================================================================================="
payload='{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [
"pressure"
]
}
},
"notification": {
"http": {
"url": "http://localhost:'$LISTENER_PORT'/notify"
},
"attrs": [
"pressure"
]
},
"expires": "2022-12-21T17:16:63.00Z"
}'
orionCurl --url /v2/subscriptions --payload "$payload"
echo
echo


echo "02. Create a subscription with an wrong date value in the 'minutes' component of the 'expires' field, see error"
echo "==============================================================================================================="
payload='{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [
"pressure"
]
}
},
"notification": {
"http": {
"url": "http://localhost:'$LISTENER_PORT'/notify"
},
"attrs": [
"pressure"
]
},
"expires": "2022-12-21T17:65:03.00Z"
}'
orionCurl --url /v2/subscriptions --payload "$payload"
echo
echo


echo "03. Create a subscription with an wrong date value in the 'hours' component of the 'expires' field, see error"
echo "============================================================================================================="
payload='{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [
"pressure"
]
}
},
"notification": {
"http": {
"url": "http://localhost:'$LISTENER_PORT'/notify"
},
"attrs": [
"pressure"
]
},
"expires": "2022-12-21T25:16:11.00Z"
}'
orionCurl --url /v2/subscriptions --payload "$payload"
echo
echo


echo "04. Create a subscription with an wrong date value in the 'days' component of the 'expires' field, see error"
echo "============================================================================================================"
payload='{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [
"pressure"
]
}
},
"notification": {
"http": {
"url": "http://localhost:'$LISTENER_PORT'/notify"
},
"attrs": [
"pressure"
]
},
"expires": "2022-12-34T17:16:23.00Z"
}'
orionCurl --url /v2/subscriptions --payload "$payload"
echo
echo


echo "05. Create a subscription with an wrong date value in the 'months' component of the 'expires' field, see error"
echo "=============================================================================================================="
payload='{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [
"pressure"
]
}
},
"notification": {
"http": {
"url": "http://localhost:'$LISTENER_PORT'/notify"
},
"attrs": [
"pressure"
]
},
"expires": "2022-13-21T17:16:43.00Z"
}'
orionCurl --url /v2/subscriptions --payload "$payload"
echo
echo


echo "06. Create a subscription with an wrong date value in the 'years' component of the 'expires' field, see error"
echo "============================================================================================================="
payload='{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [
"pressure"
]
}
},
"notification": {
"http": {
"url": "http://localhost:'$LISTENER_PORT'/notify"
},
"attrs": [
"pressure"
]
},
"expires": "1822-12-21T17:16:23.00Z"
}'
orionCurl --url /v2/subscriptions --payload "$payload"
echo
echo


echo "07. Create a subscription with an valid date value in 'expires' field, (success case)"
echo "====================================================================================="
payload='{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [
"pressure"
]
}
},
"notification": {
"http": {
"url": "http://localhost:'$LISTENER_PORT'/notify"
},
"attrs": [
"pressure"
]
},
"expires": "2025-12-31T23:59:59.00Z"
}'
orionCurl --url /v2/subscriptions --payload "$payload"
echo
echo


--REGEXPECT--
01. Create a subscription with an wrong date value in the 'seconds' component of the 'expires' field, see error
===============================================================================================================
HTTP/1.1 400 Bad Request
Date: REGEX(.*)
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Content-Type: application/json
Content-Length: 68

{
"description": "expires has an invalid format",
"error": "BadRequest"
}


02. Create a subscription with an wrong date value in the 'minutes' component of the 'expires' field, see error
===============================================================================================================
HTTP/1.1 400 Bad Request
Date: REGEX(.*)
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Content-Type: application/json
Content-Length: 68

{
"description": "expires has an invalid format",
"error": "BadRequest"
}


03. Create a subscription with an wrong date value in the 'hours' component of the 'expires' field, see error
=============================================================================================================
HTTP/1.1 400 Bad Request
Date: REGEX(.*)
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Content-Type: application/json
Content-Length: 68

{
"description": "expires has an invalid format",
"error": "BadRequest"
}


04. Create a subscription with an wrong date value in the 'days' component of the 'expires' field, see error
============================================================================================================
HTTP/1.1 400 Bad Request
Date: REGEX(.*)
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Content-Type: application/json
Content-Length: 68

{
"description": "expires has an invalid format",
"error": "BadRequest"
}


05. Create a subscription with an wrong date value in the 'months' component of the 'expires' field, see error
==============================================================================================================
HTTP/1.1 400 Bad Request
Date: REGEX(.*)
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Content-Type: application/json
Content-Length: 68

{
"description": "expires has an invalid format",
"error": "BadRequest"
}


06. Create a subscription with an wrong date value in the 'years' component of the 'expires' field, see error
=============================================================================================================
HTTP/1.1 400 Bad Request
Date: REGEX(.*)
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Content-Type: application/json
Content-Length: 68

{
"description": "expires has an invalid format",
"error": "BadRequest"
}


07. Create a subscription with an valid date value in 'expires' field, (success case)
=====================================================================================
HTTP/1.1 201 Created
Date: REGEX(.*)
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Location: /v2/subscriptions/REGEX([0-9a-f]{24})
Content-Length: 0



--TEARDOWN--
brokerStop CB
dbDrop CB

0 comments on commit fbccd91

Please sign in to comment.