From 19e747f1669d8ae64619686569929a59d5722b8c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 13 Jan 2021 15:38:19 +0100 Subject: Finished implementing config tests --- config/game.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'config/game.go') diff --git a/config/game.go b/config/game.go index c34c697..b5f4d39 100644 --- a/config/game.go +++ b/config/game.go @@ -1,11 +1,15 @@ package config import ( - "errors" "regexp" + + "github.com/pkg/errors" + "golang.org/x/sys/unix" ) var reValidGameName = regexp.MustCompile(`^[\w\._]+$`) +var reValidFileMode = regexp.MustCompile(`^0[\d]{3}$`) +var reSpace = regexp.MustCompile(`^\s$`) // Game struct containers the configuration for a game type Game struct { @@ -15,8 +19,6 @@ type Game struct { FileMode string `yaml:"FileMode"` // Commands is the command list Commands []string `yaml:"Commands"` - // ScoreFile is relative to the chroot path for the game - ScoreCommands []string `yaml:"ScoreCommands"` // Env is the environment in which to exec the commands Env map[string]string `yaml:"Env"` } @@ -27,9 +29,35 @@ func (g *Game) validate(name string) error { return errors.New("Invalid Game name, must match regex `^[\\w\\._]+$` : " + name) } // ChrootPath TODO + if err := unix.Access(g.ChrootPath, unix.R_OK|unix.X_OK); err != nil { + return errors.Wrapf(err, "Invalid ChrootPath : %s", g.ChrootPath) + } // FileMode + if ok := reValidFileMode.MatchString(g.FileMode); !ok { + return errors.New("Invalid File Mode, must match regex `^0[\\d]{3}$` : " + name) + } // Commands - // ScoreFile + if len(g.Commands) == 0 { + return errors.New("Invalid game " + name + " has no commands") + } + for i := 0; i < len(g.Commands); i++ { + if err := validateCommand(g.Commands[i]); err != nil { + return errors.Wrapf(err, "Failed to validate Commands for game %s", name) + } + } // Env + for k, _ := range g.Env { + for _, c := range k { + switch c { + case '=': + return errors.New("Environment variable key must not contain equal sign") + case '\000': + return errors.New("Environment variable key must not contain null character") + } + if reSpace.MatchString(string(c)) { + return errors.New("Environment variable key must not contain spaces") + } + } + } return nil } -- cgit v1.2.3