Skip to content

Commit f7b5869

Browse files
author
Mina Farid
authored
Adding Fuzzing Test Target LevelDb (#1685)
1 parent 1843a0e commit f7b5869

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2018 Google
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
separator=":"
16+
# Enum values for component labels
17+
terminator="0"
18+
tablename="5"
19+
batchid="10"
20+
canonicalid="11"
21+
targetid="12"
22+
userid="13"
23+
pathsegment="62"
24+
unknown="63"
25+
# Constants.
26+
"version"
27+
"mutation"
28+
"document_mutation"
29+
"mutation_queue"
30+
"target_global"
31+
"target"
32+
"query_target"
33+
"target_document"
34+
"document_target"
35+
"remote_document"
36+
# Special characters.
37+
underscore="_"
38+
dot="."
39+
semicolon=";"
40+
quote="'"
41+
quotes="\""
42+
foreslash="/"
43+
backslash="\\"
44+
dollar="$"
45+
hash="#"
46+
asterisk="*"
47+
bracket1="("
48+
bracket2=")"
49+
bracket3="{"
50+
bracket4="}"
51+
bracket5="<"
52+
bracket6=">"
53+
ques="?"
54+
excl="!"
55+
at="@"
56+
hat="^"
57+
and="&"
58+
plus="+"
59+
dash="-"
60+
mod="|"
61+

Firestore/fuzzing/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ cc_fuzz_test(
5656
SOURCES resourcepath_fuzzer.cc
5757
DEPENDS firebase_firestore_model
5858
)
59+
60+
# LevelDB fuzzer.
61+
cc_fuzz_test(
62+
leveldb_fuzzer
63+
DICTIONARY ${fuzzing_resources}/LevelDb/leveldb.dictionary
64+
SOURCES leveldb_fuzzer.cc
65+
DEPENDS firebase_firestore_local_persistence_leveldb
66+
)

Firestore/fuzzing/leveldb_fuzzer.cc

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <cstddef>
18+
#include <cstdint>
19+
#include <string>
20+
21+
#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
22+
#include "Firestore/core/src/firebase/firestore/local/leveldb_util.h"
23+
24+
using firebase::firestore::local::LevelDbDocumentMutationKey;
25+
using firebase::firestore::local::LevelDbDocumentTargetKey;
26+
using firebase::firestore::local::LevelDbMutationKey;
27+
using firebase::firestore::local::LevelDbMutationQueueKey;
28+
using firebase::firestore::local::LevelDbQueryTargetKey;
29+
using firebase::firestore::local::LevelDbRemoteDocumentKey;
30+
using firebase::firestore::local::LevelDbTargetDocumentKey;
31+
using firebase::firestore::local::LevelDbTargetGlobalKey;
32+
using firebase::firestore::local::LevelDbTargetKey;
33+
using firebase::firestore::model::BatchId;
34+
using firebase::firestore::model::ResourcePath;
35+
36+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
37+
const char* str_ptr = reinterpret_cast<const char*>(data);
38+
absl::string_view str{str_ptr, size};
39+
leveldb::Slice slice = firebase::firestore::local::MakeSlice(str);
40+
41+
// Test DescribeKey(std::string) which calls MakeSlice(std::string).
42+
try {
43+
firebase::firestore::local::DescribeKey(str);
44+
} catch (...) {
45+
// Ignore caught errors and assertions.
46+
}
47+
48+
// Test LevelDbMutationKey methods.
49+
try {
50+
LevelDbMutationKey::KeyPrefix(str);
51+
} catch (...) {
52+
// Ignore caught errors and assertions.
53+
}
54+
55+
try {
56+
BatchId batch_id{static_cast<int>(size)};
57+
LevelDbMutationKey::Key(str, batch_id);
58+
} catch (...) {
59+
// Ignore caught errors and assertions.
60+
}
61+
62+
try {
63+
LevelDbMutationKey key;
64+
key.Decode(str);
65+
} catch (...) {
66+
// Ignore caught errors and assertions.
67+
}
68+
69+
// Test LevelDbDocumentMutationKey methods.
70+
try {
71+
LevelDbDocumentMutationKey::KeyPrefix(str);
72+
} catch (...) {
73+
// Ignore caught errors and assertions.
74+
}
75+
76+
try {
77+
LevelDbDocumentMutationKey key;
78+
key.Decode(str);
79+
} catch (...) {
80+
// Ignore caught errors and assertions.
81+
}
82+
83+
// Test LevelDbMutationQueueKey methods.
84+
try {
85+
LevelDbMutationQueueKey::Key(str);
86+
} catch (...) {
87+
// Ignore caught errors and assertions.
88+
}
89+
90+
try {
91+
LevelDbMutationQueueKey key;
92+
key.Decode(slice);
93+
} catch (...) {
94+
// Ignore caught errors and assertions.
95+
}
96+
97+
// Test LevelDbTargetGlobalKey methods.
98+
try {
99+
LevelDbTargetGlobalKey key;
100+
key.Decode(slice);
101+
} catch (...) {
102+
// ignore caught errors and assertions.
103+
}
104+
105+
// Test LevelDbTargetKey methods.
106+
try {
107+
LevelDbTargetKey key;
108+
key.Decode(slice);
109+
} catch (...) {
110+
// ignore caught errors and assertions.
111+
}
112+
113+
// Test LevelDbQueryTargetKey methods.
114+
try {
115+
LevelDbQueryTargetKey::KeyPrefix(str);
116+
} catch (...) {
117+
// Ignore caught errors and assertions.
118+
}
119+
120+
try {
121+
LevelDbQueryTargetKey key;
122+
key.Decode(str);
123+
} catch (...) {
124+
// Ignore caught errors and assertions.
125+
}
126+
127+
// Test LevelDbTargetDocumentKey methods.
128+
try {
129+
LevelDbTargetDocumentKey key;
130+
key.Decode(str);
131+
} catch (...) {
132+
// Ignore caught errors and assertions.
133+
}
134+
135+
// Test LevelDbDocumentTargetKey methods.
136+
try {
137+
ResourcePath rp = ResourcePath::FromString(str);
138+
LevelDbDocumentTargetKey::KeyPrefix(rp);
139+
} catch (...) {
140+
// Ignore caught errors and assertions.
141+
}
142+
143+
try {
144+
LevelDbDocumentTargetKey key;
145+
key.Decode(str);
146+
} catch (...) {
147+
// Ignore caught errors and assertions.
148+
}
149+
150+
// Test LevelDbRemoteDocumentKey methods.
151+
try {
152+
ResourcePath rp = ResourcePath::FromString(str);
153+
LevelDbRemoteDocumentKey::KeyPrefix(rp);
154+
} catch (...) {
155+
// Ignore caught errors and assertions.
156+
}
157+
158+
try {
159+
LevelDbRemoteDocumentKey key;
160+
key.Decode(str);
161+
} catch (...) {
162+
// Ignore caught errors and assertions.
163+
}
164+
165+
return 0;
166+
}

0 commit comments

Comments
 (0)