Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2018 Clevernet, Inc. |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | |
| 5 | import unittest |
| 6 | from bcc import BPF |
| 7 | |
| 8 | class TestLicense(unittest.TestCase): |
| 9 | gpl_only_text = """ |
| 10 | #include <uapi/linux/ptrace.h> |
| 11 | BPF_STACK_TRACE(stack_traces, 10240); |
| 12 | struct gpl_s { |
| 13 | u64 ts; |
| 14 | int id; |
| 15 | }; |
| 16 | BPF_PERF_OUTPUT(events); |
| 17 | int license_program(struct pt_regs *ctx) { |
| 18 | struct gpl_s data = {}; |
| 19 | data.id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID); |
| 20 | data.ts = bpf_ktime_get_ns(); |
| 21 | events.perf_submit(ctx, &data, sizeof(data)); |
| 22 | return 0; |
| 23 | } |
| 24 | """ |
| 25 | |
| 26 | proprietary_text = """ |
| 27 | #include <uapi/linux/ptrace.h> |
| 28 | struct key_t { |
| 29 | u64 ip; |
| 30 | u32 pid; |
| 31 | u32 uid; |
| 32 | char comm[16]; |
| 33 | }; |
| 34 | |
| 35 | BPF_HASH(counts, struct key_t); |
| 36 | |
| 37 | int license_program(struct pt_regs *ctx) { |
| 38 | struct key_t key = {}; |
| 39 | u64 zero = 0 , *val; |
| 40 | u64 pid = bpf_get_current_pid_tgid(); |
| 41 | u32 uid = bpf_get_current_uid_gid(); |
| 42 | |
| 43 | key.ip = PT_REGS_IP(ctx); |
| 44 | key.pid = pid & 0xFFFFFFFF; |
| 45 | key.uid = uid & 0xFFFFFFFF; |
| 46 | bpf_get_current_comm(&(key.comm), 16); |
| 47 | |
| 48 | val = counts.lookup_or_init(&key, &zero); // update counter |
| 49 | (*val)++; |
| 50 | return 0; |
| 51 | } |
| 52 | """ |
| 53 | |
| 54 | def license(self, lic): |
| 55 | return ''' |
| 56 | #define BPF_LICENSE %s |
| 57 | ''' % (lic) |
| 58 | |
| 59 | def load_bpf_code(self, bpf_code): |
| 60 | event_name = bpf_code.get_syscall_fnname("read") |
| 61 | bpf_code.attach_kprobe(event=event_name, fn_name="license_program") |
| 62 | bpf_code.detach_kprobe(event=event_name) |
| 63 | |
| 64 | def test_default(self): |
| 65 | b = BPF(text=self.gpl_only_text) |
| 66 | self.load_bpf_code(b) |
| 67 | |
Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 68 | def test_gpl_helper_macro(self): |
| 69 | b = BPF(text=self.gpl_only_text + self.license('GPL')) |
| 70 | self.load_bpf_code(b) |
| 71 | |
Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 72 | def test_proprietary_macro(self): |
| 73 | b = BPF(text=self.proprietary_text + self.license('Proprietary')) |
| 74 | self.load_bpf_code(b) |
| 75 | |
Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 76 | def test_gpl_compatible_macro(self): |
| 77 | b = BPF(text=self.gpl_only_text + self.license('Dual BSD/GPL')) |
| 78 | self.load_bpf_code(b) |
| 79 | |
Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 80 | def test_proprietary_words_macro(self): |
| 81 | b = BPF(text=self.proprietary_text + self.license('Proprietary license')) |
| 82 | self.load_bpf_code(b) |
| 83 | |
| 84 | @unittest.expectedFailure |
Oriol Arcas | ab5772f | 2018-06-21 12:30:35 +0200 | [diff] [blame^] | 85 | def test_cflags_fail(self): |
| 86 | b = BPF(text=self.gpl_only_text, cflags=["-DBPF_LICENSE=GPL"]) |
| 87 | self.load_bpf_code(b) |
| 88 | |
| 89 | @unittest.expectedFailure |
| 90 | def test_cflags_macro_fail(self): |
| 91 | b = BPF(text=self.gpl_only_text + self.license('GPL'), cflags=["-DBPF_LICENSE=GPL"]) |
Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 92 | self.load_bpf_code(b) |
| 93 | |
| 94 | @unittest.expectedFailure |
| 95 | def test_empty_fail_macro(self): |
| 96 | b = BPF(text=self.gpl_only_text + self.license('')) |
| 97 | self.load_bpf_code(b) |
| 98 | |
| 99 | @unittest.expectedFailure |
Oriol Arcas | ab5772f | 2018-06-21 12:30:35 +0200 | [diff] [blame^] | 100 | def test_proprietary_fail_macro(self): |
| 101 | b = BPF(text=self.gpl_only_text + self.license('Proprietary license')) |
Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 102 | self.load_bpf_code(b) |
| 103 | |
| 104 | @unittest.expectedFailure |
Oriol Arcas | ab5772f | 2018-06-21 12:30:35 +0200 | [diff] [blame^] | 105 | def test_proprietary_cflags_fail(self): |
| 106 | b = BPF(text=self.proprietary_text, cflags=["-DBPF_LICENSE=Proprietary"]) |
Oriol Arcas | 9aab22e | 2018-06-18 12:54:10 +0200 | [diff] [blame] | 107 | self.load_bpf_code(b) |
| 108 | |
| 109 | if __name__ == "__main__": |
| 110 | unittest.main() |