mirror of
https://github.com/status-im/consul.git
synced 2025-02-23 02:48: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"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -364,17 +365,27 @@ type UnixSocket struct {
|
|||||||
|
|
||||||
func populateUnixSocket(addr string) (*UnixSocket, error) {
|
func populateUnixSocket(addr string) (*UnixSocket, error) {
|
||||||
if !strings.HasPrefix(addr, "unix://") {
|
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://"), ";")
|
splitAddr := strings.Split(strings.TrimPrefix(addr, "unix://"), ";")
|
||||||
if len(splitAddr) != 4 {
|
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]}
|
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])
|
return nil, fmt.Errorf("Invalid user given for Unix socket ownership: %v", splitAddr[1])
|
||||||
} else {
|
} else {
|
||||||
if uid64, err := strconv.ParseInt(userVal.Uid, 10, 32); err != nil {
|
if uid64, err := strconv.ParseInt(userVal.Uid, 10, 32); err != nil {
|
||||||
|
@ -5,8 +5,10 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -1068,3 +1070,65 @@ func TestReadConfigPaths_dir(t *testing.T) {
|
|||||||
t.Fatalf("bad: %#v", config)
|
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