104 lines
2.8 KiB
C++
104 lines
2.8 KiB
C++
#include <gtest/gtest.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "mocs.h"
|
|
#include "tftp.h"
|
|
|
|
tftp_custom_file_t file = {
|
|
.data = (char*)"1234567890",
|
|
.len = 11,
|
|
.name = (char*)"test.txt",
|
|
.offset = 0
|
|
};
|
|
tftp_custom_file_t write_file = {
|
|
.data = NULL,
|
|
.len = 0,
|
|
.name = (char*)"test.txt",
|
|
.offset = 0
|
|
};
|
|
|
|
TEST(TFTP, custom_fseek)
|
|
{
|
|
tftp_custom_fseek(&file, 5, SEEK_SET);
|
|
EXPECT_EQ(file.offset, 5);
|
|
tftp_custom_fseek(&file, 5, SEEK_CUR);
|
|
EXPECT_EQ(file.offset, 10);
|
|
}
|
|
|
|
TEST(TFTP, custom_fread)
|
|
{
|
|
char buf[11];
|
|
tftp_custom_fseek(&file, 0, SEEK_SET);
|
|
size_t bytes = tftp_custom_fread(buf, 11, &file);
|
|
EXPECT_EQ(bytes, 11);
|
|
EXPECT_EQ(file.offset, 11);
|
|
EXPECT_EQ(memcmp(buf, "1234567890", 10), 0);
|
|
|
|
memset(buf, 0, 11);
|
|
|
|
tftp_custom_fseek(&file, 0, SEEK_SET);
|
|
bytes = tftp_custom_fread(buf, 11, &file);
|
|
EXPECT_EQ(bytes, 11);
|
|
EXPECT_EQ(memcmp(buf, "1234567890", 10), 0);
|
|
|
|
memset(buf, 0, 11);
|
|
|
|
tftp_custom_fseek(&file, 0, SEEK_SET);
|
|
bytes = tftp_custom_fread(buf, 5, &file);
|
|
EXPECT_EQ(bytes, 5);
|
|
EXPECT_EQ(memcmp(buf, "12345", 5), 0);
|
|
|
|
memset(buf, 0, 11);
|
|
|
|
bytes = tftp_custom_fread(buf, 5, &file);
|
|
EXPECT_EQ(bytes, 5);
|
|
EXPECT_EQ(memcmp(buf, "67890", 5), 0);
|
|
}
|
|
|
|
TEST(TFTP, custom_fwrite) {
|
|
write_file.data = (char*)malloc(22 * sizeof(char));
|
|
write_file.len = 22;
|
|
tftp_custom_fwrite("0987654321", 10, &write_file);
|
|
EXPECT_EQ(write_file.offset, 10);
|
|
EXPECT_EQ(write_file.len, 22);
|
|
EXPECT_EQ(memcmp(write_file.data, "0987654321", 10), 0);
|
|
|
|
tftp_custom_fwrite("1234567890", 10, &write_file);
|
|
EXPECT_EQ(write_file.offset, 20);
|
|
EXPECT_EQ(write_file.len, 22);
|
|
EXPECT_EQ(memcmp(write_file.data, "09876543211234567890", 21), 0);
|
|
|
|
free(write_file.data);
|
|
write_file.data = NULL;
|
|
write_file.len = 0;
|
|
}
|
|
|
|
TEST(TFTP, custom_fread_fwrite_suggested_by_Lorenz) {
|
|
char buf[21];
|
|
write_file.data = (char*)malloc(21 * sizeof(char));
|
|
write_file.len = 21;
|
|
write_file.offset = 0;
|
|
tftp_custom_fwrite("1234567890123456789", 19, &write_file);
|
|
|
|
tftp_custom_fwrite("0987654321", 11, &write_file);
|
|
// the above function does not write the null terminator
|
|
EXPECT_EQ(write_file.offset, 21);
|
|
EXPECT_EQ(write_file.len, 21);
|
|
EXPECT_EQ(memcmp(write_file.data, "12345678901234567890", 20), 0);
|
|
|
|
tftp_custom_fseek(&write_file, 0, SEEK_SET);
|
|
tftp_custom_fread(buf, 20, &write_file);
|
|
// the above function does not write the null terminator
|
|
buf[20] = '\0';
|
|
EXPECT_EQ(memcmp(buf, "12345678901234567890", 21), 0);
|
|
free(write_file.data);
|
|
write_file.data = NULL;
|
|
write_file.len = 0;
|
|
|
|
file.offset = 9;
|
|
tftp_custom_fread(buf, 20, &file);
|
|
EXPECT_EQ(memcmp(buf, "0", 2), 0);
|
|
} |