Skip to content

Commit

Permalink
Merge pull request #41 from jsimonetti/fix_tests
Browse files Browse the repository at this point in the history
[test] Fix tests
  • Loading branch information
nberlee authored Sep 27, 2023
2 parents 50b92f3 + ed7b1cc commit e51274c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
on: [push, pull_request]
name: Build and test Go application

concurrency:
group: ${{ github.workflow }}-$${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
test:
name: Test Go latest two versions
strategy:
matrix:
go-version: ['oldstable', 'stable']
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Install deps
run: sudo apt-get -yy install libpcap-dev

- name: Checkout code
uses: actions/checkout@v4

- name: Download dependencies
run: go mod download

- name: Go Vet
run: go vet ./...

- name: Test
run: go test -v ./...

- name: Check for vulnerabilities
id: govulncheck
uses: golang/[email protected]
with:
cache: false
go-version-input: ${{ matrix.go-version }}
go-package: ./...
12 changes: 6 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"testing"
)

var devices = map[macAddress]bonjourDevice{
"00:14:22:01:23:45": bonjourDevice{OriginPool: 45, SharedPools: []uint16{42, 1042, 46}},
"00:14:22:01:23:46": bonjourDevice{OriginPool: 46, SharedPools: []uint16{176, 148}},
"00:14:22:01:23:47": bonjourDevice{OriginPool: 47, SharedPools: []uint16{1042, 1717, 13}},
var devices = map[macAddress]multicastDevice{
"00:14:22:01:23:45": {Description: "Test Chromecast", OriginPool: 45, SharedPools: []uint16{42, 1042, 46}},
"00:14:22:01:23:46": {Description: "Test Spotify Air", OriginPool: 46, SharedPools: []uint16{176, 148}},
"00:14:22:01:23:47": {Description: "Test Spotify Air", OriginPool: 47, SharedPools: []uint16{1042, 1717, 13}},
}

func TestReadConfig(t *testing.T) {
// Check that a valid config file is read adequately
validTestConfigFile := "config_test.toml"
computedCfg, err := readConfig(validTestConfigFile)
expectedCfg := brconfig{
expectedCfg := config{
NetInterface: "test0",
Devices: devices,
}
Expand All @@ -31,7 +31,7 @@ func TestReadConfig(t *testing.T) {
// Check that a non-existant config file is handled adequately
nonexistantConfigFile := "nonexistant_test.toml"
computedCfg, err = readConfig(nonexistantConfigFile)
if !reflect.DeepEqual(computedCfg, brconfig{}) {
if !reflect.DeepEqual(computedCfg, config{}) {
t.Error("Error in readConfig(): unexpected config returned for non-existant config file")
}
if !os.IsNotExist(err) {
Expand Down
29 changes: 23 additions & 6 deletions packet_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"io"
"net"
"reflect"
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestParseDNSPayload(t *testing.T) {
questionPacketPayload, _, _ := parseUDPLayer(questionPacket)

questionExpectedResult := true
questionComputedResult := parseDNSPayload(questionPacketPayload)
questionComputedResult, _ := parseDNSPayload(questionPacketPayload)
if !reflect.DeepEqual(questionExpectedResult, questionComputedResult) {
t.Error("Error in parseDNSPayload() for DNS queries")
}
Expand All @@ -195,7 +196,7 @@ func TestParseDNSPayload(t *testing.T) {
answerPacketPayload, _, _ := parseUDPLayer(answerPacket)

answerExpectedResult := false
answerComputedResult := parseDNSPayload(answerPacketPayload)
answerComputedResult, _ := parseDNSPayload(answerPacketPayload)
if !reflect.DeepEqual(answerExpectedResult, answerComputedResult) {
t.Error("Error in parseDNSPayload() for DNS answers")
}
Expand Down Expand Up @@ -289,6 +290,8 @@ func TestSendBonjourPacket(t *testing.T) {
vlanTag: parseVLANTag(initialPacketIPv4),
srcMAC: srcMACv4,
dstMAC: dstMACv4,
srcIP: &srcIPv4Test,
dstIP: &dstIPv4Test,
isDNSQuery: true,
isIPv6: false,
}
Expand All @@ -299,6 +302,8 @@ func TestSendBonjourPacket(t *testing.T) {
vlanTag: parseVLANTag(initialPacketIPv6),
srcMAC: srcMACv6,
dstMAC: dstMACv6,
srcIP: &srcIPv6Test,
dstIP: &dstIPv6Test,
isDNSQuery: true,
isIPv6: true,
}
Expand All @@ -315,13 +320,25 @@ func TestSendBonjourPacket(t *testing.T) {

pw := &mockPacketWriter{packet: nil}

sendPacket(pw, &bonjourTestPacketIPv4, newVlanTag, brMACTest)
if !reflect.DeepEqual(expectedPacketIPv4.Layers(), pw.packet.Layers()) {
sendPacket(pw, &bonjourTestPacketIPv4, newVlanTag, srcMACTest, dstMACTest, srcIPv4Test, dstIPv4Test)
if !cmpPacket(expectedPacketIPv4.Layers(), pw.packet.Layers()) {
t.Error("Error in sendBonjourPacket() for IPv4")
}

sendPacket(pw, &bonjourTestPacketIPv6, newVlanTag, brMACTest)
if !reflect.DeepEqual(expectedPacketIPv6.Layers(), pw.packet.Layers()) {
sendPacket(pw, &bonjourTestPacketIPv6, newVlanTag, srcMACTest, dstMACTest, srcIPv6Test, dstIPv6Test)
if !cmpPacket(expectedPacketIPv6.Layers(), pw.packet.Layers()) {
t.Error("Error in sendBonjourPacket() for IPv6")
}
}

// We cannot compare slices of packet layers directly, so we compare the payload of the UDP layer instead.
func cmpPacket(a, b []gopacket.Layer) bool {
udpLayer := 3
if len(a) < udpLayer || len(b) < udpLayer {
return false
}

aBytes := a[udpLayer].(*layers.UDP).Payload
bBytes := b[udpLayer].(*layers.UDP).Payload
return bytes.Equal(aBytes, bBytes)
}

0 comments on commit e51274c

Please sign in to comment.