Skip to content

Commit

Permalink
test bad iface names for GetIpForIface()
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhiram committed Feb 25, 2015
1 parent cc4abf0 commit 8006e7f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 44 deletions.
87 changes: 43 additions & 44 deletions cmd/wol/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"os"
"testing"
"os"
"regexp"
"runtime"
"testing"
)

// Helper regex to strip the preamble from the function name. This is
Expand Down Expand Up @@ -41,23 +41,23 @@ func TestDecodeToMacIface(test *testing.T) {

// Validate the EncodeFromMacIface function
func TestEncodeFromMacIface(test *testing.T) {
var TestCases = []MacIface{
{"00:00:00:00:00:00", "eth0"},
{"00:00:00:00:00:AA", ""},
}

for _, entry := range TestCases {
// First encode the MacIface to a bunch of bytes
buf, err := EncodeFromMacIface(entry.Mac, entry.Iface)
assert.Nil(test, err)

// Invoke the function and validate that it is equal
// to our starting MacIface
result, err := DecodeToMacIface(buf)
assert.Nil(test, err)
assert.Equal(test, entry.Mac, result.Mac)
assert.Equal(test, entry.Iface, result.Iface)
}
var TestCases = []MacIface{
{"00:00:00:00:00:00", "eth0"},
{"00:00:00:00:00:AA", ""},
}

for _, entry := range TestCases {
// First encode the MacIface to a bunch of bytes
buf, err := EncodeFromMacIface(entry.Mac, entry.Iface)
assert.Nil(test, err)

// Invoke the function and validate that it is equal
// to our starting MacIface
result, err := DecodeToMacIface(buf)
assert.Nil(test, err)
assert.Equal(test, entry.Mac, result.Mac)
assert.Equal(test, entry.Iface, result.Iface)
}
}

// Validate that an invalid db path errors out
Expand All @@ -67,7 +67,6 @@ func TestInvalidDbPath(test *testing.T) {
assert.Nil(test, aliases)
}


//////////////////////////////////////////////////////////////////////////////
// Test suite: AliasDBTests
// Validate various parts of the DB functionality which needs
Expand Down Expand Up @@ -190,30 +189,30 @@ func (suite *AliasDBTests) TestDeleteAlias() {

// Adding a duplicate entry should overwrite the original one
func (suite *AliasDBTests) TestGetAlias() {
var mi MacIface

var TestCases = []struct {
alias, mac, iface string
}{
{"one", "00:00:00:00:00:00", "eth0"},
{"two", "00:00:00:00:00:AA", "eth1"},
{"thr", "00:00:00:00:11:00", ""},
{"fou", "00:00:00:00:11:AA", ""},
}

for _, entry := range TestCases {
err := suite.aliases.Add(entry.alias, entry.mac, entry.iface)
assert.Nil(suite.T(), err)

mi, err = suite.aliases.Get(entry.alias)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), entry.mac, mi.Mac)
assert.Equal(suite.T(), entry.iface, mi.Iface)
}

// Negative test case - aliases which do not exist
mi, err := suite.aliases.Get("foobar")
assert.NotNil(suite.T(), err)
var mi MacIface

var TestCases = []struct {
alias, mac, iface string
}{
{"one", "00:00:00:00:00:00", "eth0"},
{"two", "00:00:00:00:00:AA", "eth1"},
{"thr", "00:00:00:00:11:00", ""},
{"fou", "00:00:00:00:11:AA", ""},
}

for _, entry := range TestCases {
err := suite.aliases.Add(entry.alias, entry.mac, entry.iface)
assert.Nil(suite.T(), err)

mi, err = suite.aliases.Get(entry.alias)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), entry.mac, mi.Mac)
assert.Equal(suite.T(), entry.iface, mi.Iface)
}

// Negative test case - aliases which do not exist
mi, err := suite.aliases.Get("foobar")
assert.NotNil(suite.T(), err)
}

// Group up all the test suites we wish to run and dispatch them here
Expand Down
16 changes: 16 additions & 0 deletions magic_packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ func TestGetIpFromInterface(test *testing.T) {
}
}
}

func TestGetIpFromInterfaceNegative(test *testing.T) {
// Test some fake interfaces
var NegativeTestCases = []struct {
iface string
}{
{"fake-interface-0"},
{"fake-interface-1"},
}

for _, t := range NegativeTestCases {
addr, err := GetIpFromInterface(t.iface)
assert.Nil(test, addr)
assert.NotNil(test, err)
}
}

0 comments on commit 8006e7f

Please sign in to comment.