github.com/vishvananda/netlink@v1.3.0/nl/xfrm_policy_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 *XfrmUserpolicyId) write(b []byte) { 11 native := NativeEndian() 12 msg.Sel.write(b[0:SizeofXfrmSelector]) 13 native.PutUint32(b[SizeofXfrmSelector:SizeofXfrmSelector+4], msg.Index) 14 b[SizeofXfrmSelector+4] = msg.Dir 15 copy(b[SizeofXfrmSelector+5:SizeofXfrmSelector+8], msg.Pad[:]) 16 } 17 18 func (msg *XfrmUserpolicyId) serializeSafe() []byte { 19 b := make([]byte, SizeofXfrmUserpolicyId) 20 msg.write(b) 21 return b 22 } 23 24 func deserializeXfrmUserpolicyIdSafe(b []byte) *XfrmUserpolicyId { 25 var msg = XfrmUserpolicyId{} 26 binary.Read(bytes.NewReader(b[0:SizeofXfrmUserpolicyId]), NativeEndian(), &msg) 27 return &msg 28 } 29 30 func TestXfrmUserpolicyIdDeserializeSerialize(t *testing.T) { 31 var orig = make([]byte, SizeofXfrmUserpolicyId) 32 rand.Read(orig) 33 safemsg := deserializeXfrmUserpolicyIdSafe(orig) 34 msg := DeserializeXfrmUserpolicyId(orig) 35 testDeserializeSerialize(t, orig, safemsg, msg) 36 } 37 38 func (msg *XfrmUserpolicyInfo) write(b []byte) { 39 const CfgEnd = SizeofXfrmSelector + SizeofXfrmLifetimeCfg 40 const CurEnd = CfgEnd + SizeofXfrmLifetimeCur 41 native := NativeEndian() 42 msg.Sel.write(b[0:SizeofXfrmSelector]) 43 msg.Lft.write(b[SizeofXfrmSelector:CfgEnd]) 44 msg.Curlft.write(b[CfgEnd:CurEnd]) 45 native.PutUint32(b[CurEnd:CurEnd+4], msg.Priority) 46 native.PutUint32(b[CurEnd+4:CurEnd+8], msg.Index) 47 b[CurEnd+8] = msg.Dir 48 b[CurEnd+9] = msg.Action 49 b[CurEnd+10] = msg.Flags 50 b[CurEnd+11] = msg.Share 51 copy(b[CurEnd+12:CurEnd+16], msg.Pad[:]) 52 } 53 54 func (msg *XfrmUserpolicyInfo) serializeSafe() []byte { 55 b := make([]byte, SizeofXfrmUserpolicyInfo) 56 msg.write(b) 57 return b 58 } 59 60 func deserializeXfrmUserpolicyInfoSafe(b []byte) *XfrmUserpolicyInfo { 61 var msg = XfrmUserpolicyInfo{} 62 binary.Read(bytes.NewReader(b[0:SizeofXfrmUserpolicyInfo]), NativeEndian(), &msg) 63 return &msg 64 } 65 66 func TestXfrmUserpolicyInfoDeserializeSerialize(t *testing.T) { 67 var orig = make([]byte, SizeofXfrmUserpolicyInfo) 68 rand.Read(orig) 69 safemsg := deserializeXfrmUserpolicyInfoSafe(orig) 70 msg := DeserializeXfrmUserpolicyInfo(orig) 71 testDeserializeSerialize(t, orig, safemsg, msg) 72 } 73 74 func (msg *XfrmUserTmpl) write(b []byte) { 75 const AddrEnd = SizeofXfrmId + 4 + SizeofXfrmAddress 76 native := NativeEndian() 77 msg.XfrmId.write(b[0:SizeofXfrmId]) 78 native.PutUint16(b[SizeofXfrmId:SizeofXfrmId+2], msg.Family) 79 copy(b[SizeofXfrmId+2:SizeofXfrmId+4], msg.Pad1[:]) 80 msg.Saddr.write(b[SizeofXfrmId+4 : AddrEnd]) 81 native.PutUint32(b[AddrEnd:AddrEnd+4], msg.Reqid) 82 b[AddrEnd+4] = msg.Mode 83 b[AddrEnd+5] = msg.Share 84 b[AddrEnd+6] = msg.Optional 85 b[AddrEnd+7] = msg.Pad2 86 native.PutUint32(b[AddrEnd+8:AddrEnd+12], msg.Aalgos) 87 native.PutUint32(b[AddrEnd+12:AddrEnd+16], msg.Ealgos) 88 native.PutUint32(b[AddrEnd+16:AddrEnd+20], msg.Calgos) 89 } 90 91 func (msg *XfrmUserTmpl) serializeSafe() []byte { 92 b := make([]byte, SizeofXfrmUserTmpl) 93 msg.write(b) 94 return b 95 } 96 97 func deserializeXfrmUserTmplSafe(b []byte) *XfrmUserTmpl { 98 var msg = XfrmUserTmpl{} 99 binary.Read(bytes.NewReader(b[0:SizeofXfrmUserTmpl]), NativeEndian(), &msg) 100 return &msg 101 } 102 103 func TestXfrmUserTmplDeserializeSerialize(t *testing.T) { 104 var orig = make([]byte, SizeofXfrmUserTmpl) 105 rand.Read(orig) 106 safemsg := deserializeXfrmUserTmplSafe(orig) 107 msg := DeserializeXfrmUserTmpl(orig) 108 testDeserializeSerialize(t, orig, safemsg, msg) 109 }