1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Add more FS tests

Including a test for #1685
This commit is contained in:
Ivan Grokhotkov 2016-03-10 09:12:37 +03:00
parent 6a7551e1f0
commit 1692798860
2 changed files with 121 additions and 26 deletions

View File

@ -75,14 +75,14 @@ gcov: test
find $(CORE_PATH) -name "*.gcno" -exec $(GCOV) -r -pb {} + find $(CORE_PATH) -name "*.gcno" -exec $(GCOV) -r -pb {} +
build-info: build-info:
echo "-------- build tools info --------" @echo "-------- build tools info --------"
echo "CC: " $(CC) @echo "CC: " $(CC)
$(CC) -v $(CC) -v
echo "CXX: " $(CXX) @echo "CXX: " $(CXX)
$(CXX) -v $(CXX) -v
echo "GCOV: " $(GCOV) @echo "GCOV: " $(GCOV)
$(GCOV) -v $(GCOV) -v
echo "----------------------------------" @echo "----------------------------------"
$(BINARY_DIRECTORY): $(BINARY_DIRECTORY):
mkdir -p $@ mkdir -p $@

View File

@ -1,43 +1,82 @@
/* /*
test_fs.cpp - host side file system tests test_fs.cpp - host side file system tests
Copyright © 2016 Ivan Grokhotkov Copyright © 2016 Ivan Grokhotkov
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
*/ */
#include <catch.hpp> #include <catch.hpp>
#include <map>
#include <FS.h> #include <FS.h>
#include "../common/spiffs_mock.h" #include "../common/spiffs_mock.h"
#include <spiffs/spiffs.h>
static void createFile (const char* name, const char* content)
{
auto f = SPIFFS.open(name, "w");
REQUIRE(f);
if (content) {
f.print(content);
}
}
static String readFile (const char* name)
{
auto f = SPIFFS.open(name, "r");
if (f) {
return f.readString();
}
return String();
}
static std::set<String> listDir (const char* path)
{
std::set<String> result;
Dir dir = SPIFFS.openDir(path);
while (dir.next()) {
REQUIRE(result.find(dir.fileName()) == std::end(result));
result.insert(dir.fileName());
}
return result;
}
TEST_CASE("FS can begin","[fs]") TEST_CASE("FS can begin","[fs]")
{ {
SPIFFS_MOCK_DECLARE(1024, 8, 512); SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.begin()); REQUIRE(SPIFFS.begin());
} }
TEST_CASE("FS can't begin with zero size","[fs]")
{
SPIFFS_MOCK_DECLARE(0, 8, 512);
REQUIRE_FALSE(SPIFFS.begin());
}
TEST_CASE("Before begin is called, open will fail","[fs]")
{
SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE_FALSE(SPIFFS.open("/foo", "w"));
}
TEST_CASE("FS can create file","[fs]") TEST_CASE("FS can create file","[fs]")
{ {
SPIFFS_MOCK_DECLARE(1024, 8, 512); SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.begin()); REQUIRE(SPIFFS.begin());
{ createFile("/test", "");
File f = SPIFFS.open("config.txt", "w"); REQUIRE(SPIFFS.exists("/test"));
REQUIRE(f);
}
REQUIRE(SPIFFS.exists("config.txt"));
} }
TEST_CASE("Files can be written and appended to","[fs]") TEST_CASE("Files can be written and appended to","[fs]")
{ {
SPIFFS_MOCK_DECLARE(1024, 8, 512); SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.begin()); REQUIRE(SPIFFS.begin());
{ {
File f = SPIFFS.open("config1.txt", "w"); File f = SPIFFS.open("config1.txt", "w");
@ -61,20 +100,76 @@ TEST_CASE("Files can be written and appended to","[fs]")
TEST_CASE("Files persist after reset", "[fs]") TEST_CASE("Files persist after reset", "[fs]")
{ {
SPIFFS_MOCK_DECLARE(1024, 8, 512); SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.begin()); REQUIRE(SPIFFS.begin());
{ createFile("config1.txt", "file 1");
File f = SPIFFS.open("config1.txt", "w");
REQUIRE(f);
f.println("file 1");
}
SPIFFS_MOCK_RESET(); SPIFFS_MOCK_RESET();
REQUIRE(SPIFFS.begin()); REQUIRE(SPIFFS.begin());
{ REQUIRE(readFile("config1.txt") == "file 1");
File f = SPIFFS.open("config1.txt", "r");
REQUIRE(f);
REQUIRE(f.readString() == "file 1\r\n");
}
} }
TEST_CASE("Filesystem is empty after format", "[fs]")
{
SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.format());
REQUIRE(SPIFFS.begin());
createFile("/1", "first");
createFile("/2", "second");
REQUIRE(SPIFFS.format());
Dir root = SPIFFS.openDir("/");
size_t count = 0;
while (root.next()) {
++count;
}
REQUIRE(count == 0);
}
TEST_CASE("Dir lists all files", "[fs]")
{
SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.begin());
createFile("/empty", "");
createFile("/not_empty", "some text");
createFile("/another", "more text");
createFile("/subdir/empty", "");
createFile("/subdir/not_empty", "text again");
auto files = listDir("/");
REQUIRE(files.size() == 5);
REQUIRE(files.find("/empty") != std::end(files));
REQUIRE(files.find("/not_empty") != std::end(files));
REQUIRE(files.find("/another") != std::end(files));
REQUIRE(files.find("/subdir/empty") != std::end(files));
REQUIRE(files.find("/subdir/not_empty") != std::end(files));
}
TEST_CASE("File names which are too long are rejected", "[fs]")
{
SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.begin());
const char* emptyName = "";
const char* longName_31 = "/234567890123456789012345678901";
const char* longName_32 = "/2345678901234567890123456789012";
REQUIRE_FALSE(SPIFFS.open(emptyName, "w"));
REQUIRE_FALSE(SPIFFS.open(emptyName, "r"));
REQUIRE_FALSE(SPIFFS.exists(emptyName));
REQUIRE_FALSE(SPIFFS.open(longName_32, "w"));
REQUIRE_FALSE(SPIFFS.open(longName_32, "r"));
REQUIRE_FALSE(SPIFFS.exists(longName_32));
REQUIRE(SPIFFS.open(longName_31, "w"));
REQUIRE(SPIFFS.open(longName_31, "r"));
REQUIRE(SPIFFS.exists(longName_31));
auto files = listDir("");
REQUIRE(files.empty());
}
TEST_CASE("#1685 Duplicate files", "[fs][bugreport]") {
SPIFFS_MOCK_DECLARE(64, 8, 512);
REQUIRE(SPIFFS.begin());
createFile("/config", "some text");
createFile("/data", "");
readFile("/config");
createFile("/data", "more text");
listDir("/");
}