Skip to content

Commit

Permalink
Add hostToNetworkShort unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessOne91 committed Jun 7, 2024
1 parent 720ab06 commit ddb6921
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ run: build
@./bin/bisturi

test:
@$$GO_EXECUTABLE_PATH test -v ./...
@$$GO_EXECUTABLE_PATH test -v -race ./...
4 changes: 2 additions & 2 deletions cmd/bisturi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"net"

"github.com/NamelessOne91/bisturi/socket"
"github.com/NamelessOne91/bisturi/sockets"
)

var iface = flag.String("i", "eth0", "The network interface to listen to")
Expand All @@ -21,7 +21,7 @@ func main() {
}

// SYS_SOCKET syscall
rs, err := socket.NewRawSocket(*protocol)
rs, err := sockets.NewRawSocket(*protocol)
if err != nil {
log.Fatalf("Failed to open raw socket: %v", err)
}
Expand Down
6 changes: 4 additions & 2 deletions socket/raw_socket.go → sockets/raw_socket.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socket
package sockets

import (
"errors"
Expand All @@ -17,8 +17,10 @@ var protocolEthernetType = map[string]uint16{
"arp": syscall.ETH_P_ARP,
"ip": syscall.ETH_P_IP,
"ipv6": syscall.ETH_P_IPV6,
"udp": syscall.ETH_P_IP, // UDP is part of IP, needs special handling if filtered specifically
"udp": syscall.ETH_P_IP, // UDP and TCP are part of IP, need special handling if filtered specifically
"udp6": syscall.ETH_P_IPV6,
"tcp": syscall.ETH_P_IP,
"tcp6": syscall.ETH_P_IPV6,
}

var errUnsupportedProtocol = errors.New("unsupported protocol")
Expand Down
41 changes: 41 additions & 0 deletions sockets/raw_socket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sockets

import (
"testing"
)

func TestHostToNetworkShort(t *testing.T) {
tests := []struct {
name string
host uint16
expected uint16
}{
{
name: "Little Endian to Big Endian 1",
host: 0xff00,
expected: 0x00ff,
},
{
name: "Little Endian to Big Endian 2",
host: 0xf00f,
expected: 0x0ff0,
},
{
name: "Little Endian to Big Endian 3",
host: 0x0ff0,
expected: 0xf00f,
},
{
name: "Little Endian to Big Endian 4",
host: 0xfff0,
expected: 0xf0ff,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hostToNetworkShort(tt.host); got != tt.expected {
t.Errorf("expected %016b to become %016b: got %016b", tt.host, tt.expected, got)
}
})
}
}

0 comments on commit ddb6921

Please sign in to comment.