From d90fdf7953be4e9156509c2105f22f3ba4801ff2 Mon Sep 17 00:00:00 2001 From: Ajabep Date: Thu, 26 Sep 2024 21:07:38 +0200 Subject: [PATCH 1/5] Typo Copy/Paste failure! --- .github/workflows/go.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 7f35c98..6dc433f 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -37,9 +37,6 @@ jobs: version: "latest" install-go: false - - name: Go StaticCheck - run: go vet ./... - - name: Build run: go build -v ./... From 025b9af08f2a6bce073cd953abe90597237b2c3d Mon Sep 17 00:00:00 2001 From: Ajabep Date: Thu, 26 Sep 2024 21:24:15 +0200 Subject: [PATCH 2/5] Add the race flag for go test commands --- .github/workflows/go.yml | 2 +- .github/workflows/sonarcloud.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 6dc433f..acd7c81 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -41,4 +41,4 @@ jobs: run: go build -v ./... - name: Test - run: go test -v ./... + run: go test -race -v ./... diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 6dd3897..f987f7d 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -65,7 +65,7 @@ jobs: go-version: '1.22' - name: Testing... - run: go test -coverprofile cover.out -v ./... -json >tests.json + run: go test -race -coverprofile cover.out -v ./... -json >tests.json - name: GoLangCi-Lint... uses: golangci/golangci-lint-action@v6 From 07fc4057d4720e941aa8ce9a5d73ce225f9b1709 Mon Sep 17 00:00:00 2001 From: Ajabep Date: Thu, 26 Sep 2024 21:24:35 +0200 Subject: [PATCH 3/5] Add govulncheck GH actions --- .github/workflows/govulncheck.yml | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/govulncheck.yml diff --git a/.github/workflows/govulncheck.yml b/.github/workflows/govulncheck.yml new file mode 100644 index 0000000..d28cacb --- /dev/null +++ b/.github/workflows/govulncheck.yml @@ -0,0 +1,39 @@ + +name: GoVulnCheck + +on: + push: + branches: [ "master", "main", $default-branch ] + pull_request: + branches: [ "master", "main", $default-branch ] + +permissions: + contents: read + +jobs: + govulncheck_job: + runs-on: ubuntu-latest + steps: + + - name: Harden Runner + uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1 + with: + egress-policy: audit + + - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 + + - id: govulncheck + uses: golang/govulncheck-action@v1 + with: + go-package: ./... + output-format: sarif + output-file: results.sarif + + - name: Upload SARIF file + uses: github/codeql-action/upload-sarif@v3 + with: + # Path to SARIF file relative to the root of the repository + sarif_file: results.sarif + # Optional category for the results + # Used to differentiate multiple results for one commit + category: govulncheck \ No newline at end of file From f052ee41f1004c829e26e497205c2d2c110b6530 Mon Sep 17 00:00:00 2001 From: Ajabep Date: Sat, 12 Oct 2024 20:06:41 +0200 Subject: [PATCH 4/5] Fix race condition in unit tests --- tests/MainSupervisor.go | 50 ++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/tests/MainSupervisor.go b/tests/MainSupervisor.go index 08e4eff..05ea6c3 100644 --- a/tests/MainSupervisor.go +++ b/tests/MainSupervisor.go @@ -1,11 +1,13 @@ package tests import ( + "context" "encoding/base64" "encoding/json" "fmt" "os" "os/exec" + "sync/atomic" "testing" "time" @@ -19,6 +21,7 @@ type MainSupervisor struct { testName string main MainFunc cmd *exec.Cmd + cancel atomic.Value } // Will init a new supervisor to execute the main function without crashing the current program. @@ -48,11 +51,9 @@ func NewMainSupervisor(t *testing.T, main MainFunc) *MainSupervisor { return supervisor } +// Run the main function in background, and return if it returns in the X first milliseconds. func (m *MainSupervisor) Run(config map[string]string) (string, bool, error) { var err error - if m.cmd != nil { - m.Close() - } addr, has := config["listen"] if !has { @@ -63,25 +64,25 @@ func (m *MainSupervisor) Run(config map[string]string) (string, bool, error) { config["listen"] = addr } - mainStarted := make(chan struct{}, 1) - mainStopped := make(chan struct{}, 2) + mainStopped := make(chan struct{}) - go func(config map[string]string, mainStarted, mainStopped chan<- struct{}) { - jsonArgs, err := json.Marshal(config) - if err != nil { - panic(err) - } - rawArgs := base64.RawStdEncoding.EncodeToString(jsonArgs) + jsonArgs, err := json.Marshal(config) + if err != nil { + panic(err) + } + rawArgs := base64.RawStdEncoding.EncodeToString(jsonArgs) - m.cmd = exec.Command(os.Args[0], fmt.Sprintf("-test.run=%s", m.testName)) + ctx, cancel := context.WithCancel(context.Background()) + m.ReplaceCancel(cancel) + + go func() { + m.cmd = exec.CommandContext(ctx, os.Args[0], fmt.Sprintf("-test.run=%s", m.testName)) m.cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", m.envName, rawArgs)) - close(mainStarted) - _ = m.cmd.Run() + m.cmd.Run() close(mainStopped) - }(config, mainStarted, mainStopped) + }() - <-mainStarted mainHasReturned := false select { case <-mainStopped: @@ -91,10 +92,19 @@ func (m *MainSupervisor) Run(config map[string]string) (string, bool, error) { return addr, mainHasReturned, nil } + +// Cancel the current main run func (m *MainSupervisor) Close() { - if m.cmd != nil && m.cmd.Process != nil { - _ = m.cmd.Process.Kill() - _, _ = m.cmd.Process.Wait() + m.ReplaceCancel(nil) +} + +// Cancel the current main run, if any, and set {cancel} instead +func (m *MainSupervisor) ReplaceCancel(cancel context.CancelFunc) { + old_cancel := m.cancel.Swap(cancel) + if old_cancel != nil { + old_cancel_typed := old_cancel.(context.CancelFunc) + if old_cancel_typed != nil { + old_cancel_typed() + } } - m.cmd = nil } From 68703521c5fc0e97cb96fdbf19fbedc6cead62f1 Mon Sep 17 00:00:00 2001 From: Ajabep Date: Sun, 13 Oct 2024 13:50:10 +0200 Subject: [PATCH 5/5] Fix Sonar findings --- tests/MainSupervisor.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/MainSupervisor.go b/tests/MainSupervisor.go index 05ea6c3..0630a7a 100644 --- a/tests/MainSupervisor.go +++ b/tests/MainSupervisor.go @@ -100,11 +100,11 @@ func (m *MainSupervisor) Close() { // Cancel the current main run, if any, and set {cancel} instead func (m *MainSupervisor) ReplaceCancel(cancel context.CancelFunc) { - old_cancel := m.cancel.Swap(cancel) - if old_cancel != nil { - old_cancel_typed := old_cancel.(context.CancelFunc) - if old_cancel_typed != nil { - old_cancel_typed() + oldCancel := m.cancel.Swap(cancel) + if oldCancel != nil { + oldCancelTyped := oldCancel.(context.CancelFunc) + if oldCancelTyped != nil { + oldCancelTyped() } } }