Blame view

RIOT/tests/unittests/tests-vfs/tests-vfs-bind.c 3.25 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
  /*
   * 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_bind
   *
   * @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 "embUnit/embUnit.h"
  
  #include "vfs.h"
  
  #include "tests-vfs.h"
  
  static const uint8_t str_data[] = "This is a test file";
                                  /* 01234567890123456789 */
                                  /* 0         1          */
  
  #define _VFS_TEST_BIND_BUFSIZE 8
  
  static ssize_t _mock_write(vfs_file_t *filp, const void *src, size_t nbytes);
  static ssize_t _mock_read(vfs_file_t *filp, void *dest, size_t nbytes);
  
  static volatile int _mock_write_calls = 0;
  static volatile int _mock_read_calls = 0;
  
  static vfs_file_ops_t _test_bind_ops = {
      .read = _mock_read,
      .write = _mock_write,
  };
  
  static ssize_t _mock_write(vfs_file_t *filp, const void *src, size_t nbytes)
  {
      void *dest = filp->private_data.ptr;
      ++_mock_write_calls;
      if (nbytes > _VFS_TEST_BIND_BUFSIZE) {
          nbytes = _VFS_TEST_BIND_BUFSIZE;
      }
      memcpy(dest, src, nbytes);
      return nbytes;
  }
  
  static ssize_t _mock_read(vfs_file_t *filp, void *dest, size_t nbytes)
  {
      void *src = filp->private_data.ptr;
      ++_mock_read_calls;
      if (nbytes > _VFS_TEST_BIND_BUFSIZE) {
          nbytes = _VFS_TEST_BIND_BUFSIZE;
      }
      memcpy(dest, src, nbytes);
      return nbytes;
  }
  
  static void test_vfs_bind(void)
  {
      int fd;
      uint8_t buf[_VFS_TEST_BIND_BUFSIZE];
      fd = vfs_bind(VFS_ANY_FD, O_RDWR, &_test_bind_ops, &buf[0]);
      TEST_ASSERT(fd >= 0);
      if (fd < 0) {
          return;
      }
  
      ssize_t nbytes;
      int ncalls = _mock_write_calls;
      nbytes = vfs_write(fd, &str_data[0], sizeof(str_data));
      TEST_ASSERT_EQUAL_INT(_mock_write_calls, ncalls + 1);
      TEST_ASSERT_EQUAL_INT(_VFS_TEST_BIND_BUFSIZE, nbytes);
      TEST_ASSERT_EQUAL_INT(0, memcmp(&str_data[0], &buf[0], nbytes));
  
      char strbuf[64];
      memset(strbuf, '\0', sizeof(strbuf));
      ncalls = _mock_read_calls;
      nbytes = vfs_read(fd, strbuf, sizeof(strbuf));
      TEST_ASSERT_EQUAL_INT(_mock_read_calls, ncalls + 1);
      TEST_ASSERT_EQUAL_INT(_VFS_TEST_BIND_BUFSIZE, nbytes);
      TEST_ASSERT_EQUAL_INT(0, memcmp(&buf[0], &strbuf[0], nbytes));
  
      int res = vfs_close(fd);
      TEST_ASSERT_EQUAL_INT(0, res);
  }
  
  static void test_vfs_bind__leak_fds(void)
  {
      /* This test was added after a bug was discovered in the _allocate_fd code to
       * make sure that fds are not leaked when doing multiple bind/close calls */
      /* Try >VFS_MAX_OPEN_FILES times to open and close fds to see that they are
       * not leaked */
      for (unsigned int i = 0; i < VFS_MAX_OPEN_FILES; ++i) {
          test_vfs_bind();
      }
      /* The following will fail if the fds above are not properly freed */
      test_vfs_bind();
  }
  
  Test *tests_vfs_bind_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_vfs_bind),
          new_TestFixture(test_vfs_bind__leak_fds),
      };
  
      EMB_UNIT_TESTCALLER(vfs_bind_tests, NULL, NULL, fixtures);
  
      return (Test *)&vfs_bind_tests;
  }
  
  /** @} */