owrt/uci_wireless_interface_test.go

147 lines
3.6 KiB
Go
Raw Normal View History

package owrt
import (
2018-10-24 11:56:49 +02:00
"fmt"
"io/ioutil"
"os"
"testing"
)
const (
ifaceName = "Test"
ifaceNetwork = "Pyxis"
ifaceSysDevName = "wlanX"
ifaceEnc = "psk"
ifaceSSID = "PyxisWifi"
ifaceKey = "qsmdflkjqslmdfkjqslmfkdj"
ifaceDevice = "radioX"
ifaceMode = "ap"
ifaceBssid = "00:00:00:00:00"
)
func TestGetSysDevName(t *testing.T) {
iface := NewUCIWirelessInterface()
iface.Name = ifaceName
iface.Index = 1
iface.Device = ifaceDevice
iface.DevicePath = "soc/soc:pcie/pci0000:00/0000:00:02.0/0000:02:00.0"
iface.Mode = ifaceMode
iface.Ssid = ifaceSSID
iface.Bssid = ifaceBssid
iface.Network = ifaceNetwork
iface.Encryption = ifaceEnc
iface.Key = ifaceKey
2018-10-24 11:56:49 +02:00
testDirectory := "/tmp/sys/devices/platform/soc/soc:pcie/pci0000:00/0000:00:02.0/0000:02:00.0/net/wlan1/"
if err := os.MkdirAll(testDirectory, os.ModePerm); err != nil {
t.Fatalf("Impossible to setup test directory with test file")
}
testFile := fmt.Sprintf("%s/address", testDirectory)
d1 := []byte("00:00:00:84:00:63\n")
err := ioutil.WriteFile(testFile, d1, 0644)
if err != nil {
t.Fatalf("Impossible to setup test directory with test file")
}
if g, e := iface.GetSysDevName("/tmp/sys/"), "wlan1"; g != e {
t.Fatalf("UCIWirelessInterface.GetDeviceSysName() failed ! Got: %s Expect: %s", g, e)
}
iface.SysDevName = "wlanX"
2018-10-24 11:56:49 +02:00
if g, e := iface.GetSysDevName("/tmp/sys/"), "wlanX"; g != e {
t.Fatalf("UCIWirelessInterface.GetDeviceSysName() failed ! Got: %s Expect: %s", g, e)
}
}
func TestCreate(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec, "")
iface := NewUCIWirelessInterface()
iface.Name = ifaceName
iface.Device = ifaceDevice
iface.Mode = ifaceMode
iface.Ssid = ifaceSSID
iface.Bssid = ifaceBssid
iface.Network = ifaceNetwork
iface.Encryption = ifaceEnc
iface.Key = ifaceKey
if iface.Create(uci).ReturnCode != 0 {
t.Fatalf("UCIWirelessInterface.Create() failed !")
}
}
func TestUpdate(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec, "")
iface := NewUCIWirelessInterface()
iface.Name = ifaceName
iface.Index = 1
iface.Device = ifaceDevice
iface.SysDevName = ifaceSysDevName
iface.Mode = ifaceMode
iface.Ssid = ifaceSSID
iface.Bssid = ifaceBssid
iface.Network = ifaceNetwork
iface.Encryption = ifaceEnc
iface.Key = ifaceKey
if iface.Create(uci).ReturnCode != 0 {
t.Fatalf("UCIWirelessInterface.Create() failed !")
}
iface.Name = "Tutu"
if iface.Update(uci).ReturnCode != 0 {
t.Fatalf("UCIWirelessInterface.Update() failed !")
}
}
func TestDelete(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec, "")
iface := NewUCIWirelessInterface()
iface.Name = ifaceName
iface.Index = 1
iface.Device = ifaceDevice
iface.SysDevName = ifaceSysDevName
iface.Mode = ifaceMode
iface.Ssid = ifaceSSID
iface.Bssid = ifaceBssid
iface.Network = ifaceNetwork
iface.Encryption = ifaceEnc
iface.Key = ifaceKey
if iface.Delete(uci).ReturnCode != 0 {
t.Fatalf("UCIWirelessInterface.Delete() failed !")
}
}
func TestConnect(t *testing.T) {
exec := createMockExecutor("", "", 0)
uci := NewUCIWithExecutor(exec, "")
iface := NewUCIWirelessInterface()
iface.Name = ifaceName
iface.Index = 1
iface.Device = ifaceDevice
iface.SysDevName = ifaceSysDevName
iface.Mode = ifaceMode
iface.Ssid = ifaceSSID
iface.Bssid = ifaceBssid
iface.Network = ifaceNetwork
iface.Encryption = ifaceEnc
iface.Key = ifaceKey
wifiCell := NewWifiCell("PyxisWifi", "01:01:01:01:01", "psk")
if iface.Connect(uci, wifiCell, "Toto").ReturnCode != 0 {
t.Fatalf("UCIWirelessInterface.Delete() failed !")
}
}