ehide: Read the state file instead of running ehide state

ehide.IsOn ran "ehide state", a Python script, which took ~150 ms on
low-end DUTs, and testHookLocal called it before every test.

Read /run/ehide/ehide_state directly as the script does, and treat a
missing file as off if ehide is installed, because ehide creates the
file on its first start in a boot. The other cases still run the script,
so the error when ehide is not installed is kept as before.

BUG=b:575250142 (meta)
BUG=b:585306884
TEST=go test go.chromium.org/tast-tests/cros/local/network/ehide

Change-Id: Ia5a965c6e6d8499e4fdf0138d3245f09949a4027
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/tast-tests/+/8811756
Commit-Queue: Yoshiki IGUCHI <yoshiki@chromium.org>
Tast-Review: Hidehiko Abe <hidehiko@chromium.org>
Tested-by: Yoshiki IGUCHI <yoshiki@chromium.org>
Reviewed-by: Zikai Chen <chenzikai@google.com>
Auto-Submit: Yoshiki IGUCHI <yoshiki@chromium.org>
diff --git a/src/go.chromium.org/tast-tests/cros/common/network/ehideconst/constants.go b/src/go.chromium.org/tast-tests/cros/common/network/ehideconst/constants.go
index 7147f5d..6d9b9c0 100644
--- a/src/go.chromium.org/tast-tests/cros/common/network/ehideconst/constants.go
+++ b/src/go.chromium.org/tast-tests/cros/common/network/ehideconst/constants.go
@@ -20,4 +20,8 @@
 	// ResolvConfPath is the path to the resolv.conf file for the physical
 	// network.
 	ResolvConfPath = "/run/dhclient/etc/resolv.conf"
+
+	// StateFilePath is the path to the file where ehide records its state.
+	// See ehide_daemon.py in platform2/ethernet-hide.
+	StateFilePath = "/run/ehide/ehide_state"
 )
diff --git a/src/go.chromium.org/tast-tests/cros/local/network/ehide/utils.go b/src/go.chromium.org/tast-tests/cros/local/network/ehide/utils.go
index 858977aa..6a0221c 100644
--- a/src/go.chromium.org/tast-tests/cros/local/network/ehide/utils.go
+++ b/src/go.chromium.org/tast-tests/cros/local/network/ehide/utils.go
@@ -6,22 +6,43 @@
 
 import (
 	"context"
+	"os"
+	"os/exec"
 	"strings"
 
 	"go.chromium.org/tast-tests/cros/common/network/ehideconst"
 	"go.chromium.org/tast-tests/cros/common/testexec"
+	"go.chromium.org/tast/core/errors"
 )
 
 // IsOn checks whether ehide is on.
 func IsOn(ctx context.Context) (bool, error) {
-	state, err := getState(ctx)
+	state, err := getState(ctx, ehideconst.StateFilePath)
 	if err != nil {
 		return false, err
 	}
 	return state == ehideconst.EhideStateOn, nil
 }
 
