Skip to content

Commit

Permalink
[tests] Rewrite seek_tell FS test
Browse files Browse the repository at this point in the history
Rewrite test to self check what it can.

Signed-off-by: Mariusz Zaborski <[email protected]>
  • Loading branch information
oshogbo committed Aug 26, 2022
1 parent 33576d3 commit ae6cad8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 121 deletions.
9 changes: 9 additions & 0 deletions libos/test/fs/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,12 @@ void fill_random(void* buffer, size_t size) {
for (size_t i = 0; i < size; i++)
((uint8_t*)buffer)[i] = rand() % 256;
}

uint64_t file_size(const char* path) {
struct stat st;

if (stat(path, &st) < 0)
fatal_error("Failed do stat file %s: %s\n", path, strerror(errno));

return st.st_size;
}
1 change: 1 addition & 0 deletions libos/test/fs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ FILE* open_output_stdio(const char* path, bool rdwr);
void write_stdio(const char* path, FILE* f, const void* buffer, size_t size);
void* alloc_buffer(size_t size);
void fill_random(void* buffer, size_t size);
uint64_t file_size(const char* path);

void copy_data(int fi, int fo, const char* input_path, const char* output_path, size_t size);
147 changes: 88 additions & 59 deletions libos/test/fs/seek_tell.c
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
#include <inttypes.h>

#include "common.h"

#define EXTEND_SIZE 4097

static void seek_input_fd(const char* path) {
// Verify Unsigned
#define VERIFYU(msg, expected, got) do { \
uint64_t __l = (expected); \
uint64_t __r = (got); \
if (__l != __r) { \
fatal_error("%s:%d %s (expected %"PRIu64", got %"PRIu64")", \
__func__, __LINE__, msg, __l, __r); \
} \
} while(0)

static void seek_input_fd(const char* path, uint64_t size) {
int f = open_input_fd(path);
printf("open(%s) input OK\n", path);

seek_fd(path, f, 0, SEEK_SET);
printf("seek(%s) input start OK\n", path);
seek_fd(path, f, 0, SEEK_END);
printf("seek(%s) input end OK\n", path);
off_t pos = tell_fd(path, f);
printf("tell(%s) input end OK: %zd\n", path, pos);
VERIFYU("tell position mismatch", 0, pos);

seek_fd(path, f, 0, SEEK_END);
pos = tell_fd(path, f);
VERIFYU("tell position mismatch", size, pos);

seek_fd(path, f, -pos, SEEK_END); // rewind
printf("seek(%s) input rewind OK\n", path);
pos = tell_fd(path, f);
printf("tell(%s) input start OK: %zd\n", path, pos);
VERIFYU("tell position mismatch", 0, pos);

close_fd(path, f);
printf("close(%s) input OK\n", path);
}

