2023-05-17 11:36:31 -04:00
|
|
|
//go:build integration && linux && cgo
|
2022-04-07 10:39:21 -04:00
|
|
|
|
2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2023-07-18 10:20:03 -04:00
|
|
|
"encoding/json"
|
2022-08-02 06:35:23 -04:00
|
|
|
"flag"
|
2022-04-05 09:12:20 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2023-07-17 07:55:31 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/internal/diskencryption"
|
2023-07-18 10:20:03 -04:00
|
|
|
ccryptsetup "github.com/edgelesssys/constellation/v2/internal/cryptsetup"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-07-18 10:20:03 -04:00
|
|
|
cryptsetup "github.com/martinjungblut/go-cryptsetup"
|
2022-04-05 09:12:20 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-04-05 09:12:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
devicePath = "testDevice"
|
|
|
|
mappedDevice = "mappedDevice"
|
|
|
|
)
|
|
|
|
|
2022-08-02 06:35:23 -04:00
|
|
|
var diskPath = flag.String("disk", "", "Path to the disk to use for the benchmark")
|
|
|
|
|
|
|
|
func setup(sizeGB int) error {
|
|
|
|
return exec.Command("/bin/dd", "if=/dev/random", fmt.Sprintf("of=%s", devicePath), "bs=1G", fmt.Sprintf("count=%d", sizeGB)).Run()
|
2022-04-05 09:12:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func teardown() error {
|
|
|
|
return exec.Command("/bin/rm", "-f", devicePath).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-08-02 06:35:23 -04:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
if os.Getuid() != 0 {
|
|
|
|
fmt.Printf("This test suite requires root privileges, as libcrypsetup uses the kernel's device mapper.\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/issues/1262
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
)
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
result := m.Run()
|
|
|
|
os.Exit(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapper(t *testing.T) {
|
2022-08-02 06:35:23 -04:00
|
|
|
cryptsetup.SetDebugLevel(cryptsetup.CRYPT_LOG_ERROR)
|
|
|
|
cryptsetup.SetLogCallback(func(_ int, message string) { fmt.Println(message) })
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
2022-08-02 06:35:23 -04:00
|
|
|
require.NoError(setup(1), "failed to setup test disk")
|
2022-04-05 09:12:20 -04:00
|
|
|
defer func() { require.NoError(teardown(), "failed to delete test disk") }()
|
|
|
|
|
2023-07-17 07:55:31 -04:00
|
|
|
mapper, free, err := diskencryption.New(devicePath, logger.NewTest(t))
|
2022-04-05 09:12:20 -04:00
|
|
|
require.NoError(err, "failed to initialize crypt device")
|
2023-07-17 07:55:31 -04:00
|
|
|
defer free()
|
2022-04-05 09:12:20 -04:00
|
|
|
|
2023-07-18 10:20:03 -04:00
|
|
|
assert.False(mapper.IsInitialized())
|
2022-04-12 08:24:36 -04:00
|
|
|
|
|
|
|
// Format and map disk
|
2022-04-05 09:12:20 -04:00
|
|
|
passphrase := "unit-test"
|
|
|
|
require.NoError(mapper.FormatDisk(passphrase), "failed to format disk")
|
|
|
|
require.NoError(mapper.MapDisk(mappedDevice, passphrase), "failed to map disk")
|
|
|
|
require.NoError(mapper.UnmapDisk(mappedDevice), "failed to remove disk mapping")
|
2022-04-12 08:24:36 -04:00
|
|
|
|
2023-07-18 10:20:03 -04:00
|
|
|
// Make sure token was set
|
|
|
|
ccrypt := ccryptsetup.New()
|
|
|
|
freeDevice, err := ccrypt.Init(devicePath)
|
|
|
|
require.NoError(err, "failed to initialize crypt device")
|
|
|
|
defer freeDevice()
|
|
|
|
require.NoError(ccrypt.LoadLUKS2(), "failed to load LUKS2")
|
|
|
|
|
|
|
|
tokenJSON, err := ccrypt.TokenJSONGet(ccryptsetup.ConstellationStateDiskTokenID)
|
|
|
|
require.NoError(err, "token should have been set")
|
|
|
|
var token struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Keyslots []string `json:"keyslots"`
|
|
|
|
DiskIsInitialized bool `json:"diskIsInitialized"`
|
|
|
|
}
|
|
|
|
require.NoError(json.Unmarshal([]byte(tokenJSON), &token))
|
|
|
|
assert.False(token.DiskIsInitialized, "disk should be marked as not initialized")
|
|
|
|
assert.False(ccrypt.ConstellationStateDiskTokenIsInitialized(), "disk should be marked as not initialized")
|
|
|
|
|
|
|
|
// Disk should still be marked as not initialized because token is set to false.
|
|
|
|
assert.False(mapper.IsInitialized())
|
2022-04-12 08:24:36 -04:00
|
|
|
|
|
|
|
// Try to map disk with incorrect passphrase
|
2022-04-05 09:12:20 -04:00
|
|
|
assert.Error(mapper.MapDisk(mappedDevice, "invalid-passphrase"), "was able to map disk with incorrect passphrase")
|
2022-04-11 08:25:19 -04:00
|
|
|
|
2023-07-18 10:20:03 -04:00
|
|
|
// Disk can be reformatted without manually re-initializing a mapper
|
|
|
|
passphrase2 := passphrase + "2"
|
|
|
|
require.NoError(mapper.FormatDisk(passphrase2), "failed to format disk")
|
|
|
|
require.NoError(mapper.MapDisk(mappedDevice, passphrase2), "failed to map disk")
|
|
|
|
require.NoError(mapper.UnmapDisk(mappedDevice), "failed to remove disk mapping")
|
2022-06-29 10:17:23 -04:00
|
|
|
}
|