| 1 |
package cel |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"fmt" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/google/go-cmp/cmp" |
| 11 |
gecel "github.com/google/go-eventlog/cel" |
| 12 |
"github.com/google/go-tpm-tools/client" |
| 13 |
"github.com/google/go-tpm-tools/internal/test" |
| 14 |
pb "github.com/google/go-tpm-tools/proto/attest" |
| 15 |
"github.com/google/go-tpm/legacy/tpm2" |
| 16 |
"github.com/google/go-tpm/tpmutil" |
| 17 |
) |
| 18 |
|
| 19 |
func TestCosEventlog(t *testing.T) { |
| 20 |
tpm := test.GetTPM(t) |
| 21 |
defer client.CheckedClose(t, tpm) |
| 22 |
cel := gecel.NewPCR() |
| 23 |
|
| 24 |
testEvents := []struct { |
| 25 |
cosNestedEventType CosType |
| 26 |
pcr int |
| 27 |
eventPayload []byte |
| 28 |
}{ |
| 29 |
{ImageRefType, test.DebugPCR, []byte("docker.io/bazel/experimental/test:latest")}, |
| 30 |
{ImageDigestType, test.DebugPCR, []byte("sha256:781d8dfdd92118436bd914442c8339e653b83f6bf3c1a7a98efcfb7c4fed7483")}, |
| 31 |
{RestartPolicyType, test.DebugPCR, []byte(pb.RestartPolicy_Never.String())}, |
| 32 |
{ImageIDType, test.DebugPCR, []byte("sha256:5DF4A1AC347DCF8CF5E9D0ABC04B04DB847D1B88D3B1CC1006F0ACB68E5A1F4B")}, |
| 33 |
{EnvVarType, test.DebugPCR, []byte("foo=bar")}, |
| 34 |
{EnvVarType, test.DebugPCR, []byte("override-env-1=foo")}, |
| 35 |
{EnvVarType, test.DebugPCR, []byte("baz=foo=bar")}, |
| 36 |
{EnvVarType, test.DebugPCR, []byte("empty=")}, |
| 37 |
{EnvVarType, test.DebugPCR, []byte("override-env-2=foo")}, |
| 38 |
{OverrideEnvType, test.DebugPCR, []byte("override-env-1=foo")}, |
| 39 |
{OverrideEnvType, test.DebugPCR, []byte("override-env-2=foo")}, |
| 40 |
{ArgType, test.DebugPCR, []byte("--x")}, |
| 41 |
{ArgType, test.DebugPCR, []byte("--override-arg-1")}, |
| 42 |
{ArgType, test.DebugPCR, []byte("--override-arg-2")}, |
| 43 |
{OverrideArgType, test.DebugPCR, []byte("--override-arg1")}, |
| 44 |
{OverrideArgType, test.DebugPCR, []byte("--override-arg2")}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, testEvent := range testEvents { |
| 48 |
cosEvent := CosTlv{testEvent.cosNestedEventType, testEvent.eventPayload} |
| 49 |
|
| 50 |
pcrSels, err := client.AllocatedPCRs(tpm) |
| 51 |
if err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
|
| 55 |
var hashAlgos []crypto.Hash |
| 56 |
for _, sel := range pcrSels { |
| 57 |
hashAlgo, err := sel.Hash.Hash() |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
hashAlgos = append(hashAlgos, hashAlgo) |
| 62 |
} |
| 63 |
|
| 64 |
cel.AppendEvent(cosEvent, hashAlgos, testEvent.pcr, func(hs crypto.Hash, pcr int, digest []byte) error { |
| 65 |
tpm2Alg, err := tpm2.HashToAlgorithm(hs) |
| 66 |
if err != nil { |
| 67 |
return err |
| 68 |
} |
| 69 |
if err := tpm2.PCRExtend(tpm, tpmutil.Handle(pcr), tpm2Alg, digest, ""); err != nil { |
| 70 |
return fmt.Errorf("failed to extend event to PCR%d: %v", pcr, err) |
| 71 |
} |
| 72 |
return nil |
| 73 |
}) |
| 74 |
} |
| 75 |
|
| 76 |
var buf bytes.Buffer |
| 77 |
if err := cel.EncodeCEL(&buf); err != nil { |
| 78 |
t.Fatal(err) |
| 79 |
} |
| 80 |
decodedcel, err := gecel.DecodeToCEL(&buf) |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
|
| 85 |
if len(decodedcel.Records()) != len(testEvents) { |
| 86 |
t.Errorf("should have %d records, but got %d", len(testEvents), len(decodedcel.Records())) |
| 87 |
} |
| 88 |
|
| 89 |
for i, testEvent := range testEvents { |
| 90 |
extractedCos, err := ParseToCosTlv(decodedcel.Records()[i].Content) |
| 91 |
if err != nil { |
| 92 |
t.Fatal(err) |
| 93 |
} |
| 94 |
|
| 95 |
want := CosTlv{testEvent.cosNestedEventType, testEvent.eventPayload} |
| 96 |
if !cmp.Equal(extractedCos, want) { |
| 97 |
t.Errorf("decoded COS TLV got %+v, want %+v", extractedCos, want) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestParseEnvVar(t *testing.T) { |
| 103 |
tests := []struct { |
| 104 |
testName string |
| 105 |
envVar string |
| 106 |
envName string |
| 107 |
envValue string |
| 108 |
expectedErrSubstring string |
| 109 |
}{ |
| 110 |
{"normal case 1", "foo=bar", "foo", "bar", ""}, |
| 111 |
{"normal case 2", "FOO=1", "FOO", "1", ""}, |
| 112 |
{"normal case 3", "SESSION_MANAGER=\"`\\local/:@?%/tmp/.u/1,unix/.com:/tmp/.u/5\"", "SESSION_MANAGER", "\"`\\local/:@?%/tmp/.u/1,unix/.com:/tmp/.u/5\"", ""}, |
| 113 |
{"no =", "foo", "", "", "malformed env var, doesn't contain '='"}, |
| 114 |
{"empty", "", "", "", "malformed env var, doesn't contain '='"}, |
| 115 |
{"empty value", "foo=", "foo", "", ""}, |
| 116 |
{"multiple =", "foo=bar=baz=", "foo", "bar=baz=", ""}, |
| 117 |
{"bad name", "3foo=bar=baz=", "", "", "env name must start with an alpha character or '_'"}, |
| 118 |
{"bad name quote", "foo\"=bar=baz=", "", "", "env name must start with an alpha character or '_'"}, |
| 119 |
{"empty name", "=bar=baz=", "", "", "env name must start with an alpha character or '_'"}, |
| 120 |
{"non utf-8 value", string([]byte{'f', '=', 0xC0, 2, 2, '='}), "", "", "malformed env value, contains non-utf8 character"}, |
| 121 |
{"non utf-8 name", string([]byte{'a', 0xC0, 2, 2, '='}), "", "", "malformed env name, contains non-utf8 character"}, |
| 122 |
} |
| 123 |
|
| 124 |
for _, test := range tests { |
| 125 |
t.Run(test.testName, func(t *testing.T) { |
| 126 |
n, v, err := ParseEnvVar(test.envVar) |
| 127 |
|
| 128 |
if n != test.envName { |
| 129 |
t.Errorf("envName mismatch, want [%s], got [%s]", test.envName, n) |
| 130 |
} |
| 131 |
if v != test.envValue { |
| 132 |
t.Errorf("envValue mismatch, want [%s], got [%s]", test.envValue, v) |
| 133 |
} |
| 134 |
if test.expectedErrSubstring == "" { |
| 135 |
if err != nil { |
| 136 |
t.Errorf("expected no error, but got [%s]", err) |
| 137 |
} else { |
| 138 |
formattedEnvVar, err := FormatEnvVar(test.envName, test.envValue) |
| 139 |
if err != nil { |
| 140 |
t.Errorf("expected no error, but got [%s]", err) |
| 141 |
} else if formattedEnvVar != test.envVar { |
| 142 |
t.Errorf("formattedEnvVar mismatch, want [%s], got [%s]", test.envVar, formattedEnvVar) |
| 143 |
} |
| 144 |
} |
| 145 |
} else { |
| 146 |
if err == nil { |
| 147 |
t.Errorf("expected error substring [%s], but got no error", test.expectedErrSubstring) |
| 148 |
} else if !strings.Contains(err.Error(), test.expectedErrSubstring) { |
| 149 |
t.Errorf("expected error substring [%s], but got [%v]", test.expectedErrSubstring, err) |
| 150 |
} |
| 151 |
} |
| 152 |
}) |
| 153 |
} |
| 154 |
} |