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

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

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

Powered by Google App Engine
This is Rietveld 408576698