def test_expireCNTAndCIN(self) -> None: """ Create and expire <CNT> and <CIN> """ self.assertTrue(isTestExpirations()) self.assertIsNotNone(TestExpiration.ae) dct = { 'm2m:cnt': { 'et': getResourceDate( expirationCheckDelay), # 2 seconds in the future 'rn': cntRN } } r, rsc = CREATE(aeURL, TestExpiration.originator, T.CNT, dct) self.assertEqual(rsc, RC.created) dct = {'m2m:cin': {'cnf': 'text/plain:0', 'con': 'AnyValue'}} for _ in range(0, 5): r, rsc = CREATE(cntURL, TestExpiration.originator, T.CIN, dct) self.assertEqual(rsc, RC.created) cinRn = findXPath(r, 'm2m:cin/rn') self.assertIsNotNone(cinRn) r, rsc = RETRIEVE(f'{cntURL}/{cinRn}', TestExpiration.originator) self.assertEqual(rsc, RC.OK) time.sleep( expirationSleep) # give the server a moment to expire the resource r, rsc = RETRIEVE(cntURL, TestExpiration.originator) # retrieve CNT again self.assertEqual(rsc, RC.notFound) r, rsc = RETRIEVE(f'{cntURL}/{cinRn}', TestExpiration.originator) # retrieve CIN again self.assertEqual(rsc, RC.notFound)
def test_expireCNTViaMIALarge(self) -> None: """ Expire <CNT> via too large MIA """ self.assertTrue(isTestExpirations()) self.assertIsNotNone(TestExpiration.ae) dct = {'m2m:cnt': {'rn': cntRN, 'mia': tooLargeExpirationDelta()}} r, rsc = CREATE(aeURL, TestExpiration.originator, T.CNT, dct) self.assertEqual(rsc, RC.created) self.assertEqual(findXPath(r, 'm2m:cnt/mia'), tooLargeExpirationDelta()) dct = {'m2m:cin': {'cnf': 'text/plain:0', 'con': 'AnyValue'}} tooLargeET = getResourceDate(tooLargeExpirationDelta()) for _ in range(0, 5): r, rsc = CREATE(cntURL, TestExpiration.originator, T.CIN, dct) self.assertEqual(rsc, RC.created) self.assertLess(findXPath(r, 'm2m:cin/et'), tooLargeET) time.sleep( expirationSleep ) # give the server a moment to expire the CIN's (which should not happen this time) r, rsc = RETRIEVE(cntURL, TestExpiration.originator) self.assertEqual(rsc, RC.OK) self.assertEqual(findXPath(r, 'm2m:cnt/cni'), 5) # Still all children r, rsc = DELETE(cntURL, TestExpiration.originator) self.assertEqual(rsc, RC.deleted)
def test_checkHTTPRETWrong(self) -> None: """ Check Request Expiration Timeout in request (past date) -> Fail""" _, rsc = RETRIEVE(cseURL, ORIGINATOR, headers={C.hfRET: getResourceDate(-10) }) # request expiration in 10 seconds self.assertEqual(rsc, RC.requestTimeout)
def _constructResponse(frm: str, to: str, jsn: dict) -> dict: responseData = { 'fr': frm, 'to': to, 'rsc': 2000 if not failVerification else 4101, 'ot': getResourceDate() } if (rqi := jsn.get('rqi')): responseData['rqi'] = rqi
def test_createTSIsForTSwithSize(self) -> None: """ CREATE multiple <TSI>s to <TS> with size restrictions """ # First fill up the container for _ in range(int(maxBS / 3)): dct = { 'm2m:tsi': { 'dgt': getResourceDate(), 'con': 'x' * int(maxBS / 3) } } r, rsc = CREATE(tsURL, TestTS_TSI.originator, T.TSI, dct) self.assertEqual(rsc, RC.created, r) # Test latest TSI for x r, rsc = RETRIEVE(f'{tsURL}/la', TestTS_TSI.originator) self.assertEqual(rsc, RC.OK, r) self.assertIsNotNone(findXPath(r, 'm2m:tsi/con'), r) self.assertTrue(findXPath(r, 'm2m:tsi/con').startswith('x'), r) self.assertEqual(len(findXPath(r, 'm2m:tsi/con')), int(maxBS / 3), r) # Add another TSI dct = { 'm2m:tsi': { 'dgt': getResourceDate(), 'con': 'y' * int(maxBS / 3) } } r, rsc = CREATE(tsURL, TestTS_TSI.originator, T.TSI, dct) self.assertEqual(rsc, RC.created, r) # Test latest TSI for y r, rsc = RETRIEVE(f'{tsURL}/la', TestTS_TSI.originator) self.assertEqual(rsc, RC.OK, r) self.assertIsNotNone(findXPath(r, 'm2m:tsi/con'), r) self.assertTrue(findXPath(r, 'm2m:tsi/con').startswith('y'), r) self.assertEqual(len(findXPath(r, 'm2m:tsi/con')), int(maxBS / 3), r) # Test TS r, rsc = RETRIEVE(tsURL, TestTS_TSI.originator) self.assertEqual(rsc, RC.OK, r) self.assertIsNotNone(findXPath(r, 'm2m:ts/cni'), r) self.assertEqual(findXPath(r, 'm2m:ts/cni'), 3, r) self.assertIsNotNone(findXPath(r, 'm2m:ts/cbs'), r) self.assertEqual(findXPath(r, 'm2m:ts/cbs'), maxBS, r)
def test_createCNTExpirationInThePast(self) -> None: """ Create <CNT> and ET in the past -> Fail """ self.assertIsNotNone(TestExpiration.ae) dct = { 'm2m:cnt': { 'rn': cntRN, 'et': getResourceDate(-60) # 1 minute in the past } } r, rsc = CREATE(aeURL, TestExpiration.originator, T.CNT, dct) self.assertEqual(rsc, RC.badRequest, r)
def test_updateCNTWithEtNull(self) -> None: """ Update <CNT> and remove ET in another update """ self.assertIsNotNone(TestExpiration.ae) dct = { 'm2m:cnt': { 'rn': cntRN, 'et': getResourceDate(60) # 1 minute in the future } } r, rsc = CREATE(aeURL, TestExpiration.originator, T.CNT, dct) self.assertEqual(rsc, RC.created) self.assertIsNotNone((origEt := findXPath(r, 'm2m:cnt/et')))
def test_expireCNT(self) -> None: """ Create and expire <CNT> """ self.assertTrue(isTestExpirations()) self.assertIsNotNone(TestExpiration.ae) dct = { 'm2m:cnt': { 'rn': cntRN, 'et': getResourceDate( expirationCheckDelay) # 2 seconds in the future } } _, rsc = CREATE(aeURL, TestExpiration.originator, T.CNT, dct) self.assertEqual(rsc, RC.created) time.sleep( expirationSleep) # give the server a moment to expire the resource _, rsc = RETRIEVE(cntURL, TestExpiration.originator) self.assertEqual(rsc, RC.notFound)
def test_createTSItooLarge(self) -> None: """ CREATE <TSI> to <TS> with size > mbs -> Fail """ dct = {'m2m:tsi': {'dgt': getResourceDate(), 'con': 'x' * (maxBS + 1)}} r, rsc = CREATE(tsURL, TestTS_TSI.originator, T.TSI, dct) self.assertEqual(rsc, RC.notAcceptable, r)
def test_createTSIexactSize(self) -> None: """ CREATE <TSI> to <TS> with exact max size""" dct = {'m2m:tsi': {'dgt': getResourceDate(), 'con': 'x' * maxBS}} r, rsc = CREATE(tsURL, TestTS_TSI.originator, T.TSI, dct) self.assertEqual(rsc, RC.created, r) self.assertEqual(findXPath(r, 'm2m:tsi/cs'), maxBS, r)
def test_checkHTTPmissingOriginator(self) -> None: """ Check missing originator in request""" _, rsc = RETRIEVE(cseURL, None, headers={C.hfRET: getResourceDate(10) }) # request expiration in 10 seconds self.assertEqual(rsc, RC.badRequest)
def setUpClass(cls) -> None: cls.crTimestamp1 = getResourceDate(-timeDelta) # first timestamp dct = { 'm2m:ae': { 'rn': aeRN, 'api': 'NMyApp1Id', 'rr': True, 'srv': ['3'] } } cls.ae, rsc = CREATE(cseURL, 'C', T.AE, dct) # AE to work under assert rsc == RC.created, 'cannot create parent AE' cls.originator = findXPath(cls.ae, 'm2m:ae/aei') # create first container & CIN dct = {'m2m:cnt': {'rn': cntRN, 'lbl': ['cntLbl']}} cls.cnt, rsc = CREATE(aeURL, cls.originator, T.CNT, dct) assert rsc == RC.created, 'cannot create container' # create 5 contentInstances with different labels for i in range(0, 5): dct = { 'm2m:cin': { 'cnf': 'text/plain:0', 'con': 'aValue', 'lbl': ['tag:%d' % i] } } _, rsc = CREATE(cntURL, TestDiscovery.originator, T.CIN, dct) # create second container & CIN dct = { 'm2m:cnt': { 'rn': cnt2RN, } } cls.cnt2, rsc = CREATE(aeURL, cls.originator, T.CNT, dct) assert rsc == RC.created, 'cannot create container' # create 5 contentInstances with different labels for i in range(0, 5): dct = { 'm2m:cin': { 'cnf': 'text/x-plain:0', 'con': 'bValue', 'lbl': ['tag:%d' % i] } } _, rsc = CREATE(f'{cntURL}2', TestDiscovery.originator, T.CIN, dct) # create Node & MgmtObjs dct = {'m2m:nod': {'rn': nodRN, 'ni': nodeID}} _, rsc = CREATE(cseURL, ORIGINATOR, T.NOD, dct) assert rsc == RC.created, 'cannot create Node' dct = { 'm2m:mem': { 'mgd': T.MEM, 'rn': memRN, 'dc': 'aMem', 'mma': 1234, 'mmt': 4321 } } _, rsc = CREATE(nodURL, ORIGINATOR, T.MGMTOBJ, dct) assert rsc == RC.created, 'cannot create m2m:mem' dct = { 'm2m:bat': { 'mgd': T.BAT, 'rn': batRN, 'dc': 'aBat', 'btl': 23, 'bts': 5 } } _, rsc = CREATE(nodURL, ORIGINATOR, T.MGMTOBJ, dct) assert rsc == RC.created, 'cannot create m2m:bat' dct = { 'm2m:bat': { 'mgd': T.BAT, 'rn': bat2RN, 'dc': 'aBat', 'btl': 23, 'bts': 5 } } _, rsc = CREATE(nodURL, ORIGINATOR, T.MGMTOBJ, dct) assert rsc == RC.created, 'cannot create m2m:bat (2)' cls.crTimestamp2 = getResourceDate() # Second timestamp