diff --git a/internal/api/agent_events_auth_test.go b/internal/api/agent_events_auth_test.go index ca002eff9c..4252d9d2d3 100644 --- a/internal/api/agent_events_auth_test.go +++ b/internal/api/agent_events_auth_test.go @@ -132,7 +132,9 @@ func (w *agentEventRefreshWriter) Flush() { // of WithValue's context to prove it cannot retain earlier refresh snapshots. func TestAgentEventTokenRefreshKeepsBoundedContext(t *testing.T) { for _, transport := range []string{"header", "bearer"} { + transport := transport t.Run(transport, func(t *testing.T) { + t.Parallel() const orgID = "refresh-tenant" token := newTokenRecord(t, agentEventLifecycleToken, []string{config.ScopeMonitoringRead, config.ScopeAIExecute}, nil) token.OrgID = orgID @@ -314,7 +316,9 @@ func TestAgentEventTokenLifecycleClosesIdleStream(t *testing.T) { func TestAgentEventTokenLifecycleResolvesLiveTenantInventory(t *testing.T) { const orgID = "tenant-a" for _, change := range []string{"replaced", "removed", "same_credential_in_new_owner", "changes_during_validation", "no_global_fallback"} { + change := change t.Run(change, func(t *testing.T) { + t.Parallel() record := newTokenRecord(t, agentEventLifecycleToken, []string{config.ScopeMonitoringRead, config.ScopeAIExecute}, nil) record.OrgID = orgID base := &config.Config{} diff --git a/internal/api/change_password_session_admin_test.go b/internal/api/change_password_session_admin_test.go index d07fe32f6b..423f56377d 100644 --- a/internal/api/change_password_session_admin_test.go +++ b/internal/api/change_password_session_admin_test.go @@ -10,7 +10,7 @@ import ( "time" "github.com/rcourtman/pulse-go-rewrite/internal/config" - "golang.org/x/crypto/bcrypt" + internalauth "github.com/rcourtman/pulse-go-rewrite/pkg/auth" ) const changePasswordProbeCurrent = "correct-admin-password" @@ -18,11 +18,11 @@ const changePasswordProbeCurrent = "correct-admin-password" func newChangePasswordRouter(t *testing.T) (*Router, *config.Config) { t.Helper() dir := t.TempDir() - hashed, err := bcrypt.GenerateFromPassword([]byte(changePasswordProbeCurrent), bcrypt.DefaultCost) + hashed, err := internalauth.HashPassword(changePasswordProbeCurrent) if err != nil { - t.Fatalf("bcrypt: %v", err) + t.Fatalf("hash password: %v", err) } - cfg := &config.Config{DataPath: dir, ConfigPath: dir, AuthUser: "admin", AuthPass: string(hashed)} + cfg := &config.Config{DataPath: dir, ConfigPath: dir, AuthUser: "admin", AuthPass: hashed} router := newTestRouter(t, cfg, nil, nil, nil, nil, "1.0.0") cleanupTestRouter(t, router) return router, cfg @@ -93,9 +93,9 @@ func TestChangePasswordBasicAuthPathUnaffected(t *testing.T) { func newChangePasswordRouterWithToken(t *testing.T, rawToken string, scopes []string) (*Router, *config.Config) { t.Helper() dir := t.TempDir() - hashed, err := bcrypt.GenerateFromPassword([]byte(changePasswordProbeCurrent), bcrypt.DefaultCost) + hashed, err := internalauth.HashPassword(changePasswordProbeCurrent) if err != nil { - t.Fatalf("bcrypt: %v", err) + t.Fatalf("hash password: %v", err) } record, err := config.NewAPITokenRecord(rawToken, "change-password-caller", scopes) if err != nil { @@ -103,7 +103,7 @@ func newChangePasswordRouterWithToken(t *testing.T, rawToken string, scopes []st } cfg := &config.Config{ DataPath: dir, ConfigPath: dir, - AuthUser: "admin", AuthPass: string(hashed), + AuthUser: "admin", AuthPass: hashed, APITokens: []config.APITokenRecord{*record}, } cfg.SortAPITokens() diff --git a/internal/api/docker_agents_report_size_test.go b/internal/api/docker_agents_report_size_test.go index 7f62642cd8..e77423fa8e 100644 --- a/internal/api/docker_agents_report_size_test.go +++ b/internal/api/docker_agents_report_size_test.go @@ -27,7 +27,9 @@ func TestDockerAgentReportSizeContractAcceptsOrdinaryAndLargeFleets(t *testing.T } for _, test := range tests { + test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() handler, _ := newDockerAgentHandlers(t, nil) body := dockerFleetReportBody(t, test.runtime, test.containers) if test.compressed { @@ -50,6 +52,7 @@ func TestDockerAgentReportSizeContractAcceptsOrdinaryAndLargeFleets(t *testing.T func TestDockerAgentReportSizeContractBoundaries(t *testing.T) { t.Run("uncompressed exact encoded limit accepted", func(t *testing.T) { + t.Parallel() handler, _ := newDockerAgentHandlers(t, nil) body := padDockerReportBody(t, dockerFleetReportBody(t, "docker", 1), agentsdocker.ReportEncodedBodyLimitBytes) rec := postDockerReport(handler, body, "") @@ -59,6 +62,7 @@ func TestDockerAgentReportSizeContractBoundaries(t *testing.T) { }) t.Run("uncompressed byte over encoded limit rejected", func(t *testing.T) { + t.Parallel() handler := &DockerAgentHandlers{} body := padDockerReportBody(t, dockerFleetReportBody(t, "docker", 1), agentsdocker.ReportEncodedBodyLimitBytes+1) rec := postDockerReport(handler, body, "") @@ -66,6 +70,7 @@ func TestDockerAgentReportSizeContractBoundaries(t *testing.T) { }) t.Run("gzip exact encoded limit accepted", func(t *testing.T) { + t.Parallel() handler, _ := newDockerAgentHandlers(t, nil) body := gzipAPIReportBodyWithEncodedSize( t, @@ -79,6 +84,7 @@ func TestDockerAgentReportSizeContractBoundaries(t *testing.T) { }) t.Run("gzip byte over encoded limit rejected before decompression", func(t *testing.T) { + t.Parallel() handler := &DockerAgentHandlers{} body := gzipAPIReportBodyWithEncodedSize( t, @@ -90,6 +96,7 @@ func TestDockerAgentReportSizeContractBoundaries(t *testing.T) { }) t.Run("gzip exact decoded limit accepted", func(t *testing.T) { + t.Parallel() handler, _ := newDockerAgentHandlers(t, nil) decoded := padDockerReportBody(t, dockerFleetReportBody(t, "podman", 1), agentsdocker.ReportDecodedBodyLimitBytes) rec := postDockerReport(handler, gzipAPIReportBody(t, decoded), "gzip") @@ -99,6 +106,7 @@ func TestDockerAgentReportSizeContractBoundaries(t *testing.T) { }) t.Run("gzip byte over decoded limit rejected", func(t *testing.T) { + t.Parallel() handler := &DockerAgentHandlers{} decoded := padDockerReportBody(t, dockerFleetReportBody(t, "podman", 1), agentsdocker.ReportDecodedBodyLimitBytes+1) rec := postDockerReport(handler, gzipAPIReportBody(t, decoded), "gzip") diff --git a/internal/api/oidc_only_admin_parity_test.go b/internal/api/oidc_only_admin_parity_test.go index 26fa6d361c..4b14c3cf1e 100644 --- a/internal/api/oidc_only_admin_parity_test.go +++ b/internal/api/oidc_only_admin_parity_test.go @@ -10,7 +10,6 @@ import ( "github.com/rcourtman/pulse-go-rewrite/internal/config" "github.com/rcourtman/pulse-go-rewrite/pkg/auth" - "golang.org/x/crypto/bcrypt" ) func adminParitySession(t *testing.T, user string) *http.Cookie { @@ -31,12 +30,12 @@ func adminParityConfig(t *testing.T, adminUser string) *config.Config { } cfg := &config.Config{DataPath: dir, ConfigPath: dir, APITokens: []config.APITokenRecord{*record}} if adminUser != "" { - hashed, err := bcrypt.GenerateFromPassword([]byte("admin-parity-password"), bcrypt.DefaultCost) + hashed, err := auth.HashPassword("admin-parity-password") if err != nil { - t.Fatalf("bcrypt: %v", err) + t.Fatalf("hash password: %v", err) } cfg.AuthUser = adminUser - cfg.AuthPass = string(hashed) + cfg.AuthPass = hashed } return cfg } diff --git a/internal/api/platform_admin_session_parity_test.go b/internal/api/platform_admin_session_parity_test.go index d864e14374..b1081326ec 100644 --- a/internal/api/platform_admin_session_parity_test.go +++ b/internal/api/platform_admin_session_parity_test.go @@ -12,7 +12,6 @@ import ( "github.com/rcourtman/pulse-go-rewrite/internal/config" "github.com/rcourtman/pulse-go-rewrite/internal/models" "github.com/rcourtman/pulse-go-rewrite/pkg/auth" - "golang.org/x/crypto/bcrypt" ) func platformAdminSession(t *testing.T, user string) *http.Cookie { @@ -27,12 +26,12 @@ func platformAdminConfig(t *testing.T, adminUser string) *config.Config { dir := t.TempDir() cfg := &config.Config{DataPath: dir, ConfigPath: dir} if adminUser != "" { - hashed, err := bcrypt.GenerateFromPassword([]byte("platform-admin-password"), bcrypt.DefaultCost) + hashed, err := auth.HashPassword("platform-admin-password") if err != nil { - t.Fatalf("bcrypt: %v", err) + t.Fatalf("hash password: %v", err) } cfg.AuthUser = adminUser - cfg.AuthPass = string(hashed) + cfg.AuthPass = hashed } return cfg } diff --git a/internal/api/reporting_boundary_test.go b/internal/api/reporting_boundary_test.go index d4a6291085..ff3291b35f 100644 --- a/internal/api/reporting_boundary_test.go +++ b/internal/api/reporting_boundary_test.go @@ -112,27 +112,37 @@ func TestReportingSubjectBoundaryExemptionIsExact(t *testing.T) { }) } } + sourceFiles := make(map[string][]byte) + for _, dir := range []string{"internal/api"} { + err := filepath.WalkDir(filepath.Join(repo, dir), func(path string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + if entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { + return nil + } + rel, err := filepath.Rel(repo, path) + if err != nil { + return err + } + data, err := os.ReadFile(path) + if err != nil { + return err + } + sourceFiles[rel] = data + return nil + }) + if err != nil { + t.Fatal(err) + } + } for _, tc := range variants { + tc := tc t.Run(tc.name, func(t *testing.T) { + t.Parallel() root := t.TempDir() - for _, dir := range []string{"internal/api"} { - err := filepath.WalkDir(filepath.Join(repo, dir), func(path string, entry fs.DirEntry, err error) error { - if err != nil { - return err - } - if entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { - return nil - } - rel, err := filepath.Rel(repo, path) - if err != nil { - return err - } - copyReportingBoundaryFile(t, path, filepath.Join(root, rel)) - return nil - }) - if err != nil { - t.Fatal(err) - } + for rel, data := range sourceFiles { + writeReportingBoundaryFile(t, filepath.Join(root, rel), data) } apiDir := filepath.Join(root, "internal/api") if tc.file != "" { @@ -148,12 +158,8 @@ func TestReportingSubjectBoundaryExemptionIsExact(t *testing.T) { } } -func copyReportingBoundaryFile(t *testing.T, source, target string) { +func writeReportingBoundaryFile(t *testing.T, target string, data []byte) { t.Helper() - data, err := os.ReadFile(source) - if err != nil { - t.Fatal(err) - } if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil { t.Fatal(err) } diff --git a/internal/api/router_integration_test.go b/internal/api/router_integration_test.go index 330875455a..7266592fbb 100644 --- a/internal/api/router_integration_test.go +++ b/internal/api/router_integration_test.go @@ -913,7 +913,7 @@ func TestAlertAcknowledge_AllowsPrintableAlertIDs(t *testing.T) { } func TestStateEndpointReturnsMockData(t *testing.T) { - srv := newFleetIntegrationServer(t) + srv := newIntegrationServer(t) res, err := http.Get(srv.server.URL + "/api/state") if err != nil { @@ -1277,7 +1277,7 @@ func TestHealthEndpointHeadAllowed(t *testing.T) { } func TestConfigNodesUsesMockTopology(t *testing.T) { - srv := newFleetIntegrationServer(t) + srv := newIntegrationServer(t) res, err := http.Get(srv.server.URL + "/api/config/nodes") if err != nil { @@ -1300,7 +1300,7 @@ func TestConfigNodesUsesMockTopology(t *testing.T) { } func TestMockModeToggleEndpoint(t *testing.T) { - srv := newFleetIntegrationServer(t) + srv := newIntegrationServer(t) if !mock.IsMockEnabled() { t.Fatalf("mock mode should be enabled at start of test") diff --git a/internal/api/security_setup_fixture_isolation_test.go b/internal/api/security_setup_fixture_isolation_test.go index 31eb74b3fe..9653410102 100644 --- a/internal/api/security_setup_fixture_isolation_test.go +++ b/internal/api/security_setup_fixture_isolation_test.go @@ -24,7 +24,9 @@ func TestQuickSecuritySetupFixturesInFreshProcesses(t *testing.T) { "TestQuickSecuritySetupRotatesWithBasicAuth", "TestQuickSecuritySetupAllowsRecoveryTokenRotation", } { + name := name t.Run(name, func(t *testing.T) { + t.Parallel() ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) defer cancel() cmd := exec.CommandContext(ctx, executable, diff --git a/internal/api/security_status_capability_enforcement_test.go b/internal/api/security_status_capability_enforcement_test.go index 493b7fbbb6..8e4e183888 100644 --- a/internal/api/security_status_capability_enforcement_test.go +++ b/internal/api/security_status_capability_enforcement_test.go @@ -10,7 +10,6 @@ import ( "github.com/rcourtman/pulse-go-rewrite/internal/config" "github.com/rcourtman/pulse-go-rewrite/pkg/auth" - "golang.org/x/crypto/bcrypt" ) func newCapabilityConfig(t *testing.T, adminUser string) *config.Config { @@ -18,12 +17,12 @@ func newCapabilityConfig(t *testing.T, adminUser string) *config.Config { tempDir := t.TempDir() cfg := &config.Config{DataPath: tempDir, ConfigPath: tempDir} if adminUser != "" { - hashed, err := bcrypt.GenerateFromPassword([]byte("capability-password"), bcrypt.DefaultCost) + hashed, err := auth.HashPassword("capability-password") if err != nil { - t.Fatalf("bcrypt: %v", err) + t.Fatalf("hash password: %v", err) } cfg.AuthUser = adminUser - cfg.AuthPass = string(hashed) + cfg.AuthPass = hashed } return cfg } diff --git a/internal/api/token_stream_lifetime_test.go b/internal/api/token_stream_lifetime_test.go index a44d5edbc4..9f3972e807 100644 --- a/internal/api/token_stream_lifetime_test.go +++ b/internal/api/token_stream_lifetime_test.go @@ -31,7 +31,9 @@ func TestTokenStreamWriterNativeMedia(t *testing.T) { responseMediaCase{"sse_parameters", "Text/Event-Stream; charset=iso-8859-1", "text/event-stream; charset=utf-8"}) for _, media := range cases { for _, commit := range []string{"write", "header", "flush", "informational"} { + media, commit := media, commit t.Run(media.name+"/"+commit, func(t *testing.T) { + t.Parallel() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(r.Context()) writer := &tokenStreamWriter{writer: w, flusher: w.(http.Flusher), ctx: ctx, cancel: cancel, allowed: func() bool { return true }} @@ -447,7 +449,9 @@ func TestProtectedReadStreamsKeepOrgAndInstanceSplit(t *testing.T) { func TestTokenStreamWriterChecksReplayQueueAndFinalClosure(t *testing.T) { for _, stage := range []string{"before_replay", "between_replay", "queued_write", "flush", "after_handler"} { + stage := stage t.Run(stage, func(t *testing.T) { + t.Parallel() cfg := newTestConfigWithTokens(t, newTokenRecord(t, scopedReadToken, []string{config.ScopeSettingsRead}, nil)) revoke := func() { config.Mu.Lock(); cfg.RemoveAPIToken(cfg.APITokens[0].ID); config.Mu.Unlock() } var retained http.ResponseWriter diff --git a/internal/api/token_stream_shutdown_test.go b/internal/api/token_stream_shutdown_test.go index 23c57ee695..c0fcb6356e 100644 --- a/internal/api/token_stream_shutdown_test.go +++ b/internal/api/token_stream_shutdown_test.go @@ -366,6 +366,7 @@ func requireWithin(t *testing.T, label string, done <-chan struct{}) { // withdrawal expire it, and nothing is written afterwards. func TestTokenStreamWriterInterruptsStalledTransport(t *testing.T) { t.Run("close", func(t *testing.T) { + t.Parallel() writer, base, wrote := guardOverStalledTransport(func() bool { return true }) closed := make(chan struct{}) go func() { writer.close(); close(closed) }() @@ -380,6 +381,7 @@ func TestTokenStreamWriterInterruptsStalledTransport(t *testing.T) { }) t.Run("withdrawal", func(t *testing.T) { + t.Parallel() var allowed atomic.Bool allowed.Store(true) writer, _, wrote := guardOverStalledTransport(allowed.Load) @@ -397,6 +399,7 @@ func TestTokenStreamWriterInterruptsStalledTransport(t *testing.T) { }) t.Run("deadline control", func(t *testing.T) { + t.Parallel() writer, base, wrote := guardOverStalledTransport(func() bool { return true }) set := make(chan struct{}) go func() { @@ -533,7 +536,9 @@ func (p *panickingTransport) SetWriteDeadline(time.Time) error { // write that no longer exists. func TestTokenStreamWriterClearsIOStateOnTransportPanic(t *testing.T) { for _, op := range []string{"write", "header", "flush"} { + op := op t.Run(op, func(t *testing.T) { + t.Parallel() base := &panickingTransport{ResponseRecorder: httptest.NewRecorder(), op: op} ctx, cancel := context.WithCancel(context.Background()) defer cancel()