github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/nl/xfrm_linux_test.go (about) 1 package nl 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "encoding/binary" 7 "testing" 8 ) 9 10 func (msg *XfrmAddress) write(b []byte) { 11 copy(b[0:SizeofXfrmAddress], msg[:]) 12 } 13 14 func (msg *XfrmAddress) serializeSafe() []byte { 15 b := make([]byte, SizeofXfrmAddress) 16 msg.write(b) 17 return b 18 } 19 20 func deserializeXfrmAddressSafe(b []byte) *XfrmAddress { 21 var msg = XfrmAddress{} 22 binary.Read(bytes.NewReader(b[0:SizeofXfrmAddress]), NativeEndian(), &msg) 23 return &msg 24 } 25 26 func TestXfrmAddressDeserializeSerialize(t *testing.T) { 27 var orig = make([]byte, SizeofXfrmAddress) 28 rand.Read(orig) 29 safemsg := deserializeXfrmAddressSafe(orig) 30 msg := DeserializeXfrmAddress(orig) 31 testDeserializeSerialize(t, orig, safemsg, msg) 32 } 33 34 func (msg *XfrmSelector) write(b []byte) { 35 const AddrEnd = SizeofXfrmAddress * 2 36 native := NativeEndian() 37 msg.Daddr.write(b[0:SizeofXfrmAddress]) 38 msg.Saddr.write(b[SizeofXfrmAddress:AddrEnd]) 39 native.PutUint16(b[AddrEnd:AddrEnd+2], msg.Dport) 40 native.PutUint16(b[AddrEnd+2:AddrEnd+4], msg.DportMask) 41 native.PutUint16(b[AddrEnd+4:AddrEnd+6], msg.Sport) 42 native.PutUint16(b[AddrEnd+6:AddrEnd+8], msg.SportMask) 43 native.PutUint16(b[AddrEnd+8:AddrEnd+10], msg.Family) 44 b[AddrEnd+10] = msg.PrefixlenD 45 b[AddrEnd+11] = msg.PrefixlenS 46 b[AddrEnd+12] = msg.Proto 47 copy(b[AddrEnd+13:AddrEnd+16], msg.Pad[:]) 48 native.PutUint32(b[AddrEnd+16:AddrEnd+20], uint32(msg.Ifindex)) 49 native.PutUint32(b[AddrEnd+20:AddrEnd+24], msg.User) 50 } 51 52 func (msg *XfrmSelector) serializeSafe() []byte { 53 length := SizeofXfrmSelector 54 b := make([]byte, length) 55 msg.write(b) 56 return b 57 } 58 59 func deserializeXfrmSelectorSafe(b []byte) *XfrmSelector { 60 var msg = XfrmSelector{} 61 binary.Read(bytes.NewReader(b[0:SizeofXfrmSelector]), NativeEndian(), &msg) 62 return &msg 63 } 64 65 func TestXfrmSelectorDeserializeSerialize(t *testing.T) { 66 var orig = make([]byte, SizeofXfrmSelector) 67 rand.Read(orig) 68 safemsg := deserializeXfrmSelectorSafe(orig) 69 msg := DeserializeXfrmSelector(orig) 70 testDeserializeSerialize(t, orig, safemsg, msg) 71 } 72 73 func (msg *XfrmLifetimeCfg) write(b []byte) { 74 native := NativeEndian() 75 native.PutUint64(b[0:8], msg.SoftByteLimit) 76 native.PutUint64(b[8:16], msg.HardByteLimit) 77 native.PutUint64(b[16:24], msg.SoftPacketLimit) 78 native.PutUint64(b[24:32], msg.HardPacketLimit) 79 native.PutUint64(b[32:40], msg.SoftAddExpiresSeconds) 80 native.PutUint64(b[40:48], msg.HardAddExpiresSeconds) 81 native.PutUint64(b[48:56], msg.SoftUseExpiresSeconds) 82 native.PutUint64(b[56:64], msg.HardUseExpiresSeconds) 83 } 84 85 func (msg *XfrmLifetimeCfg) serializeSafe() []byte { 86 length := SizeofXfrmLifetimeCfg 87 b := make([]byte, length) 88 msg.write(b) 89 return b 90 } 91 92 func deserializeXfrmLifetimeCfgSafe(b []byte) *XfrmLifetimeCfg { 93 var msg = XfrmLifetimeCfg{} 94 binary.Read(bytes.NewReader(b[0:SizeofXfrmLifetimeCfg]), NativeEndian(), &msg) 95 return &msg 96 } 97 98 func TestXfrmLifetimeCfgDeserializeSerialize(t *testing.T) { 99 var orig = make([]byte, SizeofXfrmLifetimeCfg) 100 rand.Read(orig) 101 safemsg := deserializeXfrmLifetimeCfgSafe(orig) 102 msg := DeserializeXfrmLifetimeCfg(orig) 103 testDeserializeSerialize(t, orig, safemsg, msg) 104 } 105 106 func (msg *XfrmLifetimeCur) write(b []byte) { 107 native := NativeEndian() 108 native.PutUint64(b[0:8], msg.Bytes) 109 native.PutUint64(b[8:16], msg.Packets) 110 native.PutUint64(b[16:24], msg.AddTime) 111 native.PutUint64(b[24:32], msg.UseTime) 112 } 113 114 func (msg *XfrmLifetimeCur) serializeSafe() []byte { 115 length := SizeofXfrmLifetimeCur 116 b := make([]byte, length) 117 msg.write(b) 118 return b 119 } 120 121 func deserializeXfrmLifetimeCurSafe(b []byte) *XfrmLifetimeCur { 122 var msg = XfrmLifetimeCur{} 123 binary.Read(bytes.NewReader(b[0:SizeofXfrmLifetimeCur]), NativeEndian(), &msg) 124 return &msg 125 } 126 127 func TestXfrmLifetimeCurDeserializeSerialize(t *testing.T) { 128 var orig = make([]byte, SizeofXfrmLifetimeCur) 129 rand.Read(orig) 130 safemsg := deserializeXfrmLifetimeCurSafe(orig) 131 msg := DeserializeXfrmLifetimeCur(orig) 132 testDeserializeSerialize(t, orig, safemsg, msg) 133 } 134 135 func (msg *XfrmId) write(b []byte) { 136 native := NativeEndian() 137 msg.Daddr.write(b[0:SizeofXfrmAddress]) 138 native.PutUint32(b[SizeofXfrmAddress:SizeofXfrmAddress+4], msg.Spi) 139 b[SizeofXfrmAddress+4] = msg.Proto 140 copy(b[SizeofXfrmAddress+5:SizeofXfrmAddress+8], msg.Pad[:]) 141 } 142 143 func (msg *XfrmId) serializeSafe() []byte { 144 b := make([]byte, SizeofXfrmId) 145 msg.write(b) 146 return b 147 } 148 149 func deserializeXfrmIdSafe(b []byte) *XfrmId { 150 var msg = XfrmId{} 151 binary.Read(bytes.NewReader(b[0:SizeofXfrmId]), NativeEndian(), &msg) 152 return &msg 153 } 154 155 func TestXfrmIdDeserializeSerialize(t *testing.T) { 156 var orig = make([]byte, SizeofXfrmId) 157 rand.Read(orig) 158 safemsg := deserializeXfrmIdSafe(orig) 159 msg := DeserializeXfrmId(orig) 160 testDeserializeSerialize(t, orig, safemsg, msg) 161 }