Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for MAC address representations #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jwieringa
Copy link
Member

@jwieringa jwieringa commented May 18, 2019

Adds tests to verify MAC Address conversion to different types of representations.

Successful tests:

% go test
PASS
ok  	github.com/RadiusNetworks/go-beacon	0.006s

Changing the data to show failing tests.

diff --git a/mac_address_test.go b/mac_address_test.go
index 6015091..7e0f12a 100644
--- a/mac_address_test.go
+++ b/mac_address_test.go
@@ -15,14 +15,14 @@ var testCases []mac
 func init() {
        testCases = []mac{
                {
-                       "fe:fd:55:c9:64:24",
+                       "be:fd:55:c9:64:24",
                        [6]byte{0x24, 0x64, 0xc9, 0x55, 0xfd, 0xfe},
-                       []byte{'"', 'f', 'e', ':', 'f', 'd', ':', '5', '5', ':', 'c', '9', ':', '6', '4', ':', '2', '4', '"'},
+                       []byte{'"', 'd', 'e', ':', 'f', 'd', ':', '5', '5', ':', 'c', '9', ':', '6', '4', ':', '2', '4', '"'},
                },
                {
-                       "29:fe:fd:41:21:20",
+                       "2b:fe:fd:41:21:20",
                        [6]byte{0x20, 0x21, 0x41, 0xfd, 0xfe, 0x29},
-                       []byte{'"', '2', '9', ':', 'f', 'e', ':', 'f', 'd', ':', '4', '1', ':', '2', '1', ':', '2', '0', '"'},
+                       []byte{'"', 'd', '9', ':', 'f', 'e', ':', 'f', 'd', ':', '4', '1', ':', '2', '1', ':', '2', '0', '"'},
                },
        }
 }
% go test
--- FAIL: TestMacString (0.00s)
    mac_address_test.go:34: result fe:fd:55:c9:64:24; want be:fd:55:c9:64:24
    mac_address_test.go:34: result 29:fe:fd:41:21:20; want 2b:fe:fd:41:21:20
--- FAIL: TestMacMarshalJSON (0.00s)
    mac_address_test.go:46: result "fe:fd:55:c9:64:24"; want "de:fd:55:c9:64:24"
    mac_address_test.go:46: result "29:fe:fd:41:21:20"; want "d9:fe:fd:41:21:20"
--- FAIL: TestMacUnmarshalJSON (0.00s)
    mac_address_test.go:56: result de:fd:55:c9:64:24; want be:fd:55:c9:64:24
    mac_address_test.go:56: result d9:fe:fd:41:21:20; want 2b:fe:fd:41:21:20
--- FAIL: TestParseMacAddress (0.00s)
    mac_address_test.go:66: result be:fd:55:c9:64:24; want fe:fd:55:c9:64:24
    mac_address_test.go:66: result 2b:fe:fd:41:21:20; want 29:fe:fd:41:21:20
FAIL
exit status 1
FAIL	github.com/RadiusNetworks/go-beacon	0.006s

Adds tests to verify MAC Address conversion to
different types of representations.
@jwieringa jwieringa self-assigned this May 18, 2019
func TestMacUnmarshalJSON(t *testing.T) {
for _, tc := range testCases {
addr := MacAddress{}
addr.UnmarshalJSON(tc.text)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be:

Suggested change
addr.UnmarshalJSON(tc.text)
addr.UnmarshalJSON([]byte(tc.hex))

func TestMacMarshalJSON(t *testing.T) {
for _, tc := range testCases {
result, _ := MacAddress(tc.bits).MarshalJSON()
if !lenEqual(result, tc.text) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these conditional checks are possibly replaceable with bytes.Equal:

		if !bytes.Equal(result, tc.text) {
			t.Errorf("result %s; want %s", result, tc.text)
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants