Skip to content

Commit

Permalink
chore: add check if path to file is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
leszek-vechain committed Jun 7, 2024
1 parent a98b88e commit 5973d17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions entrypoint/api/http_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,21 @@ func TestStartStopHandler(t *testing.T) {
wantBody: "Unable to stop network - network no-exist is not configured\n",
},
{
name: "Load existing preset network",
name: "Load existing preset network with valid artifact path",
target: "/preset/noop-network",
payload: "{ \"artifactPath\": \"noop/dir\" }",
payload: "{ \"artifactPath\": \"http_api_test.go\" }",
method: http.MethodPost,
wantStatus: http.StatusOK,
wantBody: "{\"networkId\": \"noop\"}",
},
{
name: "Load existing preset network with invalid artifact path",
target: "/preset/noop-network",
payload: "{ \"artifactPath\": \"blob\" }",
method: http.MethodPost,
wantStatus: http.StatusBadRequest,
wantBody: "file does not exist at location: blob",
},
{
name: "Load existing preset network no config",
target: "/preset/noop-network",
Expand Down
14 changes: 14 additions & 0 deletions preset/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log/slog"

Check failure on line 5 in preset/preset.go

View workflow job for this annotation

GitHub Actions / Run Unit Tests / unit_tests

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/log/slog)
"math/big"
"os"

"github.com/vechain/networkhub/network"
"github.com/vechain/thor/v2/genesis"
Expand All @@ -17,6 +18,14 @@ type Networks struct {
presets map[string]*network.Network
}

func fileExists(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return err == nil
}

func NewPresetNetworks() *Networks {
return &Networks{
presets: map[string]*network.Network{},
Expand All @@ -37,6 +46,11 @@ func (p *Networks) Load(id string, configPayload *APIConfigPayload) (*network.Ne
if configPayload == nil || configPayload.ArtifactPath == "" {
return nil, fmt.Errorf("preset config must be set")
}

if !fileExists(configPayload.ArtifactPath) {
return nil, fmt.Errorf("file does not exist at location: %s", configPayload.ArtifactPath)
}

// override the default path
for _, node := range preset.Nodes {
node.ExecArtifact = configPayload.ArtifactPath
Expand Down

0 comments on commit 5973d17

Please sign in to comment.