summaryrefslogtreecommitdiffstats
path: root/sql/mojo/vfs_unittest.cc
blob: e51f1bd1e11531a51b6831d875852c1a96ebb1f9 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>
#include <memory>
#include <utility>

#include "base/macros.h"
#include "components/filesystem/public/interfaces/file_system.mojom.h"
#include "mojo/shell/public/cpp/application_impl.h"
#include "mojo/shell/public/cpp/application_test_base.h"
#include "mojo/util/capture_util.h"
#include "sql/mojo/mojo_vfs.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/sqlite/sqlite3.h"

namespace std {

// This deleter lets us be safe with sqlite3 objects, which aren't really the
// structs, but slabs of new uint8_t[size].
template <>
struct default_delete<sqlite3_file> {
  inline void operator()(sqlite3_file* ptr) const {
    // Why don't we call file->pMethods->xClose() here? Because it's not
    // guaranteed to be valid. sqlite3_file "objects" can be in partially
    // initialized states.
    delete [] reinterpret_cast<uint8_t*>(ptr);
  }
};

}  // namespace std

namespace sql {

const char kFileName[] = "TestingDatabase.db";

class VFSTest : public mojo::test::ApplicationTestBase,
                public filesystem::FileSystemClient {
 public:
  VFSTest() : binding_(this) {}
  ~VFSTest() override {}

  sqlite3_vfs* vfs() {
    return sqlite3_vfs_find(NULL);
  }

  scoped_ptr<sqlite3_file> MakeFile() {
    return scoped_ptr<sqlite3_file>(reinterpret_cast<sqlite3_file*>(
        new uint8_t[vfs()->szOsFile]));
  }

  void SetUp() override {
    mojo::test::ApplicationTestBase::SetUp();

    application_impl()->ConnectToService("mojo:filesystem", &files_);

    filesystem::FileSystemClientPtr client;
    binding_.Bind(GetProxy(&client));

    filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
    filesystem::DirectoryPtr directory;
    files_->OpenFileSystem("temp", GetProxy(&directory), std::move(client),
                           mojo::Capture(&error));
    ASSERT_TRUE(files_.WaitForIncomingResponse());
    ASSERT_EQ(filesystem::FILE_ERROR_OK, error);

    vfs_.reset(new ScopedMojoFilesystemVFS(std::move(directory)));
  }

  void TearDown() override {
    vfs_.reset();
    mojo::test::ApplicationTestBase::TearDown();
  }

  void OnFileSystemShutdown() override {
  }

 private:
  filesystem::FileSystemPtr files_;
  scoped_ptr<ScopedMojoFilesystemVFS> vfs_;
  mojo::Binding<filesystem::FileSystemClient> binding_;

  DISALLOW_COPY_AND_ASSIGN(VFSTest);
};

TEST_F(VFSTest, TestInstalled) {
  EXPECT_EQ("mojo", std::string(vfs()->zName));
}

TEST_F(VFSTest, ExclusiveOpen) {
  // Opening a non-existent file exclusively should work.
  scoped_ptr<sqlite3_file> file(MakeFile());
  int out_flags;
  int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                        SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_CREATE,
                        &out_flags);
  EXPECT_EQ(SQLITE_OK, rc);

  // Opening it a second time exclusively shouldn't.
  scoped_ptr<sqlite3_file> file2(MakeFile());
  rc = vfs()->xOpen(vfs(), kFileName, file2.get(),
                    SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_CREATE,
                    &out_flags);
  EXPECT_NE(SQLITE_OK, rc);

  file->pMethods->xClose(file.get());
}

TEST_F(VFSTest, NonexclusiveOpen) {
  // Opening a non-existent file should work.
  scoped_ptr<sqlite3_file> file(MakeFile());
  int out_flags;
  int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                        SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                        &out_flags);
  EXPECT_EQ(SQLITE_OK, rc);

  // Opening it a second time should work.
  scoped_ptr<sqlite3_file> file2(MakeFile());
  rc = vfs()->xOpen(vfs(), kFileName, file2.get(),
                    SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                    &out_flags);
  EXPECT_EQ(SQLITE_OK, rc);

  file->pMethods->xClose(file.get());
  file->pMethods->xClose(file2.get());
}

TEST_F(VFSTest, NullFilenameOpen) {
  // Opening a file with a null filename should return a valid file object.
  scoped_ptr<sqlite3_file> file(MakeFile());
  int out_flags;
  int rc = vfs()->xOpen(
      vfs(), nullptr, file.get(),
      SQLITE_OPEN_DELETEONCLOSE | SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
      &out_flags);
  EXPECT_EQ(SQLITE_OK, rc);

  file->pMethods->xClose(file.get());
}

TEST_F(VFSTest, DeleteOnClose) {
  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(
        vfs(), kFileName, file.get(),
        SQLITE_OPEN_DELETEONCLOSE | SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
        &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);
    file->pMethods->xClose(file.get());
  }

  // The file shouldn't exist now.
  int result = 0;
  vfs()->xAccess(vfs(), kFileName, SQLITE_ACCESS_EXISTS, &result);
  EXPECT_FALSE(result);
}

