| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package hardwareinfo
- import (
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "runtime"
- "strings"
- "testing"
- )
- // ---------------------------------------------------------------------------
- // NewInfoServer / ArOZInfo tests – platform-independent
- // ---------------------------------------------------------------------------
- // TestNewInfoServer verifies that NewInfoServer returns a non-nil Server
- // with the ArOZInfo values preserved.
- func TestNewInfoServer(t *testing.T) {
- info := ArOZInfo{
- BuildVersion: "1.0",
- DeviceVendor: "TestVendor",
- DeviceModel: "TestModel",
- VendorIcon: "icon.png",
- SN: "SN-0001",
- HostOS: runtime.GOOS,
- CPUArch: runtime.GOARCH,
- HostName: "testhost",
- }
- s := NewInfoServer(info)
- if s == nil {
- t.Fatal("NewInfoServer() returned nil")
- }
- if s.hostInfo.BuildVersion != info.BuildVersion {
- t.Errorf("BuildVersion = %q, expected %q", s.hostInfo.BuildVersion, info.BuildVersion)
- }
- if s.hostInfo.DeviceVendor != info.DeviceVendor {
- t.Errorf("DeviceVendor = %q, expected %q", s.hostInfo.DeviceVendor, info.DeviceVendor)
- }
- if s.hostInfo.DeviceModel != info.DeviceModel {
- t.Errorf("DeviceModel = %q, expected %q", s.hostInfo.DeviceModel, info.DeviceModel)
- }
- }
- // TestNewInfoServerEmpty verifies that NewInfoServer works with a zero-value ArOZInfo.
- func TestNewInfoServerEmpty(t *testing.T) {
- s := NewInfoServer(ArOZInfo{})
- if s == nil {
- t.Fatal("NewInfoServer(ArOZInfo{}) returned nil")
- }
- }
- // ---------------------------------------------------------------------------
- // GetArOZInfo HTTP handler tests
- // ---------------------------------------------------------------------------
- // TestGetArOZInfoHandler verifies the handler returns valid JSON.
- func TestGetArOZInfoHandler(t *testing.T) {
- info := ArOZInfo{
- BuildVersion: "2.0",
- DeviceVendor: "Acme",
- DeviceModel: "Widget",
- VendorIcon: "data:image/png;base64,abc",
- SN: "SN-42",
- HostOS: "linux",
- CPUArch: "amd64",
- HostName: "node1",
- }
- s := NewInfoServer(info)
- req := httptest.NewRequest(http.MethodGet, "/aroz/info", nil)
- w := httptest.NewRecorder()
- s.GetArOZInfo(w, req)
- resp := w.Result()
- if resp.StatusCode != http.StatusOK {
- t.Errorf("GetArOZInfo status = %d, expected 200", resp.StatusCode)
- }
- var got ArOZInfo
- if err := json.NewDecoder(resp.Body).Decode(&got); err != nil {
- t.Fatalf("failed to decode response: %v", err)
- }
- // VendorIcon should be stripped when icon param is not "true"
- if got.VendorIcon != "" {
- t.Errorf("VendorIcon should be empty by default, got %q", got.VendorIcon)
- }
- if got.BuildVersion != info.BuildVersion {
- t.Errorf("BuildVersion = %q, expected %q", got.BuildVersion, info.BuildVersion)
- }
- }
- // TestGetArOZInfoHandlerWithIcon verifies the handler includes the VendorIcon
- // when the query parameter icon=true is provided.
- func TestGetArOZInfoHandlerWithIcon(t *testing.T) {
- icon := "data:image/png;base64,iVBORw0KGgo="
- info := ArOZInfo{
- BuildVersion: "3.0",
- VendorIcon: icon,
- }
- s := NewInfoServer(info)
- req := httptest.NewRequest(http.MethodGet, "/aroz/info?icon=true", nil)
- w := httptest.NewRecorder()
- s.GetArOZInfo(w, req)
- var got ArOZInfo
- if err := json.NewDecoder(w.Result().Body).Decode(&got); err != nil {
- t.Fatalf("failed to decode response: %v", err)
- }
- if got.VendorIcon != icon {
- t.Errorf("VendorIcon = %q, expected %q", got.VendorIcon, icon)
- }
- }
- // ---------------------------------------------------------------------------
- // filterGrepResults – platform-independent helper
- // ---------------------------------------------------------------------------
- func TestFilterGrepResults(t *testing.T) {
- cases := []struct {
- result string
- sep string
- expected string
- }{
- {"key: value with spaces", ":", "value with spaces"},
- {"no separator here", ":", "no separator here"},
- {"a:b:c", ":", "b"},
- {"", ":", ""},
- }
- for _, tc := range cases {
- got := filterGrepResults(tc.result, tc.sep)
- if got != tc.expected {
- t.Errorf("filterGrepResults(%q, %q) = %q, expected %q", tc.result, tc.sep, got, tc.expected)
- }
- }
- }
- // ---------------------------------------------------------------------------
- // Platform-specific HTTP handler smoke tests
- // ---------------------------------------------------------------------------
- // TestGetCPUInfoHandler issues a request to the platform-specific GetCPUInfo
- // handler and checks that the response body is non-empty JSON.
- func TestGetCPUInfoHandler(t *testing.T) {
- req := httptest.NewRequest(http.MethodGet, "/", nil)
- w := httptest.NewRecorder()
- GetCPUInfo(w, req)
- body := w.Body.String()
- if strings.TrimSpace(body) == "" {
- t.Error("GetCPUInfo handler returned empty body")
- }
- }
- // TestIfconfigHandler checks that the Ifconfig handler returns a response
- // without panicking. On Linux the iw/ip commands may fail in CI; the handler
- // should still produce an empty array or a valid result.
- func TestIfconfigHandler(t *testing.T) {
- req := httptest.NewRequest(http.MethodGet, "/", nil)
- w := httptest.NewRecorder()
- Ifconfig(w, req)
- // Just verify it doesn't panic and writes something.
- _ = w.Body.String()
- }
- // TestGetDriveStatHandler exercises GetDriveStat; df may not be available
- // to an unprivileged user on some Unix systems.
- func TestGetDriveStatHandler(t *testing.T) {
- req := httptest.NewRequest(http.MethodGet, "/", nil)
- w := httptest.NewRecorder()
- GetDriveStat(w, req)
- _ = w.Body.String()
- }
- // TestGetUSBHandler exercises GetUSB; the hardware may not be present in CI.
- func TestGetUSBHandler(t *testing.T) {
- if runtime.GOOS != "linux" && runtime.GOOS != "darwin" && runtime.GOOS != "freebsd" && runtime.GOOS != "windows" {
- t.Skip("GetUSB is not implemented on this platform")
- }
- req := httptest.NewRequest(http.MethodGet, "/", nil)
- w := httptest.NewRecorder()
- GetUSB(w, req)
- _ = w.Body.String()
- }
- // TestGetRamInfoHandler verifies that GetRamInfo does not panic.
- func TestGetRamInfoHandler(t *testing.T) {
- req := httptest.NewRequest(http.MethodGet, "/", nil)
- w := httptest.NewRecorder()
- GetRamInfo(w, req)
- body := w.Body.String()
- if strings.TrimSpace(body) == "" {
- t.Error("GetRamInfo handler returned empty body")
- }
- }
- func TestOrUndefined(t *testing.T) {
- tests := []struct {
- in string
- want string
- }{
- {"AMD Ryzen 5 5600X 6-Core Processor", "AMD Ryzen 5 5600X 6-Core Processor"},
- {"", "Undefined"},
- {" ", "Undefined"},
- }
- for _, tt := range tests {
- if got := orUndefined(tt.in); got != tt.want {
- t.Errorf("orUndefined(%q) = %q, want %q", tt.in, got, tt.want)
- }
- }
- }
- // PrintSystemHardwareDebugMessage only logs; it must not panic on any platform
- func TestPrintSystemHardwareDebugMessage(t *testing.T) {
- PrintSystemHardwareDebugMessage()
- }
|