github.com/jayanthvn/pure-gobpf@v0.0.0-20230623131354-8d1d959d9e0b/pkg/elfparser/elf_test.go (about)

     1  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License").
     4  // You may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  
    15  package elfparser
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/golang/mock/gomock"
    22  	mock_ebpf_maps "github.com/jayanthvn/pure-gobpf/pkg/ebpf_maps/mocks"
    23  	mock_ebpf_progs "github.com/jayanthvn/pure-gobpf/pkg/ebpf_progs/mocks"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  type testMocks struct {
    28  	path       string
    29  	ctrl       *gomock.Controller
    30  	ebpf_progs *mock_ebpf_progs.MockBpfProgAPIs
    31  	ebpf_maps  *mock_ebpf_maps.MockBpfMapAPIs
    32  }
    33  
    34  func setup(t *testing.T, testPath string) *testMocks {
    35  	ctrl := gomock.NewController(t)
    36  	return &testMocks{
    37  		path:       testPath,
    38  		ctrl:       ctrl,
    39  		ebpf_progs: mock_ebpf_progs.NewMockBpfProgAPIs(ctrl),
    40  		ebpf_maps:  mock_ebpf_maps.NewMockBpfMapAPIs(ctrl),
    41  	}
    42  }
    43  
    44  func TestLoadelf(t *testing.T) {
    45  	m := setup(t, "../../test-data/tc.ingress.bpf.elf")
    46  	defer m.ctrl.Finish()
    47  	f, _ := os.Open(m.path)
    48  	defer f.Close()
    49  
    50  	ctrl := gomock.NewController(t)
    51  	mockAPIs := mock_ebpf_maps.NewMockBpfMapAPIs(ctrl)
    52  	mockProgAPIs := mock_ebpf_progs.NewMockBpfProgAPIs(ctrl)
    53  
    54  	mockAPIs.EXPECT().CreateMap(gomock.Any()).AnyTimes()
    55  	mockProgAPIs.EXPECT().LoadProg(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
    56  	mockAPIs.EXPECT().PinMap(gomock.Any()).AnyTimes()
    57  	mockAPIs.EXPECT().BpfGetMapFromPinPath(gomock.Any()).AnyTimes()
    58  	mockProgAPIs.EXPECT().BpfGetProgFromPinPath(gomock.Any()).AnyTimes()
    59  	mockProgAPIs.EXPECT().GetBPFProgAssociatedMapsIDs(gomock.Any()).AnyTimes()
    60  
    61  	_, _, err := doLoadELF(f, mockAPIs, mockProgAPIs, "test")
    62  	assert.NoError(t, err)
    63  }
    64  
    65  func TestLoadelfWithoutReloc(t *testing.T) {
    66  	m := setup(t, "../../test-data/tc.bpf.elf")
    67  	defer m.ctrl.Finish()
    68  	f, _ := os.Open(m.path)
    69  	defer f.Close()
    70  
    71  	ctrl := gomock.NewController(t)
    72  	mockAPIs := mock_ebpf_maps.NewMockBpfMapAPIs(ctrl)
    73  	mockProgAPIs := mock_ebpf_progs.NewMockBpfProgAPIs(ctrl)
    74  
    75  	mockAPIs.EXPECT().CreateMap(gomock.Any()).AnyTimes()
    76  	mockProgAPIs.EXPECT().LoadProg(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
    77  	mockAPIs.EXPECT().PinMap(gomock.Any()).AnyTimes()
    78  	mockAPIs.EXPECT().BpfGetMapFromPinPath(gomock.Any()).AnyTimes()
    79  	mockProgAPIs.EXPECT().BpfGetProgFromPinPath(gomock.Any()).AnyTimes()
    80  	mockProgAPIs.EXPECT().GetBPFProgAssociatedMapsIDs(gomock.Any()).AnyTimes()
    81  
    82  	_, _, err := doLoadELF(f, mockAPIs, mockProgAPIs, "test")
    83  	assert.NoError(t, err)
    84  }
    85  
    86  func TestLoadelfWithoutProg(t *testing.T) {
    87  	m := setup(t, "../../test-data/test.map.bpf.elf")
    88  	defer m.ctrl.Finish()
    89  	f, _ := os.Open(m.path)
    90  	defer f.Close()
    91  
    92  	ctrl := gomock.NewController(t)
    93  	mockAPIs := mock_ebpf_maps.NewMockBpfMapAPIs(ctrl)
    94  	mockProgAPIs := mock_ebpf_progs.NewMockBpfProgAPIs(ctrl)
    95  
    96  	mockAPIs.EXPECT().CreateMap(gomock.Any()).AnyTimes()
    97  	mockProgAPIs.EXPECT().LoadProg(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
    98  	mockAPIs.EXPECT().PinMap(gomock.Any()).AnyTimes()
    99  	mockAPIs.EXPECT().BpfGetMapFromPinPath(gomock.Any()).AnyTimes()
   100  	mockProgAPIs.EXPECT().BpfGetProgFromPinPath(gomock.Any()).AnyTimes()
   101  	mockProgAPIs.EXPECT().GetBPFProgAssociatedMapsIDs(gomock.Any()).AnyTimes()
   102  
   103  	_, _, err := doLoadELF(f, mockAPIs, mockProgAPIs, "test")
   104  	assert.NoError(t, err)
   105  }