github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/fou_test.go (about) 1 //go:build linux 2 // +build linux 3 4 package netlink 5 6 import ( 7 "testing" 8 ) 9 10 func TestFouDeserializeMsg(t *testing.T) { 11 var msg []byte 12 13 // deserialize a valid message 14 msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0, 0, 6, 0, 1, 0, 21, 179, 0, 0, 5, 0, 3, 0, 4, 0, 0, 0, 5, 0, 4, 0, 1, 0, 0, 0} 15 if fou, err := deserializeFouMsg(msg); err != nil { 16 t.Error(err.Error()) 17 } else { 18 19 // check if message was deserialized correctly 20 if fou.Family != FAMILY_V4 { 21 t.Errorf("expected family %d, got %d", FAMILY_V4, fou.Family) 22 } 23 24 if fou.Port != 5555 { 25 t.Errorf("expected port 5555, got %d", fou.Port) 26 } 27 28 if fou.Protocol != 4 { // ipip 29 t.Errorf("expected protocol 4, got %d", fou.Protocol) 30 } 31 32 if fou.EncapType != FOU_ENCAP_DIRECT { 33 t.Errorf("expected encap type %d, got %d", FOU_ENCAP_DIRECT, fou.EncapType) 34 } 35 } 36 37 // deserialize truncated attribute header 38 msg = []byte{3, 1, 0, 0, 5, 0} 39 if _, err := deserializeFouMsg(msg); err == nil { 40 t.Error("expected attribute header truncated error") 41 } else if err != ErrAttrHeaderTruncated { 42 t.Errorf("unexpected error: %s", err.Error()) 43 } 44 45 // deserialize truncated attribute header 46 msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0} 47 if _, err := deserializeFouMsg(msg); err == nil { 48 t.Error("expected attribute body truncated error") 49 } else if err != ErrAttrBodyTruncated { 50 t.Errorf("unexpected error: %s", err.Error()) 51 } 52 } 53 54 func TestFouAddDel(t *testing.T) { 55 // foo-over-udp was merged in 3.18 so skip these tests if the kernel is too old 56 minKernelRequired(t, 3, 18) 57 58 // the fou module is usually not compiled in the kernel so we'll load it 59 tearDown := setUpNetlinkTestWithKModule(t, "fou") 60 defer tearDown() 61 62 fou := Fou{ 63 Port: 5555, 64 Family: FAMILY_V4, 65 Protocol: 4, // ipip 66 EncapType: FOU_ENCAP_DIRECT, 67 } 68 69 if err := FouAdd(fou); err != nil { 70 t.Fatal(err) 71 } 72 73 list, err := FouList(FAMILY_V4) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 if len(list) != 1 { 79 t.Fatalf("expected 1 fou, got %d", len(list)) 80 } 81 82 if list[0].Port != fou.Port { 83 t.Errorf("expected port %d, got %d", fou.Port, list[0].Port) 84 } 85 86 if list[0].Family != fou.Family { 87 t.Errorf("expected family %d, got %d", fou.Family, list[0].Family) 88 } 89 90 if list[0].Protocol != fou.Protocol { 91 t.Errorf("expected protocol %d, got %d", fou.Protocol, list[0].Protocol) 92 } 93 94 if list[0].EncapType != fou.EncapType { 95 t.Errorf("expected encaptype %d, got %d", fou.EncapType, list[0].EncapType) 96 } 97 98 if err := FouDel(Fou{Port: fou.Port, Family: fou.Family}); err != nil { 99 t.Fatal(err) 100 } 101 102 list, err = FouList(FAMILY_V4) 103 if err != nil { 104 t.Fatal(err) 105 } 106 107 if len(list) != 0 { 108 t.Fatalf("expected 0 fou, got %d", len(list)) 109 } 110 }