Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Phase 1]: Tunnel offchain relayer #1

Merged
merged 20 commits into from
Nov 13, 2024
Merged
Prev Previous commit
Next Next commit
add validate invalid toml field logic
  • Loading branch information
Tanut Lertwarachai authored and Tanut Lertwarachai committed Oct 16, 2024
commit 0d83598f61a1d3b1a7aea34d5aaa3192f38da854
48 changes: 43 additions & 5 deletions falcon/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,56 @@ func TestLoadConfig(t *testing.T) {
require.Equal(t, expect, actual)
}

func TestLoadWrongConfig(t *testing.T) {
func TestInvalidTypeLoadConfig(t *testing.T) {
tmpDir := t.TempDir()

// custom config
customCfgPath := path.Join(tmpDir, "custom.toml")
// invalid type of value
invalidType := "'600000'"

// create new toml config file
cfg := `
cfg := fmt.Sprintf(`
target_chains = []
checking_packet_interval = '60000000000'
checking_packet_interval = %v

[bandchain]
rpc_endpoints = ['http://localhost:26657']
timeout = 7
`
`, invalidType)

// Write the file
err := os.WriteFile(customCfgPath, []byte(cfg), 0o600)
require.NoError(t, err)

app := falcon.NewApp(nil, nil, tmpDir, false, nil)

err = app.InitConfigFile(tmpDir, customCfgPath)

// should fail when try to unmarshal
require.ErrorContains(t, err, fmt.Sprintf("LoadConfig file %v error", customCfgPath))

// file should not be created
require.NoFileExists(t, path.Join(tmpDir, "config", "config.toml"))
}

func TestInvalidFieldLoadConfig(t *testing.T) {
tmpDir := t.TempDir()

// custom config
customCfgPath := path.Join(tmpDir, "custom.toml")
// invalid field name
invalidField := "checking_packet_intervalsss"

// create new toml config file
cfg := fmt.Sprintf(`
target_chains = []
%v = 60000000000

[bandchain]
rpc_endpoints = ['http://localhost:26657']
timeout = 7
`, invalidField)
// Write the file
err := os.WriteFile(customCfgPath, []byte(cfg), 0o600)
require.NoError(t, err)
Expand All @@ -164,9 +199,12 @@ func TestLoadWrongConfig(t *testing.T) {

err = app.InitConfigFile(tmpDir, customCfgPath)


// should fail when try to unmarshal
require.ErrorContains(t, err, fmt.Sprintf("LoadConfig file %v error toml", customCfgPath))
require.ErrorContains(t, err, fmt.Sprintf("LoadConfig file %v error", customCfgPath))
require.ErrorContains(t, err, fmt.Sprintf("invalid field in TOML: %v", invalidField))

// file should not be created
require.NoFileExists(t, path.Join(tmpDir, "config", "config.toml"))
}

41 changes: 40 additions & 1 deletion falcon/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package falcon

import (
"fmt"
"os"
"reflect"
"time"

"github.com/pelletier/go-toml/v2"
Expand Down Expand Up @@ -42,12 +44,49 @@ func LoadConfig(file string) (Config, error) {
return Config{}, err
}

// unmarshall them into struct
// unmarshall them with Config into struct
cfgWrapper := &Config{}
err = toml.Unmarshal(byt, cfgWrapper)
if err != nil {
return Config{}, err
}

// unmarshall them with raw map[string] into struct
var rawData map[string]interface{}
err = toml.Unmarshal(byt, &rawData)
if err != nil {
return Config{}, err
}

// validate if there is invalid field in toml file
err = validateConfigFields(rawData, *cfgWrapper)
if err != nil {
return Config{}, err
}

return *cfgWrapper, nil
}

// Function to validate invalid fields in the TOML
func validateConfigFields(rawData map[string]interface{}, cfg Config) error {
// Use reflection to get the struct field names
expectedFields := make(map[string]bool)
val := reflect.ValueOf(cfg)
typ := val.Type()

// Build a set of expected field names from the struct tags
for i := 0; i < val.NumField(); i++ {
tag := typ.Field(i).Tag.Get("toml")
if tag != "" {
expectedFields[tag] = true
}
}

// Compare the map keys (raw TOML fields) with the expected field names
for field := range rawData {
if !expectedFields[field] {
return fmt.Errorf("invalid field in TOML: %s", field)
}
}
return nil
}
Loading