Blame view

RIOT/tests/unittests/tests-vfs/tests-vfs-mount-constfs.c 5.78 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  /*
   * Copyright (C) 2016 Eistec AB
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   *
   * @file
   * @brief       Unittests for vfs_mount, vfs_umount, ConstFS, VFS POSIX wrappers
   *
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   */
  #include <errno.h>
  #include <stdint.h>
  #include <string.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <unistd.h>
  
  #include "embUnit/embUnit.h"
  
  #include "vfs.h"
  #include "fs/constfs.h"
  
  #include "tests-vfs.h"
  
  static const uint8_t bin_data[] = {
      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
      0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
      0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
  };
  static const uint8_t str_data[] = "This is a test file";
                                  /* 01234567890123456789 */
                                  /* 0         1          */
  static const constfs_file_t _files[] = {
      {
          .path = "/test.txt",
          .data = str_data,
          .size = sizeof(str_data),
      },
      {
          .path = "/data.bin",
          .data = bin_data,
          .size = sizeof(bin_data),
      },
  };
  
  static const constfs_t fs_data = {
      .files = _files,
      .nfiles = sizeof(_files) / sizeof(_files[0]),
  };
  
  static vfs_mount_t _test_vfs_mount_invalid_mount = {
      .mount_point = "test",
      .fs = &constfs_file_system,
      .private_data = (void *)&fs_data,
  };
  
  static vfs_mount_t _test_vfs_mount = {
      .mount_point = "/test",
      .fs = &constfs_file_system,
      .private_data = (void *)&fs_data,
  };
  
  static void test_vfs_mount_umount(void)
  {
      int res;
      res = vfs_mount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
      res = vfs_umount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  }
  
  static void test_vfs_mount__invalid(void)
  {
      int res;
      res = vfs_mount(NULL);
      TEST_ASSERT(res < 0);
  
      res = vfs_mount(&_test_vfs_mount_invalid_mount);
      TEST_ASSERT(res < 0);
  }
  
  static void test_vfs_umount__invalid_mount(void)
  {
      int res;
      res = vfs_umount(NULL);
      TEST_ASSERT(res < 0);
      res = vfs_umount(&_test_vfs_mount);
      TEST_ASSERT(res < 0);
  }
  
  static void test_vfs_constfs_open(void)
  {
      int res;
      res = vfs_mount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      int fd;
      fd = vfs_open("/test/notfound", O_RDONLY, 0);
      TEST_ASSERT(fd == -ENOENT);
      if (fd >= 0) {
          vfs_close(fd);
      }
      fd = vfs_open("/test/test.txt", O_WRONLY, 0);
      TEST_ASSERT(fd == -EROFS);
      if (fd >= 0) {
          vfs_close(fd);
      }
      fd = vfs_open("/test/test.txt", O_RDWR, 0);
      TEST_ASSERT(fd == -EROFS);
      if (fd >= 0) {
          vfs_close(fd);
      }
      fd = vfs_open("/test/test.txt", O_RDONLY, 0);
      TEST_ASSERT(fd >= 0);
      if (fd >= 0) {
          res = vfs_close(fd);
          TEST_ASSERT_EQUAL_INT(0, res);
      }
  
      res = vfs_umount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  }
  
  static void test_vfs_constfs_read_lseek(void)
  {
      int res;
      res = vfs_mount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      int fd = vfs_open("/test/test.txt", O_RDONLY, 0);
      TEST_ASSERT(fd >= 0);
  
      char strbuf[64];
      memset(strbuf, '\0', sizeof(strbuf));
      ssize_t nbytes;
      nbytes = vfs_read(fd, strbuf, sizeof(strbuf));
      TEST_ASSERT_EQUAL_INT(sizeof(str_data), nbytes);
      TEST_ASSERT_EQUAL_STRING((const char *)&str_data[0], (const char *)&strbuf[0]);
  
      off_t pos;
      /* lseek to the middle */
      memset(strbuf, '\0', sizeof(strbuf));
      pos = vfs_lseek(fd, sizeof(str_data) / 2, SEEK_SET);
      TEST_ASSERT_EQUAL_INT(sizeof(str_data) / 2, pos);
      nbytes = vfs_read(fd, strbuf, sizeof(strbuf));
      TEST_ASSERT_EQUAL_INT((sizeof(str_data) + 1) / 2, nbytes); /* + 1 for rounding up */
      TEST_ASSERT_EQUAL_STRING((const char *)&str_data[sizeof(str_data) / 2], (const char *)&strbuf[0]);
  
      /* lseek to near the end */
      memset(strbuf, '\0', sizeof(strbuf));
      pos = vfs_lseek(fd, -1, SEEK_END);
      TEST_ASSERT_EQUAL_INT(sizeof(str_data) - 1, pos);
      nbytes = vfs_read(fd, strbuf, sizeof(strbuf));
      TEST_ASSERT_EQUAL_INT(1, nbytes);
      TEST_ASSERT_EQUAL_STRING((const char *)&str_data[sizeof(str_data) - 1], (const char *)&strbuf[0]);
  
      res = vfs_fcntl(fd, F_GETFL, 0);
      TEST_ASSERT_EQUAL_INT(O_RDONLY, res);
  
      res = vfs_close(fd);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      res = vfs_umount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  }
  
  #if MODULE_NEWLIB || defined(BOARD_NATIVE)
  static void test_vfs_constfs__posix(void)
  {
      int res;
      res = vfs_mount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      int fd = open("/test/test.txt", O_RDONLY, 0);
      TEST_ASSERT(fd >= 0);
  
      char strbuf[64];
      memset(strbuf, '\0', sizeof(strbuf));
      ssize_t nbytes;
      nbytes = read(fd, strbuf, sizeof(strbuf));
      TEST_ASSERT_EQUAL_INT(sizeof(str_data), nbytes);
      TEST_ASSERT_EQUAL_STRING((const char *)&str_data[0], (const char *)&strbuf[0]);
  
  #if HAVE_FCNTL
      /* fcntl support is optional in newlib */
      res = fcntl(fd, F_GETFL, 0);
      TEST_ASSERT_EQUAL_INT(O_RDONLY, res);
  #endif
  
      res = close(fd);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      res = vfs_umount(&_test_vfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  }
  #endif
  
  Test *tests_vfs_mount_constfs_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_vfs_mount_umount),
          new_TestFixture(test_vfs_mount__invalid),
          new_TestFixture(test_vfs_umount__invalid_mount),
          new_TestFixture(test_vfs_constfs_open),
          new_TestFixture(test_vfs_constfs_read_lseek),
  #if MODULE_NEWLIB || defined(BOARD_NATIVE)
          new_TestFixture(test_vfs_constfs__posix),
  #endif
      };
  
      EMB_UNIT_TESTCALLER(vfs_mount_tests, NULL, NULL, fixtures);
  
      return (Test *)&vfs_mount_tests;
  }
  
  /** @} */