Files
ESP8266lib/ext/sd/tests/TestCreate.cpp
2021-02-21 09:33:08 +01:00

128 lines
3.1 KiB
C++

#include <gtest/gtest.h>
#include "Helper.h"
#include "../AccessHelper.h"
#include "../fat32/FS.h"
// dd if=/dev/zero of=/tmp/ram/orig.fat32 bs=8192 count=4096
// mkfs.vfat -F 32 -R 32 -s 8 orig.fat32
// hexdump -v -C -n 512 orig.fat32
TEST(TestCreat, structure) {
FAT32::FSHeader header;
ASSERT_EQ(0x0B, (uint8_t*)&header.bytesPerSector - (uint8_t*)&header);
ASSERT_EQ(0x0D, (uint8_t*)&header.sectorsPerCluster - (uint8_t*)&header);
ASSERT_EQ(0x0E, (uint8_t*)&header.numReservedSectors - (uint8_t*)&header);
ASSERT_EQ(0x10, (uint8_t*)&header.numberOfFATs - (uint8_t*)&header);
ASSERT_EQ(0x15, (uint8_t*)&header.mediaDescriptor - (uint8_t*)&header);
ASSERT_EQ(0x20, (uint8_t*)&header.sectorsInPartition - (uint8_t*)&header);
ASSERT_EQ(0x24, (uint8_t*)&header.sectorsPerFAT - (uint8_t*)&header);
ASSERT_EQ(0x2C, (uint8_t*)&header.rootDirFirstCluster - (uint8_t*)&header);
}
TEST (TestCreat, writeRead) {
using BlockDev = AccessHelper<TestDevice>;
using FS = FAT32::FS<BlockDev>;
size_t size = 32*1024*1024;
TestDevice dev(size);
BlockDev bDev(dev);
FS fs(bDev, 0);
fs.setup(size);
for (int i = 0; i < 64; ++i) {
char name[64];
sprintf(name, "test%03d.txt", i);
const size_t size = 128 + i * 512;
const size_t sizeA = (size/4096+1) * 4096;
std::cout << name << " - " << size << std::endl;
FAT32::FS<BlockDev>::File2 f = fs.getOrCreateFile(name);
uint8_t* data = (uint8_t*)malloc(128 + i * 512);
for (uint32_t j = 0; j < size; ++j) {data[j] = j;}
uint32_t written = f.write(size, data);
free(data);
ASSERT_EQ(size, written);
ASSERT_EQ(size, f.getSize());
ASSERT_EQ(sizeA, f.getAllocatedSize());
}
dev.toFile("/tmp/ram/1.fat32");
// mount -t vfat 1.fat32 /mnt/fat/ && ls -l /mnt/fat/ && cat /mnt/fat/test001.txt && umount /mnt/fat
// READ AGAIN
FS fs2(bDev, 0);
FS::DirIterator di = fs2.getRoot();
for (int i = 0; i < 64; ++i) {
char name[64];
sprintf(name, "test%03d.txt", i);
const size_t size = 128 + i * 512;
const size_t sizeA = (size/4096+1) * 4096;
FAT32::DirEntryAt dea = di.nextUsable();
FS::File2 f = fs.open2(dea);
ASSERT_EQ(name, dea.getName());
ASSERT_EQ(name, f.getName());
ASSERT_EQ(size, f.getSize());
ASSERT_EQ(sizeA, f.getAllocatedSize());
uint8_t* data = (uint8_t*)malloc(128 + i * 512);
uint32_t read = f.read(size, data);
ASSERT_EQ(size, read);
for (uint32_t j = 0; j < size; ++j) {
ASSERT_EQ((uint8_t)j, data[j]);
}
free(data);
}
}
TEST (TestCreate, getOrCreateFile) {
using BlockDev = AccessHelper<TestDevice>;
using FS = FAT32::FS<BlockDev>;
size_t size = 32*1024*1024;
TestDevice dev(size);
BlockDev bDev(dev);
FS fs(bDev, 0);
fs.setup(size);
FS::File2 f1 = fs.getOrCreateFile("test.txt");
ASSERT_EQ(0, f1.getSize());
ASSERT_EQ(4096, f1.getAllocatedSize());
ASSERT_EQ("test.txt", f1.getName());
uint8_t d1[128];
uint8_t d2[128];
for (int i = 0; i < 128; ++i) {d1[i] = 1;}
ASSERT_EQ(128, f1.write(128, d1));
FS::File2 f2 = fs.getOrCreateFile("test.txt");
ASSERT_EQ(128, f2.getSize());
ASSERT_EQ(128, f2.read(128, d2));
for (uint32_t i = 0; i < 128; ++i) {ASSERT_EQ(d1[i], d2[i]);}
}