src/util/test/vfs.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/test/suite.h"
7
8#include "util/vfs.h"
9
10#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
11M_TEST_DEFINE(openNullPathR) {
12 struct VFile* vf = VFileOpen(NULL, O_RDONLY);
13 assert_null(vf);
14}
15
16M_TEST_DEFINE(openNullPathW) {
17 struct VFile* vf = VFileOpen(NULL, O_WRONLY);
18 assert_null(vf);
19}
20
21M_TEST_DEFINE(openNullPathCreate) {
22 struct VFile* vf = VFileOpen(NULL, O_CREAT);
23 assert_null(vf);
24}
25
26M_TEST_DEFINE(openNullPathWCreate) {
27 struct VFile* vf = VFileOpen(NULL, O_WRONLY | O_CREAT);
28 assert_null(vf);
29}
30#endif
31
32M_TEST_DEFINE(openNullMem0) {
33 struct VFile* vf = VFileFromMemory(NULL, 0);
34 assert_null(vf);
35}
36
37M_TEST_DEFINE(openNullMemNonzero) {
38 struct VFile* vf = VFileFromMemory(NULL, 32);
39 assert_null(vf);
40}
41
42M_TEST_DEFINE(openNullConstMem0) {
43 struct VFile* vf = VFileFromConstMemory(NULL, 0);
44 assert_null(vf);
45}
46
47M_TEST_DEFINE(openNullConstMemNonzero) {
48 struct VFile* vf = VFileFromConstMemory(NULL, 32);
49 assert_null(vf);
50}
51
52M_TEST_DEFINE(openNullMemChunk0) {
53 struct VFile* vf = VFileMemChunk(NULL, 0);
54 assert_non_null(vf);
55 assert_int_equal(vf->size(vf), 0);
56 vf->close(vf);
57}
58
59M_TEST_DEFINE(openNonNullMemChunk0) {
60 const uint8_t bytes[32] = {};
61 struct VFile* vf = VFileMemChunk(bytes, 0);
62 assert_non_null(vf);
63 assert_int_equal(vf->size(vf), 0);
64 vf->close(vf);
65}
66
67M_TEST_DEFINE(openNullMemChunkNonzero) {
68 struct VFile* vf = VFileMemChunk(NULL, 32);
69 assert_non_null(vf);
70 assert_int_equal(vf->size(vf), 32);
71 vf->close(vf);
72}
73
74M_TEST_DEFINE(resizeMem) {
75 uint8_t bytes[32];
76 struct VFile* vf = VFileFromMemory(bytes, 32);
77 assert_non_null(vf);
78 assert_int_equal(vf->size(vf), 32);
79 vf->truncate(vf, 64);
80 assert_int_equal(vf->size(vf), 32);
81 vf->truncate(vf, 16);
82 assert_int_equal(vf->size(vf), 32);
83 vf->close(vf);
84}
85
86M_TEST_DEFINE(resizeConstMem) {
87 uint8_t bytes[32];
88 struct VFile* vf = VFileFromConstMemory(bytes, 32);
89 assert_non_null(vf);
90 assert_int_equal(vf->size(vf), 32);
91 vf->truncate(vf, 64);
92 assert_int_equal(vf->size(vf), 32);
93 vf->truncate(vf, 16);
94 assert_int_equal(vf->size(vf), 32);
95 vf->close(vf);
96}
97
98M_TEST_DEFINE(resizeMemChunk) {
99 uint8_t bytes[32];
100 struct VFile* vf = VFileMemChunk(bytes, 32);
101 assert_non_null(vf);
102 assert_int_equal(vf->size(vf), 32);
103 vf->truncate(vf, 64);
104 assert_int_equal(vf->size(vf), 64);
105 vf->truncate(vf, 16);
106 assert_int_equal(vf->size(vf), 16);
107 vf->close(vf);
108}
109
110M_TEST_DEFINE(mapMem) {
111 uint8_t bytes[32] = "Test Pattern";
112 struct VFile* vf = VFileFromMemory(bytes, 32);
113 assert_non_null(vf);
114 void* mapped = vf->map(vf, 32, MAP_READ);
115 assert_non_null(mapped);
116 assert_ptr_equal(mapped, &bytes);
117 vf->unmap(vf, mapped, 32);
118 vf->close(vf);
119}
120
121M_TEST_DEFINE(mapConstMem) {
122 uint8_t bytes[32] = "Test Pattern";
123 struct VFile* vf = VFileFromConstMemory(bytes, 32);
124 assert_non_null(vf);
125 void* mapped = vf->map(vf, 32, MAP_READ);
126 assert_non_null(mapped);
127 assert_ptr_equal(mapped, &bytes);
128 vf->unmap(vf, mapped, 32);
129 vf->close(vf);
130}
131
132M_TEST_DEFINE(mapMemChunk) {
133 uint8_t bytes[32] = "Test Pattern";
134 struct VFile* vf = VFileMemChunk(bytes, 32);
135 assert_non_null(vf);
136 void* mapped = vf->map(vf, 32, MAP_READ);
137 assert_non_null(mapped);
138 assert_ptr_not_equal(mapped, &bytes);
139 assert_memory_equal(mapped, &bytes, 32);
140 vf->write(vf, bytes, sizeof(bytes));
141 void* mapped2 = vf->map(vf, 32, MAP_READ);
142 assert_ptr_equal(mapped, mapped2);
143 vf->unmap(vf, mapped, 32);
144 vf->unmap(vf, mapped2, 32);
145 vf->close(vf);
146}
147
148M_TEST_SUITE_DEFINE(VFS,
149#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
150 cmocka_unit_test(openNullPathR),
151 cmocka_unit_test(openNullPathW),
152 cmocka_unit_test(openNullPathCreate),
153 cmocka_unit_test(openNullPathWCreate),
154#endif
155 cmocka_unit_test(openNullMem0),
156 cmocka_unit_test(openNullMemNonzero),
157 cmocka_unit_test(openNullConstMem0),
158 cmocka_unit_test(openNullConstMemNonzero),
159 cmocka_unit_test(openNullMemChunk0),
160 cmocka_unit_test(openNonNullMemChunk0),
161 cmocka_unit_test(openNullMemChunkNonzero),
162 cmocka_unit_test(resizeMem),
163 cmocka_unit_test(resizeConstMem),
164 cmocka_unit_test(resizeMemChunk),
165 cmocka_unit_test(mapMem),
166 cmocka_unit_test(mapConstMem),
167 cmocka_unit_test(mapMemChunk))