2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAskToConfirm(t *testing.T) {
|
|
|
|
// errAborted is an error where the user aborted the action.
|
|
|
|
errAborted := errors.New("user aborted")
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "test",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ok, err := askToConfirm(cmd, "777")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return errAborted
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
input string
|
|
|
|
wantErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"user confirms": {"y\n", nil},
|
|
|
|
"user confirms long": {"yes\n", nil},
|
|
|
|
"user disagrees": {"n\n", errAborted},
|
|
|
|
"user disagrees long": {"no\n", errAborted},
|
|
|
|
"user is first unsure, but agrees": {"what?\ny\n", nil},
|
|
|
|
"user is first unsure, but disagrees": {"wait.\nn\n", errAborted},
|
|
|
|
"repeated invalid input": {"h\nb\nq\n", ErrInvalidInput},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
out := &bytes.Buffer{}
|
2022-03-22 11:03:15 -04:00
|
|
|
cmd.SetOut(out)
|
2022-04-13 07:01:38 -04:00
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
2022-03-22 11:03:15 -04:00
|
|
|
in := bytes.NewBufferString(tc.input)
|
|
|
|
cmd.SetIn(in)
|
|
|
|
|
|
|
|
err := cmd.Execute()
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.ErrorIs(err, tc.wantErr)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
output, err := io.ReadAll(out)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Contains(string(output), "777")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWarnAboutPCRs(t *testing.T) {
|
|
|
|
zero := []byte("00000000000000000000000000000000")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
pcrs map[uint32][]byte
|
|
|
|
dontWarnInit bool
|
|
|
|
wantWarnings []string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"no warnings": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
6: zero,
|
|
|
|
7: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
10: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"no warnings for missing non critical values": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"warn for BIOS": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantWarnings: []string{"BIOS"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"warn for OPROM": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantWarnings: []string{"OPROM"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"warn for MBR": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
5: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantWarnings: []string{"MBR"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"warn for kernel": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
9: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantWarnings: []string{"kernel"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"warn for initrd": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
8: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantWarnings: []string{"initrd"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"warn for initialization": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
11: zero,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
dontWarnInit: false,
|
|
|
|
wantWarnings: []string{"initialization"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"don't warn for initialization": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
11: zero,
|
|
|
|
},
|
|
|
|
dontWarnInit: true,
|
|
|
|
},
|
|
|
|
"multi warning": {
|
|
|
|
pcrs: map[uint32][]byte{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantWarnings: []string{
|
2022-03-22 11:03:15 -04:00
|
|
|
"BIOS",
|
|
|
|
"OPROM",
|
|
|
|
"MBR",
|
|
|
|
"initialization",
|
|
|
|
"initrd",
|
|
|
|
"kernel",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bad config": {
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: []byte("000"),
|
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
cmd := newInitCmd()
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.SetOut(&out)
|
|
|
|
var errOut bytes.Buffer
|
|
|
|
cmd.SetErr(&errOut)
|
|
|
|
|
|
|
|
err := warnAboutPCRs(cmd, tc.pcrs, !tc.dontWarnInit)
|
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-04-26 10:54:05 -04:00
|
|
|
if len(tc.wantWarnings) == 0 {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Empty(errOut.String())
|
|
|
|
} else {
|
2022-04-26 10:54:05 -04:00
|
|
|
for _, warning := range tc.wantWarnings {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Contains(errOut.String(), warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|