Brenden Blanco | 5a888c7 | 2015-08-24 21:33:25 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) PLUMgrid, Inc. |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | |
| 5 | from bpf import BPF |
| 6 | import os |
Brenden Blanco | 5a888c7 | 2015-08-24 21:33:25 -0700 | [diff] [blame] | 7 | import sys |
| 8 | from unittest import main, TestCase |
| 9 | |
| 10 | class TestKprobeRgx(TestCase): |
| 11 | def setUp(self): |
| 12 | self.b = BPF(text=""" |
| 13 | typedef struct { int idx; } Key; |
| 14 | typedef struct { u64 val; } Val; |
| 15 | BPF_TABLE("array", Key, Val, stats, 3); |
| 16 | int hello(void *ctx) { |
| 17 | stats.lookup_or_init(&(Key){1}, &(Val){0})->val++; |
| 18 | return 0; |
| 19 | } |
| 20 | int goodbye(void *ctx) { |
| 21 | stats.lookup_or_init(&(Key){2}, &(Val){0})->val++; |
| 22 | return 0; |
| 23 | } |
| 24 | """) |
Brenden Blanco | b71f9fa | 2015-08-24 23:55:59 -0700 | [diff] [blame] | 25 | self.b.attach_kprobe(event_re="^SyS_bp.*", fn_name="hello") |
| 26 | self.b.attach_kretprobe(event_re="^SyS_bp.*", fn_name="goodbye") |
Brenden Blanco | 5a888c7 | 2015-08-24 21:33:25 -0700 | [diff] [blame] | 27 | |
| 28 | def test_send1(self): |
Brenden Blanco | 5a888c7 | 2015-08-24 21:33:25 -0700 | [diff] [blame] | 29 | k1 = self.b["stats"].Key(1) |
| 30 | k2 = self.b["stats"].Key(2) |
Brenden Blanco | b71f9fa | 2015-08-24 23:55:59 -0700 | [diff] [blame] | 31 | self.assertEqual(self.b["stats"][k1].val, self.b["stats"][k2].val + 1) |
Brenden Blanco | 5a888c7 | 2015-08-24 21:33:25 -0700 | [diff] [blame] | 32 | |
| 33 | if __name__ == "__main__": |
| 34 | main() |