name_value_pairs_parser_fuzzer.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2021 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. #include <stddef.h>
  5. #include <stdint.h>
  6. #include "chromeos/system/name_value_pairs_parser.h"
  7. namespace chromeos {
  8. namespace system {
  9. // We need a class that can be friend of NameValuePairsParser because we fuzz
  10. // input to private methods that underpin the public methods.
  11. class NameValuePairsParserFuzzer {
  12. public:
  13. void testOneInput(const uint8_t* data, size_t size) {
  14. const std::string input = std::string(data, data + size);
  15. name_value_map_.clear();
  16. testInputAsVpdDumpLine(input);
  17. testInputAsCrossystemOutputLine(input);
  18. testInputAsVpdDumpValuesForKey(input);
  19. }
  20. private:
  21. void testInputAsVpdDumpLine(const std::string& input) {
  22. NameValuePairsParser parser(&name_value_map_);
  23. parser.ParseNameValuePairs(input, NameValuePairsFormat::kVpdDump,
  24. "testAsVpd");
  25. }
  26. void testInputAsCrossystemOutputLine(const std::string& input) {
  27. NameValuePairsParser parser(&name_value_map_);
  28. parser.ParseNameValuePairs(input, NameValuePairsFormat::kCrossystem,
  29. "testAsCrossystem");
  30. }
  31. void testInputAsVpdDumpValuesForKey(const std::string& input) {
  32. // Test with the input as is as a value (which may be malformed due to
  33. // the presence of newlines in it).
  34. testInputAsVpdDumpValueForKey(input);
  35. // Test with the input as a value on the same line (i.e., without any
  36. // newline in it).
  37. std::string value = input;
  38. value.erase(std::remove(value.begin(), value.end(), '\n'), value.end());
  39. testInputAsVpdDumpValueForKey(value);
  40. // TODO(crbug.com/1250434): Check that the value for "key" is |value|.
  41. }
  42. void testInputAsVpdDumpValueForKey(const std::string& input) {
  43. name_value_map_.erase("key");
  44. testInputAsVpdDumpLine(std::string("\"key\"=\"") + input + "\"\n");
  45. }
  46. NameValuePairsParser::NameValueMap name_value_map_;
  47. };
  48. } // namespace system
  49. } // namespace chromeos
  50. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  51. chromeos::system::NameValuePairsParserFuzzer fuzzer;
  52. fuzzer.testOneInput(data, size);
  53. return 0;
  54. }