Skip to content

Commit 1692798

Browse files
committed
Add more FS tests
Including a test for esp8266#1685
1 parent 6a7551e commit 1692798

File tree

2 files changed

+120
-25
lines changed

2 files changed

+120
-25
lines changed

tests/host/Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ gcov: test
7575
find $(CORE_PATH) -name "*.gcno" -exec $(GCOV) -r -pb {} +
7676

7777
build-info:
78-
echo "-------- build tools info --------"
79-
echo "CC: " $(CC)
78+
@echo "-------- build tools info --------"
79+
@echo "CC: " $(CC)
8080
$(CC) -v
81-
echo "CXX: " $(CXX)
81+
@echo "CXX: " $(CXX)
8282
$(CXX) -v
83-
echo "GCOV: " $(GCOV)
83+
@echo "GCOV: " $(GCOV)
8484
$(GCOV) -v
85-
echo "----------------------------------"
85+
@echo "----------------------------------"
8686

8787
$(BINARY_DIRECTORY):
8888
mkdir -p $@

tests/host/fs/test_fs.cpp

Lines changed: 115 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,82 @@
11
/*
22
test_fs.cpp - host side file system tests
33
Copyright © 2016 Ivan Grokhotkov
4-
4+
55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
77
in the Software without restriction, including without limitation the rights
88
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
copies of the Software, and to permit persons to whom the Software is
1010
furnished to do so, subject to the following conditions:
11-
11+
1212
The above copyright notice and this permission notice shall be included in
1313
all copies or substantial portions of the Software.
1414
*/
1515

1616
#include <catch.hpp>
17+
#include <map>
1718
#include <FS.h>
1819
#include "../common/spiffs_mock.h"
20+
#include <spiffs/spiffs.h>
21+
22+
static void createFile (const char* name, const char* content)
23+
{
24+
auto f = SPIFFS.open(name, "w");
25+
REQUIRE(f);
26+
if (content) {
27+
f.print(content);
28+
}
29+
}
1930

31+
static String readFile (const char* name)
32+
{
33+
auto f = SPIFFS.open(name, "r");
34+
if (f) {
35+
return f.readString();
36+
}
37+
return String();
38+
}
39+
40+
static std::set<String> listDir (const char* path)
41+
{
42+
std::set<String> result;
43+
Dir dir = SPIFFS.openDir(path);
44+
while (dir.next()) {
45+
REQUIRE(result.find(dir.fileName()) == std::end(result));
46+
result.insert(dir.fileName());
47+
}
48+
return result;
49+
}
2050

2151
TEST_CASE("FS can begin","[fs]")
2252
{
23-
SPIFFS_MOCK_DECLARE(1024, 8, 512);
53+
SPIFFS_MOCK_DECLARE(64, 8, 512);
2454
REQUIRE(SPIFFS.begin());
2555
}
2656

57+
TEST_CASE("FS can't begin with zero size","[fs]")
58+
{
59+
SPIFFS_MOCK_DECLARE(0, 8, 512);
60+
REQUIRE_FALSE(SPIFFS.begin());
61+
}
62+
63+
TEST_CASE("Before begin is called, open will fail","[fs]")
64+
{
65+
SPIFFS_MOCK_DECLARE(64, 8, 512);
66+
REQUIRE_FALSE(SPIFFS.open("/foo", "w"));
67+
}
68+
2769
TEST_CASE("FS can create file","[fs]")
2870
{
29-
SPIFFS_MOCK_DECLARE(1024, 8, 512);
71+
SPIFFS_MOCK_DECLARE(64, 8, 512);
3072
REQUIRE(SPIFFS.begin());
31-
{
32-
File f = SPIFFS.open("config.txt", "w");
33-
REQUIRE(f);
34-
}
35-
REQUIRE(SPIFFS.exists("config.txt"));
73+
createFile("/test", "");
74+
REQUIRE(SPIFFS.exists("/test"));
3675
}
3776

