void testMethodCodes() { CoapPDU *pdu = NULL; uint8_t *buffer[4]; for(int i=0; i<4; i++) { switch(i) { case 0: pdu = new CoapPDU((uint8_t*)buffer,4,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_CHANGED); pdu->setVersion(1); pdu->setMessageID(rand()%0xFFFF); for(int codeIndex=0; codeIndex<COAP_NUM_MESSAGE_CODES; codeIndex++) { pdu->setCode(coapCodeVector[codeIndex]); CU_ASSERT_EQUAL_FATAL(pdu->getCode(),coapCodeVector[codeIndex]); } if(i%2) { DBG("%d DELETE",i); delete pdu; } } }
void testPayload() { // test for both buffer and dynamic CoapPDU *pdu = NULL; uint8_t *buffer[32]; for(int i=0; i<4; i++) { switch(i) { case 0: pdu = new CoapPDU((uint8_t*)buffer,32,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->printBin(); pdu->setCode(CoapPDU::COAP_GET); pdu->setVersion(1); pdu->printBin(); pdu->setMessageID(0x1234); pdu->setURI((char*)"test",4); pdu->printBin(); CU_ASSERT_FATAL(pdu->setPayload(NULL,4)==1); CU_ASSERT_FATAL(pdu->setPayload((uint8_t*)"test",0)==1); pdu->setPayload((uint8_t*)"\1\2\3",3); pdu->printBin(); CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),3); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUA,pdu->getPDULength()); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\1\2\3",pdu->getPayloadLength()); DBG("Trying to increase payload size"); pdu->setPayload((uint8_t*)"\4\3\2\1",4); pdu->printBin(); CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),4); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUB,pdu->getPDULength()); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\4\3\2\1",pdu->getPayloadLength()); DBG("Trying to reduce payload size"); pdu->setPayload((uint8_t*)"\1\2",2); pdu->printBin(); CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),2); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUC,pdu->getPDULength()); CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\1\2",pdu->getPayloadLength()); if(i%2) { DBG("%d DELETE",i); delete pdu; } } }
void testOptionInsertion(void) { CoapPDU *pdu = NULL; uint8_t *buffer[64]; for(int constructorType=0; constructorType<4; constructorType++) { DBG("New iteration: constructorType: %d",constructorType); switch(constructorType) { case 0: pdu = new CoapPDU((uint8_t*)buffer,64,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setVersion(1); pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_CHANGED); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestA,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(11,3,(uint8_t*)"\x55\x55\x55"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestB,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(11,3,(uint8_t*)"\xff\xff\xff"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestC,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(7,3,(uint8_t*)"\xf7\xf7\xf7"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestD,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(200,3,(uint8_t*)"\x01\x02\x03"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestE,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(190,3,(uint8_t*)"\x03\x02\x01"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestF,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(300,3,(uint8_t*)"\x01\x02\x03"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestG,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(195,3,(uint8_t*)"\x03\x02\x01"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestH,pdu->getPDUPointer(),pdu->getPDULength()); pdu->addOption(1950,3,(uint8_t*)"\x03\x02\x01"); CU_ASSERT_NSTRING_EQUAL_FATAL(optionInsertionTestI,pdu->getPDUPointer(),pdu->getPDULength()); if(constructorType%2) { DBG("%d DELETE",constructorType); delete pdu; } } }
void testTokenInsertion(void) { CoapPDU *pdu = NULL; uint8_t *buffer[64]; for(int constructorType=0; constructorType<4; constructorType++) { DBG("New iteration: constructorType: %d",constructorType); switch(constructorType) { case 0: pdu = new CoapPDU((uint8_t*)buffer,64,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_CHANGED); pdu->setVersion(2); pdu->setToken((uint8_t*)"\3\2\1\0",4); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionA,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\4\3\2\1\0",5); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionB,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\7\6\5\4\3\2\1",8); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionC,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\4\3\2\1\0",5); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionB,pdu->getPDUPointer(),pdu->getPDULength()); pdu->setToken((uint8_t*)"\3\2\1\0",4); CU_ASSERT_NSTRING_EQUAL_FATAL(tokenInsertionA,pdu->getPDUPointer(),pdu->getPDULength()); CU_ASSERT_FATAL(pdu->setToken(NULL,4)==1); CU_ASSERT_FATAL(pdu->setToken((uint8_t*)"a",0)==1); if(constructorType%2) { DBG("%d DELETE",constructorType); delete pdu; } } }
void testMessageID() { CoapPDU *pdu = NULL; uint8_t *buffer[4]; for(int i=0; i<4; i++) { switch(i) { case 0: pdu = new CoapPDU((uint8_t*)buffer,4,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } uint16_t messageID = 0, readID = 0; pdu->setMessageID(0x0000); CU_ASSERT_EQUAL_FATAL(pdu->getMessageID(),0x0000); pdu->setMessageID(0x0001); CU_ASSERT_EQUAL_FATAL(pdu->getMessageID(),0x0001); pdu->setMessageID(0xFFFF); CU_ASSERT_EQUAL_FATAL(pdu->getMessageID(),0xFFFF); for(int j=0; j<100; j++) { messageID = rand()%0xFFFF; pdu->setMessageID(messageID); readID = pdu->getMessageID(); CU_ASSERT_EQUAL_FATAL(messageID,readID); } if(i%2) { DBG("%d DELETE",i); delete pdu; } } }
void testHeaderFirstByteConstruction(void) { CoapPDU *pdu = NULL; uint8_t *buffer[64]; for(int constructorType=0; constructorType<4; constructorType++) { DBG("New iteration: constructorType: %d",constructorType); switch(constructorType) { case 0: pdu = new CoapPDU((uint8_t*)buffer,64,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } for(int pduVersion=0; pduVersion<4; pduVersion++) { for(int pduTypeIndex=0; pduTypeIndex<4; pduTypeIndex++) { for(int tokenLength=0; tokenLength<9; tokenLength++) { pdu->setVersion(pduVersion); pdu->setType(coapTypeVector[pduTypeIndex]); pdu->setTokenLength(tokenLength); CU_ASSERT_EQUAL_FATAL(pdu->getVersion(),pduVersion); CU_ASSERT_EQUAL_FATAL(pdu->getType(),coapTypeVector[pduTypeIndex]); CU_ASSERT_EQUAL_FATAL(pdu->getTokenLength(),tokenLength); } } } if(constructorType%2) { DBG("%d DELETE",constructorType); delete pdu; } } }
void testURISetting(void) { // locals int bufLen = 64, inLen = 0, outLen = 0, expectedLen = 0; char outBuf[64]; CoapPDU *pdu = NULL; char *inBuf = NULL, *expectedBuf = NULL; uint8_t *buffer[64]; // iterate over URIs for(int i=0; i<numURISetStrings; i++) { inBuf = (char*)uriInStrings[i]; inLen = strlen(inBuf); expectedBuf = (char*)uriOutStrings[i]; expectedLen = strlen(expectedBuf); // construct PDU //pdu = new CoapPDU(); for(int constructorType=0; constructorType<4; constructorType++) { DBG("New iteration: constructorType: %d",constructorType); switch(constructorType) { case 0: pdu = new CoapPDU((uint8_t*)buffer,64,0); break; case 1: pdu->reset(); break; case 2: pdu = new CoapPDU(); break; case 3: pdu->reset(); break; } pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_CHANGED); pdu->setVersion(1); pdu->setMessageID(rand()%0xFFFF); // set URI-PATH options in one operation from URI pdu->setURI(inBuf,inLen); //pdu->printHuman(); // check that read URI is the same pdu->getURI(outBuf,bufLen,&outLen); DBG("Got \"%s\" with length %d, supposed to get: \"%s\" with length %d",outBuf,outLen,expectedBuf,expectedLen); CU_ASSERT_EQUAL_FATAL(expectedLen,outLen); CU_ASSERT_NSTRING_EQUAL_FATAL(expectedBuf,outBuf,expectedLen); //delete pdu; if(constructorType%2) { DBG("%d DELETE",constructorType); delete pdu; } } } // test failure cases pdu = new CoapPDU(); pdu->setMessageID(rand()%0xFFFF); CU_ASSERT_FATAL(pdu->setURI(NULL,3)==1); CU_ASSERT_FATAL(pdu->setURI((char*)"hello",5)==0); CU_ASSERT_FATAL(pdu->getURI(NULL,3,NULL)==1); CU_ASSERT_FATAL(pdu->getURI(outBuf,20,NULL)==1); CU_ASSERT_FATAL(pdu->getURI(outBuf,0,&outLen)==1); CU_ASSERT_FATAL(pdu->getURI(outBuf,2,&outLen)==1); CU_ASSERT_FATAL(pdu->getURI(outBuf,3,&outLen)==1); CU_ASSERT_FATAL(pdu->getURI(outBuf,7,&outLen)==1); CU_ASSERT_FATAL(pdu->getURI(outBuf,8,&outLen)==0); CU_ASSERT_NSTRING_EQUAL_FATAL(outBuf,"/hello",5); delete pdu; // case where there is no URI pdu = new CoapPDU(); CU_ASSERT_FATAL(pdu->getURI(outBuf,8,&outLen)==0); CU_ASSERT_EQUAL_FATAL(outLen,0); delete pdu; };