mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-29 12:41:03 +00:00
Better packed test
This commit is contained in:
1 parent
2af5d07467
commit
3a46055e52
14 files changed
+161
-80
No files matched your search
+86
@@ -0,0 +1,86 @@
|
||||
// Copyright (c) Cesanta Software Limited
|
||||
// All rights reserved.
|
||||
|
||||
// This program is used to pack arbitrary data into a C binary. It takes
|
||||
// a list of files as an input, and produces a .c data file that contains
|
||||
// contents of all these files as a collection of byte arrays.
|
||||
//
|
||||
// Usage:
|
||||
// 1. Compile this file:
|
||||
// cc -o pack pack.c
|
||||
//
|
||||
// 2. Convert list of files into single .c:
|
||||
// ./pack file1.data file2.data > fs.c
|
||||
//
|
||||
// 3. In your application code, you can access files using this function:
|
||||
// const char *mg_unpack(const char *file_name, size_t *size);
|
||||
//
|
||||
// 4. Build your app with fs.c:
|
||||
// cc -o my_app my_app.c fs.c
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
static const char *code =
|
||||
"const char *mg_unlist(size_t no) {\n"
|
||||
" return packed_files[no].name;\n"
|
||||
"}\n"
|
||||
"const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {\n"
|
||||
" const struct packed_file *p;\n"
|
||||
" for (p = packed_files; p->name != NULL; p++) {\n"
|
||||
" if (strcmp(p->name, name) != 0) continue;\n"
|
||||
" if (size != NULL) *size = p->size - 1;\n"
|
||||
" if (mtime != NULL) *mtime = p->mtime;\n"
|
||||
" return (const char *) p->data;\n"
|
||||
" }\n"
|
||||
" return NULL;\n"
|
||||
"}\n";
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i, j, ch;
|
||||
|
||||
printf("%s", "#include <stddef.h>\n");
|
||||
printf("%s", "#include <string.h>\n");
|
||||
printf("%s", "#include <time.h>\n");
|
||||
printf("%s", "\n");
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
char ascii[12];
|
||||
FILE *fp = fopen(argv[i], "rb");
|
||||
if (fp == NULL) printf("Cannot open %s\n", argv[i]), exit(EXIT_FAILURE);
|
||||
|
||||
printf("static const unsigned char v%d[] = {\n", i);
|
||||
for (j = 0; (ch = fgetc(fp)) != EOF; j++) {
|
||||
if (j == (int) sizeof(ascii)) {
|
||||
printf(" // %.*s\n", j, ascii);
|
||||
j = 0;
|
||||
}
|
||||
ascii[j] = (char) ((ch >= ' ' && ch <= '~' && ch != '\\') ? ch : '.');
|
||||
printf(" %3u,", ch);
|
||||
}
|
||||
// Append zero byte at the end, to make text files appear in memory
|
||||
// as nul-terminated strings.
|
||||
// printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii);
|
||||
printf(" 0 // %.*s\n};\n", j, ascii);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
printf("%s", "\nstatic const struct packed_file {\n");
|
||||
printf("%s", " const char *name;\n");
|
||||
printf("%s", " const unsigned char *data;\n");
|
||||
printf("%s", " size_t size;\n");
|
||||
printf("%s", " time_t mtime;\n");
|
||||
printf("%s", "} packed_files[] = {\n");
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
struct stat st;
|
||||
stat(argv[i], &st);
|
||||
printf(" {\"%s\", v%d, sizeof(v%d), %lu},\n", argv[i], i, i, st.st_mtime);
|
||||
}
|
||||
printf("%s", " {NULL, NULL, 0, 0}\n");
|
||||
printf("%s", "};\n\n");
|
||||
printf("%s", code);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
+8
-6
@@ -1368,19 +1368,22 @@ static void eh7(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
static void test_packed(void) {
|
||||
struct mg_mgr mgr;
|
||||
const char *url = "http://127.0.0.1:12351";
|
||||
char buf[FETCH_BUF_SIZE] = "";
|
||||
char buf[FETCH_BUF_SIZE] = "", *data = mg_file_read("Makefile", NULL);
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, url, eh7, NULL);
|
||||
// ASSERT(fetch(&mgr, buf, url, "GET /packed/ HTTP/1.0\n\n") == 404);
|
||||
// fetch(&mgr, buf, url, "GET / HTTP/1.0\n\n");
|
||||
fetch(&mgr, buf, url, "GET /Makefile HTTP/1.0\n\n");
|
||||
printf("--------\n%s\n", buf);
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /Makefile HTTP/1.0\n\n") == 200);
|
||||
ASSERT(cmpbody(buf, data) == 0);
|
||||
free(data);
|
||||
ASSERT(fetch(&mgr, buf, url, "GET / HTTP/1.0\n\n") == 200);
|
||||
// printf("--------\n%s\n", buf);
|
||||
// exit(0);
|
||||
mg_mgr_free(&mgr);
|
||||
ASSERT(mgr.conns == NULL);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
mg_log_set("3");
|
||||
test_packed();
|
||||
test_crc32();
|
||||
test_multipart();
|
||||
test_http_chunked();
|
||||
@@ -1403,7 +1406,6 @@ int main(void) {
|
||||
test_http_no_content_length();
|
||||
test_http_pipeline();
|
||||
test_http_range();
|
||||
test_packed();
|
||||
test_mqtt();
|
||||
printf("SUCCESS. Total tests: %d\n", s_num_tests);
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
Reference in new issue
Block a user