Blame view

RIOT/tests/unittests/tests-devfs/tests-devfs.c 3.71 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
  /*
   * 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 DevFS
   *
   * @author      Joakim Nohlgård <joakim.nohlgard@eistec.se>
   */
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  
  #include "fs/devfs.h"
  
  #include "embUnit/embUnit.h"
  
  #include "tests-devfs.h"
  
  static int _mock_open(vfs_file_t *filp, const char *name, int flags, mode_t mode, const char *abs_path);
  static ssize_t _mock_read(vfs_file_t *filp, void *dest, size_t nbytes);
  static ssize_t _mock_write(vfs_file_t *filp, const void *src, size_t nbytes);
  
  static volatile int _mock_open_calls = 0;
  static volatile int _mock_read_calls = 0;
  static volatile int _mock_write_calls = 0;
  
  static int _mock_private_data;
  
  static const vfs_file_ops_t _mock_devfs_ops = {
      .open = _mock_open,
      .read = _mock_read,
      .write = _mock_write,
  };
  
  static int _mock_private_data_tag = 4321;
  
  static devfs_t _mock_devfs_node = {
      .path = "/mock0",
      .f_op = &_mock_devfs_ops,
      .private_data = &_mock_private_data_tag,
  };
  
  static vfs_mount_t _test_devfs_mount = {
      .fs = &devfs_file_system,
      .mount_point = "/test",
      .private_data = &_mock_private_data,
  };
  
  static int _mock_open(vfs_file_t *filp, const char *name, int flags, mode_t mode, const char *abs_path)
  {
      (void) name;
      (void) flags;
      (void) mode;
      (void) abs_path;
      if (filp->private_data.ptr != &_mock_private_data_tag) {
          return -4321;
      }
      int *np = filp->mp->private_data;
      ++(*np);
      ++_mock_open_calls;
      return 0;
  }
  
  static ssize_t _mock_read(vfs_file_t *filp, void *dest, size_t nbytes)
  {
      (void) dest;
      (void) nbytes;
      if (filp->private_data.ptr != &_mock_private_data_tag) {
          return -4321;
      }
      int *np = filp->mp->private_data;
      ++(*np);
      ++_mock_read_calls;
      return 0;
  }
  
  static ssize_t _mock_write(vfs_file_t *filp, const void *src, size_t nbytes)
  {
      (void) src;
      (void) nbytes;
      if (filp->private_data.ptr != &_mock_private_data_tag) {
          return -4321;
      }
      int *np = filp->mp->private_data;
      ++(*np);
      ++_mock_write_calls;
      return 0;
  }
  
  static void test_devfs_register(void)
  {
      int res = devfs_register(NULL);
      TEST_ASSERT(res < 0);
  
      res = devfs_register(&_mock_devfs_node);
      TEST_ASSERT(res == 0);
  
      res = devfs_register(&_mock_devfs_node);
      TEST_ASSERT(res < 0);
  
      res = devfs_unregister(&_mock_devfs_node);
      TEST_ASSERT(res == 0);
  
      res = devfs_unregister(&_mock_devfs_node);
      TEST_ASSERT(res < 0);
  }
  
  static void test_devfs_mount_open(void)
  {
      _mock_private_data = 12345;
      int res;
      res = vfs_mount(&_test_devfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(_mock_private_data, 12345);
  
      res = devfs_register(&_mock_devfs_node);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      int count = _mock_open_calls;
      int fd = vfs_open("/test/mock0", O_RDWR, 0);
      TEST_ASSERT(fd >= 0);
      TEST_ASSERT_EQUAL_INT(count + 1, _mock_open_calls);
      TEST_ASSERT_EQUAL_INT(_mock_private_data, 12346);
  
      res = vfs_close(fd);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      res = devfs_unregister(&_mock_devfs_node);
      TEST_ASSERT_EQUAL_INT(0, res);
  
      res = vfs_umount(&_test_devfs_mount);
      TEST_ASSERT_EQUAL_INT(0, res);
  }
  
  Test *tests_devfs_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_devfs_register),
          new_TestFixture(test_devfs_mount_open),
      };
  
      EMB_UNIT_TESTCALLER(devfs_tests, NULL, NULL, fixtures);
  
      return (Test *)&devfs_tests;
  }
  
  void tests_devfs(void)
  {
      TESTS_RUN(tests_devfs_tests());
  }
  /** @} */