fuzz_binary_message.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "ua_server_internal.h"
  5. #include "ua_config_default.h"
  6. #include "ua_log_stdout.h"
  7. #include "ua_plugin_log.h"
  8. #include "testing_networklayers.h"
  9. /*
  10. ** Main entry point. The fuzzer invokes this function with each
  11. ** fuzzed input.
  12. */
  13. extern "C" int
  14. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  15. UA_Connection c = createDummyConnection(65535, NULL);
  16. UA_ServerConfig *config = UA_ServerConfig_new_default();
  17. UA_Server *server = UA_Server_new(config);
  18. if (server == NULL) {
  19. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  20. "Could not create server instance using UA_Server_new");
  21. return 1;
  22. }
  23. // we need to copy the message because it will be freed in the processing function
  24. UA_ByteString msg = UA_ByteString();
  25. UA_StatusCode retval = UA_ByteString_allocBuffer(&msg, size);
  26. if(retval != UA_STATUSCODE_GOOD)
  27. return (int)retval;
  28. memcpy(msg.data, data, size);
  29. UA_Server_processBinaryMessage(server, &c, &msg);
  30. // if we got an invalid chunk, the message is not deleted, so delete it here
  31. UA_ByteString_deleteMembers(&msg);
  32. UA_Server_run_shutdown(server);
  33. UA_Server_delete(server);
  34. UA_ServerConfig_delete(config);
  35. c.close(&c);
  36. UA_Connection_deleteMembers(&c);
  37. return 0;
  38. }