Skip to content

Commit

Permalink
unit testing for hotpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
imgurbot12 committed Sep 16, 2017
1 parent d4cfadb commit 3625435
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package goaway2

import (
"strconv"
"testing"
)

/***Variables***/
var cache = NewRedBlackTree()
var kv = NewRedBlackKV()

/***Benchmarks***/

func BenchmarkCache(b *testing.B) {
var i int64
for i = 0; i < int64(b.N); i++ {
key := strconv.FormatInt(i, 10)
cache.Set(kv, key, "fuck my shit")
}
}

/***Tests***/
30 changes: 30 additions & 0 deletions firewall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package goaway2

import (
"fmt"
"testing"

netfilter "github.com/AkihiroSuda/go-netfilter-queue"
)

func TestFirewallHandler(t *testing.T) {
fw := NewFirewall()
// check if outbound dns packet is dropped
if fw.checkRules(&PacketData{
SrcIP: "192.168.200.114",
SrcPort: 10048,
DstIP: "8.8.8.8",
DstPort: 53,
}) == netfilter.NF_DROP {
fmt.Println("Packet #1 Dropped")
}
// check if inbound dns packet response is dropped
if fw.checkRules(&PacketData{
SrcIP: "8.8.8.8",
SrcPort: 53,
DstIP: "192.168.200.114",
DstPort: 10048,
}) == netfilter.NF_DROP {
fmt.Println("Packet #2 Dropped")
}
}
53 changes: 53 additions & 0 deletions rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package goaway2

import "testing"

/***Variables***/

var exampleRule = &fwRule{
Zone: zone("any"),
SrcIP: convertIPs("192.168.200.114"),
SrcPort: convertPorts("any"),
DstIP: convertIPs("8.8.8.8"),
DstPort: convertPorts("53"),
}
var examplePktData = &PacketData{
SrcIP: "192.168.200.114",
SrcPort: 10048,
DstIP: "8.8.8.8",
DstPort: 53,
}

/***Benchmarks***/

func BenchmarkRuleValidate(b *testing.B) {
for i := 0; i < b.N; i++ {
exampleRule.Validate(examplePktData)
}
}

func BenchmarkZoneValidate(b *testing.B) {
for i := 0; i < b.N; i++ {
exampleRule.Zone.Validate(examplePktData.SrcIP)
}
}

func BenchmarkIPValidate(b *testing.B) {
for i := 0; i < b.N; i++ {
exampleRule.SrcIP.Validate(examplePktData.SrcIP)
}
}

func BenchmarkPortValidate(b *testing.B) {
for i := 0; i < b.N; i++ {
exampleRule.SrcPort.Validate(examplePktData.SrcPort)
}
}

/***Unit-Tests***/

func TestRuleVerification(t *testing.T) {
if !exampleRule.Validate(examplePktData) {
t.Fatalf("Unable to validate packet against rule!\n")
}
}

0 comments on commit 3625435

Please sign in to comment.