diff --git a/Makefile b/Makefile index 0050955..e6f4f18 100644 --- a/Makefile +++ b/Makefile @@ -96,6 +96,9 @@ install: $(TARGET) install -d $(PREFIX)/share/treebird/ cp -r dist/ $(PREFIX)/share/treebird/ +test: + make -C test + apache_start: ./scripts/fcgistarter.sh @@ -119,4 +122,4 @@ clean_deps: clean_all: clean clean_deps -.PHONY: all filec clean update clean clean_deps clean_all +.PHONY: all filec clean update clean clean_deps clean_all test diff --git a/src/mime.c b/src/mime.c index 701fc89..0d9a6b2 100644 --- a/src/mime.c +++ b/src/mime.c @@ -22,17 +22,18 @@ #include #include "mime.h" -char* get_mime_boundary() +char* get_mime_boundary(char* content_type_str, char** bound) { - if (!getenv("CONTENT_TYPE")) return 1; + char* content = content_type_str ? content_type_str : getenv("CONTENT_TYPE"); // Data gets changed in place - char* content_type = malloc(strlen(getenv("CONTENT_TYPE"))+1); + char* content_type = malloc(strlen(content)+1); if (!content_type) { perror("malloc"); exit(1); } + strcpy(content_type, content); char* bound_str; char* boundary; @@ -55,10 +56,12 @@ char* get_mime_boundary() if ((tmp = strchr(boundary, '\"'))) *tmp = '\0'; - return 0; + *bound = boundary; + + return content_type; error: free(content_type); - return 1; + return NULL; } char* strnws(char* str) diff --git a/src/mime.h b/src/mime.h index 6416766..631d79c 100644 --- a/src/mime.h +++ b/src/mime.h @@ -19,7 +19,7 @@ #ifndef MIME_H #define MIME_H -char* get_mime_boundary(); +char* get_mime_boundary(char* content_type, char** res); char* read_mime_data(char* boundary, char* begin, struct http_form_info* info); #endif /* MIME_H */ diff --git a/test/Makefile b/test/Makefile index b51a52b..17c96e3 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,10 +1,10 @@ CC ?= cc MASTODONT_DIR = mastodont-c/ MASTODONT = $(MASTODONT_DIR)libmastodont.a -CFLAGS += -Wall -I ../$(MASTODONT_DIR)include/ -Wno-unused-variable -Wno-discarded-qualifiers -I/usr/include/ $(shell pkg-config --cflags libcurl libcjson libpcre) +CFLAGS += -g -Wall -I ../$(MASTODONT_DIR)include/ -Wno-unused-variable -Wno-discarded-qualifiers -I/usr/include/ $(shell pkg-config --cflags libcurl libcjson libpcre) LDFLAGS = -L./../$(MASTODONT_DIR) -lmastodont $(shell pkg-config --libs libcjson libcurl libpcre) -lfcgi TARGET = tests -SRC = unit/main.c +SRC = unit/main.c ../src/mime.c OBJ = $(patsubst %.c,%.o,$(SRC)) all: $(TARGET) diff --git a/test/tests b/test/tests index 4e2bbbb..d3d1a98 100755 Binary files a/test/tests and b/test/tests differ diff --git a/test/unit/mime_multipart.c b/test/unit/mime_multipart.c index 4ac91d0..248c666 100644 --- a/test/unit/mime_multipart.c +++ b/test/unit/mime_multipart.c @@ -1,29 +1,35 @@ -#include "../mine.h" -#define BOUNDARY_CONTENT_T1 "Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266" -#define BOUNDARY_CONTENT_T2 "Content-Type: multipart/form-data; boundary=\"---------------------------9051914041544843365972754266\"" +#include "../../src/mime.h" +#define BOUNDARY_CONTENT_T1 "multipart/form-data; boundary=---------------------------9051914041544843365972754266" +#define BOUNDARY_CONTENT_T2 "multipart/form-data; boundary=\"---------------------------9051914041544843365972754266\"" #define BOUNDARY_RES_T "---------------------------9051914041544843365972754266" -#define MULTIPART_TEST "-----------------------------9051914041544843365972754266\r\n" -"Content-Disposition: form-data; name=\"text\"\r\n\r\n" - -"text default\r\n" -"-----------------------------9051914041544843365972754266\r\n" -"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" -"Content-Type: text/plain\r\n\r\n" - -"Content of a.txt.\r\n\r\n" - -"-----------------------------9051914041544843365972754266\r\n" -"Content-Disposition: form-data; name=\"file2\"; filename\"a.html\"\r\n" -"Content-Type: text/html\r\n\r\n" - -"Content of a.html.\r\n\r\n" - +#define MULTIPART_TEST "-----------------------------9051914041544843365972754266\r\n" \ +"Content-Disposition: form-data; name=\"text\"\r\n\r\n" \ +"text default\r\n" \ +"-----------------------------9051914041544843365972754266\r\n" \ +"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" \ +"Content-Type: text/plain\r\n\r\n" \ +"Content of a.txt.\r\n\r\n" \ +"-----------------------------9051914041544843365972754266\r\n" \ +"Content-Disposition: form-data; name=\"file2\"; filename\"a.html\"\r\n" \ +"Content-Type: text/html\r\n\r\n" \ +"Content of a.html.\r\n\r\n" \ "-----------------------------9051914041544843365972754266--" +#include +#include int mime_multipart_test(void) { + char* bound, *bound2; + char* mem = get_mime_boundary(BOUNDARY_CONTENT_T2, &bound); + char* mem2 = get_mime_boundary(BOUNDARY_CONTENT_T1, &bound2); + assert(bound != NULL && bound2 != NULL); + assert(strcmp(bound, BOUNDARY_RES_T) == 0 && + strcmp(bound2, BOUNDARY_RES_T) == 0); + free(mem); + free(mem2); + return 0; }