-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_test.go
More file actions
85 lines (69 loc) · 1.96 KB
/
Copy pathsocket_test.go
File metadata and controls
85 lines (69 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package rawsocket
import (
"math"
"net"
"strings"
"testing"
)
// TestUnixSocket_Write is a test function that tests the Write method of the UnixSocket type.
func TestUnixSocket_Write(t *testing.T) {
t.Log("TestUnixSocket_Write")
// Open a raw socket with TCP protocol
unixSocket, err := OpenRawSocket(IPPROTO_TCP)
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "permission") {
t.Skip(err)
}
t.Error(err)
return
}
// Create a TCP object with SYN flag set and legit options enabled.
tcp := NewTCP(WithTCPSYN(true), WithTCPLegitOptions(true))
// Create the source TCP address using the GetSelfIP function.
src := net.TCPAddr{IP: GetSelfIP()}
// Create the destination TCP address with IP 8.8.8.8 and port 443.
dest := net.TCPAddr{
IP: net.IPv4(8, 8, 8, 8),
Port: 443,
}
// Build the TCP packet using the Build method of the TCP object.
packet := tcp.Build(src, dest)
// Create the IP address object for the destination IP.
ipAddr := &net.IPAddr{IP: dest.IP}
// Write the packet to the Unix socket using the Write method.
n, err := unixSocket.Write(packet, ipAddr)
if err != nil {
t.Error(err)
return
}
// Log the number of bytes sent in the packet.
t.Logf("Packet sent, Length: %d bytes", n)
}
// TestUnixSocket_Read tests the Read function of the UnixSocket.
func TestUnixSocket_Read(t *testing.T) {
// Log test name
t.Log("TestUnixSocket_Read")
// Open a raw socket with TCP protocol
unixSocket, err := OpenRawSocket(IPPROTO_TCP)
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "permission") {
t.Skip(err)
}
t.Error(err)
return
}
// Create a buffer with maximum size
var buff = make([]byte, math.MaxUint16)
// Iterate 5 times
for i := 0; i < 5; i++ {
// Read from the UnixSocket
n, addr, err := unixSocket.Read(buff)
// Check for error
if err != nil {
t.Error(err)
break
}
// Log received packet and its address
t.Logf("Packet %v received from %s", buff[:n], addr)
}
}