github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/nl/tc_linux_test.go (about) 1 package nl 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "encoding/binary" 7 "testing" 8 ) 9 10 /* TcMsg */ 11 func (msg *TcMsg) write(b []byte) { 12 native := NativeEndian() 13 b[0] = msg.Family 14 copy(b[1:4], msg.Pad[:]) 15 native.PutUint32(b[4:8], uint32(msg.Ifindex)) 16 native.PutUint32(b[8:12], msg.Handle) 17 native.PutUint32(b[12:16], msg.Parent) 18 native.PutUint32(b[16:20], msg.Info) 19 } 20 21 func (msg *TcMsg) serializeSafe() []byte { 22 length := SizeofTcMsg 23 b := make([]byte, length) 24 msg.write(b) 25 return b 26 } 27 28 func deserializeTcMsgSafe(b []byte) *TcMsg { 29 var msg = TcMsg{} 30 binary.Read(bytes.NewReader(b[0:SizeofTcMsg]), NativeEndian(), &msg) 31 return &msg 32 } 33 34 func TestTcMsgDeserializeSerialize(t *testing.T) { 35 var orig = make([]byte, SizeofTcMsg) 36 rand.Read(orig) 37 safemsg := deserializeTcMsgSafe(orig) 38 msg := DeserializeTcMsg(orig) 39 testDeserializeSerialize(t, orig, safemsg, msg) 40 } 41 42 /* TcActionMsg */ 43 func (msg *TcActionMsg) write(b []byte) { 44 b[0] = msg.Family 45 copy(b[1:4], msg.Pad[:]) 46 } 47 48 func (msg *TcActionMsg) serializeSafe() []byte { 49 length := SizeofTcActionMsg 50 b := make([]byte, length) 51 msg.write(b) 52 return b 53 } 54 55 func deserializeTcActionMsgSafe(b []byte) *TcActionMsg { 56 var msg = TcActionMsg{} 57 binary.Read(bytes.NewReader(b[0:SizeofTcActionMsg]), NativeEndian(), &msg) 58 return &msg 59 } 60 61 func TestTcActionMsgDeserializeSerialize(t *testing.T) { 62 var orig = make([]byte, SizeofTcActionMsg) 63 rand.Read(orig) 64 safemsg := deserializeTcActionMsgSafe(orig) 65 msg := DeserializeTcActionMsg(orig) 66 testDeserializeSerialize(t, orig, safemsg, msg) 67 } 68 69 /* TcRateSpec */ 70 func (msg *TcRateSpec) write(b []byte) { 71 native := NativeEndian() 72 b[0] = msg.CellLog 73 b[1] = msg.Linklayer 74 native.PutUint16(b[2:4], msg.Overhead) 75 native.PutUint16(b[4:6], uint16(msg.CellAlign)) 76 native.PutUint16(b[6:8], msg.Mpu) 77 native.PutUint32(b[8:12], msg.Rate) 78 } 79 80 func (msg *TcRateSpec) serializeSafe() []byte { 81 length := SizeofTcRateSpec 82 b := make([]byte, length) 83 msg.write(b) 84 return b 85 } 86 87 func deserializeTcRateSpecSafe(b []byte) *TcRateSpec { 88 var msg = TcRateSpec{} 89 binary.Read(bytes.NewReader(b[0:SizeofTcRateSpec]), NativeEndian(), &msg) 90 return &msg 91 } 92 93 func TestTcRateSpecDeserializeSerialize(t *testing.T) { 94 var orig = make([]byte, SizeofTcRateSpec) 95 rand.Read(orig) 96 safemsg := deserializeTcRateSpecSafe(orig) 97 msg := DeserializeTcRateSpec(orig) 98 testDeserializeSerialize(t, orig, safemsg, msg) 99 } 100 101 /* TcTbfQopt */ 102 func (msg *TcTbfQopt) write(b []byte) { 103 native := NativeEndian() 104 msg.Rate.write(b[0:SizeofTcRateSpec]) 105 start := SizeofTcRateSpec 106 msg.Peakrate.write(b[start : start+SizeofTcRateSpec]) 107 start += SizeofTcRateSpec 108 native.PutUint32(b[start:start+4], msg.Limit) 109 start += 4 110 native.PutUint32(b[start:start+4], msg.Buffer) 111 start += 4 112 native.PutUint32(b[start:start+4], msg.Mtu) 113 } 114 115 func (msg *TcTbfQopt) serializeSafe() []byte { 116 length := SizeofTcTbfQopt 117 b := make([]byte, length) 118 msg.write(b) 119 return b 120 } 121 122 func deserializeTcTbfQoptSafe(b []byte) *TcTbfQopt { 123 var msg = TcTbfQopt{} 124 binary.Read(bytes.NewReader(b[0:SizeofTcTbfQopt]), NativeEndian(), &msg) 125 return &msg 126 } 127 128 func TestTcTbfQoptDeserializeSerialize(t *testing.T) { 129 var orig = make([]byte, SizeofTcTbfQopt) 130 rand.Read(orig) 131 safemsg := deserializeTcTbfQoptSafe(orig) 132 msg := DeserializeTcTbfQopt(orig) 133 testDeserializeSerialize(t, orig, safemsg, msg) 134 } 135 136 /* TcHtbCopt */ 137 func (msg *TcHtbCopt) write(b []byte) { 138 native := NativeEndian() 139 msg.Rate.write(b[0:SizeofTcRateSpec]) 140 start := SizeofTcRateSpec 141 msg.Ceil.write(b[start : start+SizeofTcRateSpec]) 142 start += SizeofTcRateSpec 143 native.PutUint32(b[start:start+4], msg.Buffer) 144 start += 4 145 native.PutUint32(b[start:start+4], msg.Cbuffer) 146 start += 4 147 native.PutUint32(b[start:start+4], msg.Quantum) 148 start += 4 149 native.PutUint32(b[start:start+4], msg.Level) 150 start += 4 151 native.PutUint32(b[start:start+4], msg.Prio) 152 } 153 154 func (msg *TcHtbCopt) serializeSafe() []byte { 155 length := SizeofTcHtbCopt 156 b := make([]byte, length) 157 msg.write(b) 158 return b 159 } 160 161 func deserializeTcHtbCoptSafe(b []byte) *TcHtbCopt { 162 var msg = TcHtbCopt{} 163 binary.Read(bytes.NewReader(b[0:SizeofTcHtbCopt]), NativeEndian(), &msg) 164 return &msg 165 } 166 167 func TestTcHtbCoptDeserializeSerialize(t *testing.T) { 168 var orig = make([]byte, SizeofTcHtbCopt) 169 rand.Read(orig) 170 safemsg := deserializeTcHtbCoptSafe(orig) 171 msg := DeserializeTcHtbCopt(orig) 172 testDeserializeSerialize(t, orig, safemsg, msg) 173 }