Skip to content

Commit 2564279

Browse files
committed
leveldb_snappy_test.cc: re-use the test from the iOS SDK instead of re-writing it.
1 parent 555a406 commit 2564279

File tree

1 file changed

+5
-282
lines changed

1 file changed

+5
-282
lines changed

firestore/integration_test_internal/src/leveldb_snappy_test.cc

Lines changed: 5 additions & 282 deletions
Original file line numberDiff line numberDiff line change
@@ -20,285 +20,8 @@
2020
#error "This test should not be included in Android."
2121
#endif
2222

23-
#include <array>
24-
#include <fstream>
25-
#include <functional>
26-
#include <iostream>
27-
#include <memory>
28-
#include <string>
29-
30-
#include "Firestore/core/src/local/leveldb_util.h"
31-
#include "Firestore/core/src/util/filesystem.h"
32-
#include "Firestore/core/src/util/path.h"
33-
34-
#include "gtest/gtest.h"
35-
#include "leveldb/db.h"
36-
37-
// TODO(dconeybe) Reference this test in the iOS SDK instead of making a
38-
// copy of it here in the C++ SDK.
39-
40-
namespace {
41-
42-
using ::firebase::firestore::local::ConvertStatus;
43-
using ::firebase::firestore::util::Filesystem;
44-
using ::firebase::firestore::util::Path;
45-
46-
// Creates a LevelDb database that uses Snappy compression for at least one of
47-
// its blocks. Attempting to iterate over this database using a LevelDb library
48-
// that does not have Snappy compression compiled in will return a failed status
49-
// with reason "corruption".
50-
Path CreateLevelDbDatabaseThatUsesSnappyCompression();
51-
52-
// Creates and opens a LevelDb database that contains at least one block that
53-
// is compressed with Snappy compression, then iterates over it, invoking the
54-
// given callback with the status at each point in the iteration. Once the
55-
// callback is invoked with a `status` where `status.ok()` is not true, then
56-
// iteration will stop and the callback will not be invoked again.
57-
void IterateOverLevelDbDatabaseThatUsesSnappyCompression(
58-
std::function<void(const leveldb::Status&)>);
59-
60-
#if FIREBASE_TESTS_BUILT_BY_CMAKE
61-
62-
// Ensure that LevelDb is compiled with Snappy compression support.
63-
// See https://github.com/firebase/firebase-ios-sdk/pull/9596 for details.
64-
TEST(LevelDbSnappy, LevelDbSupportsSnappy) {
65-
IterateOverLevelDbDatabaseThatUsesSnappyCompression(
66-
[](const leveldb::Status& status) {
67-
ASSERT_TRUE(status.ok()) << ConvertStatus(status);
68-
});
69-
}
70-
71-
#else // FIREBASE_TESTS_BUILT_BY_CMAKE
72-
73-
// Ensure that LevelDb is NOT compiled with Snappy compression support.
74-
TEST(LevelDbSnappy, LevelDbDoesNotSupportSnappy) {
75-
bool got_failed_status = false;
76-
IterateOverLevelDbDatabaseThatUsesSnappyCompression(
77-
[&](const leveldb::Status& status) {
78-
if (!status.ok()) {
79-
got_failed_status = true;
80-
ASSERT_TRUE(status.IsCorruption()) << ConvertStatus(status);
81-
}
82-
});
83-
84-
if (!HasFailure()) {
85-
ASSERT_TRUE(got_failed_status)
86-
<< "Reading a Snappy-compressed LevelDb database was successful; "
87-
"however, it should NOT have been successful "
88-
"since Snappy support is expected to NOT be available.";
89-
}
90-
}
91-
92-
#endif // FIREBASE_TESTS_BUILT_BY_CMAKE
93-
94-
void IterateOverLevelDbDatabaseThatUsesSnappyCompression(
95-
std::function<void(const leveldb::Status&)> callback) {
96-
std::unique_ptr<leveldb::DB> db;
97-
{
98-
Path leveldb_path = CreateLevelDbDatabaseThatUsesSnappyCompression();
99-
if (leveldb_path.empty()) {
100-
return;
101-
}
102-
103-
leveldb::Options options;
104-
options.create_if_missing = false;
105-
106-
leveldb::DB* db_ptr;
107-
leveldb::Status status =
108-
leveldb::DB::Open(options, leveldb_path.ToUtf8String(), &db_ptr);
109-
110-
ASSERT_TRUE(status.ok())
111-
<< "Opening LevelDb database " << leveldb_path.ToUtf8String()
112-
<< " failed: " << ConvertStatus(status);
113-
114-
db.reset(db_ptr);
115-
}
116-
117-
std::unique_ptr<leveldb::Iterator> it(
118-
db->NewIterator(leveldb::ReadOptions()));
119-
for (it->SeekToFirst(); it->Valid(); it->Next()) {
120-
callback(it->status());
121-
if (!it->status().ok()) {
122-
return;
123-
}
124-
}
125-
126-
// Invoke the callback on the final status.
127-
callback(it->status());
128-
}
129-
130-
template <typename T>
131-
void WriteFile(const Path& dir,
132-
const std::string& file_name,
133-
const T& data_array) {
134-
Filesystem* fs = Filesystem::Default();
135-
{
136-
auto status = fs->RecursivelyCreateDir(dir);
137-
if (!status.ok()) {
138-
FAIL() << "Creating directory failed: " << dir.ToUtf8String() << " ("
139-
<< status.error_message() << ")";
140-
}
141-
}
142-
143-
Path file = dir.AppendUtf8(file_name);
144-
std::ofstream out_file(file.native_value(), std::ios::binary);
145-
if (!out_file) {
146-
FAIL() << "Unable to open file for writing: " << file.ToUtf8String();
147-
}
148-
149-
out_file.write(reinterpret_cast<const char*>(data_array.data()),
150-
data_array.size());
151-
out_file.close();
152-
if (!out_file) {
153-
FAIL() << "Writing to file failed: " << file.ToUtf8String();
154-
}
155-
}
156-
157-
const std::array<unsigned char, 0x00000165> LevelDbSnappyFile_000005_ldb{
158-
0x84, 0x03, 0x80, 0x00, 0x42, 0x00, 0x85, 0x71, 0x75, 0x65, 0x72, 0x79,
159-
0x5F, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x00, 0x01, 0x8B, 0x43, 0x6F,
160-
0x6C, 0x41, 0x2F, 0x44, 0x6F, 0x63, 0x41, 0x2F, 0x43, 0x6F, 0x6C, 0x42,
161-
0x01, 0x0A, 0x68, 0x42, 0x7C, 0x66, 0x3A, 0x7C, 0x6F, 0x62, 0x3A, 0x5F,
162-
0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x5F, 0x5F, 0x61, 0x73, 0x63, 0x00, 0x01,
163-
0x8C, 0x82, 0x80, 0x01, 0x07, 0x00, 0x05, 0x01, 0x08, 0x01, 0x13, 0x50,
164-
0x11, 0x3E, 0x01, 0x16, 0x00, 0x0A, 0x05, 0x15, 0xF0, 0x3C, 0x00, 0x08,
165-
0x02, 0x20, 0x05, 0x32, 0x4A, 0x12, 0x48, 0x70, 0x72, 0x6F, 0x6A, 0x65,
166-
0x63, 0x74, 0x73, 0x2F, 0x54, 0x65, 0x73, 0x74, 0x54, 0x65, 0x72, 0x6D,
167-
0x69, 0x6E, 0x61, 0x74, 0x65, 0x2F, 0x64, 0x61, 0x74, 0x61, 0x62, 0x61,
168-
0x73, 0x65, 0x73, 0x2F, 0x28, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74,
169-
0x29, 0x2F, 0x64, 0x6F, 0x63, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x73, 0x01,
170-
0x7B, 0x3E, 0x85, 0x00, 0x0C, 0x0D, 0x07, 0x50, 0x08, 0x15, 0x5A, 0x00,
171-
0x03, 0xFE, 0x5A, 0x00, 0x2E, 0x5A, 0x00, 0x38, 0x07, 0x12, 0x06, 0x5F,
172-
0x67, 0x6C, 0x6F, 0x62, 0x61, 0x6C, 0x00, 0x01, 0x80, 0x01, 0x0B, 0x11,
173-
0x65, 0x1C, 0x10, 0x05, 0x20, 0x01, 0x12, 0x07, 0x06, 0x09, 0x15, 0x10,
174-
0x00, 0x03, 0x01, 0x10, 0x04, 0x00, 0x01, 0x09, 0x10, 0x24, 0x01, 0x12,
175-
0x01, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x01, 0x35, 0x00, 0x06,
176-
0x09, 0x15, 0x10, 0x37, 0x0C, 0x07, 0x01, 0x05, 0x09, 0x0B, 0x10, 0x36,
177-
0x0C, 0x07, 0x01, 0x04, 0x09, 0x0B, 0x10, 0x35, 0x0C, 0x07, 0x01, 0x03,
178-
0x09, 0x0B, 0x4C, 0x34, 0x0C, 0x07, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00,
179-
0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
180-
0x2C, 0x6E, 0xE0, 0xF4, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
181-
0x00, 0xC0, 0xF2, 0xA1, 0xB0, 0x00, 0x09, 0x03, 0x86, 0x01, 0xFF, 0xFF,
182-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x87, 0x02, 0x00, 0x00, 0x00, 0x00,
183-
0x01, 0x00, 0x00, 0x00, 0x00, 0x58, 0xC2, 0x94, 0x06, 0x8C, 0x02, 0x08,
184-
0x99, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187-
0x00, 0x57, 0xFB, 0x80, 0x8B, 0x24, 0x75, 0x47, 0xDB,
188-
};
189-
const std::array<unsigned char, 0x000000E8> LevelDbSnappyFile_000017_ldb{
190-
0x00, 0x14, 0x50, 0x85, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x00, 0x01,
191-
0x8C, 0x82, 0x80, 0x01, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
192-
0x02, 0x20, 0x0A, 0x32, 0x4A, 0x12, 0x48, 0x70, 0x72, 0x6F, 0x6A, 0x65,
193-
0x63, 0x74, 0x73, 0x2F, 0x54, 0x65, 0x73, 0x74, 0x54, 0x65, 0x72, 0x6D,
194-
0x69, 0x6E, 0x61, 0x74, 0x65, 0x2F, 0x64, 0x61, 0x74, 0x61, 0x62, 0x61,
195-
0x73, 0x65, 0x73, 0x2F, 0x28, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6C, 0x74,
196-
0x29, 0x2F, 0x64, 0x6F, 0x63, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x73, 0x2F,
197-
0x43, 0x6F, 0x6C, 0x41, 0x2F, 0x44, 0x6F, 0x63, 0x41, 0x2F, 0x43, 0x6F,
198-
0x6C, 0x42, 0x2F, 0x44, 0x6F, 0x63, 0x42, 0x07, 0x12, 0x06, 0x5F, 0x67,
199-
0x6C, 0x6F, 0x62, 0x61, 0x6C, 0x00, 0x01, 0x80, 0x01, 0x0D, 0x00, 0x00,
200-
0x00, 0x00, 0x00, 0x00, 0x08, 0x02, 0x10, 0x0A, 0x20, 0x01, 0x00, 0x00,
201-
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xCD, 0xE0, 0x39, 0x00,
202-
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xF2, 0xA1, 0xB0,
203-
0x00, 0x09, 0x03, 0x86, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
204-
0x00, 0x8A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
205-
0xE4, 0xA7, 0x7E, 0x74, 0x8F, 0x01, 0x08, 0x9C, 0x01, 0x17, 0x00, 0x00,
206-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0xFB, 0x80, 0x8B,
209-
0x24, 0x75, 0x47, 0xDB,
210-
};
211-
const std::array<unsigned char, 0x00000000> LevelDbSnappyFile_000085_ldb{};
212-
const std::array<unsigned char, 0x00000010> LevelDbSnappyFile_CURRENT{
213-
0x4D, 0x41, 0x4E, 0x49, 0x46, 0x45, 0x53, 0x54,
214-
0x2D, 0x30, 0x30, 0x30, 0x30, 0x38, 0x34, 0x0A,
215-
};
216-
const std::array<unsigned char, 0x000000B5> LevelDbSnappyFile_LOG_old{
217-
0x32, 0x30, 0x32, 0x32, 0x2F, 0x30, 0x34, 0x2F, 0x30, 0x34, 0x2D, 0x31,
218-
0x31, 0x3A, 0x33, 0x39, 0x3A, 0x34, 0x36, 0x2E, 0x32, 0x35, 0x37, 0x32,
219-
0x35, 0x31, 0x20, 0x30, 0x78, 0x37, 0x30, 0x30, 0x30, 0x30, 0x35, 0x33,
220-
0x31, 0x34, 0x30, 0x30, 0x30, 0x20, 0x52, 0x65, 0x63, 0x6F, 0x76, 0x65,
221-
0x72, 0x69, 0x6E, 0x67, 0x20, 0x6C, 0x6F, 0x67, 0x20, 0x23, 0x38, 0x31,
222-
0x0A, 0x32, 0x30, 0x32, 0x32, 0x2F, 0x30, 0x34, 0x2F, 0x30, 0x34, 0x2D,
223-
0x31, 0x31, 0x3A, 0x33, 0x39, 0x3A, 0x34, 0x36, 0x2E, 0x33, 0x30, 0x34,
224-
0x35, 0x35, 0x32, 0x20, 0x30, 0x78, 0x37, 0x30, 0x30, 0x30, 0x30, 0x35,
225-
0x33, 0x31, 0x34, 0x30, 0x30, 0x30, 0x20, 0x44, 0x65, 0x6C, 0x65, 0x74,
226-
0x65, 0x20, 0x74, 0x79, 0x70, 0x65, 0x3D, 0x33, 0x20, 0x23, 0x38, 0x30,
227-
0x0A, 0x32, 0x30, 0x32, 0x32, 0x2F, 0x30, 0x34, 0x2F, 0x30, 0x34, 0x2D,
228-
0x31, 0x31, 0x3A, 0x33, 0x39, 0x3A, 0x34, 0x36, 0x2E, 0x33, 0x30, 0x35,
229-
0x30, 0x36, 0x34, 0x20, 0x30, 0x78, 0x37, 0x30, 0x30, 0x30, 0x30, 0x35,
230-
0x33, 0x31, 0x34, 0x30, 0x30, 0x30, 0x20, 0x44, 0x65, 0x6C, 0x65, 0x74,
231-
0x65, 0x20, 0x74, 0x79, 0x70, 0x65, 0x3D, 0x30, 0x20, 0x23, 0x38, 0x31,
232-
0x0A,
233-
};
234-
const std::array<unsigned char, 0x000000B5> LevelDbSnappyFile_LOG{
235-
0x32, 0x30, 0x32, 0x32, 0x2F, 0x30, 0x34, 0x2F, 0x30, 0x34, 0x2D, 0x31,
236-
0x31, 0x3A, 0x35, 0x36, 0x3A, 0x35, 0x36, 0x2E, 0x34, 0x39, 0x33, 0x31,
237-
0x34, 0x32, 0x20, 0x30, 0x78, 0x37, 0x30, 0x30, 0x30, 0x30, 0x61, 0x32,
238-
0x35, 0x34, 0x30, 0x30, 0x30, 0x20, 0x52, 0x65, 0x63, 0x6F, 0x76, 0x65,
239-
0x72, 0x69, 0x6E, 0x67, 0x20, 0x6C, 0x6F, 0x67, 0x20, 0x23, 0x38, 0x33,
240-
0x0A, 0x32, 0x30, 0x32, 0x32, 0x2F, 0x30, 0x34, 0x2F, 0x30, 0x34, 0x2D,
241-
0x31, 0x31, 0x3A, 0x35, 0x36, 0x3A, 0x35, 0x36, 0x2E, 0x35, 0x33, 0x34,
242-
0x37, 0x34, 0x35, 0x20, 0x30, 0x78, 0x37, 0x30, 0x30, 0x30, 0x30, 0x61,
243-
0x32, 0x35, 0x34, 0x30, 0x30, 0x30, 0x20, 0x44, 0x65, 0x6C, 0x65, 0x74,
244-
0x65, 0x20, 0x74, 0x79, 0x70, 0x65, 0x3D, 0x33, 0x20, 0x23, 0x38, 0x32,
245-
0x0A, 0x32, 0x30, 0x32, 0x32, 0x2F, 0x30, 0x34, 0x2F, 0x30, 0x34, 0x2D,
246-
0x31, 0x31, 0x3A, 0x35, 0x36, 0x3A, 0x35, 0x36, 0x2E, 0x35, 0x33, 0x35,
247-
0x32, 0x34, 0x32, 0x20, 0x30, 0x78, 0x37, 0x30, 0x30, 0x30, 0x30, 0x61,
248-
0x32, 0x35, 0x34, 0x30, 0x30, 0x30, 0x20, 0x44, 0x65, 0x6C, 0x65, 0x74,
249-
0x65, 0x20, 0x74, 0x79, 0x70, 0x65, 0x3D, 0x30, 0x20, 0x23, 0x38, 0x33,
250-
0x0A,
251-
};
252-
const std::array<unsigned char, 0x000000C2> LevelDbSnappyFile_MANIFEST_000084{
253-
0x45, 0x63, 0x9F, 0xDD, 0xAC, 0x00, 0x01, 0x01, 0x1A, 0x6C, 0x65, 0x76,
254-
0x65, 0x6C, 0x64, 0x62, 0x2E, 0x42, 0x79, 0x74, 0x65, 0x77, 0x69, 0x73,
255-
0x65, 0x43, 0x6F, 0x6D, 0x70, 0x61, 0x72, 0x61, 0x74, 0x6F, 0x72, 0x07,
256-
0x00, 0x05, 0xE5, 0x02, 0x42, 0x85, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5F,
257-
0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x00, 0x01, 0x8B, 0x43, 0x6F, 0x6C,
258-
0x41, 0x2F, 0x44, 0x6F, 0x63, 0x41, 0x2F, 0x43, 0x6F, 0x6C, 0x42, 0x2F,
259-
0x44, 0x6F, 0x63, 0x42, 0x7C, 0x66, 0x3A, 0x7C, 0x6F, 0x62, 0x3A, 0x5F,
260-
0x5F, 0x6E, 0x61, 0x6D, 0x65, 0x5F, 0x5F, 0x61, 0x73, 0x63, 0x00, 0x01,
261-
0x8C, 0x82, 0x80, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
262-
0x85, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x00, 0x01, 0x80, 0x01,
263-
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x11, 0xE8, 0x01,
264-
0x14, 0x85, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x00, 0x01, 0x8C, 0x82,
265-
0x80, 0x01, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x85, 0x74,
266-
0x61, 0x72, 0x67, 0x65, 0x74, 0x5F, 0x67, 0x6C, 0x6F, 0x62, 0x61, 0x6C,
267-
0x00, 0x01, 0x80, 0x01, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB1,
268-
0x03, 0xAC, 0xBA, 0x08, 0x00, 0x01, 0x02, 0x55, 0x09, 0x00, 0x03, 0x56,
269-
0x04, 0x0D,
270-
};
271-
272-
Path LevelDbDir() {
273-
Filesystem* fs = Filesystem::Default();
274-
Path dir = fs->TempDir().AppendUtf8("LevelDbSnappyTest");
275-
276-
// Delete the directory first to ensure isolation between runs.
277-
auto status = fs->RecursivelyRemove(dir);
278-
EXPECT_TRUE(status.ok()) << "Failed to clean up leveldb in directory "
279-
<< dir.ToUtf8String() << ": " << status.ToString();
280-
if (!status.ok()) {
281-
return {};
282-
}
283-
284-
return dir;
285-
}
286-
287-
Path CreateLevelDbDatabaseThatUsesSnappyCompression() {
288-
Path leveldb_dir = LevelDbDir();
289-
if (leveldb_dir.empty()) {
290-
return {};
291-
}
292-
293-
WriteFile(leveldb_dir, "000005.ldb", LevelDbSnappyFile_000005_ldb);
294-
WriteFile(leveldb_dir, "000017.ldb", LevelDbSnappyFile_000017_ldb);
295-
WriteFile(leveldb_dir, "000085.ldb", LevelDbSnappyFile_000085_ldb);
296-
WriteFile(leveldb_dir, "CURRENT", LevelDbSnappyFile_CURRENT);
297-
WriteFile(leveldb_dir, "LOG.old", LevelDbSnappyFile_LOG_old);
298-
WriteFile(leveldb_dir, "LOG", LevelDbSnappyFile_LOG);
299-
WriteFile(leveldb_dir, "MANIFEST-000084", LevelDbSnappyFile_MANIFEST_000084);
300-
301-
return leveldb_dir;
302-
}
303-
304-
} // namespace
23+
// Just re-use the unit test from the iOS SDK.
24+
// TODO(dconeybe) Import ALL the unit tests from the iOS SDK by adding them
25+
// to the CMakeLists.txt in the parent directory of this file. That way we can
26+
// run all of the tests from the iOS SDK on each platform targeted by this repo.
27+
#include "Firestore/core/test/unit/local/leveldb_snappy_test.cc"

0 commit comments

Comments
 (0)