2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2017-09-06 11:44:11 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/internal/testutil"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-09-06 11:44:11 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoadOrCreateTrustKey
|
|
|
|
func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) {
|
|
|
|
tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-06 11:44:11 -04:00
|
|
|
defer os.RemoveAll(tmpKeyFolderPath)
|
|
|
|
|
|
|
|
tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-06 11:44:11 -04:00
|
|
|
|
|
|
|
_, err = loadOrCreateTrustKey(tmpKeyFile.Name())
|
|
|
|
testutil.ErrorContains(t, err, "Error loading key file")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadOrCreateTrustKeyCreateKeyWhenFileDoesNotExist(t *testing.T) {
|
|
|
|
tmpKeyFolderPath := fs.NewDir(t, "api-trustkey-test")
|
|
|
|
defer tmpKeyFolderPath.Remove()
|
|
|
|
|
|
|
|
// Without the need to create the folder hierarchy
|
|
|
|
tmpKeyFile := tmpKeyFolderPath.Join("keyfile")
|
|
|
|
|
|
|
|
key, err := loadOrCreateTrustKey(tmpKeyFile)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, key != nil)
|
2017-09-06 11:44:11 -04:00
|
|
|
|
|
|
|
_, err = os.Stat(tmpKeyFile)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "key file doesn't exist")
|
2017-09-06 11:44:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadOrCreateTrustKeyCreateKeyWhenDirectoryDoesNotExist(t *testing.T) {
|
|
|
|
tmpKeyFolderPath := fs.NewDir(t, "api-trustkey-test")
|
|
|
|
defer tmpKeyFolderPath.Remove()
|
|
|
|
tmpKeyFile := tmpKeyFolderPath.Join("folder/hierarchy/keyfile")
|
|
|
|
|
|
|
|
key, err := loadOrCreateTrustKey(tmpKeyFile)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, key != nil)
|
2017-09-06 11:44:11 -04:00
|
|
|
|
|
|
|
_, err = os.Stat(tmpKeyFile)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "key file doesn't exist")
|
2017-09-06 11:44:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadOrCreateTrustKeyCreateKeyNoPath(t *testing.T) {
|
|
|
|
defer os.Remove("keyfile")
|
|
|
|
key, err := loadOrCreateTrustKey("keyfile")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, key != nil)
|
2017-09-06 11:44:11 -04:00
|
|
|
|
|
|
|
_, err = os.Stat("keyfile")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "key file doesn't exist")
|
2017-09-06 11:44:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) {
|
|
|
|
tmpKeyFile := filepath.Join("testdata", "keyfile")
|
|
|
|
key, err := loadOrCreateTrustKey(tmpKeyFile)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-06 11:44:11 -04:00
|
|
|
expected := "AWX2:I27X:WQFX:IOMK:CNAK:O7PW:VYNB:ZLKC:CVAE:YJP2:SI4A:XXAY"
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Contains(key.String(), expected))
|
2017-09-06 11:44:11 -04:00
|
|
|
}
|