@@ -2,6 +2,7 @@ package cmd
22
33import (
44 "bytes"
5+ "strings"
56 "testing"
67
78 "github.com/balyakin/sudocheck/internal/model"
@@ -35,3 +36,54 @@ func TestExitForFindingsFailsOnMedium(t *testing.T) {
3536 t .Fatal ("expected non-zero exit code" )
3637 }
3738}
39+
40+ func TestRunScanConfigRefusesRootUser (t * testing.T ) {
41+ runAsRootForTest (t )
42+
43+ stdout := bytes.Buffer {}
44+ stderr := bytes.Buffer {}
45+
46+ exitCode := runScanConfig (scanConfig {}, & stdout , & stderr , "dev" )
47+
48+ if exitCode != exitUsage {
49+ t .Fatalf ("expected usage exit code, got %d" , exitCode )
50+ }
51+ if stdout .String () != "" {
52+ t .Fatalf ("expected empty stdout, got %q" , stdout .String ())
53+ }
54+ if ! strings .Contains (stderr .String (), rootScanGuidance ) {
55+ t .Fatalf ("expected root guidance in stderr, got %q" , stderr .String ())
56+ }
57+ }
58+
59+ func TestRunBaselineInitConfigRefusesRootUser (t * testing.T ) {
60+ runAsRootForTest (t )
61+
62+ stdout := bytes.Buffer {}
63+ stderr := bytes.Buffer {}
64+ config := baselineInitConfig {output : "sudocheck.baseline.json" }
65+
66+ exitCode := runBaselineInitConfig (config , & stdout , & stderr )
67+
68+ if exitCode != exitUsage {
69+ t .Fatalf ("expected usage exit code, got %d" , exitCode )
70+ }
71+ if stdout .String () != "" {
72+ t .Fatalf ("expected empty stdout, got %q" , stdout .String ())
73+ }
74+ if ! strings .Contains (stderr .String (), rootScanGuidance ) {
75+ t .Fatalf ("expected root guidance in stderr, got %q" , stderr .String ())
76+ }
77+ }
78+
79+ func runAsRootForTest (t * testing.T ) {
80+ t .Helper ()
81+
82+ originalGetEffectiveUserID := getEffectiveUserID
83+ getEffectiveUserID = func () int {
84+ return rootUserID
85+ }
86+ t .Cleanup (func () {
87+ getEffectiveUserID = originalGetEffectiveUserID
88+ })
89+ }
0 commit comments