leveldb_fuzzer.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include <cstddef>
  17. #include <cstdint>
  18. #include <string>
  19. #include "Firestore/core/src/local/leveldb_key.h"
  20. #include "Firestore/core/src/local/leveldb_util.h"
  21. using firebase::firestore::local::LevelDbDocumentMutationKey;
  22. using firebase::firestore::local::LevelDbDocumentTargetKey;
  23. using firebase::firestore::local::LevelDbMutationKey;
  24. using firebase::firestore::local::LevelDbMutationQueueKey;
  25. using firebase::firestore::local::LevelDbQueryTargetKey;
  26. using firebase::firestore::local::LevelDbRemoteDocumentKey;
  27. using firebase::firestore::local::LevelDbTargetDocumentKey;
  28. using firebase::firestore::local::LevelDbTargetGlobalKey;
  29. using firebase::firestore::local::LevelDbTargetKey;
  30. using firebase::firestore::model::BatchId;
  31. using firebase::firestore::model::ResourcePath;
  32. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  33. const char *str_ptr = reinterpret_cast<const char *>(data);
  34. std::string str{str_ptr, size};
  35. leveldb::Slice slice = firebase::firestore::local::MakeSlice(str);
  36. // Test DescribeKey(std::string) which calls MakeSlice(std::string).
  37. try {
  38. firebase::firestore::local::DescribeKey(str);
  39. } catch (...) {
  40. // Ignore caught errors and assertions.
  41. }
  42. // Test LevelDbMutationKey methods.
  43. try {
  44. LevelDbMutationKey::KeyPrefix(str);
  45. } catch (...) {
  46. // Ignore caught errors and assertions.
  47. }
  48. try {
  49. BatchId batch_id{static_cast<int>(size)};
  50. LevelDbMutationKey::Key(str, batch_id);
  51. } catch (...) {
  52. // Ignore caught errors and assertions.
  53. }
  54. try {
  55. LevelDbMutationKey key;
  56. (void)key.Decode(str);
  57. } catch (...) {
  58. // Ignore caught errors and assertions.
  59. }
  60. // Test LevelDbDocumentMutationKey methods.
  61. try {
  62. LevelDbDocumentMutationKey::KeyPrefix(str);
  63. } catch (...) {
  64. // Ignore caught errors and assertions.
  65. }
  66. try {
  67. LevelDbDocumentMutationKey key;
  68. (void)key.Decode(str);
  69. } catch (...) {
  70. // Ignore caught errors and assertions.
  71. }
  72. // Test LevelDbMutationQueueKey methods.
  73. try {
  74. LevelDbMutationQueueKey::Key(str);
  75. } catch (...) {
  76. // Ignore caught errors and assertions.
  77. }
  78. try {
  79. LevelDbMutationQueueKey key;
  80. (void)key.Decode(str);
  81. } catch (...) {
  82. // Ignore caught errors and assertions.
  83. }
  84. // Test LevelDbTargetGlobalKey methods.
  85. try {
  86. LevelDbTargetGlobalKey key;
  87. (void)key.Decode(slice);
  88. } catch (...) {
  89. // ignore caught errors and assertions.
  90. }
  91. // Test LevelDbTargetKey methods.
  92. try {
  93. LevelDbTargetKey key;
  94. (void)key.Decode(slice);
  95. } catch (...) {
  96. // ignore caught errors and assertions.
  97. }
  98. // Test LevelDbQueryTargetKey methods.
  99. try {
  100. LevelDbQueryTargetKey::KeyPrefix(str);
  101. } catch (...) {
  102. // Ignore caught errors and assertions.
  103. }
  104. try {
  105. LevelDbQueryTargetKey key;
  106. (void)key.Decode(str);
  107. } catch (...) {
  108. // Ignore caught errors and assertions.
  109. }
  110. // Test LevelDbTargetDocumentKey methods.
  111. try {
  112. LevelDbTargetDocumentKey key;
  113. (void)key.Decode(str);
  114. } catch (...) {
  115. // Ignore caught errors and assertions.
  116. }
  117. // Test LevelDbDocumentTargetKey methods.
  118. try {
  119. ResourcePath rp = ResourcePath::FromString(str);
  120. LevelDbDocumentTargetKey::KeyPrefix(rp);
  121. } catch (...) {
  122. // Ignore caught errors and assertions.
  123. }
  124. try {
  125. LevelDbDocumentTargetKey key;
  126. (void)key.Decode(str);
  127. } catch (...) {
  128. // Ignore caught errors and assertions.
  129. }
  130. // Test LevelDbRemoteDocumentKey methods.
  131. try {
  132. ResourcePath rp = ResourcePath::FromString(str);
  133. LevelDbRemoteDocumentKey::KeyPrefix(rp);
  134. } catch (...) {
  135. // Ignore caught errors and assertions.
  136. }
  137. try {
  138. LevelDbRemoteDocumentKey key;
  139. (void)key.Decode(str);
  140. } catch (...) {
  141. // Ignore caught errors and assertions.
  142. }
  143. return 0;
  144. }