1 /* 2 * Copyright (c) 2018 Politecnico di Torino 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "BPF.h" 18 19 #include "catch.hpp" 20 21 TEST_CASE("test prog table", "[prog_table]") { 22 const std::string BPF_PROGRAM = R"( 23 BPF_TABLE("prog", int, int, myprog, 16); 24 )"; 25 26 const std::string BPF_PROGRAM2 = R"( 27 int hello(struct __sk_buff *skb) { 28 return 1; 29 } 30 )"; 31 32 ebpf::StatusTuple res(0); 33 34 ebpf::BPF bpf; 35 res = bpf.init(BPF_PROGRAM); 36 REQUIRE(res.code() == 0); 37 38 ebpf::BPFProgTable t = bpf.get_prog_table("myprog"); 39 40 ebpf::BPF bpf2; 41 res = bpf2.init(BPF_PROGRAM2); 42 REQUIRE(res.code() == 0); 43 44 int fd; 45 res = bpf2.load_func("hello", BPF_PROG_TYPE_SCHED_CLS, fd); 46 REQUIRE(res.code() == 0); 47 48 SECTION("update and remove") { 49 // update element 50 res = t.update_value(0, fd); 51 REQUIRE(res.code() == 0); 52 53 // remove element 54 res = t.remove_value(0); 55 REQUIRE(res.code() == 0); 56 57 // update out of range element 58 res = t.update_value(17, fd); 59 REQUIRE(res.code() != 0); 60 61 // remove out of range element 62 res = t.remove_value(17); 63 REQUIRE(res.code() != 0); 64 } 65 } 66