static void seek_input_stdio(const char* path) {
static void seek_input_stdio(const char* path, uint64_t size) {
FILE* f = open_input_stdio(path);
printf("fopen(%s) input OK\n", path);

seek_stdio(path, f, 0, SEEK_SET);
printf("fseek(%s) input start OK\n", path);
seek_stdio(path, f, 0, SEEK_END);
printf("fseek(%s) input end OK\n", path);
off_t pos = tell_stdio(path, f);
printf("ftell(%s) input end OK: %zd\n", path, pos);
VERIFYU("ftell position mismatch", 0, pos);

seek_stdio(path, f, 0, SEEK_END);
pos = tell_stdio(path, f);
VERIFYU("ftell position mismatch", size, pos);

seek_stdio(path, f, -pos, SEEK_END); // rewind
printf("fseek(%s) input rewind OK\n", path);
pos = tell_stdio(path, f);
printf("ftell(%s) input start OK: %zd\n", path, pos);
VERIFYU("ftell position mismatch", 0, pos);

close_stdio(path, f);
printf("fclose(%s) input OK\n", path);
}

static void seek_output_fd(const char* path) {
static void seek_output_fd(const char* path, uint64_t size) {
uint8_t buf[EXTEND_SIZE + 1] = {1};
int f = open_output_fd(path, /*rdwr=*/true);
printf("open(%s) output OK\n", path);

seek_fd(path, f, 0, SEEK_SET);
printf("seek(%s) output start OK\n", path);
seek_fd(path, f, 0, SEEK_END);
printf("seek(%s) output end OK\n", path);
off_t pos = tell_fd(path, f);
printf("tell(%s) output end OK: %zd\n", path, pos);
VERIFYU("tell position mismatch", 0, pos);

seek_fd(path, f, 0, SEEK_END);
pos = tell_fd(path, f);
VERIFYU("tell position mismatch", size, pos);

seek_fd(path, f, EXTEND_SIZE, SEEK_CUR); // extend
printf("seek(%s) output end 2 OK\n", path);
write_fd(path, f, buf, 1);
size += 1;
seek_fd(path, f, -EXTEND_SIZE - 1, SEEK_CUR); // rewind to former end
printf("seek(%s) output end 3 OK\n", path);
read_fd(path, f, buf, EXTEND_SIZE + 1);
for (size_t i = 0; i < EXTEND_SIZE + 1; i++) {
for (uint64_t i = 0; i < EXTEND_SIZE + 1; i++) {
if (i == EXTEND_SIZE) {
if (buf[i] != 1)
fatal_error("invalid last byte\n");
Expand All @@ -61,39 +76,43 @@ static void seek_output_fd(const char* path) {
fatal_error("extended buffer not zeroed\n");
}
}
size += EXTEND_SIZE;
pos = tell_fd(path, f);
printf("tell(%s) output end 2 OK: %zd\n", path, pos);
seek_fd(path, f, pos * 2, SEEK_SET);
printf("seek(%s) output beyond end OK\n", path);
VERIFYU("tell position mismatch", size, pos);

uint64_t pos_over = pos * 2;
seek_fd(path, f, pos_over, SEEK_SET);
pos = tell_fd(path, f);
printf("tell(%s) output beyond end OK: %zd\n", path, pos);
ssize_t ret = read(f, &buf, 1);
printf("read(%s) output OK: %zd\n", path, ret);
VERIFYU("tell position mismatch", pos_over, pos);

uint64_t ret = read(f, &buf, 1);
VERIFYU("read failed", 0, ret);

seek_fd(path, f, 0, SEEK_END);
printf("seek(%s) output end 4 OK\n", path);
pos = tell_fd(path, f);
printf("tell(%s) output end 3 OK: %zd\n", path, pos);
VERIFYU("tell position mismatch", size, pos);

close_fd(path, f);
printf("close(%s) output OK\n", path);
}

static void seek_output_stdio(const char* path) {
static void seek_output_stdio(const char* path, uint64_t size) {
uint8_t buf[EXTEND_SIZE + 1] = {1};
FILE* f = open_output_stdio(path, /*rdwr=*/true);
printf("fopen(%s) output OK\n", path);

seek_stdio(path, f, 0, SEEK_SET);
printf("fseek(%s) output start OK\n", path);
seek_stdio(path, f, 0, SEEK_END);
printf("fseek(%s) output end OK\n", path);
off_t pos = tell_stdio(path, f);
printf("ftell(%s) output end OK: %zd\n", path, pos);
VERIFYU("ftell position mismatch", 0, pos);

seek_stdio(path, f, 0, SEEK_END);
pos = tell_stdio(path, f);
VERIFYU("ftell position mismatch", size, pos);

seek_stdio(path, f, EXTEND_SIZE, SEEK_CUR); // extend
printf("fseek(%s) output end 2 OK\n", path);
write_stdio(path, f, buf, 1);
size += 1;
seek_stdio(path, f, -EXTEND_SIZE - 1, SEEK_CUR); // rewind to former end
printf("fseek(%s) output end 3 OK\n", path);
read_stdio(path, f, buf, EXTEND_SIZE + 1);
for (size_t i = 0; i < EXTEND_SIZE + 1; i++) {
for (uint64_t i = 0; i < EXTEND_SIZE + 1; i++) {
if (i == EXTEND_SIZE) {
if (buf[i] != 1)
fatal_error("invalid last byte\n");
Expand All @@ -102,31 +121,41 @@ static void seek_output_stdio(const char* path) {
fatal_error("extended buffer not zeroed\n");
}
}
size += EXTEND_SIZE;
pos = tell_stdio(path, f);
printf("ftell(%s) output end 2 OK: %zd\n", path, pos);
seek_stdio(path, f, pos * 2, SEEK_SET);
printf("fseek(%s) output beyond end OK\n", path);
VERIFYU("ftell position mismatch", size, pos);

uint64_t pos_over = pos * 2;
seek_stdio(path, f, pos_over, SEEK_SET);
pos = tell_stdio(path, f);
printf("ftell(%s) output beyond end OK: %zd\n", path, pos);
ssize_t ret = fread(&buf, 1, 1, f);
printf("fread(%s) output OK: %zd\n", path, ret);
VERIFYU("ftell position mismatch", pos_over, pos);

uint64_t ret = fread(&buf, 1, 1, f);
VERIFYU("fread failed", 0, ret);

seek_stdio(path, f, 0, SEEK_END);
printf("fseek(%s) output end 4 OK\n", path);
pos = tell_stdio(path, f);
printf("ftell(%s) output end 3 OK: %zd\n", path, pos);
VERIFYU("ftell position mismatch", size, pos);

close_stdio(path, f);
printf("fclose(%s) output OK\n", path);
}

int main(int argc, char* argv[]) {
setup();

if (argc < 4)
fatal_error("Usage: %s <input_path> <output_path_1> <output_path_2>\n", argv[0]);

setup();
seek_input_fd(argv[1]);
seek_input_stdio(argv[1]);
seek_output_fd(argv[2]);
seek_output_stdio(argv[3]);
uint64_t argv1_size = file_size(argv[1]);
uint64_t argv2_size = file_size(argv[2]);
uint64_t argv3_size = file_size(argv[3]);

seek_input_fd(argv[1], argv1_size);
seek_input_stdio(argv[1], argv1_size);
seek_output_fd(argv[2], argv2_size);
seek_output_stdio(argv[3], argv3_size);

printf("Test passed\n");

return 0;
}
5 changes: 2 additions & 3 deletions libos/test/fs/test_enc.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ def test_115_seek_tell(self):
output_path_2 = os.path.join(self.OUTPUT_DIR, 'test_115b')
self.copy_input(plaintext_path, output_path_1) # encrypt
self.copy_input(plaintext_path, output_path_2)
stdout, stderr = self.run_binary(['seek_tell', input_path, output_path_1, output_path_2])
self.verify_seek_tell(stdout, stderr, input_path, output_path_1, output_path_2,
self.FILE_SIZES[-1])
stdout, _ = self.run_binary(['seek_tell', input_path, output_path_1, output_path_2])
self.verify_seek_tell(stdout, output_path_1, output_path_2, self.FILE_SIZES[-1])

# overrides TC_00_FileSystem to change input dir (from plaintext to encrypted)
def test_130_file_stat(self):
Expand Down
63 changes: 4 additions & 59 deletions libos/test/fs/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,62 +97,8 @@ def test_110_read_write(self):
self.assertIn('close(' + file_path + ') RW OK', stdout)

# pylint: disable=too-many-arguments
def verify_seek_tell(self, stdout, stderr, input_path, output_path_1, output_path_2, size):
self.assertNotIn('ERROR: ', stderr)

# seek_input_fd:
self.assertIn('open(' + input_path + ') input OK', stdout)
self.assertIn('seek(' + input_path + ') input start OK', stdout)
self.assertIn('seek(' + input_path + ') input end OK', stdout)
self.assertIn('tell(' + input_path + ') input end OK: ' + str(size), stdout)
self.assertIn('seek(' + input_path + ') input rewind OK', stdout)
self.assertIn('tell(' + input_path + ') input start OK: 0', stdout)
self.assertIn('close(' + input_path + ') input OK', stdout)

# seek_input_stdio:
self.assertIn('fopen(' + input_path + ') input OK', stdout)
self.assertIn('fseek(' + input_path + ') input start OK', stdout)
self.assertIn('fseek(' + input_path + ') input end OK', stdout)
self.assertIn('ftell(' + input_path + ') input end OK: ' + str(size), stdout)
self.assertIn('fseek(' + input_path + ') input rewind OK', stdout)
self.assertIn('ftell(' + input_path + ') input start OK: 0', stdout)
self.assertIn('fclose(' + input_path + ') input OK', stdout)

# seek_output_fd:
self.assertIn('open(' + output_path_1 + ') output OK', stdout)
self.assertIn('seek(' + output_path_1 + ') output start OK', stdout)
self.assertIn('seek(' + output_path_1 + ') output end OK', stdout)
self.assertIn('tell(' + output_path_1 + ') output end OK: ' + str(size), stdout)
self.assertIn('seek(' + output_path_1 + ') output end 2 OK', stdout)
self.assertIn('seek(' + output_path_1 + ') output end 3 OK', stdout)
self.assertIn('tell(' + output_path_1 + ') output end 2 OK: ' + str(size + 4098), stdout)
self.assertIn('seek(' + output_path_1 + ') output beyond end OK', stdout)
self.assertIn(
'tell(' + output_path_1 + ') output beyond end OK: ' + str((size + 4098) * 2),
stdout
)
self.assertIn('read(' + output_path_1 + ') output OK: 0', stdout)
self.assertIn('seek(' + output_path_1 + ') output end 4 OK', stdout)
self.assertIn('tell(' + output_path_1 + ') output end 3 OK: ' + str(size + 4098), stdout)
self.assertIn('close(' + output_path_1 + ') output OK', stdout)

# seek_output_stdio:
self.assertIn('fopen(' + output_path_2 + ') output OK', stdout)
self.assertIn('fseek(' + output_path_2 + ') output start OK', stdout)
self.assertIn('fseek(' + output_path_2 + ') output end OK', stdout)
self.assertIn('ftell(' + output_path_2 + ') output end OK: ' + str(size), stdout)
self.assertIn('fseek(' + output_path_2 + ') output end 2 OK', stdout)
self.assertIn('fseek(' + output_path_2 + ') output end 3 OK', stdout)
self.assertIn('ftell(' + output_path_2 + ') output end 2 OK: ' + str(size + 4098), stdout)
self.assertIn('fseek(' + output_path_2 + ') output beyond end OK', stdout)
self.assertIn(
'ftell(' + output_path_2 + ') output beyond end OK: ' + str((size + 4098) * 2),
stdout
)
self.assertIn('fread(' + output_path_2 + ') output OK: 0', stdout)
self.assertIn('fseek(' + output_path_2 + ') output end 4 OK', stdout)
self.assertIn('ftell(' + output_path_2 + ') output end 3 OK: ' + str(size + 4098), stdout)
self.assertIn('fclose(' + output_path_2 + ') output OK', stdout)
def verify_seek_tell(self, stdout, output_path_1, output_path_2, size):
self.assertIn('Test passed', stdout)

expected_size = size + 4098
self.verify_size(output_path_1, expected_size)
Expand All @@ -164,9 +110,8 @@ def test_115_seek_tell(self):
output_path_2 = os.path.join(self.OUTPUT_DIR, 'test_115b')
self.copy_input(input_path, output_path_1)
self.copy_input(input_path, output_path_2)
stdout, stderr = self.run_binary(['seek_tell', input_path, output_path_1, output_path_2])
self.verify_seek_tell(stdout, stderr, input_path, output_path_1, output_path_2,
self.FILE_SIZES[-1])
stdout, _ = self.run_binary(['seek_tell', input_path, output_path_1, output_path_2])
self.verify_seek_tell(stdout, output_path_1, output_path_2, self.FILE_SIZES[-1])

def test_120_file_delete(self):
file_path = 'test_120'
Expand Down

0 comments on commit ae6cad8

Please sign in to comment.