1
1
/*
2
2
test_fs.cpp - host side file system tests
3
3
Copyright © 2016 Ivan Grokhotkov
4
-
4
+
5
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
of this software and associated documentation files (the "Software"), to deal
7
7
in the Software without restriction, including without limitation the rights
8
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
copies of the Software, and to permit persons to whom the Software is
10
10
furnished to do so, subject to the following conditions:
11
-
11
+
12
12
The above copyright notice and this permission notice shall be included in
13
13
all copies or substantial portions of the Software.
14
14
*/
15
15
16
16
#include < catch.hpp>
17
+ #include < map>
17
18
#include < FS.h>
18
19
#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
+ }
19
30
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
+ }
20
50
21
51
TEST_CASE (" FS can begin" ," [fs]" )
22
52
{
23
- SPIFFS_MOCK_DECLARE (1024 , 8 , 512 );
53
+ SPIFFS_MOCK_DECLARE (64 , 8 , 512 );
24
54
REQUIRE (SPIFFS.begin ());
25
55
}
26
56
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
+
27
69
TEST_CASE (" FS can create file" ," [fs]" )
28
70
{
29
- SPIFFS_MOCK_DECLARE (1024 , 8 , 512 );
71
+ SPIFFS_MOCK_DECLARE (64 , 8 , 512 );
30
72
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" ));
36
75
}
37
76
38
77
TEST_CASE (" Files can be written and appended to" ," [fs]" )
39
78
{
40
- SPIFFS_MOCK_DECLARE (1024 , 8 , 512 );
79
+ SPIFFS_MOCK_DECLARE (64 , 8 , 512 );
41
80
REQUIRE (SPIFFS.begin ());
42
81
{
43
82
File f = SPIFFS.open (" config1.txt" , " w" );
@@ -61,20 +100,76 @@ TEST_CASE("Files can be written and appended to","[fs]")
61
100
62
101
TEST_CASE (" Files persist after reset" , " [fs]" )
63
102
{
64
- SPIFFS_MOCK_DECLARE (1024 , 8 , 512 );
103
+ SPIFFS_MOCK_DECLARE (64 , 8 , 512 );
65
104
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
+
71
107
SPIFFS_MOCK_RESET ();
72
108
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;
77
125
}
126
+ REQUIRE (count == 0 );
78
127
}
79
128
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
+ }
80
146
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