Skip to content

Commit

Permalink
Add tests for MD5Builder, reformat and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Jun 13, 2016
1 parent 3640757 commit 063e4cc
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 62 deletions.
107 changes: 66 additions & 41 deletions cores/esp8266/MD5Builder.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,102 @@
#include "Arduino.h"
#include "md5.h"
#include "MD5Builder.h"
#include <Arduino.h>
#include <MD5Builder.h>

#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)
uint8_t hex_char_to_byte(uint8_t c)
{
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
(c >= '0'&& c<= '9') ? (c - (uint8_t)'0') : 0;
}

void MD5Builder::begin(void){
memset(_buf, 0x00, 16);
MD5Init(&_ctx);
void MD5Builder::begin(void)
{
memset(_buf, 0x00, 16);
MD5Init(&_ctx);
}

void MD5Builder::add(uint8_t * data, uint16_t len){
MD5Update(&_ctx, data, len);
void MD5Builder::add(uint8_t * data, uint16_t len)
{
MD5Update(&_ctx, data, len);
}

void MD5Builder::addHexString(const char * data){
uint16_t i, len = strlen(data);
uint8_t * tmp = (uint8_t*)malloc(len/2);
if(tmp == NULL)
return;
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);
add(tmp, len/2);
free(tmp);
void MD5Builder::addHexString(const char * data)
{
uint16_t i, len = strlen(data);
uint8_t * tmp = (uint8_t*)malloc(len/2);
if(tmp == NULL) {
return;
}
for(i=0; i<len; i+=2) {
uint8_t high = hex_char_to_byte(data[i]);
uint8_t low = hex_char_to_byte(data[i+1]);
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
}
add(tmp, len/2);
free(tmp);
}

bool MD5Builder::addStream(Stream & stream, const size_t maxLen) {
const int buf_size = 512;
int maxLengthLeft = maxLen;
uint8_t * buf = (uint8_t*) malloc(buf_size);
bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
{
const int buf_size = 512;
int maxLengthLeft = maxLen;
uint8_t * buf = (uint8_t*) malloc(buf_size);

if(!buf) {
return false;
}

if(buf) {
int bytesAvailable = stream.available();
while((bytesAvailable > 0) && (maxLengthLeft > 0)) {

// determine number of bytes to read
int readBytes = bytesAvailable;
if(readBytes > maxLengthLeft) readBytes = maxLengthLeft ; // read only until max_len
if(readBytes > buf_size) readBytes = buf_size; // not read more the buffer can handle
if(readBytes > maxLengthLeft) {
readBytes = maxLengthLeft ; // read only until max_len
}
if(readBytes > buf_size) {
readBytes = buf_size; // not read more the buffer can handle
}

// read data and check if we got something
int numBytesRead = stream.readBytes(buf, readBytes);
if(numBytesRead< 1) return false;
if(numBytesRead< 1) {
return false;
}

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

delay(0); // time for network streams
yield(); // time for network streams

// update available number of bytes
maxLengthLeft -= numBytesRead;
bytesAvailable = stream.available();
}
printf("ba: %d mll: %d\n", bytesAvailable, maxLengthLeft);
free(buf);
return true;
} else {
return false;
}
}

void MD5Builder::calculate(void){
MD5Final(_buf, &_ctx);
void MD5Builder::calculate(void)
{
MD5Final(_buf, &_ctx);
}

void MD5Builder::getBytes(uint8_t * output){
memcpy(output, _buf, 16);
void MD5Builder::getBytes(uint8_t * output)
{
memcpy(output, _buf, 16);
}

void MD5Builder::getChars(char * output){
for(uint8_t i = 0; i < 16; i++)
sprintf(output + (i * 2), "%02x", _buf[i]);
void MD5Builder::getChars(char * output)
{
for(uint8_t i = 0; i < 16; i++) {
sprintf(output + (i * 2), "%02x", _buf[i]);
}
}

String MD5Builder::toString(void){
char out[33];
getChars(out);
return String(out);
}
String MD5Builder::toString(void)
{
char out[33];
getChars(out);
return String(out);
}
3 changes: 2 additions & 1 deletion cores/esp8266/MD5Builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#ifndef __ESP8266_MD5_BUILDER__
#define __ESP8266_MD5_BUILDER__

#include "Arduino.h"
#include <WString.h>
#include <Stream.h>
#include "md5.h"

class MD5Builder {
Expand Down
17 changes: 8 additions & 9 deletions cores/esp8266/StreamString.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@


class StreamString: public Stream, public String {

size_t write(const uint8_t *buffer, size_t size);
size_t write(uint8_t data);

int available();
int read();
int peek();
void flush();

public:
size_t write(const uint8_t *buffer, size_t size) override;
size_t write(uint8_t data) override;

int available() override;
int read() override;
int peek() override;
void flush() override;
};


Expand Down
6 changes: 3 additions & 3 deletions cores/esp8266/Updater.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef ESP8266UPDATER_H
#define ESP8266UPDATER_H

#include "Arduino.h"
#include "flash_utils.h"
#include "MD5Builder.h"
#include <Arduino.h>
#include <flash_utils.h>
#include <MD5Builder.h>

#define UPDATE_ERROR_OK (0)
#define UPDATE_ERROR_WRITE (1)
Expand Down
27 changes: 20 additions & 7 deletions tests/host/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
OBJECT_DIRECTORY := obj
BINARY_DIRECTORY := bin
OUTPUT_BINARY := $(BINARY_DIRECTORY)/host_tests
CORE_PATH := ../../cores/esp8266
Expand All @@ -18,6 +17,7 @@ CORE_CPP_FILES := $(addprefix $(CORE_PATH)/,\
FS.cpp \
spiffs_api.cpp \
pgmspace.cpp \
MD5Builder.cpp \
)

CORE_C_FILES := $(addprefix $(CORE_PATH)/,\
Expand All @@ -35,6 +35,10 @@ MOCK_CPP_FILES := $(addprefix common/,\
WMath.cpp \
)

MOCK_C_FILES := $(addprefix common/,\
md5.c \
)

INC_PATHS += $(addprefix -I, \
common \
$(CORE_PATH) \
Expand All @@ -43,18 +47,23 @@ INC_PATHS += $(addprefix -I, \
TEST_CPP_FILES := \
fs/test_fs.cpp \
core/test_pgmspace.cpp \
core/test_md5builder.cpp \


CXXFLAGS += -std=c++11 -Wall -coverage -O0
CFLAGS += -std=c99 -Wall -coverage -O0
CXXFLAGS += -std=c++11 -Wall -coverage -O0 -fno-common
CFLAGS += -std=c99 -Wall -coverage -O0 -fno-common
LDFLAGS += -coverage -O0

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

C_SOURCE_FILES = $(CORE_C_FILES)
C_SOURCE_FILES = $(MOCK_C_FILES) $(CORE_C_FILES)
CPP_SOURCE_FILES = $(MOCK_CPP_FILES) $(CORE_CPP_FILES) $(TEST_CPP_FILES)
C_OBJECTS = $(C_SOURCE_FILES:.c=.c.o)

CPP_OBJECTS = $(CPP_SOURCE_FILES:.cpp=.cpp.o)
CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp.o) $(CORE_CPP_FILES:.cpp=.cpp.o)
CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp.o)

CPP_OBJECTS = $(CPP_OBJECTS_CORE) $(CPP_OBJECTS_TESTS)

OBJECTS = $(C_OBJECTS) $(CPP_OBJECTS)
COVERAGE_FILES = $(OBJECTS:.o=.gc*)
Expand Down Expand Up @@ -95,5 +104,9 @@ $(C_OBJECTS): %.c.o: %.c
$(CPP_OBJECTS): %.cpp.o: %.cpp
$(CXX) $(CXXFLAGS) $(INC_PATHS) -c -o $@ $<

$(OUTPUT_BINARY): $(BINARY_DIRECTORY) $(OBJECTS)
$(CXX) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY)
$(BINARY_DIRECTORY)/core.a: $(C_OBJECTS) $(CPP_OBJECTS_CORE)
ar -rcu $@ $(C_OBJECTS) $(CPP_OBJECTS_CORE)
ranlib -c $@

$(OUTPUT_BINARY): $(BINARY_DIRECTORY) $(CPP_OBJECTS_TESTS) $(BINARY_DIRECTORY)/core.a
$(CXX) $(LDFLAGS) $(CPP_OBJECTS_TESTS) $(BINARY_DIRECTORY)/core.a $(LIBS) -o $(OUTPUT_BINARY)
6 changes: 5 additions & 1 deletion tests/host/common/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ extern "C" void yield()

extern "C" void __panic_func(const char* file, int line, const char* func) {
abort();
}
}

extern "C" void delay(unsigned long ms)
{
}
Loading

0 comments on commit 063e4cc

Please sign in to comment.