blob: 2cb94c06025bb08d7eafcad85db0ba948b1471fd [file] [log] [blame]
Brenden Blanco5a888c72015-08-24 21:33:25 -07001#!/usr/bin/env python
2# Copyright (c) PLUMgrid, Inc.
3# Licensed under the Apache License, Version 2.0 (the "License")
4
5from bpf import BPF
6import os
Brenden Blanco5a888c72015-08-24 21:33:25 -07007import sys
8from unittest import main, TestCase
9
10class 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 Blancob71f9fa2015-08-24 23:55:59 -070025 self.b.attach_kprobe(event_re="^SyS_bp.*", fn_name="hello")
26 self.b.attach_kretprobe(event_re="^SyS_bp.*", fn_name="goodbye")
Brenden Blanco5a888c72015-08-24 21:33:25 -070027
28 def test_send1(self):
Brenden Blanco5a888c72015-08-24 21:33:25 -070029 k1 = self.b["stats"].Key(1)
30 k2 = self.b["stats"].Key(2)
Brenden Blancob71f9fa2015-08-24 23:55:59 -070031 self.assertEqual(self.b["stats"][k1].val, self.b["stats"][k2].val + 1)
Brenden Blanco5a888c72015-08-24 21:33:25 -070032
33if __name__ == "__main__":
34 main()