/* Unprivileged unit test of the actual shared emission helper, with fake BPF helpers. */ #include #include #include typedef uint32_t u32; typedef uint64_t u64; #define BPF_MAP_TYPE_PERCPU_ARRAY 6 #define __uint(name, value) int (*name)[value] #define __type(name, value) value *name #define SEC(name) #undef __always_inline #define __always_inline inline __attribute__((always_inline)) static int events; static long output_result; static int output_calls; static int lookup_missing; static long bpf_ringbuf_output(void *, void *, u64, u64); static void *bpf_map_lookup_elem(void *, const void *); #include "../event_stats.h" static struct event_emission_counts counts; static long bpf_ringbuf_output(void *map, void *data, u64 size, u64 flags) { assert(map == &events && data != NULL && size == 1 && flags == 0); output_calls++; return output_result; } static void *bpf_map_lookup_elem(void *map, const void *key) { assert(map == &event_stats && *(const u32 *)key == 0); return lookup_missing ? NULL : &counts; } int main(void) { char data = 0; assert(sizeof(counts) == 16); assert(emit_counted_event(&data, 1) == 0); assert(counts.attempted == 1 && counts.failed == 0 && output_calls == 1); output_result = -11; assert(emit_counted_event(&data, 1) == -11); assert(counts.attempted == 2 && counts.failed == 1 && output_calls == 2); lookup_missing = 1; assert(emit_counted_event(&data, 1) == -11); assert(counts.attempted == 2 && counts.failed == 1 && output_calls == 3); return 0; }