1#!/usr/bin/env python 2# Copyright (c) PLUMgrid, Inc. 3# Licensed under the Apache License, Version 2.0 (the "License") 4 5from bcc import BPF 6from simulation import Simulation 7import sys 8import os 9import tempfile 10from unittest import main, TestCase 11 12 13error_msg = "R0 invalid mem access 'map_value_or_null'\n" 14 15text = """ 16 #include <uapi/linux/ptrace.h> 17 #include <bcc/proto.h> 18 BPF_HASH(t1, int, int, 10); 19 int sim_port(struct __sk_buff *skb) { 20 int x = 0, *y; 21 """ 22repeat = """ 23 y = t1.lookup(&x); 24 if (!y) return 0; 25 x = *y; 26 """ 27end = """ 28 y = t1.lookup(&x); 29 x = *y; 30 return 0; 31 } 32 """ 33for i in range(0,300): 34 text += repeat 35text += end 36 37class TestBPFProgLoad(TestCase): 38 39 def setUp(self): 40 self.fp = tempfile.TemporaryFile() 41 os.dup2(self.fp.fileno(), sys.stderr.fileno()) 42 43 def tearDown(self): 44 self.fp.close() 45 46 47 def test_log_debug(self): 48 b = BPF(text=text, debug=2) 49 try: 50 ingress = b.load_func("sim_port",BPF.SCHED_CLS) 51 except Exception: 52 self.fp.flush() 53 self.fp.seek(0) 54 self.assertEqual(error_msg in self.fp.read().decode(), True) 55 56 57 def test_log_no_debug(self): 58 b = BPF(text=text, debug=0) 59 try: 60 ingress = b.load_func("sim_port",BPF.SCHED_CLS) 61 except Exception: 62 self.fp.flush() 63 self.fp.seek(0) 64 self.assertEqual(error_msg in self.fp.read().decode(), True) 65 66 67if __name__ == "__main__": 68 main() 69 70 71