From c5ec39d332d7ca3dba90ca77c47a4b824f1e471d Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Tue, 26 Aug 2014 15:19:19 -0700 Subject: [PATCH] Added unit tests for AP configuration. --- aironet.go | 5 ++-- aironet_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 aironet_test.go diff --git a/aironet.go b/aironet.go index 22ae048..8bab77e 100644 --- a/aironet.go +++ b/aironet.go @@ -15,7 +15,8 @@ import ( "sync" ) -const aironetTelnetPort = 23 +var aironetTelnetPort = 23 + const ( red1Vlan = 11 red2Vlan = 12 @@ -94,7 +95,7 @@ func getSsids() (map[string]int, error) { } // Parse out the SSIDs and VLANs from the config dump. - re := regexp.MustCompile("(?s)dot11 ssid (\\w+)\\s+vlan (\\d+)") + re := regexp.MustCompile("(?s)dot11 ssid (\\w+)\\s+vlan (1[1-6])") ssidMatches := re.FindAllStringSubmatch(config, -1) if ssidMatches == nil { // There are probably no SSIDs currently configured. diff --git a/aironet_test.go b/aironet_test.go new file mode 100644 index 0000000..703c4d5 --- /dev/null +++ b/aironet_test.go @@ -0,0 +1,77 @@ +// Copyright 2014 Team 254. All Rights Reserved. +// Author: pat@patfairbank.com (Patrick Fairbank) + +package main + +import ( + "bytes" + "fmt" + "github.com/stretchr/testify/assert" + "net" + "testing" + "time" +) + +func TestConfigureAironet(t *testing.T) { + aironetTelnetPort = 9023 + eventSettings = &EventSettings{ApAddress: "127.0.0.1", ApUsername: "user", ApPassword: "password"} + var command string + + // Should do nothing if current configuration is blank. + mockTelnet(t, aironetTelnetPort, "", &command) + assert.Nil(t, ConfigureTeamWifi(nil, nil, nil, nil, nil, nil)) + assert.Equal(t, "", command) + + // Should remove any existing teams but not other SSIDs. + aironetTelnetPort += 1 + mockTelnet(t, aironetTelnetPort, + "dot11 ssid 1\nvlan 1\ndot11 ssid 254\nvlan 12\ndot11 ssid Cheesy Arena\nvlan 17\n", &command) + assert.Nil(t, ConfigureTeamWifi(nil, nil, nil, nil, nil, nil)) + assert.Equal(t, "user\npassword\nterminal length 0\nconfig terminal\nno dot11 ssid 254\nend\n"+ + "copy running-config startup-config\n\nexit\n", command) + + // Should configure new teams and leave existing ones alone if still needed. + aironetTelnetPort += 1 + mockTelnet(t, aironetTelnetPort, "dot11 ssid 254\nvlan 11\n", &command) + assert.Nil(t, ConfigureTeamWifi(&Team{Id: 254, WpaKey: "aaaaaaaa"}, nil, nil, nil, nil, + &Team{Id: 1114, WpaKey: "bbbbbbbb"})) + assert.Equal(t, "user\npassword\nterminal length 0\nconfig terminal\ndot11 ssid 1114\nvlan 16\n"+ + "authentication open\nauthentication key-management wpa version 2\nmbssid guest-mode\nwpa-psk ascii "+ + "bbbbbbbb\ninterface Dot11Radio1\nssid 1114\nend\ncopy running-config startup-config\n\nexit\n", + command) + + // Should reject a missing WPA key. + aironetTelnetPort += 1 + mockTelnet(t, aironetTelnetPort, "", &command) + err := ConfigureTeamWifi(&Team{Id: 254}, nil, nil, nil, nil, nil) + if assert.NotNil(t, err) { + assert.Contains(t, err.Error(), "Invalid WPA key") + } +} + +func mockTelnet(t *testing.T, port int, response string, command *string) { + go func() { + // Fake the first connection which should just get the configuration. + ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + assert.Nil(t, err) + defer ln.Close() + conn, err := ln.Accept() + assert.Nil(t, err) + conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond)) + var reader bytes.Buffer + reader.ReadFrom(conn) + assert.Contains(t, reader.String(), "terminal length 0\nshow running-config\nexit\n") + conn.Write([]byte(response)) + conn.Close() + + // Fake the second connection which should configure stuff. + conn2, err := ln.Accept() + assert.Nil(t, err) + conn2.SetReadDeadline(time.Now().Add(10 * time.Millisecond)) + var reader2 bytes.Buffer + reader2.ReadFrom(conn2) + *command = reader2.String() + conn2.Close() + }() + time.Sleep(100 * time.Millisecond) // Give it some time to open the socket. +}