socket: Enforce socket length

A Unix domain socket is limited to 107 usable bytes on Linux. However,
not all code creating socket paths was checking for this limits.

Created a new `utils.BuildSocketPath()` function (with tests) to
encapsulate the logic and updated all code creating sockets to use it.

Fixes #268.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt
2018-05-09 11:28:23 +01:00
parent f6544a3524
commit bce9edd277
12 changed files with 173 additions and 40 deletions

View File

@@ -8,7 +8,9 @@ package utils
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -247,5 +249,48 @@ func TestGetSCSIAddress(t *testing.T) {
scsiAddr, err := GetSCSIAddress(test.index)
assert.Nil(t, err)
assert.Equal(t, scsiAddr, test.expectedSCSIAddress)
}
}
func TestBuildSocketPath(t *testing.T) {
assert := assert.New(t)
type testData struct {
elems []string
valid bool
expected string
}
longPath := strings.Repeat("/a", 106/2)
longestPath := longPath + "a"
pathTooLong := filepath.Join(longestPath, "x")
data := []testData{
{[]string{""}, false, ""},
{[]string{"a"}, true, "a"},
{[]string{"/a"}, true, "/a"},
{[]string{"a", "b", "c"}, true, "a/b/c"},
{[]string{"a", "/b", "c"}, true, "a/b/c"},
{[]string{"/a", "b", "c"}, true, "/a/b/c"},
{[]string{"/a", "/b", "/c"}, true, "/a/b/c"},
{[]string{longPath}, true, longPath},
{[]string{longestPath}, true, longestPath},
{[]string{pathTooLong}, false, ""},
}
for i, d := range data {
result, err := BuildSocketPath(d.elems...)
if d.valid {
assert.NoErrorf(err, "test %d, data %+v", i, d)
} else {
assert.Errorf(err, "test %d, data %+v", i, d)
}
assert.NotNil(result)
assert.Equal(d.expected, result)
}
}