-func getState(ctx context.Context) (string, error) {
+// getState returns the ehide state by reading path, or falls back to running
+// "ehide state".
+func getState(ctx context.Context, path string) (string, error) {
+	// "ehide state" only reads the state file, but running the Python script
+	// takes ~150 ms on low-end DUTs, and test hooks check the state before
+	// every test. So read the file directly.
+	b, err := os.ReadFile(path)
+	if err == nil { // if NO error
+		return strings.TrimSpace(string(b)), nil
+	}
+	// ehide creates the file when it starts for the first time in a boot, so
+	// a missing file means off as long as ehide is installed.
+	if errors.Is(err, os.ErrNotExist) {
+		if _, err := exec.LookPath(ehideconst.EhidePath); err == nil { // if NO error
+			return ehideconst.EhideStateOff, nil
+		}
+	}
+
+	// Let ehide handle the other cases, e.g. report an error if not installed.
 	output, err := testexec.CommandContext(ctx, ehideconst.EhidePath, "state").Output(testexec.DumpLogOnError)
 	if err != nil {
 		return "", err
diff --git a/src/go.chromium.org/tast-tests/cros/local/network/ehide/utils_test.go b/src/go.chromium.org/tast-tests/cros/local/network/ehide/utils_test.go
new file mode 100644
index 0000000..67911e7
--- /dev/null
+++ b/src/go.chromium.org/tast-tests/cros/local/network/ehide/utils_test.go
@@ -0,0 +1,124 @@
+// Copyright 2026 The ChromiumOS Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package ehide
+
+import (
+	"context"
+	"fmt"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"testing"
+
+	"go.chromium.org/tast-tests/cros/common/network/ehideconst"
+	"go.chromium.org/tast/core/errors"
+)
+
+const stubEhideEnv = "TEST_EHIDE_STUB_MODE"
+
+// TestMain doubles as a stub ehide binary when stubEhideEnv is set (used by
+// setupStubEhide via a symlink to os.Executable). Setting the env var to "fail"
+// exits with status 1; any other non-empty value is printed to stdout.
+func TestMain(m *testing.M) {
+	if mode := os.Getenv(stubEhideEnv); mode != "" {
+		if len(os.Args) != 2 || os.Args[1] != "state" {
+			fmt.Fprintf(os.Stderr, "unexpected args: %q\n", os.Args)
+			os.Exit(2)
+		}
+		if mode == "fail" {
+			os.Exit(1)
+		}
+		fmt.Println(mode)
+		os.Exit(0)
+	}
+	os.Exit(m.Run())
+}
+
+func TestGetState(t *testing.T) {
+	t.Setenv("PATH", t.TempDir())
+
+	for _, tc := range []struct {
+		content string
+		want    string
+	}{
+		{content: "on\n", want: ehideconst.EhideStateOn},
+		{content: "off\n", want: ehideconst.EhideStateOff},
+		{content: "set_up\n", want: "set_up"},
+	} {
+		path := filepath.Join(t.TempDir(), "ehide_state")
+		if err := os.WriteFile(path, []byte(tc.content), 0644); err != nil {
+			t.Errorf("os.WriteFile(%q) with %q failed: %v", path, tc.content, err)
+			continue
+		}
+		if got, err := getState(context.Background(), path); err != nil {
+			t.Errorf("getState with %q failed: %v", tc.content, err)
+		} else if got != tc.want {
+			t.Errorf("getState with %q = %q; want %q", tc.content, got, tc.want)
+		}
+	}
+}
+
+func TestGetStateNoStateFile(t *testing.T) {
+	// ehide should not be run, so an empty executable is enough.
+	binDir := t.TempDir()
+	if err := os.WriteFile(filepath.Join(binDir, ehideconst.EhidePath), nil, 0755); err != nil {
+		t.Fatal(err)
+	}
+	t.Setenv("PATH", binDir)
+
+	path := filepath.Join(t.TempDir(), "ehide_state")
+	if got, err := getState(context.Background(), path); err != nil {
+		t.Error("getState failed: ", err)
+	} else if got != ehideconst.EhideStateOff {
+		t.Errorf("getState = %q; want %q", got, ehideconst.EhideStateOff)
+	}
+}
+
+func TestGetStateNotInstalled(t *testing.T) {
+	t.Setenv("PATH", t.TempDir())
+
+	path := filepath.Join(t.TempDir(), "ehide_state")
+	if got, err := getState(context.Background(), path); !errors.Is(err, exec.ErrNotFound) {
+		t.Errorf("getState = (%q, %v); want %v", got, err, exec.ErrNotFound)
+	}
+}
+
+func setupStubEhide(t *testing.T, mode string) {
+	t.Helper()
+
+	exe, err := os.Executable()
+	if err != nil {
+		t.Fatal("Failed to find test executable: ", err)
+	}
+	binDir := t.TempDir()
+	// Symlink the test binary as ehide so TestMain acts as a stub executable.
+	if err := os.Symlink(exe, filepath.Join(binDir, ehideconst.EhidePath)); err != nil {
+		t.Fatal("Failed to symlink stub ehide: ", err)
+	}
+	t.Setenv("PATH", binDir)
+	t.Setenv(stubEhideEnv, mode)
+}
+
+func TestGetStateFallback(t *testing.T) {
+	setupStubEhide(t, " on ")
+
+	// Passing a directory causes os.ReadFile to fail with EISDIR (non-NotExist)
+	// even when running as root, exercising the fallback to "ehide state".
+	if got, err := getState(context.Background(), t.TempDir()); err != nil {
+		t.Error("getState failed: ", err)
+	} else if got != ehideconst.EhideStateOn {
+		t.Errorf("getState = %q; want %q", got, ehideconst.EhideStateOn)
+	}
+}
+
+func TestGetStateFallbackError(t *testing.T) {
+	setupStubEhide(t, "fail")
+
+	// A directory path triggers EISDIR, exercising the fallback to "ehide state".
+	var exitErr *exec.ExitError
+	if got, err := getState(context.Background(), t.TempDir()); !errors.As(err, &exitErr) || exitErr.ExitCode() != 1 {
+		t.Errorf("getState = (%q, %v); want *exec.ExitError with exit code 1", got, err)
+	}
+}