Unconfigure SSIDs for positions that have no team present.

This commit is contained in:
Patrick Fairbank
2018-10-05 00:23:35 -07:00
parent fe9bf7c5ba
commit 6d9aee8df0
2 changed files with 39 additions and 17 deletions

View File

@@ -133,14 +133,18 @@ func (ap *AccessPoint) generateAccessPointConfig(red1, red2, red3, blue1, blue2,
// Verifies the validity of the given team's WPA key and adds a network for it to the list to be configured.
func addTeamConfigCommands(position int, team *model.Team, commands *[]string) error {
if team == nil {
return nil
}
if len(team.WpaKey) < 8 || len(team.WpaKey) > 63 {
return fmt.Errorf("Invalid WPA key '%s' configured for team %d.", team.WpaKey, team.Id)
}
*commands = append(*commands, fmt.Sprintf("set wireless.@wifi-iface[%d].disabled='1'", position),
fmt.Sprintf("set wireless.@wifi-iface[%d].ssid=''", position),
fmt.Sprintf("set wireless.@wifi-iface[%d].key=''", position))
} else {
if len(team.WpaKey) < 8 || len(team.WpaKey) > 63 {
return fmt.Errorf("Invalid WPA key '%s' configured for team %d.", team.WpaKey, team.Id)
}
*commands = append(*commands, fmt.Sprintf("set wireless.@wifi-iface[%d].ssid='%d'", position, team.Id),
fmt.Sprintf("set wireless.@wifi-iface[%d].key='%s'", position, team.WpaKey))
*commands = append(*commands, fmt.Sprintf("set wireless.@wifi-iface[%d].disabled='0'", position),
fmt.Sprintf("set wireless.@wifi-iface[%d].ssid='%d'", position, team.Id),
fmt.Sprintf("set wireless.@wifi-iface[%d].key='%s'", position, team.WpaKey))
}
return nil
}

View File

@@ -13,28 +13,42 @@ import (
func TestConfigureAccessPoint(t *testing.T) {
model.BaseDir = ".."
ssidRe := regexp.MustCompile("ssid='([-\\w ]+)'")
wpaKeyRe := regexp.MustCompile("key='([-\\w ]+)'")
disabledRe := regexp.MustCompile("disabled='([-\\w ]+)'")
ssidRe := regexp.MustCompile("ssid='([-\\w ]*)'")
wpaKeyRe := regexp.MustCompile("key='([-\\w ]*)'")
ap := AccessPoint{teamChannel: 1234, adminChannel: 4321, adminWpaKey: "blorpy"}
// Should not configure any team SSIDs if there are no teams.
// Should disable all team SSIDs if there are no teams.
config, _ := ap.generateAccessPointConfig(nil, nil, nil, nil, nil, nil)
assert.NotContains(t, config, "set")
disableds := disabledRe.FindAllStringSubmatch(config, -1)
ssids := ssidRe.FindAllStringSubmatch(config, -1)
wpaKeys := wpaKeyRe.FindAllStringSubmatch(config, -1)
assert.Equal(t, 0, len(ssids))
assert.Equal(t, 0, len(wpaKeys))
if assert.Equal(t, 6, len(disableds)) && assert.Equal(t, 6, len(ssids)) && assert.Equal(t, 6, len(wpaKeys)) {
for i := 0; i < 6; i++ {
assert.Equal(t, "1", disableds[i][1])
assert.Equal(t, "", ssids[i][1])
assert.Equal(t, "", wpaKeys[i][1])
}
}
// Should configure two SSID for two teams.
// Should configure two SSIDs for two teams and disable the rest.
config, _ = ap.generateAccessPointConfig(&model.Team{Id: 254, WpaKey: "aaaaaaaa"}, nil, nil, nil, nil,
&model.Team{Id: 1114, WpaKey: "bbbbbbbb"})
disableds = disabledRe.FindAllStringSubmatch(config, -1)
ssids = ssidRe.FindAllStringSubmatch(config, -1)
wpaKeys = wpaKeyRe.FindAllStringSubmatch(config, -1)
if assert.Equal(t, 2, len(ssids)) && assert.Equal(t, 2, len(wpaKeys)) {
if assert.Equal(t, 6, len(disableds)) && assert.Equal(t, 6, len(ssids)) && assert.Equal(t, 6, len(wpaKeys)) {
assert.Equal(t, "0", disableds[0][1])
assert.Equal(t, "254", ssids[0][1])
assert.Equal(t, "aaaaaaaa", wpaKeys[0][1])
assert.Equal(t, "1114", ssids[1][1])
assert.Equal(t, "bbbbbbbb", wpaKeys[1][1])
for i := 1; i < 5; i++ {
assert.Equal(t, "1", disableds[i][1])
assert.Equal(t, "", ssids[i][1])
assert.Equal(t, "", wpaKeys[i][1])
}
assert.Equal(t, "0", disableds[5][1])
assert.Equal(t, "1114", ssids[5][1])
assert.Equal(t, "bbbbbbbb", wpaKeys[5][1])
}
// Should configure all SSIDs for six teams.
@@ -42,9 +56,13 @@ func TestConfigureAccessPoint(t *testing.T) {
&model.Team{Id: 2, WpaKey: "22222222"}, &model.Team{Id: 3, WpaKey: "33333333"},
&model.Team{Id: 4, WpaKey: "44444444"}, &model.Team{Id: 5, WpaKey: "55555555"},
&model.Team{Id: 6, WpaKey: "66666666"})
disableds = disabledRe.FindAllStringSubmatch(config, -1)
ssids = ssidRe.FindAllStringSubmatch(config, -1)
wpaKeys = wpaKeyRe.FindAllStringSubmatch(config, -1)
if assert.Equal(t, 6, len(ssids)) && assert.Equal(t, 6, len(wpaKeys)) {
for i := 0; i < 6; i++ {
assert.Equal(t, "0", disableds[i][1])
}
assert.Equal(t, "1", ssids[0][1])
assert.Equal(t, "11111111", wpaKeys[0][1])
assert.Equal(t, "2", ssids[1][1])