github.com/vishvananda/netlink@v1.3.0/nl/xfrm_state_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 *XfrmUsersaId) write(b []byte) { 11 native := NativeEndian() 12 msg.Daddr.write(b[0:SizeofXfrmAddress]) 13 native.PutUint32(b[SizeofXfrmAddress:SizeofXfrmAddress+4], msg.Spi) 14 native.PutUint16(b[SizeofXfrmAddress+4:SizeofXfrmAddress+6], msg.Family) 15 b[SizeofXfrmAddress+6] = msg.Proto 16 b[SizeofXfrmAddress+7] = msg.Pad 17 } 18 19 func (msg *XfrmUsersaId) serializeSafe() []byte { 20 b := make([]byte, SizeofXfrmUsersaId) 21 msg.write(b) 22 return b 23 } 24 25 func deserializeXfrmUsersaIdSafe(b []byte) *XfrmUsersaId { 26 var msg = XfrmUsersaId{} 27 binary.Read(bytes.NewReader(b[0:SizeofXfrmUsersaId]), NativeEndian(), &msg) 28 return &msg 29 } 30 31 func TestXfrmUsersaIdDeserializeSerialize(t *testing.T) { 32 var orig = make([]byte, SizeofXfrmUsersaId) 33 rand.Read(orig) 34 safemsg := deserializeXfrmUsersaIdSafe(orig) 35 msg := DeserializeXfrmUsersaId(orig) 36 testDeserializeSerialize(t, orig, safemsg, msg) 37 } 38 39 func (msg *XfrmStats) write(b []byte) { 40 native := NativeEndian() 41 native.PutUint32(b[0:4], msg.ReplayWindow) 42 native.PutUint32(b[4:8], msg.Replay) 43 native.PutUint32(b[8:12], msg.IntegrityFailed) 44 } 45 46 func (msg *XfrmStats) serializeSafe() []byte { 47 b := make([]byte, SizeofXfrmStats) 48 msg.write(b) 49 return b 50 } 51 52 func deserializeXfrmStatsSafe(b []byte) *XfrmStats { 53 var msg = XfrmStats{} 54 binary.Read(bytes.NewReader(b[0:SizeofXfrmStats]), NativeEndian(), &msg) 55 return &msg 56 } 57 58 func TestXfrmStatsDeserializeSerialize(t *testing.T) { 59 var orig = make([]byte, SizeofXfrmStats) 60 rand.Read(orig) 61 safemsg := deserializeXfrmStatsSafe(orig) 62 msg := DeserializeXfrmStats(orig) 63 testDeserializeSerialize(t, orig, safemsg, msg) 64 } 65 66 func (msg *XfrmUsersaInfo) write(b []byte) { 67 const IdEnd = SizeofXfrmSelector + SizeofXfrmId 68 const AddressEnd = IdEnd + SizeofXfrmAddress 69 const CfgEnd = AddressEnd + SizeofXfrmLifetimeCfg 70 const CurEnd = CfgEnd + SizeofXfrmLifetimeCur 71 const StatsEnd = CurEnd + SizeofXfrmStats 72 native := NativeEndian() 73 msg.Sel.write(b[0:SizeofXfrmSelector]) 74 msg.Id.write(b[SizeofXfrmSelector:IdEnd]) 75 msg.Saddr.write(b[IdEnd:AddressEnd]) 76 msg.Lft.write(b[AddressEnd:CfgEnd]) 77 msg.Curlft.write(b[CfgEnd:CurEnd]) 78 msg.Stats.write(b[CurEnd:StatsEnd]) 79 native.PutUint32(b[StatsEnd:StatsEnd+4], msg.Seq) 80 native.PutUint32(b[StatsEnd+4:StatsEnd+8], msg.Reqid) 81 native.PutUint16(b[StatsEnd+8:StatsEnd+10], msg.Family) 82 b[StatsEnd+10] = msg.Mode 83 b[StatsEnd+11] = msg.ReplayWindow 84 b[StatsEnd+12] = msg.Flags 85 copy(b[StatsEnd+13:StatsEnd+20], msg.Pad[:]) 86 } 87 88 func (msg *XfrmUsersaInfo) serializeSafe() []byte { 89 b := make([]byte, SizeofXfrmUsersaInfo) 90 msg.write(b) 91 return b 92 } 93 94 func deserializeXfrmUsersaInfoSafe(b []byte) *XfrmUsersaInfo { 95 var msg = XfrmUsersaInfo{} 96 binary.Read(bytes.NewReader(b[0:SizeofXfrmUsersaInfo]), NativeEndian(), &msg) 97 return &msg 98 } 99 100 func TestXfrmUsersaInfoDeserializeSerialize(t *testing.T) { 101 var orig = make([]byte, SizeofXfrmUsersaInfo) 102 rand.Read(orig) 103 safemsg := deserializeXfrmUsersaInfoSafe(orig) 104 msg := DeserializeXfrmUsersaInfo(orig) 105 testDeserializeSerialize(t, orig, safemsg, msg) 106 } 107 108 func (msg *XfrmAlgo) write(b []byte) { 109 native := NativeEndian() 110 copy(b[0:64], msg.AlgName[:]) 111 native.PutUint32(b[64:68], msg.AlgKeyLen) 112 copy(b[68:msg.Len()], msg.AlgKey[:]) 113 } 114 115 func (msg *XfrmAlgo) serializeSafe() []byte { 116 b := make([]byte, msg.Len()) 117 msg.write(b) 118 return b 119 } 120 121 func (msg *XfrmUserSpiInfo) write(b []byte) { 122 native := NativeEndian() 123 msg.XfrmUsersaInfo.write(b[0:SizeofXfrmUsersaInfo]) 124 native.PutUint32(b[SizeofXfrmUsersaInfo:SizeofXfrmUsersaInfo+4], msg.Min) 125 native.PutUint32(b[SizeofXfrmUsersaInfo+4:SizeofXfrmUsersaInfo+8], msg.Max) 126 } 127 128 func (msg *XfrmUserSpiInfo) serializeSafe() []byte { 129 b := make([]byte, SizeofXfrmUserSpiInfo) 130 msg.write(b) 131 return b 132 } 133 134 func deserializeXfrmUserSpiInfoSafe(b []byte) *XfrmUserSpiInfo { 135 var msg = XfrmUserSpiInfo{} 136 binary.Read(bytes.NewReader(b[0:SizeofXfrmUserSpiInfo]), NativeEndian(), &msg) 137 return &msg 138 } 139 140 func TestXfrmUserSpiInfoDeserializeSerialize(t *testing.T) { 141 var orig = make([]byte, SizeofXfrmUserSpiInfo) 142 rand.Read(orig) 143 safemsg := deserializeXfrmUserSpiInfoSafe(orig) 144 msg := DeserializeXfrmUserSpiInfo(orig) 145 testDeserializeSerialize(t, orig, safemsg, msg) 146 } 147 148 func deserializeXfrmAlgoSafe(b []byte) *XfrmAlgo { 149 var msg = XfrmAlgo{} 150 copy(msg.AlgName[:], b[0:64]) 151 binary.Read(bytes.NewReader(b[64:68]), NativeEndian(), &msg.AlgKeyLen) 152 msg.AlgKey = b[68:msg.Len()] 153 return &msg 154 } 155 156 func TestXfrmAlgoDeserializeSerialize(t *testing.T) { 157 native := NativeEndian() 158 // use a 32 byte key len 159 var orig = make([]byte, SizeofXfrmAlgo+32) 160 rand.Read(orig) 161 // set the key len to 256 bits 162 var KeyLen uint32 = 0x00000100 163 // Little Endian Big Endian 164 // orig[64] = 0 orig[64] = 0 165 // orig[65] = 1 orig[65] = 0 166 // orig[66] = 0 orig[66] = 1 167 // orig[67] = 0 orig[67] = 0 168 native.PutUint32(orig[64:68], KeyLen) 169 safemsg := deserializeXfrmAlgoSafe(orig) 170 msg := DeserializeXfrmAlgo(orig) 171 testDeserializeSerialize(t, orig, safemsg, msg) 172 } 173 174 func (msg *XfrmAlgoAuth) write(b []byte) { 175 native := NativeEndian() 176 copy(b[0:64], msg.AlgName[:]) 177 native.PutUint32(b[64:68], msg.AlgKeyLen) 178 native.PutUint32(b[68:72], msg.AlgTruncLen) 179 copy(b[72:msg.Len()], msg.AlgKey[:]) 180 } 181 182 func (msg *XfrmAlgoAuth) serializeSafe() []byte { 183 b := make([]byte, msg.Len()) 184 msg.write(b) 185 return b 186 } 187 188 func deserializeXfrmAlgoAuthSafe(b []byte) *XfrmAlgoAuth { 189 var msg = XfrmAlgoAuth{} 190 copy(msg.AlgName[:], b[0:64]) 191 binary.Read(bytes.NewReader(b[64:68]), NativeEndian(), &msg.AlgKeyLen) 192 binary.Read(bytes.NewReader(b[68:72]), NativeEndian(), &msg.AlgTruncLen) 193 msg.AlgKey = b[72:msg.Len()] 194 return &msg 195 } 196 197 func TestXfrmAlgoAuthDeserializeSerialize(t *testing.T) { 198 native := NativeEndian() 199 // use a 32 byte key len 200 var orig = make([]byte, SizeofXfrmAlgoAuth+32) 201 rand.Read(orig) 202 // set the key len to 256 bits 203 var KeyLen uint32 = 0x00000100 204 // Little Endian Big Endian 205 // orig[64] = 0 orig[64] = 0 206 // orig[65] = 1 orig[65] = 0 207 // orig[66] = 0 orig[66] = 1 208 // orig[67] = 0 orig[67] = 0 209 native.PutUint32(orig[64:68], KeyLen) 210 safemsg := deserializeXfrmAlgoAuthSafe(orig) 211 msg := DeserializeXfrmAlgoAuth(orig) 212 testDeserializeSerialize(t, orig, safemsg, msg) 213 } 214 215 func (msg *XfrmEncapTmpl) write(b []byte) { 216 native := NativeEndian() 217 native.PutUint16(b[0:2], msg.EncapType) 218 native.PutUint16(b[2:4], msg.EncapSport) 219 native.PutUint16(b[4:6], msg.EncapDport) 220 copy(b[6:8], msg.Pad[:]) 221 msg.EncapOa.write(b[8:SizeofXfrmAddress]) 222 } 223 224 func (msg *XfrmEncapTmpl) serializeSafe() []byte { 225 b := make([]byte, SizeofXfrmEncapTmpl) 226 msg.write(b) 227 return b 228 } 229 230 func deserializeXfrmEncapTmplSafe(b []byte) *XfrmEncapTmpl { 231 var msg = XfrmEncapTmpl{} 232 binary.Read(bytes.NewReader(b[0:SizeofXfrmEncapTmpl]), NativeEndian(), &msg) 233 return &msg 234 } 235 236 func TestXfrmEncapTmplDeserializeSerialize(t *testing.T) { 237 var orig = make([]byte, SizeofXfrmEncapTmpl) 238 rand.Read(orig) 239 safemsg := deserializeXfrmEncapTmplSafe(orig) 240 msg := DeserializeXfrmEncapTmpl(orig) 241 testDeserializeSerialize(t, orig, safemsg, msg) 242 } 243 244 func (msg *XfrmMark) write(b []byte) { 245 native := NativeEndian() 246 native.PutUint32(b[0:4], msg.Value) 247 native.PutUint32(b[4:8], msg.Mask) 248 } 249 250 func (msg *XfrmMark) serializeSafe() []byte { 251 b := make([]byte, SizeofXfrmMark) 252 msg.write(b) 253 return b 254 } 255 256 func deserializeXfrmMarkSafe(b []byte) *XfrmMark { 257 var msg = XfrmMark{} 258 binary.Read(bytes.NewReader(b[0:SizeofXfrmMark]), NativeEndian(), &msg) 259 return &msg 260 } 261 262 func TestXfrmMarkDeserializeSerialize(t *testing.T) { 263 var orig = make([]byte, SizeofXfrmMark) 264 rand.Read(orig) 265 safemsg := deserializeXfrmMarkSafe(orig) 266 msg := DeserializeXfrmMark(orig) 267 testDeserializeSerialize(t, orig, safemsg, msg) 268 } 269 270 func (msg *XfrmAlgoAEAD) write(b []byte) { 271 native := NativeEndian() 272 copy(b[0:64], msg.AlgName[:]) 273 native.PutUint32(b[64:68], msg.AlgKeyLen) 274 native.PutUint32(b[68:72], msg.AlgICVLen) 275 copy(b[72:msg.Len()], msg.AlgKey[:]) 276 } 277 278 func (msg *XfrmAlgoAEAD) serializeSafe() []byte { 279 b := make([]byte, msg.Len()) 280 msg.write(b) 281 return b 282 } 283 284 func deserializeXfrmAlgoAEADSafe(b []byte) *XfrmAlgoAEAD { 285 var msg = XfrmAlgoAEAD{} 286 copy(msg.AlgName[:], b[0:64]) 287 binary.Read(bytes.NewReader(b[64:68]), NativeEndian(), &msg.AlgKeyLen) 288 binary.Read(bytes.NewReader(b[68:72]), NativeEndian(), &msg.AlgICVLen) 289 msg.AlgKey = b[72:msg.Len()] 290 return &msg 291 } 292 293 func TestXfrmXfrmAlgoAeadDeserializeSerialize(t *testing.T) { 294 native := NativeEndian() 295 // use a 32 byte key len 296 var orig = make([]byte, SizeofXfrmAlgoAEAD+36) 297 rand.Read(orig) 298 // set the key len to (256 + 32) bits 299 var KeyLen uint32 = 0x00000120 300 native.PutUint32(orig[64:68], KeyLen) 301 safemsg := deserializeXfrmAlgoAEADSafe(orig) 302 msg := DeserializeXfrmAlgoAEAD(orig) 303 testDeserializeSerialize(t, orig, safemsg, msg) 304 }