| 1 |
package vnet |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/pion/logging" |
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
func TestTCPFragString(t *testing.T) { |
| 13 |
f := tcpFIN |
| 14 |
assert.Equal(t, "FIN", f.String(), "should match") |
| 15 |
f = tcpSYN |
| 16 |
assert.Equal(t, "SYN", f.String(), "should match") |
| 17 |
f = tcpRST |
| 18 |
assert.Equal(t, "RST", f.String(), "should match") |
| 19 |
f = tcpPSH |
| 20 |
assert.Equal(t, "PSH", f.String(), "should match") |
| 21 |
f = tcpACK |
| 22 |
assert.Equal(t, "ACK", f.String(), "should match") |
| 23 |
f = tcpSYN | tcpACK |
| 24 |
assert.Equal(t, "SYN-ACK", f.String(), "should match") |
| 25 |
} |
| 26 |
|
| 27 |
func TestChunk(t *testing.T) { |
| 28 |
loggerFactory := logging.NewDefaultLoggerFactory() |
| 29 |
log := loggerFactory.NewLogger("test") |
| 30 |
|
| 31 |
t.Run("ChunkUDP", func(t *testing.T) { |
| 32 |
src := &net.UDPAddr{ |
| 33 |
IP: net.ParseIP("192.168.0.2"), |
| 34 |
Port: 1234, |
| 35 |
} |
| 36 |
dst := &net.UDPAddr{ |
| 37 |
IP: net.ParseIP(demoIP), |
| 38 |
Port: 5678, |
| 39 |
} |
| 40 |
|
| 41 |
var c Chunk = newChunkUDP(src, dst) |
| 42 |
str := c.String() |
| 43 |
log.Debugf("chunk: %s", str) |
| 44 |
assert.Equal(t, udp, c.Network(), "should match") |
| 45 |
assert.True(t, strings.Contains(str, src.Network()), "should include network type") |
| 46 |
assert.True(t, strings.Contains(str, src.String()), "should include address") |
| 47 |
assert.True(t, strings.Contains(str, dst.String()), "should include address") |
| 48 |
assert.True(t, c.getSourceIP().Equal(src.IP), "ip should match") |
| 49 |
assert.True(t, c.getDestinationIP().Equal(dst.IP), "ip should match") |
| 50 |
|
| 51 |
|
| 52 |
ts := c.setTimestamp() |
| 53 |
assert.Equal(t, ts, c.getTimestamp(), "timestamp should match") |
| 54 |
|
| 55 |
uc := c.(*chunkUDP) |
| 56 |
uc.userData = []byte("Hello") |
| 57 |
|
| 58 |
cloned := c.Clone().(*chunkUDP) |
| 59 |
|
| 60 |
|
| 61 |
err := uc.setSourceAddr("2.3.4.5:4000") |
| 62 |
assert.Nil(t, err, "should succeed") |
| 63 |
assert.Equal(t, "2.3.4.5:4000", uc.SourceAddr().String()) |
| 64 |
|
| 65 |
|
| 66 |
assert.True(t, len(uc.tag) > 0, "should not be empty") |
| 67 |
assert.Equal(t, uc.tag, uc.Tag(), "should match") |
| 68 |
|
| 69 |
|
| 70 |
uc.userData[0] = []byte("!")[0] |
| 71 |
assert.Equal(t, "Hello", string(cloned.userData), "should match") |
| 72 |
assert.Equal(t, "192.168.0.2:1234", cloned.SourceAddr().String()) |
| 73 |
assert.True(t, cloned.getSourceIP().Equal(src.IP), "ip should match") |
| 74 |
assert.True(t, cloned.getDestinationIP().Equal(dst.IP), "ip should match") |
| 75 |
}) |
| 76 |
|
| 77 |
t.Run("ChunkTCP", func(t *testing.T) { |
| 78 |
src := &net.TCPAddr{ |
| 79 |
IP: net.ParseIP("192.168.0.2"), |
| 80 |
Port: 1234, |
| 81 |
} |
| 82 |
dst := &net.TCPAddr{ |
| 83 |
IP: net.ParseIP(demoIP), |
| 84 |
Port: 5678, |
| 85 |
} |
| 86 |
|
| 87 |
var c Chunk = newChunkTCP(src, dst, tcpSYN) |
| 88 |
str := c.String() |
| 89 |
log.Debugf("chunk: %s", str) |
| 90 |
assert.Equal(t, "tcp", c.Network(), "should match") |
| 91 |
assert.True(t, strings.Contains(str, src.Network()), "should include network type") |
| 92 |
assert.True(t, strings.Contains(str, src.String()), "should include address") |
| 93 |
assert.True(t, strings.Contains(str, dst.String()), "should include address") |
| 94 |
assert.True(t, c.getSourceIP().Equal(src.IP), "ip should match") |
| 95 |
assert.True(t, c.getDestinationIP().Equal(dst.IP), "ip should match") |
| 96 |
|
| 97 |
tcp, ok := c.(*chunkTCP) |
| 98 |
assert.True(t, ok, "type should match") |
| 99 |
assert.Equal(t, tcp.flags, tcpSYN, "flags should match") |
| 100 |
|
| 101 |
|
| 102 |
ts := c.setTimestamp() |
| 103 |
assert.Equal(t, ts, c.getTimestamp(), "timestamp should match") |
| 104 |
|
| 105 |
tc := c.(*chunkTCP) |
| 106 |
tc.userData = []byte("Hello") |
| 107 |
|
| 108 |
cloned := c.Clone().(*chunkTCP) |
| 109 |
|
| 110 |
|
| 111 |
err := tc.setSourceAddr("2.3.4.5:4000") |
| 112 |
assert.Nil(t, err, "should succeed") |
| 113 |
assert.Equal(t, "2.3.4.5:4000", tc.SourceAddr().String()) |
| 114 |
|
| 115 |
|
| 116 |
assert.True(t, len(tc.tag) > 0, "should not be empty") |
| 117 |
assert.Equal(t, tc.tag, tc.Tag(), "should match") |
| 118 |
|
| 119 |
|
| 120 |
tc.userData[0] = []byte("!")[0] |
| 121 |
assert.Equal(t, "Hello", string(cloned.userData), "should match") |
| 122 |
assert.Equal(t, "192.168.0.2:1234", cloned.SourceAddr().String()) |
| 123 |
assert.True(t, cloned.getSourceIP().Equal(src.IP), "ip should match") |
| 124 |
assert.True(t, cloned.getDestinationIP().Equal(dst.IP), "ip should match") |
| 125 |
|
| 126 |
|
| 127 |
err = tc.setDestinationAddr("3.4.5.6:7000") |
| 128 |
assert.Nil(t, err, "should succeed") |
| 129 |
assert.Equal(t, "3.4.5.6:7000", tc.DestinationAddr().String()) |
| 130 |
}) |
| 131 |
} |