wifi_linux_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //go:build linux
  2. // +build linux
  3. package wifi
  4. import (
  5. "runtime"
  6. "testing"
  7. )
  8. // ---------------------------------------------------------------------------
  9. // Linux-specific helper function tests
  10. // ---------------------------------------------------------------------------
  11. // TestFileExistsLinux verifies the fileExists helper.
  12. func TestFileExistsLinux(t *testing.T) {
  13. if runtime.GOOS != "linux" {
  14. t.Skip("fileExists helper is in linux-only source; test runs on Linux")
  15. }
  16. // A path that exists.
  17. if !fileExists("/proc/version") {
  18. t.Error("fileExists(\"/proc/version\") should return true on Linux")
  19. }
  20. // A path that does not exist.
  21. if fileExists("/nonexistent_path_xyz_12345") {
  22. t.Error("fileExists(\"/nonexistent_path_xyz_12345\") should return false")
  23. }
  24. }
  25. // TestFileInDirLinux verifies the fileInDir helper.
  26. func TestFileInDirLinux(t *testing.T) {
  27. if runtime.GOOS != "linux" {
  28. t.Skip("fileInDir helper is in linux-only source; test runs on Linux")
  29. }
  30. // The file is inside the directory.
  31. if !fileInDir("/tmp/foo/bar.txt", "/tmp/foo") {
  32. t.Error("fileInDir should return true when file is inside directory")
  33. }
  34. // The file is outside the directory.
  35. if fileInDir("/proc/version", "/tmp") {
  36. t.Error("fileInDir should return false when file is outside directory")
  37. }
  38. }
  39. // TestPkgExistsLinux verifies the pkg_exists helper.
  40. func TestPkgExistsLinux(t *testing.T) {
  41. if runtime.GOOS != "linux" {
  42. t.Skip("pkg_exists helper is in linux-only source; test runs on Linux")
  43. }
  44. // "sh" should always exist on Linux.
  45. if !pkg_exists("sh") {
  46. t.Error("pkg_exists(\"sh\") should return true on Linux")
  47. }
  48. // A package that almost certainly doesn't exist.
  49. if pkg_exists("this_package_does_not_exist_xyz") {
  50. t.Error("pkg_exists(\"this_package_does_not_exist_xyz\") should return false")
  51. }
  52. }
  53. // TestGetSignalLevelEstimation verifies the bar-to-dBm mapping.
  54. func TestGetSignalLevelEstimation(t *testing.T) {
  55. if runtime.GOOS != "linux" {
  56. t.Skip("getSignalLevelEstimation is in linux-only source; test runs on Linux")
  57. }
  58. database, cleanup := newTestDatabase(t)
  59. defer cleanup()
  60. wm := NewWiFiManager(database, false, "", "")
  61. cases := []struct {
  62. bar string
  63. expected string
  64. }{
  65. {"▂▄▆█", "-45 dBm[Estimated]"},
  66. {"▂▄▆_", "-55 dBm[Estimated]"},
  67. {"▂▄__", "-75 dBm[Estimated]"},
  68. {"▂___", "-85 dBm[Estimated]"},
  69. {"____", "-95 dBm[Estimated]"},
  70. {"", "-95 dBm[Estimated]"},
  71. }
  72. for _, tc := range cases {
  73. got := wm.getSignalLevelEstimation(tc.bar)
  74. if got != tc.expected {
  75. t.Errorf("getSignalLevelEstimation(%q) = %q, expected %q", tc.bar, got, tc.expected)
  76. }
  77. }
  78. }