github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/emulator/maps_hash_lru_test.go (about)

     1  package emulator
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"testing"
     6  
     7  	"github.com/dylandreimerink/gobpfld"
     8  	"github.com/dylandreimerink/gobpfld/bpfsys"
     9  	"github.com/dylandreimerink/gobpfld/bpftypes"
    10  )
    11  
    12  func TestEmulatedHashMapLRU(t *testing.T) {
    13  	m := HashMapLRU{
    14  		AbstractMap: AbstractMap{
    15  			Name: "LRU map",
    16  			Def: gobpfld.BPFMapDef{
    17  				Type:       bpftypes.BPF_MAP_TYPE_LRU_HASH,
    18  				KeySize:    4,
    19  				ValueSize:  4,
    20  				MaxEntries: 5,
    21  			},
    22  		},
    23  	}
    24  
    25  	err := m.Init()
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	makeMemPtr := func(i int) *MemoryPtr {
    31  		im := newIMM(int64(i))
    32  		return &MemoryPtr{
    33  			Memory: &ValueMemory{
    34  				Mapping: []RegisterValue{
    35  					im,
    36  					im,
    37  					im,
    38  					im,
    39  				},
    40  			},
    41  		}
    42  	}
    43  
    44  	k1 := makeMemPtr(1)
    45  	k2 := makeMemPtr(2)
    46  	k3 := makeMemPtr(3)
    47  	k4 := makeMemPtr(4)
    48  	k5 := makeMemPtr(5)
    49  	k6 := makeMemPtr(6)
    50  
    51  	// Add 5 values, filling the map
    52  	_, err = m.Update(k1, makeMemPtr(11), bpfsys.BPFMapElemAny)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	_, err = m.Update(k2, makeMemPtr(12), bpfsys.BPFMapElemAny)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	_, err = m.Update(k3, makeMemPtr(13), bpfsys.BPFMapElemAny)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	_, err = m.Update(k4, makeMemPtr(14), bpfsys.BPFMapElemAny)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	_, err = m.Update(k5, makeMemPtr(15), bpfsys.BPFMapElemAny)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	// Key 1 was added first, so it is least recently used.
    74  
    75  	// Lookup 1 and 2, no 3 should be least recently used
    76  	_, err = m.Lookup(k1)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	_, err = m.Lookup(k2)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	// Insert a sixth element, which should overwrite 3
    86  	_, err = m.Update(k6, makeMemPtr(16), bpfsys.BPFMapElemAny)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	kh := func(key *MemoryPtr) hashKey {
    92  		b, err := key.ReadRange(0, 4)
    93  		if err != nil {
    94  			t.Fatal(err)
    95  		}
    96  
    97  		return sha256.Sum256(b)
    98  	}
    99  
   100  	k1h := kh(k1)
   101  	k2h := kh(k2)
   102  	k4h := kh(k4)
   103  	k5h := kh(k5)
   104  	k6h := kh(k6)
   105  
   106  	// 6 should be the most recent since it was added last
   107  	if m.UsageList[0] != k6h {
   108  		t.Fatal("usage list 0 != k6h")
   109  	}
   110  	// 2 should be second due to the most recent lookup
   111  	if m.UsageList[1] != k2h {
   112  		t.Fatal("usage list 0 != k2h")
   113  	}
   114  	// 1 should be third due to the lookup
   115  	if m.UsageList[2] != k1h {
   116  		t.Fatal("usage list 0 != k1h")
   117  	}
   118  	if m.UsageList[3] != k5h {
   119  		t.Fatal("usage list 0 != k5h")
   120  	}
   121  	if m.UsageList[4] != k4h {
   122  		t.Fatal("usage list 0 != k4h")
   123  	}
   124  }