Skip to content

Commit

Permalink
added self-test ability to simulate POST packet and used it to do bas…
Browse files Browse the repository at this point in the history
…ic LFS tests
  • Loading branch information
openshwprojects committed Nov 20, 2022
1 parent 537dc2c commit b334024
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 18 deletions.
55 changes: 42 additions & 13 deletions src/selftest/selftest_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ const char *http_get_template1 = "GET /%s HTTP/1.1\r\n"
"\r\n"
"\r\n";

const char *http_post_template1 = "POST /%s HTTP/1.1\r\n"
"Host: 127.0.0.1\r\n"
"Connection: keep - alive\r\n"
"Content-Length: %i\r\n"
"sec-ch-ua: \"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\"\r\n"
"sec-ch-ua-mobile: ? 0\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36\r\n"
"sec-ch-ua-platform: \"Windows\"\r\n"
"Accept: */*\r\n"
"Sec-Fetch-Site: same-origin\r\n"
"Sec-Fetch-Mode: cors\r\n"
"Sec-Fetch-Dest: empty\r\n"
"Referer: http://127.0.0.1/app\r\n"
"Accept-Encoding: gzip, deflate, br\r\n"
"Accept-Language: pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6\r\n"
"\r\n"
"%s";

//jsmn_parser parser;
cJSON *g_json;
cJSON *g_sec_power;
Expand All @@ -43,13 +61,12 @@ static char buffer[8192];
static const char *replyAt;
//static jsmntok_t tokens[256]; /* We expect no more than qq JSON tokens */