3877
TEST_CASE("Files can be written and appended to","[fs]")
3978
{
40-
SPIFFS_MOCK_DECLARE(1024, 8, 512);
79+
SPIFFS_MOCK_DECLARE(64, 8, 512);
4180
REQUIRE(SPIFFS.begin());
4281
{
4382
File f = SPIFFS.open("config1.txt", "w");
@@ -61,20 +100,76 @@ TEST_CASE("Files can be written and appended to","[fs]")
61100

62101
TEST_CASE("Files persist after reset", "[fs]")
63102
{
64-
SPIFFS_MOCK_DECLARE(1024, 8, 512);
103+
SPIFFS_MOCK_DECLARE(64, 8, 512);
65104
REQUIRE(SPIFFS.begin());
66-
{
67-
File f = SPIFFS.open("config1.txt", "w");
68-
REQUIRE(f);
69-
f.println("file 1");
70-
}
105+
createFile("config1.txt", "file 1");
106+
71107
SPIFFS_MOCK_RESET();
72108
REQUIRE(SPIFFS.begin());
73-
{
74-
File f = SPIFFS.open("config1.txt", "r");
75-
REQUIRE(f);
76-
REQUIRE(f.readString() == "file 1\r\n");
109+
REQUIRE(readFile("config1.txt") == "file 1");
110+
}
111+
112+
113+
TEST_CASE("Filesystem is empty after format", "[fs]")
114+
{
115+
SPIFFS_MOCK_DECLARE(64, 8, 512);
116+
REQUIRE(SPIFFS.format());
117+
REQUIRE(SPIFFS.begin());
118+
createFile("/1", "first");
119+
createFile("/2", "second");
120+
REQUIRE(SPIFFS.format());
121+
Dir root = SPIFFS.openDir("/");
122+
size_t count = 0;
123+
while (root.next()) {
124+
++count;
77125
}
126+
REQUIRE(count == 0);
78127
}
79128

129+
TEST_CASE("Dir lists all files", "[fs]")
130+
{
131+
SPIFFS_MOCK_DECLARE(64, 8, 512);
132+
REQUIRE(SPIFFS.begin());
133+
createFile("/empty", "");
134+
createFile("/not_empty", "some text");
135+
createFile("/another", "more text");
136+
createFile("/subdir/empty", "");
137+
createFile("/subdir/not_empty", "text again");
138+
auto files = listDir("/");
139+
REQUIRE(files.size() == 5);
140+
REQUIRE(files.find("/empty") != std::end(files));
141+
REQUIRE(files.find("/not_empty") != std::end(files));
142+
REQUIRE(files.find("/another") != std::end(files));
143+
REQUIRE(files.find("/subdir/empty") != std::end(files));
144+
REQUIRE(files.find("/subdir/not_empty") != std::end(files));
145+
}
80146

147+
TEST_CASE("File names which are too long are rejected", "[fs]")
148+
{
149+
SPIFFS_MOCK_DECLARE(64, 8, 512);
150+
REQUIRE(SPIFFS.begin());
151+
const char* emptyName = "";
152+
const char* longName_31 = "/234567890123456789012345678901";
153+
const char* longName_32 = "/2345678901234567890123456789012";
154+
REQUIRE_FALSE(SPIFFS.open(emptyName, "w"));
155+
REQUIRE_FALSE(SPIFFS.open(emptyName, "r"));
156+
REQUIRE_FALSE(SPIFFS.exists(emptyName));
157+
REQUIRE_FALSE(SPIFFS.open(longName_32, "w"));
158+
REQUIRE_FALSE(SPIFFS.open(longName_32, "r"));
159+
REQUIRE_FALSE(SPIFFS.exists(longName_32));
160+
REQUIRE(SPIFFS.open(longName_31, "w"));
161+
REQUIRE(SPIFFS.open(longName_31, "r"));
162+
REQUIRE(SPIFFS.exists(longName_31));
163+
auto files = listDir("");
164+
REQUIRE(files.empty());
165+
}
166+
167+
TEST_CASE("#1685 Duplicate files", "[fs][bugreport]") {
168+
SPIFFS_MOCK_DECLARE(64, 8, 512);
169+
REQUIRE(SPIFFS.begin());
170+
createFile("/config", "some text");
171+
createFile("/data", "");
172+
readFile("/config");
173+
createFile("/data", "more text");
174+
listDir("/");
175+
}

0 commit comments

Comments
 (0)