mirror of
https://github.com/status-im/consul.git
synced 2025-02-22 18:38:19 +00:00
Tests for populateUnixSocket. Still need to write tests for the other major function, and basic socket listening tests.
This code is copyright 2014 Akamai Technologies, Inc. <opensource@akamai.com>
This commit is contained in:
parent
11a3ce0bdd
commit
8362e3e9eb
@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -364,17 +365,27 @@ type UnixSocket struct {
|
||||
|
||||
func populateUnixSocket(addr string) (*UnixSocket, error) {
|
||||
if !strings.HasPrefix(addr, "unix://") {
|
||||
return nil, fmt.Errorf("Failed to parse Unix address, format is [path];[user];[group];[mode]: %v", addr)
|
||||
return nil, fmt.Errorf("Failed to parse Unix address, format is unix://[path];[user];[group];[mode]: %v", addr)
|
||||
}
|
||||
|
||||
splitAddr := strings.Split(strings.TrimPrefix(addr, "unix://"), ";")
|
||||
if len(splitAddr) != 4 {
|
||||
return nil, fmt.Errorf("Failed to parse Unix address, format is [path];[user];[group];[mode]: %v", addr)
|
||||
return nil, fmt.Errorf("Failed to parse Unix address, format is unix://[path];[user];[group];[mode]: %v", addr)
|
||||
}
|
||||
|
||||
ret := &UnixSocket{Path: splitAddr[0]}
|
||||
|
||||
if userVal, err := user.Lookup(splitAddr[1]); err != nil {
|
||||
var userVal *user.User
|
||||
var err error
|
||||
|
||||
regex := regexp.MustCompile("[\\d]+")
|
||||
if regex.MatchString(splitAddr[1]) {
|
||||
userVal, err = user.LookupId(splitAddr[1])
|
||||
} else {
|
||||
userVal, err = user.Lookup(splitAddr[1])
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Invalid user given for Unix socket ownership: %v", splitAddr[1])
|
||||
} else {
|
||||
if uid64, err := strconv.ParseInt(userVal.Uid, 10, 32); err != nil {
|
||||
|
@ -5,8 +5,10 @@ import (
|
||||
"encoding/base64"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@ -1068,3 +1070,65 @@ func TestReadConfigPaths_dir(t *testing.T) {
|
||||
t.Fatalf("bad: %#v", config)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixSockets(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
_, err := populateUnixSocket("tcp://abc123")
|
||||
if err == nil {
|
||||
t.Fatal("Should have rejected invalid scheme")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://x;y;z")
|
||||
if err == nil {
|
||||
t.Fatal("Should have rejected invalid number of parameters in Unix socket definition")
|
||||
}
|
||||
|
||||
user, err := user.Current()
|
||||
if err != nil {
|
||||
t.Fatal("Could not get current user")
|
||||
}
|
||||
|
||||
tempdir, err := ioutil.TempDir("", "consul-test-")
|
||||
if err != nil {
|
||||
t.Fatal("Could not create a working directory")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://" + tempdir + "/unixtest.sock;osdfjo9ihf9h82;" + user.Gid + ";640")
|
||||
if err == nil {
|
||||
t.Fatal("Did not error on invalid username")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://" + tempdir + "/unixtest.sock;999999;" + user.Gid + ";640")
|
||||
if err == nil {
|
||||
t.Fatal("Did not error on invalid uid")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://" + tempdir + "/unixtest.sock;" + user.Username + ";foihafwereworg;" + ";640")
|
||||
if err == nil {
|
||||
t.Fatal("Did not error on invalid group (a name, must be gid)")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://" + tempdir + "/unixtest.sock;" + user.Username + ";999999;" + ";640")
|
||||
if err == nil {
|
||||
t.Fatal("Did not error on invalid uid")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://" + tempdir + "/unixtest.sock;" + user.Username + ";" + user.Gid + ";999")
|
||||
if err == nil {
|
||||
t.Fatal("Did not error on invalid socket mode")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://" + tempdir + "/unixtest.sock;" + user.Username + ";" + user.Gid + ";640")
|
||||
if err != nil {
|
||||
t.Fatal("Unix socket test failed for no obvious reason (using username)")
|
||||
}
|
||||
|
||||
_, err = populateUnixSocket("unix://" + tempdir + "/unixtest.sock;" + user.Uid + ";" + user.Gid + ";640")
|
||||
if err != nil {
|
||||
t.Fatal("Unix socket test failed for no obvious reason (using uid)")
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user