ebpf_unittest.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf_unittest.h"
  3. ebpf_module_t test_em;
  4. /**
  5. * Initialize structure
  6. *
  7. * Initialize structure used to run unittests
  8. */
  9. void ebpf_ut_initialize_structure(netdata_run_mode_t mode)
  10. {
  11. memset(&test_em, 0, sizeof(ebpf_module_t));
  12. test_em.thread_name = strdupz("process");
  13. test_em.config_name = test_em.thread_name;
  14. test_em.kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10 |
  15. NETDATA_V5_14;
  16. test_em.pid_map_size = ND_EBPF_DEFAULT_PID_SIZE;
  17. test_em.apps_level = NETDATA_APPS_LEVEL_REAL_PARENT;
  18. test_em.mode = mode;
  19. }
  20. /**
  21. * Clean UP Memory
  22. *
  23. * Clean up allocated data during unit test;
  24. */
  25. void ebpf_ut_cleanup_memory()
  26. {
  27. freez((void *)test_em.thread_name);
  28. }
  29. /**
  30. * Load Binary
  31. *
  32. * Test load of legacy eBPF programs.
  33. *
  34. * @return It returns 0 on success and -1 otherwise.
  35. */
  36. static int ebpf_ut_load_binary()
  37. {
  38. test_em.probe_links = ebpf_load_program(ebpf_plugin_dir, &test_em, running_on_kernel, isrh, &test_em.objects);
  39. if (!test_em.probe_links)
  40. return -1;
  41. ebpf_unload_legacy_code(test_em.objects, test_em.probe_links);
  42. return 0;
  43. }
  44. /**
  45. * Load Real Binary
  46. *
  47. * Load an existent binary inside plugin directory.
  48. *
  49. * @return It returns 0 on success and -1 otherwise.
  50. */
  51. int ebpf_ut_load_real_binary()
  52. {
  53. return ebpf_ut_load_binary();
  54. }
  55. /**
  56. * Load fake Binary
  57. *
  58. * Try to load a binary not generated by netdata.
  59. *
  60. * @return It returns 0 on success and -1 otherwise. The success for this function means we could work properly with
  61. * expected fails.
  62. */
  63. int ebpf_ut_load_fake_binary()
  64. {
  65. const char *original = test_em.thread_name;
  66. test_em.thread_name = strdupz("I_am_not_here");
  67. int ret = ebpf_ut_load_binary();
  68. ebpf_ut_cleanup_memory();
  69. test_em.thread_name = original;
  70. return !ret;
  71. }