|
| 1 | +// + build linux |
| 2 | + |
| 3 | +package vtpmhelper |
| 4 | + |
| 5 | +import ( |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/opencontainers/runc/libcontainer/vtpm" |
| 14 | + "github.com/opencontainers/runtime-spec/specs-go" |
| 15 | +) |
| 16 | + |
| 17 | +func TestCreateVTPMFail(t *testing.T) { |
| 18 | + vtpmdev := specs.VTPM{} |
| 19 | + |
| 20 | + _, err := CreateVTPM(&specs.Spec{}, &vtpmdev, 0) |
| 21 | + if err == nil { |
| 22 | + t.Fatalf("Could create vTPM without statepath %v", err) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// check prerequisites for starting a vTPM |
| 27 | +func checkPrerequisites(t *testing.T) { |
| 28 | + if os.Getuid() != 0 { |
| 29 | + t.Skip("Need to be root to run this test") |
| 30 | + } |
| 31 | + |
| 32 | + for _, executable := range []string{"swtpm_setup", "swtpm"} { |
| 33 | + if err := exec.Command(executable, "--help").Run(); err != nil { |
| 34 | + t.Skipf("Could not run %s --help: %v", executable, err) |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func createVTPM(t *testing.T, tpmversion string, createCertificates bool, runas string) *vtpm.VTPM { |
| 40 | + |
| 41 | + checkPrerequisites(t) |
| 42 | + |
| 43 | + workdir, err := ioutil.TempDir("", "runctest") |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("Could not create tmp dir: %s", err) |
| 46 | + } |
| 47 | + defer os.Remove(workdir) |
| 48 | + |
| 49 | + tpmdirname := path.Join(workdir, "myvtpm") |
| 50 | + |
| 51 | + spec := &specs.Spec{ |
| 52 | + Linux: &specs.Linux{ |
| 53 | + Devices: []specs.LinuxDevice{}, |
| 54 | + Resources: &specs.LinuxResources{}, |
| 55 | + }, |
| 56 | + } |
| 57 | + vtpmdev := &specs.VTPM{ |
| 58 | + Statepath: tpmdirname, |
| 59 | + TPMVersion: tpmversion, |
| 60 | + CreateCertificates: createCertificates, |
| 61 | + Runas: runas, |
| 62 | + } |
| 63 | + |
| 64 | + myvtpm, err := CreateVTPM(spec, vtpmdev, 0) |
| 65 | + if err != nil { |
| 66 | + if strings.Contains(err.Error(), "VTPM device driver not available") { |
| 67 | + t.Skipf("%v", err) |
| 68 | + } else { |
| 69 | + t.Fatalf("Could not create VTPM device: %v", err) |
| 70 | + } |
| 71 | + } |
| 72 | + return myvtpm |
| 73 | +} |
| 74 | + |
| 75 | +func destroyVTPM(t *testing.T, myvtpm *vtpm.VTPM) { |
| 76 | + tpmdirname := myvtpm.StatePath |
| 77 | + |
| 78 | + DestroyVTPMs([]*vtpm.VTPM{myvtpm}) |
| 79 | + |
| 80 | + if _, err := os.Stat(tpmdirname); !os.IsNotExist(err) { |
| 81 | + t.Fatalf("State directory should have been removed since it was created by vtpm-helpers") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func createRestartDestroyVTPM(t *testing.T, tpmversion string, createCertificates bool, runas string) { |
| 86 | + myvtpm := createVTPM(t, tpmversion, createCertificates, runas) |
| 87 | + |
| 88 | + err := myvtpm.Stop(false) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("VTPM could not be stopped cleanly: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + createdStatePath, err := myvtpm.Start() |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("VTPM could not be started: %v", err) |
| 96 | + } |
| 97 | + if createdStatePath { |
| 98 | + t.Fatalf("VTPM Start() should not have created the state path at this time") |
| 99 | + } |
| 100 | + |
| 101 | + destroyVTPM(t, myvtpm) |
| 102 | +} |
| 103 | + |
| 104 | +func TestCreateVTPM2(t *testing.T) { |
| 105 | + createRestartDestroyVTPM(t, "", true, "root") |
| 106 | + createRestartDestroyVTPM(t, "", false, "0") |
| 107 | + createRestartDestroyVTPM(t, "2", true, "0") |
| 108 | +} |
| 109 | + |
| 110 | +func TestCreateVTPM12(t *testing.T) { |
| 111 | + createRestartDestroyVTPM(t, "1.2", true, "root") |
| 112 | +} |
0 commit comments