2022-03-22 11:03:15 -04:00
|
|
|
package cryptmapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-24 10:17:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/mount/kms"
|
2022-03-23 06:00:07 -04:00
|
|
|
cryptsetup "github.com/martinjungblut/go-cryptsetup"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testDEK = []byte{
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubCryptDevice struct {
|
|
|
|
initErr error
|
|
|
|
activateErr error
|
|
|
|
deactivateErr error
|
|
|
|
formatErr error
|
|
|
|
loadErr error
|
|
|
|
wipeErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubCryptDevice) Init(devicePath string) error {
|
|
|
|
return c.initErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubCryptDevice) ActivateByVolumeKey(deviceName, volumeKey string, volumeKeySize, flags int) error {
|
|
|
|
return c.activateErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubCryptDevice) Deactivate(deviceName string) error {
|
|
|
|
return c.deactivateErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubCryptDevice) Format(deviceType cryptsetup.DeviceType, genericParams cryptsetup.GenericParams) error {
|
|
|
|
return c.formatErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubCryptDevice) Free() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-03-23 06:00:07 -04:00
|
|
|
func (c *stubCryptDevice) Load(cryptsetup.DeviceType) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
return c.loadErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubCryptDevice) Wipe(devicePath string, pattern int, offset, length uint64, wipeBlockSize int, flags int, progress func(size, offset uint64) int) error {
|
|
|
|
return c.wipeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseCryptDevice(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
mapper *stubCryptDevice
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2022-04-26 10:54:05 -04:00
|
|
|
mapper: &stubCryptDevice{},
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error on Init": {
|
|
|
|
mapper: &stubCryptDevice{
|
|
|
|
initErr: errors.New("error"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error on Deactivate": {
|
|
|
|
mapper: &stubCryptDevice{
|
|
|
|
deactivateErr: errors.New("error"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
err := closeCryptDevice(tc.mapper, "/dev/some-device", "volume0", "test")
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:21:19 -04:00
|
|
|
mapper := New(kms.NewStaticKMS(), &stubCryptDevice{})
|
2022-03-22 11:03:15 -04:00
|
|
|
err := mapper.CloseCryptDevice("volume01-unit-test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenCryptDevice(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
source string
|
|
|
|
volumeID string
|
|
|
|
dek string
|
|
|
|
integrity bool
|
|
|
|
mapper *stubCryptDevice
|
|
|
|
diskInfo func(disk string) (string, error)
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"success with Load": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(testDEK),
|
|
|
|
mapper: &stubCryptDevice{},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"success with error on Load": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(testDEK),
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"success with integrity": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(append(testDEK, testDEK[:32]...)),
|
|
|
|
integrity: true,
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"incorrect key size": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: "short",
|
|
|
|
mapper: &stubCryptDevice{},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error on Init": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(testDEK),
|
|
|
|
mapper: &stubCryptDevice{initErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error on Format": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(testDEK),
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr, formatErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error on Activate": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(testDEK),
|
|
|
|
mapper: &stubCryptDevice{activateErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error on diskInfo": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(testDEK),
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", someErr },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"disk is already formatted": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(testDEK),
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "ext4", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error with integrity on wipe": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(append(testDEK, testDEK[:32]...)),
|
|
|
|
integrity: true,
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr, wipeErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error with integrity on activate": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(append(testDEK, testDEK[:32]...)),
|
|
|
|
integrity: true,
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr, activateErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"error with integrity on deactivate": {
|
2022-04-26 10:54:05 -04:00
|
|
|
source: "/dev/some-device",
|
|
|
|
volumeID: "volume0",
|
|
|
|
dek: string(append(testDEK, testDEK[:32]...)),
|
|
|
|
integrity: true,
|
|
|
|
mapper: &stubCryptDevice{loadErr: someErr, deactivateErr: someErr},
|
|
|
|
diskInfo: func(disk string) (string, error) { return "", nil },
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
out, err := openCryptDevice(tc.mapper, tc.source, tc.volumeID, tc.dek, tc.integrity, tc.diskInfo)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(cryptPrefix+tc.volumeID, out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:21:19 -04:00
|
|
|
mapper := New(kms.NewStaticKMS(), &stubCryptDevice{})
|
2022-03-22 11:03:15 -04:00
|
|
|
_, err := mapper.OpenCryptDevice(context.Background(), "/dev/some-device", "volume01", false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|