Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io_test/mount_test.cc

Issue 16232016: [NaCl SDK] nacl_io: big refactor to return error value (errno). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be 2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file. 3 * found in the LICENSE file.
4 */ 4 */
5 5
6 #include <errno.h> 6 #include <errno.h>
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <string.h> 8 #include <string.h>
9 #include <string> 9 #include <string>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
11 11
12 #include "nacl_io/mount.h" 12 #include "nacl_io/mount.h"
13 #include "nacl_io/mount_dev.h" 13 #include "nacl_io/mount_dev.h"
14 #include "nacl_io/mount_mem.h" 14 #include "nacl_io/mount_mem.h"
15 #include "nacl_io/osdirent.h" 15 #include "nacl_io/osdirent.h"
16 16
17 #include "gtest/gtest.h" 17 #include "gtest/gtest.h"
18 18
19 namespace { 19 namespace {
20 20
21 class MountMemMock : public MountMem { 21 class MountMemMock : public MountMem {
22 public: 22 public:
23 MountMemMock() { 23 MountMemMock() {
24 StringMap_t map; 24 StringMap_t map;
25 Init(1, map, NULL); 25 EXPECT_EQ(0, Init(1, map, NULL));
26 }; 26 }
27 27
28 int num_nodes() { 28 int num_nodes() { return (int) inode_pool_.size(); }
29 return (int) inode_pool_.size();
30 }
31 }; 29 };
32 30
33 class MountDevMock : public MountDev { 31 class MountDevMock : public MountDev {
34 public: 32 public:
35 MountDevMock() { 33 MountDevMock() {
36 StringMap_t map; 34 StringMap_t map;
37 Init(1, map, NULL); 35 Init(1, map, NULL);
38 } 36 }
39 int num_nodes() { 37 int num_nodes() { return (int) inode_pool_.size(); }
40 return (int) inode_pool_.size();
41 }
42 }; 38 };
43 39
44 } // namespace 40 } // namespace
45 41
46 42 #define NULL_NODE ((MountNode*) NULL)
47 #define NULL_NODE ((MountNode *) NULL)
48 43
49 TEST(MountTest, Sanity) { 44 TEST(MountTest, Sanity) {
50 MountMemMock* mnt = new MountMemMock(); 45 MountMemMock* mnt = new MountMemMock();
51 MountNode* file; 46 MountNode* file;
52 MountNode* root; 47 MountNode* root;
48 MountNode* result_node;
49 size_t result_size = 0;
50 int result_bytes = 0;
53 51
54 char buf1[1024]; 52 char buf1[1024];
55 53
56 // A memory mount starts with one directory node: the root. 54 // A memory mount starts with one directory node: the root.
57 EXPECT_EQ(1, mnt->num_nodes()); 55 EXPECT_EQ(1, mnt->num_nodes());
58 56
59 // Fail to open non existent file 57 // Fail to open non existent file
60 EXPECT_EQ(NULL_NODE, mnt->Open(Path("/foo"), O_RDWR)); 58 EXPECT_EQ(ENOENT, mnt->Open(Path("/foo"), O_RDWR, &result_node));
61 EXPECT_EQ(errno, ENOENT); 59 EXPECT_EQ(NULL, result_node);
62 60
63 // Create a file 61 // Create a file
64 file = mnt->Open(Path("/foo"), O_RDWR | O_CREAT); 62 EXPECT_EQ(0, mnt->Open(Path("/foo"), O_RDWR | O_CREAT, &file));
65 EXPECT_NE(NULL_NODE, file); 63 EXPECT_NE(NULL_NODE, file);
66 if (file == NULL) return; 64 if (file == NULL)
65 return;
67 EXPECT_EQ(2, file->RefCount()); 66 EXPECT_EQ(2, file->RefCount());
68 EXPECT_EQ(2, mnt->num_nodes()); 67 EXPECT_EQ(2, mnt->num_nodes());
69 68
69 // Open the root directory for write should fail.
70 EXPECT_EQ(EISDIR, mnt->Open(Path("/"), O_RDWR, &root));
71
70 // Open the root directory 72 // Open the root directory
71 root = mnt->Open(Path("/"), O_RDWR); 73 EXPECT_EQ(0, mnt->Open(Path("/"), O_RDONLY, &root));
72 EXPECT_NE(NULL_NODE, root); 74 EXPECT_NE(NULL_NODE, root);
73 if (NULL != root) { 75 if (NULL != root) {
74 struct dirent dirs[2]; 76 struct dirent dirs[2];
75 int len = root->GetDents(0, dirs, sizeof(dirs)); 77 int len;
78 EXPECT_EQ(0, root->GetDents(0, dirs, sizeof(dirs), &len));
76 EXPECT_EQ(sizeof(struct dirent), len); 79 EXPECT_EQ(sizeof(struct dirent), len);
77 } 80 }
78 81
79 // Fail to re-create the same file 82 // Fail to re-create the same file
80 EXPECT_EQ(NULL_NODE, mnt->Open(Path("/foo"), O_RDWR | O_CREAT | O_EXCL)); 83 EXPECT_EQ(EEXIST,
81 EXPECT_EQ(errno, EEXIST); 84 mnt->Open(Path("/foo"), O_RDWR | O_CREAT | O_EXCL, &result_node));
85 EXPECT_EQ(NULL_NODE, result_node);
82 EXPECT_EQ(2, mnt->num_nodes()); 86 EXPECT_EQ(2, mnt->num_nodes());
83 87
84 // Fail to create a directory with the same name 88 // Fail to create a directory with the same name
85 EXPECT_EQ(-1, mnt->Mkdir(Path("/foo"), O_RDWR)); 89 EXPECT_EQ(EEXIST, mnt->Mkdir(Path("/foo"), O_RDWR));
86 EXPECT_EQ(errno, EEXIST);
87 90
88 // Attempt to READ/WRITE 91 // Attempt to READ/WRITE
89 EXPECT_EQ(0, file->GetSize()); 92 EXPECT_EQ(0, file->GetSize(&result_size));
90 EXPECT_EQ(sizeof(buf1), file->Write(0, buf1, sizeof(buf1))); 93 EXPECT_EQ(0, result_size);
91 EXPECT_EQ(sizeof(buf1), file->GetSize()); 94 EXPECT_EQ(0, file->Write(0, buf1, sizeof(buf1), &result_bytes));
92 EXPECT_EQ(sizeof(buf1), file->Read(0, buf1, sizeof(buf1))); 95 EXPECT_EQ(sizeof(buf1), result_bytes);
96 EXPECT_EQ(0, file->GetSize(&result_size));
97 EXPECT_EQ(sizeof(buf1), result_size);
98 EXPECT_EQ(0, file->Read(0, buf1, sizeof(buf1), &result_bytes));
99 EXPECT_EQ(sizeof(buf1), result_bytes);
93 100
94 // Attempt to open the same file 101 // Attempt to open the same file
95 EXPECT_EQ(file, mnt->Open(Path("/foo"), O_RDWR | O_CREAT)); 102 EXPECT_EQ(0, mnt->Open(Path("/foo"), O_RDWR | O_CREAT, &result_node));
96 EXPECT_EQ(sizeof(buf1), file->GetSize()); 103 EXPECT_EQ(file, result_node);
104 EXPECT_EQ(0, file->GetSize(&result_size));
105 EXPECT_EQ(sizeof(buf1), result_size);
97 EXPECT_EQ(3, file->RefCount()); 106 EXPECT_EQ(3, file->RefCount());
98 EXPECT_EQ(2, mnt->num_nodes()); 107 EXPECT_EQ(2, mnt->num_nodes());
99 108
100 // Attempt to close and delete the file 109 // Attempt to close and delete the file
101 mnt->ReleaseNode(file); 110 mnt->ReleaseNode(file);
102 EXPECT_EQ(2, mnt->num_nodes()); 111 EXPECT_EQ(2, mnt->num_nodes());
103 EXPECT_EQ(0, mnt->Unlink(Path("/foo"))); 112 EXPECT_EQ(0, mnt->Unlink(Path("/foo")));
104 EXPECT_EQ(2, mnt->num_nodes()); 113 EXPECT_EQ(2, mnt->num_nodes());
105 EXPECT_EQ(-1, mnt->Unlink(Path("/foo"))); 114 EXPECT_EQ(ENOENT, mnt->Unlink(Path("/foo")));
106 EXPECT_EQ(2, mnt->num_nodes()); 115 EXPECT_EQ(2, mnt->num_nodes());
107 EXPECT_EQ(errno, ENOENT);
108 mnt->ReleaseNode(file); 116 mnt->ReleaseNode(file);
109 EXPECT_EQ(1, mnt->num_nodes()); 117 EXPECT_EQ(1, mnt->num_nodes());
110 118
111 // Recreate foo as a directory 119 // Recreate foo as a directory
112 EXPECT_EQ(0, mnt->Mkdir(Path("/foo"), O_RDWR)); 120 EXPECT_EQ(0, mnt->Mkdir(Path("/foo"), O_RDWR));
113 121
114 // Create a file (exclusively) 122 // Create a file (exclusively)
115 file = mnt->Open(Path("/foo/bar"), O_RDWR | O_CREAT | O_EXCL); 123 EXPECT_EQ(0, mnt->Open(Path("/foo/bar"), O_RDWR | O_CREAT | O_EXCL, &file));
116 EXPECT_NE(NULL_NODE, file); 124 EXPECT_NE(NULL_NODE, file);
117 if (NULL == file) return; 125 if (NULL == file)
126 return;
118 127
119 // Attempt to delete the directory 128 // Attempt to delete the directory
120 EXPECT_EQ(-1, mnt->Rmdir(Path("/foo"))); 129 EXPECT_EQ(ENOTEMPTY, mnt->Rmdir(Path("/foo")));
121 EXPECT_EQ(errno, ENOTEMPTY);
122 130
123 // Unlink the file, then delete the directory 131 // Unlink the file, then delete the directory
124 EXPECT_EQ(0, mnt->Unlink(Path("/foo/bar"))); 132 EXPECT_EQ(0, mnt->Unlink(Path("/foo/bar")));
125 EXPECT_EQ(0, mnt->Rmdir(Path("/foo"))); 133 EXPECT_EQ(0, mnt->Rmdir(Path("/foo")));
126 EXPECT_EQ(2, mnt->num_nodes()); 134 EXPECT_EQ(2, mnt->num_nodes());
127 mnt->ReleaseNode(file); 135 mnt->ReleaseNode(file);
128 EXPECT_EQ(1, mnt->num_nodes()); 136 EXPECT_EQ(1, mnt->num_nodes());
129 137
130 // Verify the directory is gone 138 // Verify the directory is gone
131 file = mnt->Open(Path("/foo"), O_RDWR); 139 EXPECT_EQ(ENOENT, mnt->Open(Path("/foo"), O_RDWR, &file));
132 EXPECT_EQ(NULL_NODE, file); 140 EXPECT_EQ(NULL_NODE, file);
133 EXPECT_EQ(errno, ENOENT);
134 } 141 }
135 142
136 TEST(MountTest, MemMountRemove) { 143 TEST(MountTest, MemMountRemove) {
137 MountMemMock* mnt = new MountMemMock(); 144 MountMemMock* mnt = new MountMemMock();
138 MountNode* file; 145 MountNode* file;
146 MountNode* result_node;
139 147
140 EXPECT_EQ(0, mnt->Mkdir(Path("/dir"), O_RDWR)); 148 EXPECT_EQ(0, mnt->Mkdir(Path("/dir"), O_RDWR));
141 file = mnt->Open(Path("/file"), O_RDWR | O_CREAT | O_EXCL); 149 EXPECT_EQ(0, mnt->Open(Path("/file"), O_RDWR | O_CREAT | O_EXCL, &file));
142 EXPECT_NE(NULL_NODE, file); 150 EXPECT_NE(NULL_NODE, file);
143 mnt->ReleaseNode(file); 151 mnt->ReleaseNode(file);
144 152
145 EXPECT_EQ(0, mnt->Remove(Path("/dir"))); 153 EXPECT_EQ(0, mnt->Remove(Path("/dir")));
146 EXPECT_EQ(0, mnt->Remove(Path("/file"))); 154 EXPECT_EQ(0, mnt->Remove(Path("/file")));
147 155
148 EXPECT_EQ(NULL_NODE, mnt->Open(Path("/dir/foo"), O_CREAT | O_RDWR)); 156 EXPECT_EQ(ENOENT,
149 EXPECT_EQ(ENOENT, errno); 157 mnt->Open(Path("/dir/foo"), O_CREAT | O_RDWR, &result_node));
150 EXPECT_EQ(NULL_NODE, mnt->Open(Path("/file"), O_RDONLY)); 158 EXPECT_EQ(NULL_NODE, result_node);
151 EXPECT_EQ(ENOENT, errno); 159 EXPECT_EQ(ENOENT, mnt->Open(Path("/file"), O_RDONLY, &result_node));
160 EXPECT_EQ(NULL_NODE, result_node);
152 } 161 }
153 162
154 TEST(MountTest, DevNull) { 163 TEST(MountTest, DevNull) {
155 MountDevMock* mnt = new MountDevMock(); 164 MountDevMock* mnt = new MountDevMock();
156 MountNode* dev_null = mnt->Open(Path("/null"), O_RDWR); 165 MountNode* dev_null = NULL;
166 int result_bytes = 0;
167
168 ASSERT_EQ(0, mnt->Open(Path("/null"), O_RDWR, &dev_null));
157 ASSERT_NE(NULL_NODE, dev_null); 169 ASSERT_NE(NULL_NODE, dev_null);
158 170
159 // Writing to /dev/null should write everything. 171 // Writing to /dev/null should write everything.
160 const char msg[] = "Dummy test message."; 172 const char msg[] = "Dummy test message.";
161 EXPECT_EQ(strlen(msg), dev_null->Write(0, &msg[0], strlen(msg))); 173 EXPECT_EQ(0, dev_null->Write(0, &msg[0], strlen(msg), &result_bytes));
174 EXPECT_EQ(strlen(msg), result_bytes);
162 175
163 // Reading from /dev/null should read nothing. 176 // Reading from /dev/null should read nothing.
164 const int kBufferLength = 100; 177 const int kBufferLength = 100;
165 char buffer[kBufferLength]; 178 char buffer[kBufferLength];
166 EXPECT_EQ(0, dev_null->Read(0, &buffer[0], kBufferLength)); 179 EXPECT_EQ(0, dev_null->Read(0, &buffer[0], kBufferLength, &result_bytes));
180 EXPECT_EQ(0, result_bytes);
167 mnt->ReleaseNode(dev_null); 181 mnt->ReleaseNode(dev_null);
168 } 182 }
169 183
170 TEST(MountTest, DevZero) { 184 TEST(MountTest, DevZero) {
171 MountDevMock* mnt = new MountDevMock(); 185 MountDevMock* mnt = new MountDevMock();
172 MountNode* dev_zero = mnt->Open(Path("/zero"), O_RDWR); 186 MountNode* dev_zero = NULL;
187 int result_bytes = 0;
188
189 ASSERT_EQ(0, mnt->Open(Path("/zero"), O_RDWR, &dev_zero));
173 ASSERT_NE(NULL_NODE, dev_zero); 190 ASSERT_NE(NULL_NODE, dev_zero);
174 191
175 // Writing to /dev/zero should write everything. 192 // Writing to /dev/zero should write everything.
176 const char msg[] = "Dummy test message."; 193 const char msg[] = "Dummy test message.";
177 EXPECT_EQ(strlen(msg), dev_zero->Write(0, &msg[0], strlen(msg))); 194 EXPECT_EQ(0, dev_zero->Write(0, &msg[0], strlen(msg), &result_bytes));
195 EXPECT_EQ(strlen(msg), result_bytes);
178 196
179 // Reading from /dev/zero should read all zeroes. 197 // Reading from /dev/zero should read all zeroes.
180 const int kBufferLength = 100; 198 const int kBufferLength = 100;
181 char buffer[kBufferLength]; 199 char buffer[kBufferLength];
182 // First fill with all 1s. 200 // First fill with all 1s.
183 memset(&buffer[0], 0x1, kBufferLength); 201 memset(&buffer[0], 0x1, kBufferLength);
184 EXPECT_EQ(kBufferLength, dev_zero->Read(0, &buffer[0], kBufferLength)); 202 EXPECT_EQ(0, dev_zero->Read(0, &buffer[0], kBufferLength, &result_bytes));
203 EXPECT_EQ(kBufferLength, result_bytes);
185 204
186 char zero_buffer[kBufferLength]; 205 char zero_buffer[kBufferLength];
187 memset(&zero_buffer[0], 0, kBufferLength); 206 memset(&zero_buffer[0], 0, kBufferLength);
188 EXPECT_EQ(0, memcmp(&buffer[0], &zero_buffer[0], kBufferLength)); 207 EXPECT_EQ(0, memcmp(&buffer[0], &zero_buffer[0], kBufferLength));
189 mnt->ReleaseNode(dev_zero); 208 mnt->ReleaseNode(dev_zero);
190 } 209 }
191 210
192 TEST(MountTest, DevUrandom) { 211 TEST(MountTest, DevUrandom) {
193 MountDevMock* mnt = new MountDevMock(); 212 MountDevMock* mnt = new MountDevMock();
194 MountNode* dev_urandom = mnt->Open(Path("/urandom"), O_RDWR); 213 MountNode* dev_urandom = NULL;
214 int result_bytes = 0;
215
216 ASSERT_EQ(0, mnt->Open(Path("/urandom"), O_RDWR, &dev_urandom));
195 ASSERT_NE(NULL_NODE, dev_urandom); 217 ASSERT_NE(NULL_NODE, dev_urandom);
196 218
197 // Writing to /dev/zero should write everything. 219 // Writing to /dev/urandom should write everything.
198 const char msg[] = "Dummy test message."; 220 const char msg[] = "Dummy test message.";
199 EXPECT_EQ(strlen(msg), dev_urandom->Write(0, &msg[0], strlen(msg))); 221 EXPECT_EQ(0, dev_urandom->Write(0, &msg[0], strlen(msg), &result_bytes));
222 EXPECT_EQ(strlen(msg), result_bytes);
200 223
201 // Reading from /dev/urandom should read random bytes. 224 // Reading from /dev/urandom should read random bytes.
202 const int kSampleBatches = 1000; 225 const int kSampleBatches = 1000;
203 const int kSampleBatchSize = 1000; 226 const int kSampleBatchSize = 1000;
204 const int kTotalSamples = kSampleBatches * kSampleBatchSize; 227 const int kTotalSamples = kSampleBatches * kSampleBatchSize;
205 228
206 int byte_count[256] = {0}; 229 int byte_count[256] = {0};
207 230
208 unsigned char buffer[kSampleBatchSize]; 231 unsigned char buffer[kSampleBatchSize];
209 for (int batch = 0; batch < kSampleBatches; ++batch) { 232 for (int batch = 0; batch < kSampleBatches; ++batch) {
210 int bytes_read = dev_urandom->Read(0, &buffer[0], kSampleBatchSize); 233 int bytes_read = 0;
234 EXPECT_EQ(0,
235 dev_urandom->Read(0, &buffer[0], kSampleBatchSize, &bytes_read));
211 EXPECT_EQ(kSampleBatchSize, bytes_read); 236 EXPECT_EQ(kSampleBatchSize, bytes_read);
212 237
213 for (int i = 0; i < bytes_read; ++i) { 238 for (int i = 0; i < bytes_read; ++i) {
214 byte_count[buffer[i]]++; 239 byte_count[buffer[i]]++;
215 } 240 }
216 } 241 }
217 242
218 double expected_count = kTotalSamples / 256.; 243 double expected_count = kTotalSamples / 256.;
219 double chi_squared = 0; 244 double chi_squared = 0;
220 for (int i = 0; i < 256; ++i) { 245 for (int i = 0; i < 256; ++i) {
221 double difference = byte_count[i] - expected_count; 246 double difference = byte_count[i] - expected_count;
222 chi_squared += difference * difference / expected_count; 247 chi_squared += difference * difference / expected_count;
223 } 248 }
224 249
225 // Approximate chi-squared value for p-value 0.05, 255 degrees-of-freedom. 250 // Approximate chi-squared value for p-value 0.05, 255 degrees-of-freedom.
226 EXPECT_LE(chi_squared, 293.24); 251 EXPECT_LE(chi_squared, 293.24);
227 } 252 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698