Remove sscanf() from ProtocolEverflourish and add a unittest for ProtocolEverflourish::decodeData()
This commit is contained in:
parent
b50e9dd3d5
commit
ddfb87f45d
3 changed files with 59 additions and 4 deletions
|
@ -95,14 +95,12 @@ unsigned int ProtocolEverflourish::calculateChecksum(unsigned int x) {
|
|||
}
|
||||
|
||||
std::string ProtocolEverflourish::decodeData(const ControllerMessage &dataMsg) {
|
||||
std::string data = dataMsg.getParameter("data");
|
||||
unsigned int allData;
|
||||
uint64_t allData;
|
||||
unsigned int house = 0;
|
||||
unsigned int unit = 0;
|
||||
unsigned int method = 0;
|
||||
|
||||
// TODO(micke): Convert sscanf. This is slow and can overflow buffers!
|
||||
sscanf(data.c_str(), "%X", &allData); // NOLINT(runtime/printf)
|
||||
allData = dataMsg.getInt64Parameter("data");
|
||||
|
||||
house = allData & 0xFFFC00;
|
||||
house >>= 10;
|
||||
|
|
32
telldus-core/tests/service/ProtocolEverflourishTest.cpp
Normal file
32
telldus-core/tests/service/ProtocolEverflourishTest.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include "ProtocolEverflourishTest.h"
|
||||
#include "service/ProtocolEverflourish.h"
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION (ProtocolEverflourishTest);
|
||||
|
||||
class ProtocolEverflourishTest::PrivateData {
|
||||
public:
|
||||
ProtocolEverflourish *protocol;
|
||||
};
|
||||
|
||||
void ProtocolEverflourishTest :: setUp (void) {
|
||||
d = new PrivateData;
|
||||
d->protocol = new ProtocolEverflourish();
|
||||
}
|
||||
|
||||
void ProtocolEverflourishTest :: tearDown (void) {
|
||||
delete d->protocol;
|
||||
delete d;
|
||||
}
|
||||
|
||||
void ProtocolEverflourishTest :: decodeDataTest (void) {
|
||||
CPPUNIT_ASSERT_EQUAL_MESSAGE(
|
||||
"Everflourish 4242:3 ON",
|
||||
std::string("class:command;protocol:everflourish;model:selflearning;house:4242;unit:3;method:turnon;"),
|
||||
d->protocol->decodeData(ControllerMessage("protocol:everflourish;data:0x424A6F;"))
|
||||
);
|
||||
CPPUNIT_ASSERT_EQUAL_MESSAGE(
|
||||
"Everflourish 5353:4 OFF",
|
||||
std::string("class:command;protocol:everflourish;model:selflearning;house:5353;unit:4;method:turnoff;"),
|
||||
d->protocol->decodeData(ControllerMessage("protocol:everflourish;data:0x53A7E0;"))
|
||||
);
|
||||
}
|
25
telldus-core/tests/service/ProtocolEverflourishTest.h
Normal file
25
telldus-core/tests/service/ProtocolEverflourishTest.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef PROTOCOLEVERFLOURISHTEST_H
|
||||
#define PROTOCOLEVERFLOURISHTEST_H
|
||||
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
class ProtocolEverflourishTest : public CPPUNIT_NS :: TestFixture
|
||||
{
|
||||
CPPUNIT_TEST_SUITE (ProtocolEverflourishTest);
|
||||
CPPUNIT_TEST (decodeDataTest);
|
||||
CPPUNIT_TEST_SUITE_END ();
|
||||
|
||||
public:
|
||||
void setUp (void);
|
||||
void tearDown (void);
|
||||
|
||||
protected:
|
||||
void decodeDataTest(void);
|
||||
|
||||
private:
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
};
|
||||
|
||||
#endif // PROTOCOLEVERFLOURISHTEST_H
|
Loading…
Add table
Add a link
Reference in a new issue