Skip to content

Commit 063e4cc

Browse files
committed
Add tests for MD5Builder, reformat and clean up code
1 parent 3640757 commit 063e4cc

File tree

8 files changed

+484
-62
lines changed

8 files changed

+484
-62
lines changed

cores/esp8266/MD5Builder.cpp

+66-41
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,102 @@
1-
#include "Arduino.h"
2-
#include "md5.h"
3-
#include "MD5Builder.h"
1+
#include <Arduino.h>
2+
#include <MD5Builder.h>
43

5-
#define hex_char_to_byte(c) (((c)>='a'&&(c)<='f')?((c)-87):((c)>='A'&&(c)<='F')?((c)-55):((c)>='0'&&(c)<='9')?((c)-48):0)
4+
uint8_t hex_char_to_byte(uint8_t c)
5+
{
6+
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
7+
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
8+
(c >= '0'&& c<= '9') ? (c - (uint8_t)'0') : 0;
9+
}
610

7-
void MD5Builder::begin(void){
8-
memset(_buf, 0x00, 16);
9-
MD5Init(&_ctx);
11+
void MD5Builder::begin(void)
12+
{
13+
memset(_buf, 0x00, 16);
14+
MD5Init(&_ctx);
1015
}
1116

12-
void MD5Builder::add(uint8_t * data, uint16_t len){
13-
MD5Update(&_ctx, data, len);
17+
void MD5Builder::add(uint8_t * data, uint16_t len)
18+
{
19+
MD5Update(&_ctx, data, len);
1420
}
1521

16-
void MD5Builder::addHexString(const char * data){
17-
uint16_t i, len = strlen(data);
18-
uint8_t * tmp = (uint8_t*)malloc(len/2);
19-
if(tmp == NULL)
20-
return;
21-
for(i=0; i<len; i+=2) tmp[i/2] = (hex_char_to_byte(data[i]) & 0x0F) << 4 | (hex_char_to_byte(data[i+1]) & 0x0F);
22-
add(tmp, len/2);
23-
free(tmp);
22+
void MD5Builder::addHexString(const char * data)
23+
{
24+
uint16_t i, len = strlen(data);
25+
uint8_t * tmp = (uint8_t*)malloc(len/2);
26+
if(tmp == NULL) {
27+
return;
28+
}
29+
for(i=0; i<len; i+=2) {
30+
uint8_t high = hex_char_to_byte(data[i]);
31+
uint8_t low = hex_char_to_byte(data[i+1]);
32+
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
33+
}
34+
add(tmp, len/2);
35+
free(tmp);
2436
}
2537

26-
bool MD5Builder::addStream(Stream & stream, const size_t maxLen) {
27-
const int buf_size = 512;
28-
int maxLengthLeft = maxLen;
29-
uint8_t * buf = (uint8_t*) malloc(buf_size);
38+
bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
39+
{
40+
const int buf_size = 512;
41+
int maxLengthLeft = maxLen;
42+
uint8_t * buf = (uint8_t*) malloc(buf_size);
43+
44+
if(!buf) {
45+
return false;
46+
}
3047

31-
if(buf) {
3248
int bytesAvailable = stream.available();
3349
while((bytesAvailable > 0) && (maxLengthLeft > 0)) {
3450

3551
// determine number of bytes to read
3652
int readBytes = bytesAvailable;
37-
if(readBytes > maxLengthLeft) readBytes = maxLengthLeft ; // read only until max_len
38-
if(readBytes > buf_size) readBytes = buf_size; // not read more the buffer can handle
53+
if(readBytes > maxLengthLeft) {
54+
readBytes = maxLengthLeft ; // read only until max_len
55+
}
56+
if(readBytes > buf_size) {
57+
readBytes = buf_size; // not read more the buffer can handle
58+
}
3959

4060
// read data and check if we got something
4161
int numBytesRead = stream.readBytes(buf, readBytes);
42-
if(numBytesRead< 1) return false;
62+
if(numBytesRead< 1) {
63+
return false;
64+
}
4365

4466
// Update MD5 with buffer payload
4567
MD5Update(&_ctx, buf, numBytesRead);
4668

47-
delay(0); // time for network streams
69+
yield(); // time for network streams
4870

4971
// update available number of bytes
5072
maxLengthLeft -= numBytesRead;
5173
bytesAvailable = stream.available();
5274
}
75+
printf("ba: %d mll: %d\n", bytesAvailable, maxLengthLeft);
5376
free(buf);
5477
return true;
55-
} else {
56-
return false;
57-
}
5878
}
5979

60-
void MD5Builder::calculate(void){
61-
MD5Final(_buf, &_ctx);
80+
void MD5Builder::calculate(void)
81+
{
82+
MD5Final(_buf, &_ctx);
6283
}
6384

64-
void MD5Builder::getBytes(uint8_t * output){
65-
memcpy(output, _buf, 16);
85+
void MD5Builder::getBytes(uint8_t * output)
86+
{
87+
memcpy(output, _buf, 16);
6688
}
6789

68-
void MD5Builder::getChars(char * output){
69-
for(uint8_t i = 0; i < 16; i++)
70-
sprintf(output + (i * 2), "%02x", _buf[i]);
90+
void MD5Builder::getChars(char * output)
91+
{
92+
for(uint8_t i = 0; i < 16; i++) {
93+
sprintf(output + (i * 2), "%02x", _buf[i]);
94+
}
7195
}
7296

73-
String MD5Builder::toString(void){
74-
char out[33];
75-
getChars(out);
76-
return String(out);
77-
}
97+
String MD5Builder::toString(void)
98+
{
99+
char out[33];
100+
getChars(out);
101+
return String(out);
102+
}

cores/esp8266/MD5Builder.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#ifndef __ESP8266_MD5_BUILDER__
2222
#define __ESP8266_MD5_BUILDER__
2323

24-
#include "Arduino.h"
24+
#include <WString.h>
25+
#include <Stream.h>
2526
#include "md5.h"
2627

2728
class MD5Builder {

cores/esp8266/StreamString.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525

2626

2727
class StreamString: public Stream, public String {
28-
29-
size_t write(const uint8_t *buffer, size_t size);
30-
size_t write(uint8_t data);
31-
32-
int available();
33-
int read();
34-
int peek();
35-
void flush();
36-
28+
public:
29+
size_t write(const uint8_t *buffer, size_t size) override;
30+
size_t write(uint8_t data) override;
31+
32+
int available() override;
33+
int read() override;
34+
int peek() override;
35+
void flush() override;
3736
};
3837

3938

cores/esp8266/Updater.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef ESP8266UPDATER_H
22
#define ESP8266UPDATER_H
33

4-
#include "Arduino.h"
5-
#include "flash_utils.h"
6-
#include "MD5Builder.h"
4+
#include <Arduino.h>
5+
#include <flash_utils.h>
6+
#include <MD5Builder.h>
77

88
#define UPDATE_ERROR_OK (0)
99
#define UPDATE_ERROR_WRITE (1)

tests/host/Makefile

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
OBJECT_DIRECTORY := obj
21
BINARY_DIRECTORY := bin
32
OUTPUT_BINARY := $(BINARY_DIRECTORY)/host_tests
43
CORE_PATH := ../../cores/esp8266
@@ -18,6 +17,7 @@ CORE_CPP_FILES := $(addprefix $(CORE_PATH)/,\
1817
FS.cpp \
1918
spiffs_api.cpp \
2019
pgmspace.cpp \
20+
MD5Builder.cpp \
2121
)
2222

2323
CORE_C_FILES := $(addprefix $(CORE_PATH)/,\
@@ -35,6 +35,10 @@ MOCK_CPP_FILES := $(addprefix common/,\
3535
WMath.cpp \
3636
)
3737

38+
MOCK_C_FILES := $(addprefix common/,\
39+
md5.c \
40+
)
41+
3842
INC_PATHS += $(addprefix -I, \
3943
common \
4044
$(CORE_PATH) \
@@ -43,18 +47,23 @@ INC_PATHS += $(addprefix -I, \
4347
TEST_CPP_FILES := \
4448
fs/test_fs.cpp \
4549
core/test_pgmspace.cpp \
50+
core/test_md5builder.cpp \
51+
4652

47-
CXXFLAGS += -std=c++11 -Wall -coverage -O0
48-
CFLAGS += -std=c99 -Wall -coverage -O0
53+
CXXFLAGS += -std=c++11 -Wall -coverage -O0 -fno-common
54+
CFLAGS += -std=c99 -Wall -coverage -O0 -fno-common
4955
LDFLAGS += -coverage -O0
5056

5157
remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))
5258

53-
C_SOURCE_FILES = $(CORE_C_FILES)
59+
C_SOURCE_FILES = $(MOCK_C_FILES) $(CORE_C_FILES)
5460
CPP_SOURCE_FILES = $(MOCK_CPP_FILES) $(CORE_CPP_FILES) $(TEST_CPP_FILES)
5561
C_OBJECTS = $(C_SOURCE_FILES:.c=.c.o)
5662

57-
CPP_OBJECTS = $(CPP_SOURCE_FILES:.cpp=.cpp.o)
63+
CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp.o) $(CORE_CPP_FILES:.cpp=.cpp.o)
64+
CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp.o)
65+
66+
CPP_OBJECTS = $(CPP_OBJECTS_CORE) $(CPP_OBJECTS_TESTS)
5867

5968
OBJECTS = $(C_OBJECTS) $(CPP_OBJECTS)
6069
COVERAGE_FILES = $(OBJECTS:.o=.gc*)
@@ -95,5 +104,9 @@ $(C_OBJECTS): %.c.o: %.c
95104
$(CPP_OBJECTS): %.cpp.o: %.cpp
96105
$(CXX) $(CXXFLAGS) $(INC_PATHS) -c -o $@ $<
97106

98-
$(OUTPUT_BINARY): $(BINARY_DIRECTORY) $(OBJECTS)
99-
$(CXX) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY)
107+
$(BINARY_DIRECTORY)/core.a: $(C_OBJECTS) $(CPP_OBJECTS_CORE)
108+
ar -rcu $@ $(C_OBJECTS) $(CPP_OBJECTS_CORE)
109+
ranlib -c $@
110+
111+
$(OUTPUT_BINARY): $(BINARY_DIRECTORY) $(CPP_OBJECTS_TESTS) $(BINARY_DIRECTORY)/core.a
112+
$(CXX) $(LDFLAGS) $(CPP_OBJECTS_TESTS) $(BINARY_DIRECTORY)/core.a $(LIBS) -o $(OUTPUT_BINARY)

tests/host/common/Arduino.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ extern "C" void yield()
3434

3535
extern "C" void __panic_func(const char* file, int line, const char* func) {
3636
abort();
37-
}
37+
}
38+
39+
extern "C" void delay(unsigned long ms)
40+
{
41+
}

0 commit comments

Comments
 (0)