@@ -77,6 +77,58 @@ void test_read_write() {
77
77
TEST_ASSERT_EQUAL (0 , err);
78
78
}
79
79
80
+ // Simple test for iterating dir entries
81
+ void test_read_dir () {
82
+ FATFileSystem fs (" fat" );
83
+
84
+ int err = fs.mount (&bd);
85
+ TEST_ASSERT_EQUAL (0 , err);
86
+
87
+ err = fs.mkdir (" test_read_dir" , S_IRWXU | S_IRWXG | S_IRWXO);
88
+ TEST_ASSERT_EQUAL (0 , err);
89
+
90
+ err = fs.mkdir (" test_read_dir/test_dir" , S_IRWXU | S_IRWXG | S_IRWXO);
91
+ TEST_ASSERT_EQUAL (0 , err);
92
+
93
+ FileHandle *file = fs.open (" test_read_dir/test_file" , O_WRONLY | O_CREAT);
94
+ TEST_ASSERT (file);
95
+ err = file->close ();
96
+ TEST_ASSERT_EQUAL (0 , err);
97
+
98
+ // Iterate over dir checking for known files
99
+ DirHandle *dir = fs.opendir (" test_read_dir" );
100
+ TEST_ASSERT (dir);
101
+
102
+ struct dirent *de;
103
+ bool test_dir_found = false ;
104
+ bool test_file_found = true ;
105
+
106
+ while ((de = readdir (dir))) {
107
+ printf (" d_name: %.32s, d_type: %x\n " , de->d_name , de->d_type );
108
+
109
+ if (strcmp (de->d_name , " test_dir" ) == 0 ) {
110
+ test_dir_found = true ;
111
+ TEST_ASSERT_EQUAL (DT_DIR, de->d_type );
112
+ } else if (strcmp (de->d_name , " test_file" ) == 0 ) {
113
+ test_file_found = true ;
114
+ TEST_ASSERT_EQUAL (DT_REG, de->d_type );
115
+ } else {
116
+ char *buf = new char [NAME_MAX];
117
+ snprintf (buf, NAME_MAX, " Unexpected file \" %s\" " , de->d_name );
118
+ TEST_ASSERT_MESSAGE (false , buf);
119
+ }
120
+ }
121
+
122
+ TEST_ASSERT_MESSAGE (test_dir_found, " Could not find \" test_dir\" " );
123
+ TEST_ASSERT_MESSAGE (test_file_found, " Could not find \" test_file\" " );
124
+
125
+ err = dir->closedir ();
126
+ TEST_ASSERT_EQUAL (0 , err);
127
+
128
+ err = fs.unmount ();
129
+ TEST_ASSERT_EQUAL (0 , err);
130
+ }
131
+
80
132
81
133
// Test setup
82
134
utest::v1::status_t test_setup (const size_t number_of_cases) {
@@ -88,6 +140,7 @@ Case cases[] = {
88
140
Case (" Testing formating" , test_format),
89
141
Case (" Testing read write < block" , test_read_write<BLOCK_SIZE/2 >),
90
142
Case (" Testing read write > block" , test_read_write<2 *BLOCK_SIZE>),
143
+ Case (" Testing dir iteration" , test_read_dir),
91
144
};
92
145
93
146
Specification specification (test_setup, cases);
0 commit comments