wifi_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package wifi
  2. import (
  3. "os"
  4. "path/filepath"
  5. "runtime"
  6. "testing"
  7. db "imuslab.com/arozos/mod/database"
  8. )
  9. // newTestDatabase creates a temporary bolt database for use in tests.
  10. // The caller is responsible for calling the returned cleanup function.
  11. func newTestDatabase(t *testing.T) (*db.Database, func()) {
  12. t.Helper()
  13. tmpDir := t.TempDir()
  14. dbPath := filepath.Join(tmpDir, "test.db")
  15. // Create the file so the database opener can find it.
  16. f, err := os.Create(dbPath)
  17. if err != nil {
  18. t.Fatalf("failed to create temp db file: %v", err)
  19. }
  20. f.Close()
  21. database, err := db.NewDatabase(dbPath, false)
  22. if err != nil {
  23. t.Fatalf("NewDatabase() error: %v", err)
  24. }
  25. cleanup := func() {
  26. // Close the database so t.TempDir() can remove the file (Windows
  27. // refuses to delete a file that is still open).
  28. database.Close()
  29. }
  30. return database, cleanup
  31. }
  32. // ---------------------------------------------------------------------------
  33. // NewWiFiManager
  34. // ---------------------------------------------------------------------------
  35. // TestNewWiFiManager verifies that the constructor returns a non-nil manager
  36. // and creates the expected database table.
  37. func TestNewWiFiManager(t *testing.T) {
  38. database, cleanup := newTestDatabase(t)
  39. defer cleanup()
  40. wm := NewWiFiManager(database, false, "/etc/wpa_supplicant/wpa_supplicant.conf", "wlan0")
  41. if wm == nil {
  42. t.Fatal("NewWiFiManager() returned nil")
  43. }
  44. // Verify the wifi table was created.
  45. if !database.TableExists("wifi") {
  46. t.Error("NewWiFiManager() did not create 'wifi' table in the database")
  47. }
  48. }
  49. // TestNewWiFiManagerSudoMode verifies that sudo_mode is stored correctly.
  50. func TestNewWiFiManagerSudoMode(t *testing.T) {
  51. database, cleanup := newTestDatabase(t)
  52. defer cleanup()
  53. wm := NewWiFiManager(database, true, "/tmp/wpa.conf", "wlan1")
  54. if wm == nil {
  55. t.Fatal("NewWiFiManager() returned nil")
  56. }
  57. if !wm.sudo_mode {
  58. t.Error("sudo_mode should be true")
  59. }
  60. }
  61. // TestNewWiFiManagerFields verifies that all constructor arguments are stored.
  62. func TestNewWiFiManagerFields(t *testing.T) {
  63. database, cleanup := newTestDatabase(t)
  64. defer cleanup()
  65. wpaPath := "/etc/wpa_supplicant.conf"
  66. wlanName := "wlan2"
  67. wm := NewWiFiManager(database, false, wpaPath, wlanName)
  68. if wm.wpa_supplicant_path != wpaPath {
  69. t.Errorf("wpa_supplicant_path = %q, expected %q", wm.wpa_supplicant_path, wpaPath)
  70. }
  71. if wm.wan_interface_name != wlanName {
  72. t.Errorf("wan_interface_name = %q, expected %q", wm.wan_interface_name, wlanName)
  73. }
  74. if wm.database != database {
  75. t.Error("database pointer not stored correctly")
  76. }
  77. }
  78. // ---------------------------------------------------------------------------
  79. // GetWirelessInterfaces
  80. // ---------------------------------------------------------------------------
  81. // TestGetWirelessInterfaces verifies that GetWirelessInterfaces does not error
  82. // on the current platform. On Linux it may return an empty list if no wireless
  83. // hardware is present (common in CI); on Darwin/FreeBSD it always returns an
  84. // empty list (stub implementation). On Windows it calls netsh; errors from
  85. // missing netsh are tolerated.
  86. func TestGetWirelessInterfaces(t *testing.T) {
  87. database, cleanup := newTestDatabase(t)
  88. defer cleanup()
  89. wm := NewWiFiManager(database, false, "", "")
  90. switch runtime.GOOS {
  91. case "linux":
  92. ifaces, err := wm.GetWirelessInterfaces()
  93. if err != nil {
  94. t.Logf("GetWirelessInterfaces() returned error (may be expected in CI without iw): %v", err)
  95. return
  96. }
  97. // ifaces may be empty (no wireless hardware) or contain interface names.
  98. for _, iface := range ifaces {
  99. if iface == "" {
  100. t.Error("GetWirelessInterfaces() returned an empty string in the interface list")
  101. }
  102. }
  103. case "darwin", "freebsd":
  104. // Stub implementations always return an empty slice with no error.
  105. ifaces, err := wm.GetWirelessInterfaces()
  106. if err != nil {
  107. t.Errorf("GetWirelessInterfaces() unexpected error on %s: %v", runtime.GOOS, err)
  108. }
  109. if len(ifaces) != 0 {
  110. t.Errorf("GetWirelessInterfaces() on %s should return empty slice, got %v", runtime.GOOS, ifaces)
  111. }
  112. case "windows":
  113. // May fail in CI if running without wireless hardware; that's acceptable.
  114. _, err := wm.GetWirelessInterfaces()
  115. if err != nil {
  116. t.Logf("GetWirelessInterfaces() on Windows returned error (may be expected in CI): %v", err)
  117. }
  118. default:
  119. t.Skipf("GetWirelessInterfaces not tested on %s", runtime.GOOS)
  120. }
  121. }
  122. // ---------------------------------------------------------------------------
  123. // Platform-specific capability tests
  124. // ---------------------------------------------------------------------------
  125. // TestSetInterfacePowerUnsupported verifies that SetInterfacePower returns an
  126. // error on platforms that do not support the operation (Darwin, FreeBSD, Windows).
  127. func TestSetInterfacePowerUnsupported(t *testing.T) {
  128. switch runtime.GOOS {
  129. case "darwin", "freebsd", "windows":
  130. // Intentionally left without t.Skip: these platforms should return an error.
  131. case "linux":
  132. t.Skip("skipping unsupported-platform test on Linux")
  133. default:
  134. t.Skipf("platform %s not tested", runtime.GOOS)
  135. }
  136. database, cleanup := newTestDatabase(t)
  137. defer cleanup()
  138. wm := NewWiFiManager(database, false, "", "")
  139. err := wm.SetInterfacePower("wlan0", true)
  140. if err == nil {
  141. t.Errorf("SetInterfacePower() on %s should return an error, got nil", runtime.GOOS)
  142. }
  143. }
  144. // TestGetInterfacePowerStatusUnsupported verifies that GetInterfacePowerStatuts
  145. // returns an error on Darwin, FreeBSD, and Windows.
  146. func TestGetInterfacePowerStatusUnsupported(t *testing.T) {
  147. switch runtime.GOOS {
  148. case "darwin", "freebsd", "windows":
  149. // These platforms should return an error.
  150. case "linux":
  151. t.Skip("skipping unsupported-platform test on Linux")
  152. default:
  153. t.Skipf("platform %s not tested", runtime.GOOS)
  154. }
  155. database, cleanup := newTestDatabase(t)
  156. defer cleanup()
  157. wm := NewWiFiManager(database, false, "", "")
  158. _, err := wm.GetInterfacePowerStatuts("wlan0")
  159. if err == nil {
  160. t.Errorf("GetInterfacePowerStatuts() on %s should return an error, got nil", runtime.GOOS)
  161. }
  162. }
  163. // TestScanNearbyWiFiUnsupported verifies that ScanNearbyWiFi returns an error
  164. // on Darwin and FreeBSD (stub implementations).
  165. func TestScanNearbyWiFiUnsupported(t *testing.T) {
  166. switch runtime.GOOS {
  167. case "darwin", "freebsd":
  168. // These platforms should return an error.
  169. default:
  170. t.Skipf("skipping ScanNearbyWiFi unsupported test on %s", runtime.GOOS)
  171. }
  172. database, cleanup := newTestDatabase(t)
  173. defer cleanup()
  174. wm := NewWiFiManager(database, false, "", "")
  175. results, err := wm.ScanNearbyWiFi("wlan0")
  176. if err == nil {
  177. t.Errorf("ScanNearbyWiFi() on %s should return an error, got nil", runtime.GOOS)
  178. }
  179. if results == nil {
  180. t.Error("ScanNearbyWiFi() should return an empty (non-nil) slice on error")
  181. }
  182. }
  183. // TestConnectWiFiUnsupported verifies that ConnectWiFi returns an error on
  184. // Darwin, FreeBSD, and Windows stub implementations.
  185. func TestConnectWiFiUnsupported(t *testing.T) {
  186. switch runtime.GOOS {
  187. case "darwin", "freebsd", "windows":
  188. // These platforms should return an error.
  189. case "linux":
  190. t.Skip("skipping unsupported platform ConnectWiFi test on Linux")
  191. default:
  192. t.Skipf("platform %s not tested", runtime.GOOS)
  193. }
  194. database, cleanup := newTestDatabase(t)
  195. defer cleanup()
  196. wm := NewWiFiManager(database, false, "", "")
  197. result, err := wm.ConnectWiFi("TestSSID", "password", "", "")
  198. if err == nil {
  199. t.Errorf("ConnectWiFi() on %s should return an error, got nil", runtime.GOOS)
  200. }
  201. if result == nil {
  202. t.Error("ConnectWiFi() should return a non-nil result even on error")
  203. }
  204. }
  205. // TestRemoveWifiUnsupported verifies that RemoveWifi returns an error on
  206. // Darwin, FreeBSD, and Windows stub implementations.
  207. func TestRemoveWifiUnsupported(t *testing.T) {
  208. switch runtime.GOOS {
  209. case "darwin", "freebsd", "windows":
  210. // These platforms should return an error.
  211. case "linux":
  212. t.Skip("skipping unsupported platform RemoveWifi test on Linux")
  213. default:
  214. t.Skipf("platform %s not tested", runtime.GOOS)
  215. }
  216. database, cleanup := newTestDatabase(t)
  217. defer cleanup()
  218. wm := NewWiFiManager(database, false, "", "")
  219. err := wm.RemoveWifi("TestSSID")
  220. if err == nil {
  221. t.Errorf("RemoveWifi() on %s should return an error, got nil", runtime.GOOS)
  222. }
  223. }