2018-10-09 01:42:28 -04:00
|
|
|
#include "tests.h"
|
|
|
|
|
#include "../kmalloc.h"
|
|
|
|
|
|
2018-10-01 17:25:57 -04:00
|
|
|
#include <common.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#define FAIL() { \
|
|
|
|
|
fail_line = __LINE__; \
|
|
|
|
|
goto fail; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char filename[] = "README";
|
|
|
|
|
const char test_str[] = "fwrite";
|
|
|
|
|
|
|
|
|
|
extern int ff_abrt_line;
|
|
|
|
|
|
2018-10-02 17:00:27 -04:00
|
|
|
static bool stat_test() {
|
2018-10-01 17:25:57 -04:00
|
|
|
struct stat st;
|
2018-10-02 17:00:27 -04:00
|
|
|
bool ret = !(stat(filename, &st) || !st.st_size);
|
|
|
|
|
printf("stat_test: %s.\n", ret ? "passed" : "failed");
|
2018-10-01 17:25:57 -04:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool fread_test() {
|
|
|
|
|
bool ret = true;
|
|
|
|
|
int fail_line = 0;
|
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
FILE *file = {0};
|
|
|
|
|
void *buff = NULL;
|
|
|
|
|
|
2018-10-02 17:00:27 -04:00
|
|
|
if (stat(filename, &st) || !st.st_size) FAIL();
|
2018-10-01 17:25:57 -04:00
|
|
|
printf("Opening %s with size %llu...\n", filename, st.st_size);
|
|
|
|
|
file = fopen(filename, "r");
|
|
|
|
|
if (ferror(file)) FAIL();
|
|
|
|
|
|
2018-10-09 01:42:28 -04:00
|
|
|
if ((buff = kmalloc((size_t) st.st_size + 1)) == NULL) FAIL();
|
2018-10-01 17:25:57 -04:00
|
|
|
size_t read = fread(buff, (uint32_t) st.st_size, 1, file);
|
|
|
|
|
if (ferror(file) || !read) FAIL();
|
|
|
|
|
|
|
|
|
|
printf("Checking file contents...\n");
|
2018-10-01 20:51:57 -04:00
|
|
|
if (strncmp(buff, "mkdir", 5) != 0) FAIL();
|
2018-10-01 17:25:57 -04:00
|
|
|
|
|
|
|
|
printf("fread_test: passed.\n");
|
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
ret = false;
|
|
|
|
|
printf("fread_test: failed on line %d. (errno %d)\n", fail_line, errno);
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
if (file != NULL) fclose(file);
|
2018-10-09 01:42:28 -04:00
|
|
|
kfree(buff);
|
2018-10-01 17:25:57 -04:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool fwrite_test() {
|
|
|
|
|
bool ret = true;
|
|
|
|
|
int fail_line = 0;
|
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
FILE *file = {0};
|
|
|
|
|
void *rbuff = NULL;
|
|
|
|
|
void *wbuff = NULL;
|
|
|
|
|
|
2018-10-02 17:00:27 -04:00
|
|
|
if (stat(filename, &st) || !st.st_size) FAIL();
|
2018-10-01 20:51:57 -04:00
|
|
|
printf("Opening %s for updating... %lli\n", filename, st.st_size);
|
2018-10-01 17:25:57 -04:00
|
|
|
file = fopen(filename, "r+");
|
|
|
|
|
if (ferror(file)) FAIL();
|
|
|
|
|
|
2018-10-09 01:42:28 -04:00
|
|
|
if ((rbuff = kmalloc((size_t) st.st_size + 1)) == NULL) FAIL();
|
2018-10-01 17:25:57 -04:00
|
|
|
if (!fread(rbuff, (uint32_t) st.st_size, 1, file) || ferror(file)) FAIL();
|
|
|
|
|
if (fseek(file, 0, SEEK_SET) || ferror(file)) FAIL();
|
|
|
|
|
if (!fwrite(test_str, sizeof(test_str) - 1, 1, file) || ferror(file)) FAIL();
|
|
|
|
|
if (fclose(file)) FAIL();
|
|
|
|
|
|
2018-10-02 17:00:27 -04:00
|
|
|
if (stat(filename, &st) || !st.st_size) FAIL();
|
2018-10-01 20:51:57 -04:00
|
|
|
printf("Re-open %s for updating... %lli\n", filename, st.st_size);
|
2018-10-01 17:25:57 -04:00
|
|
|
file = fopen(filename, "r+");
|
|
|
|
|
if (ferror(file)) FAIL();
|
2018-10-09 01:42:28 -04:00
|
|
|
if ((wbuff = kmalloc(sizeof(test_str))) == NULL) FAIL();
|
2018-10-01 17:25:57 -04:00
|
|
|
if (fseek(file, 0, SEEK_SET) || ferror(file)) FAIL();
|
|
|
|
|
if (!fread(wbuff, sizeof(test_str), 1, file) || ferror(file)) FAIL();
|
|
|
|
|
if (strncmp(wbuff, test_str, sizeof(test_str) - 1) != 0) FAIL();
|
|
|
|
|
if (fseek(file, 0, SEEK_SET) || ferror(file)) FAIL();
|
|
|
|
|
if (!fwrite(rbuff, sizeof(test_str) - 1, 1, file) || ferror(file)) FAIL();
|
|
|
|
|
|
|
|
|
|
printf("fwrite_test: passed.\n");
|
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
ret = false;
|
|
|
|
|
printf("fwrite_test: failed on line %d. (errno %d, ff_abrt_line %d)\n", fail_line, errno, ff_abrt_line);
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
if (file != NULL) {
|
|
|
|
|
fflush(file);
|
|
|
|
|
fclose(file);
|
|
|
|
|
}
|
2018-10-09 01:42:28 -04:00
|
|
|
kfree(rbuff);
|
|
|
|
|
kfree(wbuff);
|
2018-10-01 17:25:57 -04:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool stdio_test() {
|
2018-10-02 17:00:27 -04:00
|
|
|
return stat_test() &&
|
2018-10-01 17:34:17 -04:00
|
|
|
fread_test() &&
|
2018-10-01 17:25:57 -04:00
|
|
|
fwrite_test();
|
|
|
|
|
}
|