void Test_FakeHTTPClientPacket(const char *tg) {
void Test_FakeHTTPClientPacket_Generic() {
int iResult;
int len;

http_request_t request;

sprintf(buffer, http_get_template1, tg);

iResult = strlen(buffer);

Expand All @@ -65,17 +82,27 @@ void Test_FakeHTTPClientPacket(const char *tg) {

request.replymaxlen = sizeof(outbuf);

printf("Test_FakeHTTPClientPacket fake bytes sent: %d \n", iResult);
printf("Test_FakeHTTPClientPacket_GET fake bytes sent: %d \n", iResult);
len = HTTP_ProcessPacket(&request);
outbuf[request.replylen] = 0;
printf("Test_FakeHTTPClientPacket fake bytes received: %d \n", len);
printf("Test_FakeHTTPClientPacket_GET fake bytes received: %d \n", len);

replyAt = Helper_GetPastHTTPHeader(outbuf);

}
void Test_FakeHTTPClientPacket_GET(const char *tg) {
sprintf(buffer, http_get_template1, tg);
Test_FakeHTTPClientPacket_Generic();
}
void Test_FakeHTTPClientPacket_POST(const char *tg, const char *data) {
int dataLen = strlen(data);

sprintf(buffer, http_post_template1, tg, dataLen, data);
Test_FakeHTTPClientPacket_Generic();
}
void Test_FakeHTTPClientPacket_JSON(const char *tg) {
int r;
Test_FakeHTTPClientPacket(tg);
Test_FakeHTTPClientPacket_GET(tg);

//jsmn_init(&parser);
//r = jsmn_parse(&parser, replyAt, strlen(replyAt), tokens, 256);
Expand Down Expand Up @@ -120,7 +147,9 @@ const char *Test_GetJSONValue_String(const char *keyword, const char *obj) {
printf("Test_GetJSONValue_String will return %s for %s\n", tmp->valuestring, keyword);
return tmp->valuestring;
}

const char *Test_GetLastHTMLReply() {
return replyAt;
}
void Test_Http_SingleRelayOnChannel1() {

CMD_ExecuteCommand("clearAll", 0);
Expand All @@ -130,7 +159,7 @@ void Test_Http_SingleRelayOnChannel1() {
Test_FakeHTTPClientPacket_JSON("cm?cmnd=POWER");
SELFTEST_ASSERT_JSON_VALUE_STRING(0, "POWER", "OFF");

Test_FakeHTTPClientPacket("index?tgl=1");
Test_FakeHTTPClientPacket_GET("index?tgl=1");

// read power
Test_FakeHTTPClientPacket_JSON("cm?cmnd=POWER");
Expand Down Expand Up @@ -176,23 +205,23 @@ void Test_Http_TwoRelays() {
Test_FakeHTTPClientPacket_JSON("cm?cmnd=STATUS");
SELFTEST_ASSERT_JSON_VALUE_INTEGER("Status", "Power", 0);

Test_FakeHTTPClientPacket("index?tgl=1");
Test_FakeHTTPClientPacket_GET("index?tgl=1");

// Channel 1 On
// Channel 2 Off
// In STATUS register, power is encoded as integer...
Test_FakeHTTPClientPacket_JSON("cm?cmnd=STATUS");
SELFTEST_ASSERT_JSON_VALUE_INTEGER("Status", "Power", 0b1);

Test_FakeHTTPClientPacket("index?tgl=2");
Test_FakeHTTPClientPacket_GET("index?tgl=2");
// Channel 1 On
// Channel 2 On
// In STATUS register, power is encoded as integer...
Test_FakeHTTPClientPacket_JSON("cm?cmnd=STATUS");
SELFTEST_ASSERT_JSON_VALUE_INTEGER("Status", "Power", 0b11);


Test_FakeHTTPClientPacket("index?tgl=1");
Test_FakeHTTPClientPacket_GET("index?tgl=1");
// Channel 1 Off
// Channel 2 On
// In STATUS register, power is encoded as integer...
Expand Down Expand Up @@ -222,7 +251,7 @@ void Test_Http_FourRelays() {
Test_FakeHTTPClientPacket_JSON("cm?cmnd=STATUS");
SELFTEST_ASSERT_JSON_VALUE_INTEGER("Status", "Power", 0);

Test_FakeHTTPClientPacket("index?tgl=3");
Test_FakeHTTPClientPacket_GET("index?tgl=3");

// Channel 1 Off
// Channel 2 Off
Expand All @@ -232,7 +261,7 @@ void Test_Http_FourRelays() {
Test_FakeHTTPClientPacket_JSON("cm?cmnd=STATUS");
SELFTEST_ASSERT_JSON_VALUE_INTEGER("Status", "Power", 0b0100);

Test_FakeHTTPClientPacket("index?tgl=2");
Test_FakeHTTPClientPacket_GET("index?tgl=2");
// Channel 1 Off
// Channel 2 On
// Channel 3 On
Expand All @@ -242,7 +271,7 @@ void Test_Http_FourRelays() {
SELFTEST_ASSERT_JSON_VALUE_INTEGER("Status", "Power", 0b0110);


Test_FakeHTTPClientPacket("index?tgl=4");
Test_FakeHTTPClientPacket_GET("index?tgl=4");
// Channel 1 Off
// Channel 2 On
// Channel 3 On
Expand Down
4 changes: 4 additions & 0 deletions src/selftest/selftest_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ void Test_Command_If() {
SELFTEST_ASSERT_CHANNEL(1, 1521);
CMD_ExecuteCommand("if 1 then \"setChannel 1 1500+67\"", 0);
SELFTEST_ASSERT_CHANNEL(1, 1567);
CMD_ExecuteCommand("if !1 then \"setChannel 1 3333+1111\"", 0);
SELFTEST_ASSERT_CHANNEL(1, 1567);
CMD_ExecuteCommand("if !0 then \"setChannel 1 3333+1111\"", 0);
SELFTEST_ASSERT_CHANNEL(1, 4444);

CMD_ExecuteCommand("setChannel 10 0", 0);
CMD_ExecuteCommand("setChannel 11 1", 0);
Expand Down
8 changes: 4 additions & 4 deletions src/selftest/selftest_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ void Test_LEDDriver_CW() {
SELFTEST_ASSERT_CHANNEL(2, 0);

// Tasmota style command should disable LED
Test_FakeHTTPClientPacket("cm?cmnd=POWER%200");
Test_FakeHTTPClientPacket_GET("cm?cmnd=POWER%200");
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(2, 0);
// Tasmota style command should enable LED
Test_FakeHTTPClientPacket("cm?cmnd=POWER%201");
Test_FakeHTTPClientPacket_GET("cm?cmnd=POWER%201");
SELFTEST_ASSERT_CHANNEL(1, 100);
SELFTEST_ASSERT_CHANNEL(2, 0);

Expand Down Expand Up @@ -126,12 +126,12 @@ void Test_LEDDriver_RGB() {
SELFTEST_ASSERT_CHANNEL(3, 90);

// Tasmota style command should disable LED
Test_FakeHTTPClientPacket("cm?cmnd=POWER%200");
Test_FakeHTTPClientPacket_GET("cm?cmnd=POWER%200");
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(2, 0);
SELFTEST_ASSERT_CHANNEL(3, 0);
// Tasmota style command should enable LED
Test_FakeHTTPClientPacket("cm?cmnd=POWER%201");
Test_FakeHTTPClientPacket_GET("cm?cmnd=POWER%201");
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(2, 0);
SELFTEST_ASSERT_CHANNEL(3, 90);
Expand Down
48 changes: 48 additions & 0 deletions src/selftest/selftest_lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,58 @@
#include "selftest_local.h".

void Test_LFS() {
char buffer[64];

// reset whole device
CMD_ExecuteCommand("clearAll", 0);
CMD_ExecuteCommand("lfsformat", 0);

// send file content as POST to REST interface
Test_FakeHTTPClientPacket_POST("api/lfs/unitTestFile.txt", "This is a sample file made by unit test.");
// get this file
Test_FakeHTTPClientPacket_GET("api/lfs/unitTestFile.txt");
SELFTEST_ASSERT_HTML_REPLY("This is a sample file made by unit test.");

// repeat this test with a random integer to make sure we're not somehow getting biased results from previous session
sprintf(buffer, "Here are some random numbers %i %i %i", (rand() % 10), (rand() % 100), (rand() % 200));
// send file content as POST to REST interface
Test_FakeHTTPClientPacket_POST("api/lfs/unitTestFile.txt", buffer);
// get this file
Test_FakeHTTPClientPacket_GET("api/lfs/unitTestFile.txt");
SELFTEST_ASSERT_HTML_REPLY(buffer);

// check to see if we can execute a file from within LFS
Test_FakeHTTPClientPacket_POST("api/lfs/script1.txt", "setChannel 0 123");
// exec will execute a script file in-place, all commands at once
CMD_ExecuteCommand("exec script1.txt", 0);
// is channel changed?
SELFTEST_ASSERT_CHANNEL(0, 123);


// check to see if we can execute a file from within LFS
Test_FakeHTTPClientPacket_POST("api/lfs/script2.txt", "addChannel 0 102");
// exec will execute a script file in-place, all commands at once
CMD_ExecuteCommand("exec script2.txt", 0);
// is channel changed?
SELFTEST_ASSERT_CHANNEL(0, 225);

SELFTEST_ASSERT_CHANNEL(10, 0);
// check to see if we can execute a file from within LFS
Test_FakeHTTPClientPacket_POST("api/lfs/script3.txt", "addChannel 10 2");
// exec will execute a script file in-place, all commands at once
CMD_ExecuteCommand("exec script3.txt", 0);
SELFTEST_ASSERT_CHANNEL(10, 2);
// see if we can stack several execs within backlog
CMD_ExecuteCommand("backlog exec script3.txt; exec script3.txt; exec script3.txt; exec script3.txt; exec script3.txt", 0);
SELFTEST_ASSERT_CHANNEL(10, 12);
// see if we can alias it. It will add in total 10 value to ch 10
CMD_ExecuteCommand("alias myexecs backlog exec script3.txt; exec script3.txt; exec script3.txt; exec script3.txt; exec script3.txt", 0);
SELFTEST_ASSERT_CHANNEL(10, 12);
// run command by alias
CMD_ExecuteCommand("myexecs", 0);
SELFTEST_ASSERT_CHANNEL(10, 22);

//system("pause");
}

#endif
5 changes: 4 additions & 1 deletion src/selftest/selftest_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void SelfTest_Failed(const char *file, const char *function, int line, const cha
#define SELFTEST_ASSERT_JSON_VALUE_STRING(obj, varName, res) SELFTEST_ASSERT(!strcmp(Test_GetJSONValue_String(varName,obj), res));
#define SELFTEST_ASSERT_JSON_VALUE_INTEGER(obj, varName, res) SELFTEST_ASSERT((Test_GetJSONValue_Integer(varName,obj) == res));
#define SELFTEST_ASSERT_STRING(current,expected) SELFTEST_ASSERT((strcmp(expected,current) == 0));
#define SELFTEST_ASSERT_HTML_REPLY(expected) SELFTEST_ASSERT((strcmp(Test_GetLastHTMLReply(),expected) == 0));

//#define FLOAT_EQUALS (a,b) (abs(a-b)<0.001f)
inline bool Float_Equals(float a, float b) {
Expand All @@ -38,6 +39,8 @@ void Test_Tokenizer();
void Test_Commands_Alias();
void Test_ExpandConstant();

void Test_FakeHTTPClientPacket(const char *tg);
void Test_FakeHTTPClientPacket_GET(const char *tg);
void Test_FakeHTTPClientPacket_POST(const char *tg, const char *data);
const char *Test_GetLastHTMLReply();

#endif
1 change: 1 addition & 0 deletions src/win_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void Win_DoUnitTests() {
Test_Commands_Alias();
Test_Expressions_RunTests_Basic();
Test_LEDDriver();
Test_LFS();
Test_Commands_Channels();
Test_Command_If();
Test_Command_If_Else();
Expand Down

0 comments on commit b334024

Please sign in to comment.