TEST_F(VFSTest, TestNonExistence) {
  // We shouldn't have a file exist yet in a fresh directory.
  int result = 0;
  vfs()->xAccess(vfs(), kFileName, SQLITE_ACCESS_EXISTS, &result);
  EXPECT_FALSE(result);
}

TEST_F(VFSTest, TestExistence) {
  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                          SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                          &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);

    file->pMethods->xClose(file.get());
  }

  int result = 0;
  vfs()->xAccess(vfs(), kFileName, SQLITE_ACCESS_EXISTS, &result);
  EXPECT_TRUE(result);
}

TEST_F(VFSTest, TestDelete) {
  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                          SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                          &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);

    file->pMethods->xClose(file.get());
  }

  int result = 0;
  vfs()->xAccess(vfs(), kFileName, SQLITE_ACCESS_EXISTS, &result);
  EXPECT_TRUE(result);

  vfs()->xDelete(vfs(), kFileName, 0);

  vfs()->xAccess(vfs(), kFileName, SQLITE_ACCESS_EXISTS, &result);
  EXPECT_FALSE(result);
}

TEST_F(VFSTest, TestWriteAndRead) {
  const char kBuffer[] = "One Two Three Four Five Six Seven";
  const int kBufferSize = arraysize(kBuffer);

  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                          SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                          &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);

    for (int i = 0; i < 10; ++i) {
      rc = file->pMethods->xWrite(file.get(), kBuffer, kBufferSize,
                                  i * kBufferSize);
      EXPECT_EQ(SQLITE_OK, rc);
    }

    file->pMethods->xClose(file.get());
  }

  // Expect that the size of the file is 10 * arraysize(kBuffer);
  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                          SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                          &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);

    sqlite_int64 size;
    rc = file->pMethods->xFileSize(file.get(), &size);
    EXPECT_EQ(SQLITE_OK, rc);
    EXPECT_EQ(10 * kBufferSize, size);

    file->pMethods->xClose(file.get());
  }

  // We should be able to read things back.
  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                          SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                          &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);

    char data_buffer[kBufferSize];
    memset(data_buffer, '8', kBufferSize);
    for (int i = 0; i < 10; ++i) {
      rc = file->pMethods->xRead(file.get(), data_buffer, kBufferSize,
                                 i * kBufferSize);
      EXPECT_EQ(SQLITE_OK, rc);
      EXPECT_TRUE(strncmp(kBuffer, &data_buffer[0], kBufferSize) == 0);
    }

    file->pMethods->xClose(file.get());
  }
}

TEST_F(VFSTest, PartialRead) {
  const char kBuffer[] = "One Two Three Four Five Six Seven";
  const int kBufferSize = arraysize(kBuffer);

  // Write the data once.
  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                          SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                          &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);

    rc = file->pMethods->xWrite(file.get(), kBuffer, kBufferSize, 0);
    EXPECT_EQ(SQLITE_OK, rc);

    file->pMethods->xClose(file.get());
  }

  // Now attempt to read kBufferSize + 5 from a file sized to kBufferSize.
  {
    scoped_ptr<sqlite3_file> file(MakeFile());
    int out_flags;
    int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                          SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                          &out_flags);
    EXPECT_EQ(SQLITE_OK, rc);

    const char kBufferWithFiveNulls[] =
        "One Two Three Four Five Six Seven\0\0\0\0\0";
    const int kBufferWithFiveNullsSize = arraysize(kBufferWithFiveNulls);

    char data_buffer[kBufferWithFiveNullsSize];
    memset(data_buffer, '8', kBufferWithFiveNullsSize);
    rc = file->pMethods->xRead(file.get(), data_buffer,
                               kBufferWithFiveNullsSize, 0);
    EXPECT_EQ(SQLITE_IOERR_SHORT_READ, rc);

    EXPECT_TRUE(strncmp(kBufferWithFiveNulls, &data_buffer[0],
                        kBufferWithFiveNullsSize) == 0);

    file->pMethods->xClose(file.get());
  }
}

TEST_F(VFSTest, Truncate) {
  const char kBuffer[] = "One Two Three Four Five Six Seven";
  const int kBufferSize = arraysize(kBuffer);
  const int kCharsToThree = 13;

  scoped_ptr<sqlite3_file> file(MakeFile());
  int out_flags;
  int rc = vfs()->xOpen(vfs(), kFileName, file.get(),
                        SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                        &out_flags);
  EXPECT_EQ(SQLITE_OK, rc);

  rc = file->pMethods->xWrite(file.get(), kBuffer, kBufferSize, 0);
  EXPECT_EQ(SQLITE_OK, rc);

  sqlite_int64 size;
  rc = file->pMethods->xFileSize(file.get(), &size);
  EXPECT_EQ(SQLITE_OK, rc);
  EXPECT_EQ(kBufferSize, size);

  rc = file->pMethods->xTruncate(file.get(), kCharsToThree);
  EXPECT_EQ(SQLITE_OK, rc);

  rc = file->pMethods->xFileSize(file.get(), &size);
  EXPECT_EQ(SQLITE_OK, rc);
  EXPECT_EQ(kCharsToThree, size);

  file->pMethods->xClose(file.get());
}

}  // namespace sql