2022-04-07 10:39:21 -04:00
|
|
|
//go:build integration
|
|
|
|
|
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 (
|
2022-08-02 06:35:23 -04:00
|
|
|
"flag"
|
2022-04-05 09:12:20 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/internal/mapper"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-04-05 09:12:20 -04:00
|
|
|
"github.com/martinjungblut/go-cryptsetup"
|
|
|
|
"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") }()
|
|
|
|
|
2022-08-02 06:35:23 -04:00
|
|
|
mapper, err := mapper.New(devicePath, logger.NewTest(t))
|
2022-04-05 09:12:20 -04:00
|
|
|
require.NoError(err, "failed to initialize crypt device")
|
|
|
|
defer func() { require.NoError(mapper.Close(), "failed to close crypt device") }()
|
|
|
|
|
2022-04-12 08:24:36 -04:00
|
|
|
assert.False(mapper.IsLUKSDevice())
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
assert.True(mapper.IsLUKSDevice())
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
/*
|
2022-06-29 10:17:23 -04:00
|
|
|
type fakeMetadataAPI struct{}
|
|
|
|
|
|
|
|
func (f *fakeMetadataAPI) List(ctx context.Context) ([]metadata.InstanceMetadata, error) {
|
|
|
|
return []metadata.InstanceMetadata{
|
|
|
|
{
|
|
|
|
Name: "instanceName",
|
|
|
|
ProviderID: "fake://instance-id",
|
|
|
|
Role: role.Unknown,
|
2022-08-04 05:08:20 -04:00
|
|
|
VPCIP: "192.0.2.1",
|
2022-06-29 10:17:23 -04:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
*/
|