hardwareinfo_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package hardwareinfo
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. )
  10. // ---------------------------------------------------------------------------
  11. // NewInfoServer / ArOZInfo tests – platform-independent
  12. // ---------------------------------------------------------------------------
  13. // TestNewInfoServer verifies that NewInfoServer returns a non-nil Server
  14. // with the ArOZInfo values preserved.
  15. func TestNewInfoServer(t *testing.T) {
  16. info := ArOZInfo{
  17. BuildVersion: "1.0",
  18. DeviceVendor: "TestVendor",
  19. DeviceModel: "TestModel",
  20. VendorIcon: "icon.png",
  21. SN: "SN-0001",
  22. HostOS: runtime.GOOS,
  23. CPUArch: runtime.GOARCH,
  24. HostName: "testhost",
  25. }
  26. s := NewInfoServer(info)
  27. if s == nil {
  28. t.Fatal("NewInfoServer() returned nil")
  29. }
  30. if s.hostInfo.BuildVersion != info.BuildVersion {
  31. t.Errorf("BuildVersion = %q, expected %q", s.hostInfo.BuildVersion, info.BuildVersion)
  32. }
  33. if s.hostInfo.DeviceVendor != info.DeviceVendor {
  34. t.Errorf("DeviceVendor = %q, expected %q", s.hostInfo.DeviceVendor, info.DeviceVendor)
  35. }
  36. if s.hostInfo.DeviceModel != info.DeviceModel {
  37. t.Errorf("DeviceModel = %q, expected %q", s.hostInfo.DeviceModel, info.DeviceModel)
  38. }
  39. }
  40. // TestNewInfoServerEmpty verifies that NewInfoServer works with a zero-value ArOZInfo.
  41. func TestNewInfoServerEmpty(t *testing.T) {
  42. s := NewInfoServer(ArOZInfo{})
  43. if s == nil {
  44. t.Fatal("NewInfoServer(ArOZInfo{}) returned nil")
  45. }
  46. }
  47. // ---------------------------------------------------------------------------
  48. // GetArOZInfo HTTP handler tests
  49. // ---------------------------------------------------------------------------
  50. // TestGetArOZInfoHandler verifies the handler returns valid JSON.
  51. func TestGetArOZInfoHandler(t *testing.T) {
  52. info := ArOZInfo{
  53. BuildVersion: "2.0",
  54. DeviceVendor: "Acme",
  55. DeviceModel: "Widget",
  56. VendorIcon: "data:image/png;base64,abc",
  57. SN: "SN-42",
  58. HostOS: "linux",
  59. CPUArch: "amd64",
  60. HostName: "node1",
  61. }
  62. s := NewInfoServer(info)
  63. req := httptest.NewRequest(http.MethodGet, "/aroz/info", nil)
  64. w := httptest.NewRecorder()
  65. s.GetArOZInfo(w, req)
  66. resp := w.Result()
  67. if resp.StatusCode != http.StatusOK {
  68. t.Errorf("GetArOZInfo status = %d, expected 200", resp.StatusCode)
  69. }
  70. var got ArOZInfo
  71. if err := json.NewDecoder(resp.Body).Decode(&got); err != nil {
  72. t.Fatalf("failed to decode response: %v", err)
  73. }
  74. // VendorIcon should be stripped when icon param is not "true"
  75. if got.VendorIcon != "" {
  76. t.Errorf("VendorIcon should be empty by default, got %q", got.VendorIcon)
  77. }
  78. if got.BuildVersion != info.BuildVersion {
  79. t.Errorf("BuildVersion = %q, expected %q", got.BuildVersion, info.BuildVersion)
  80. }
  81. }
  82. // TestGetArOZInfoHandlerWithIcon verifies the handler includes the VendorIcon
  83. // when the query parameter icon=true is provided.
  84. func TestGetArOZInfoHandlerWithIcon(t *testing.T) {
  85. icon := "data:image/png;base64,iVBORw0KGgo="
  86. info := ArOZInfo{
  87. BuildVersion: "3.0",
  88. VendorIcon: icon,
  89. }
  90. s := NewInfoServer(info)
  91. req := httptest.NewRequest(http.MethodGet, "/aroz/info?icon=true", nil)
  92. w := httptest.NewRecorder()
  93. s.GetArOZInfo(w, req)
  94. var got ArOZInfo
  95. if err := json.NewDecoder(w.Result().Body).Decode(&got); err != nil {
  96. t.Fatalf("failed to decode response: %v", err)
  97. }
  98. if got.VendorIcon != icon {
  99. t.Errorf("VendorIcon = %q, expected %q", got.VendorIcon, icon)
  100. }
  101. }
  102. // ---------------------------------------------------------------------------
  103. // filterGrepResults – platform-independent helper
  104. // ---------------------------------------------------------------------------
  105. func TestFilterGrepResults(t *testing.T) {
  106. cases := []struct {
  107. result string
  108. sep string
  109. expected string
  110. }{
  111. {"key: value with spaces", ":", "value with spaces"},
  112. {"no separator here", ":", "no separator here"},
  113. {"a:b:c", ":", "b"},
  114. {"", ":", ""},
  115. }
  116. for _, tc := range cases {
  117. got := filterGrepResults(tc.result, tc.sep)
  118. if got != tc.expected {
  119. t.Errorf("filterGrepResults(%q, %q) = %q, expected %q", tc.result, tc.sep, got, tc.expected)
  120. }
  121. }
  122. }
  123. // ---------------------------------------------------------------------------
  124. // Platform-specific HTTP handler smoke tests
  125. // ---------------------------------------------------------------------------
  126. // TestGetCPUInfoHandler issues a request to the platform-specific GetCPUInfo
  127. // handler and checks that the response body is non-empty JSON.
  128. func TestGetCPUInfoHandler(t *testing.T) {
  129. req := httptest.NewRequest(http.MethodGet, "/", nil)
  130. w := httptest.NewRecorder()
  131. GetCPUInfo(w, req)
  132. body := w.Body.String()
  133. if strings.TrimSpace(body) == "" {
  134. t.Error("GetCPUInfo handler returned empty body")
  135. }
  136. }
  137. // TestIfconfigHandler checks that the Ifconfig handler returns a response
  138. // without panicking. On Linux the iw/ip commands may fail in CI; the handler
  139. // should still produce an empty array or a valid result.
  140. func TestIfconfigHandler(t *testing.T) {
  141. req := httptest.NewRequest(http.MethodGet, "/", nil)
  142. w := httptest.NewRecorder()
  143. Ifconfig(w, req)
  144. // Just verify it doesn't panic and writes something.
  145. _ = w.Body.String()
  146. }
  147. // TestGetDriveStatHandler exercises GetDriveStat; df may not be available
  148. // to an unprivileged user on some Unix systems.
  149. func TestGetDriveStatHandler(t *testing.T) {
  150. req := httptest.NewRequest(http.MethodGet, "/", nil)
  151. w := httptest.NewRecorder()
  152. GetDriveStat(w, req)
  153. _ = w.Body.String()
  154. }
  155. // TestGetUSBHandler exercises GetUSB; the hardware may not be present in CI.
  156. func TestGetUSBHandler(t *testing.T) {
  157. if runtime.GOOS != "linux" && runtime.GOOS != "darwin" && runtime.GOOS != "freebsd" && runtime.GOOS != "windows" {
  158. t.Skip("GetUSB is not implemented on this platform")
  159. }
  160. req := httptest.NewRequest(http.MethodGet, "/", nil)
  161. w := httptest.NewRecorder()
  162. GetUSB(w, req)
  163. _ = w.Body.String()
  164. }
  165. // TestGetRamInfoHandler verifies that GetRamInfo does not panic.
  166. func TestGetRamInfoHandler(t *testing.T) {
  167. req := httptest.NewRequest(http.MethodGet, "/", nil)
  168. w := httptest.NewRecorder()
  169. GetRamInfo(w, req)
  170. body := w.Body.String()
  171. if strings.TrimSpace(body) == "" {
  172. t.Error("GetRamInfo handler returned empty body")
  173. }
  174. }
  175. func TestOrUndefined(t *testing.T) {
  176. tests := []struct {
  177. in string
  178. want string
  179. }{
  180. {"AMD Ryzen 5 5600X 6-Core Processor", "AMD Ryzen 5 5600X 6-Core Processor"},
  181. {"", "Undefined"},
  182. {" ", "Undefined"},
  183. }
  184. for _, tt := range tests {
  185. if got := orUndefined(tt.in); got != tt.want {
  186. t.Errorf("orUndefined(%q) = %q, want %q", tt.in, got, tt.want)
  187. }
  188. }
  189. }
  190. // PrintSystemHardwareDebugMessage only logs; it must not panic on any platform
  191. func TestPrintSystemHardwareDebugMessage(t *testing.T) {
  192. PrintSystemHardwareDebugMessage()
  193. }