Skip to content

Commit

Permalink
Enable travis CI, clean up some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhiram committed Feb 9, 2015
1 parent 2610134 commit 2b2ee16
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: go

go:
- 1.3
- tip

env:
- "PATH=$HOME/gopath/bin:$PATH"

script:
- go test -v
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ The listening interface just looks for a `Magic Packet` with it's MAC address en

It is important to remember that since this is typically sent over the [data link layer](http://en.wikipedia.org/wiki/Data_link_layer), the target machine's IP address is irrelevant.

This `Go` application tries to make it easier to send WOL packets over your LAN, by attempting to figure out the destination MAC address of a given hostname / ip address (when the target machine is up). It then remembers the MAC address for future sake.

## Installation

```
Expand Down
24 changes: 15 additions & 9 deletions lib/magic_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import (
"encoding/hex"
)

// Define globals for the MagicPacket and the MacAddress parsing
var (
delims = ":-"
re_MAC = regexp.MustCompile(`^([0-9a-fA-F]{2}[` + delims + `]){5}([0-9a-fA-F]{2})$`)
)

// A MacAddress is 6 bytes in a row
type MacAddress [6]byte

// A MagicPacket is constituted of 6 bytes of 0xFF followed by
// 16 groups of the destination MAC address.
type MagicPacket struct {
header [6]byte
payload [16]MacAddress
}

var delims = ":-"
var re_MAC = regexp.MustCompile(`^([0-9a-fA-F]{2}[` + delims + `]){5}([0-9a-fA-F]{2})$`)

/* Returns a MacAddress object given a mac address string */
// Returns a MacAddress object given a mac address string
func GetMacBytes(mac string) (*MacAddress, error) {
// Parse MAC addr
for _, delim := range delims {
Expand All @@ -37,11 +43,11 @@ func GetMacBytes(mac string) (*MacAddress, error) {
return &ret, nil
}

/* Constructs a "Magic Packet" broadcast frame which contains 6 bytes of
0xff followed by 16 repetitions of a given mac address.
This function accepts a mac address string, and returns a pointer to
a MagicPacket object */
// Constructs a "Magic Packet" broadcast frame which contains 6 bytes of
// 0xff followed by 16 repetitions of a given mac address.
//
// This function accepts a mac address string, and returns a pointer to
// a MagicPacket object */
func NewMagicPacket(mac string) (*MagicPacket, error) {
var packet MagicPacket

Expand Down
36 changes: 36 additions & 0 deletions lib/magic_packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package wol

import (
"testing"
"github.com/stretchr/testify/assert"
)

func TestGetMacBytes(test *testing.T) {
var PositiveTestCases = []struct {
mac string
expected MacAddress
} {
{ "00:00:00:00:00:00", MacAddress{0,0,0,0,0,0} },
{ "00:00:00:00:00:00", MacAddress{0,0,0,0,0,0} },
}

for _, t := range PositiveTestCases {
macAddress, err := GetMacBytes(t.mac)
assert.Equal(test, t.expected, *macAddress)
assert.Equal(test, err, nil)
}
}

func TestGetMacBytesNegative(test *testing.T) {
var NegativeTestCases = []struct {
mac string
} {
{ "00x00:00:00:00:00" },
{ "00:00:Z0:00:00:00" },
}

for _, t := range NegativeTestCases {
_, err := GetMacBytes(t.mac)
assert.NotEqual(test, err, nil)
}
}

0 comments on commit 2b2ee16

Please sign in to comment.