Blame view

RIOT/tests/unittests/tests-vfs/tests-vfs-normalize_path.c 4.72 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
  /*
   * 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_normalize_path
   *
   * @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 void test_vfs_normalize_path__noop(void)
  {
      static const char path[] = "/this/is/a/test";
      char buf[16];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT_EQUAL_INT(4, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&path[0], (const char *)&buf[0]);
  }
  
  static void test_vfs_normalize_path__slashes(void)
  {
      static const char path[] = "///////////////////////////////";
      static const char expected[] = "/";
      char buf[4];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT_EQUAL_INT(1, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&expected[0], (const char *)&buf[0]);
  }
  
  static void test_vfs_normalize_path__dot(void)
  {
      static const char path[] = "/abc/./def/././zxcv././.";
      static const char expected[] = "/abc/def/zxcv.";
      char buf[16];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT_EQUAL_INT(3, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&expected[0], (const char *)&buf[0]);
  }
  
  static void test_vfs_normalize_path__reduce(void)
  {
      static const char path[] = "/abc/../def";
      static const char expected[] = "/def";
      char buf[16];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT_EQUAL_INT(1, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&expected[0], (const char *)&buf[0]);
  }
  
  static void test_vfs_normalize_path__trailing(void)
  {
      static const char path[] = "/mydir/";
      static const char expected[] = "/mydir/";
      char buf[16];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT_EQUAL_INT(1, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&expected[0], (const char *)&buf[0]);
  }
  
  static void test_vfs_normalize_path__outside(void)
  {
      static const char path[] = "/somewhere/../..";
      static const char path2[] = "/../abdgh";
      char buf[16];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT(res < 0);
      res = vfs_normalize_path(buf, path2, sizeof(buf));
      TEST_ASSERT(res < 0);
  }
  
  static void test_vfs_normalize_path__toolong(void)
  {
      static const char path[] = "/abc";
      char buf[4];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT(res < 0);
  }
  
  static void test_vfs_normalize_path__shorten(void)
  {
  #if 0
      /* Not supported by the current implementation */
      /* The current implementation needs enough buffer space to store the longest
       * prefix path before each ../ reduction */
      static const char path[] = "/qwerty/asdfghjkl/..";
      static const char expected[] = "/qwerty";
      char buf[8];
  #endif
      static const char path[] = "/12345/6789/..";
      static const char expected[] = "/12345";
      char buf[12];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT_EQUAL_INT(1, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&expected[0], (const char *)&buf[0]);
  }
  
  static void test_vfs_normalize_path__shorten_inplace(void)
  {
      char path[] = "/qwerty/asdfghjkl/..";
      static const char expected[] = "/qwerty";
      int res = vfs_normalize_path(path, path, sizeof(path));
      TEST_ASSERT_EQUAL_INT(1, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&expected[0], (const char *)&path[0]);
  }
  
  static void test_vfs_normalize_path__empty(void)
  {
      char path[] = "";
      static const char expected[] = "";
      char buf[4];
      int res = vfs_normalize_path(buf, path, sizeof(buf));
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_STRING((const char *)&expected[0], (const char *)&path[0]);
  }
  
  Test *tests_vfs_normalize_path_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_vfs_normalize_path__noop),
          new_TestFixture(test_vfs_normalize_path__slashes),
          new_TestFixture(test_vfs_normalize_path__dot),
          new_TestFixture(test_vfs_normalize_path__reduce),
          new_TestFixture(test_vfs_normalize_path__trailing),
          new_TestFixture(test_vfs_normalize_path__outside),
          new_TestFixture(test_vfs_normalize_path__toolong),
          new_TestFixture(test_vfs_normalize_path__shorten),
          new_TestFixture(test_vfs_normalize_path__shorten_inplace),
          new_TestFixture(test_vfs_normalize_path__empty),
      };
  
      EMB_UNIT_TESTCALLER(vfs_normalize_path_tests, NULL, NULL, fixtures);
  
      return (Test *)&vfs_normalize_path_tests;
  }
  /** @} */