| 1 |
package tbs |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
// Encodes a call to Getrandom() with a buffer of length zero, making this |
| 10 |
// command an effective no-op. |
| 11 |
var getRandomRawCommand = []byte{128, 1, 0, 0, 0, 12, 0, 0, 1, 123, 0, 0} |
| 12 |
|
| 13 |
func getContext(t *testing.T) Context { |
| 14 |
ctx, err := CreateContext(TPMVersion20, IncludeTPM12|IncludeTPM20) |
| 15 |
if err != nil { |
| 16 |
t.Skipf("Skipping test as we couldn't access the TPM: %v", err) |
| 17 |
} |
| 18 |
return ctx |
| 19 |
} |
| 20 |
|
| 21 |
// Get the log by passing in progressively larger buffers |
| 22 |
func TestGetLogLargeBuffer(t *testing.T) { |
| 23 |
ctx := getContext(t) |
| 24 |
defer ctx.Close() |
| 25 |
|
| 26 |
log := make([]byte, os.Getpagesize()) |
| 27 |
for { |
| 28 |
logLen, err := ctx.GetTCGLog(log) |
| 29 |
if err == nil { |
| 30 |
if logLen == 0 { |
| 31 |
t.Fatal("Expected positive TCGLog length") |
| 32 |
} |
| 33 |
return |
| 34 |
} |
| 35 |
if err != ErrInsufficientBuffer { |
| 36 |
t.Fatalf("GetTCGLog failed: %v", err) |
| 37 |
} |
| 38 |
log = make([]byte, 2*len(log)) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Get the log by passing in nil, checking the size, and then getting the log. |
| 43 |
func TestGetLogWithNilSlice(t *testing.T) { |
| 44 |
ctx := getContext(t) |
| 45 |
defer ctx.Close() |
| 46 |
|
| 47 |
logLen, err := ctx.GetTCGLog(nil) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("First GetTCGLog failed: %v", err) |
| 50 |
} |
| 51 |
if logLen == 0 { |
| 52 |
t.Fatal("Expected positive TCGLog length") |
| 53 |
} |
| 54 |
|
| 55 |
log := make([]byte, logLen) |
| 56 |
if _, err := ctx.GetTCGLog(log); err != nil { |
| 57 |
t.Fatalf("Second GetTCGLog failed: %v", err) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// SubmitCommand can handle a nil command buffer. |
| 62 |
func TestSubmitCommandNilCommand(t *testing.T) { |
| 63 |
ctx := getContext(t) |
| 64 |
defer ctx.Close() |
| 65 |
|
| 66 |
rawResponse := make([]byte, os.Getpagesize()) |
| 67 |
_, err := ctx.SubmitCommand(NormalPriority, nil, rawResponse) |
| 68 |
if err != ErrBadParameter { |
| 69 |
t.Fatalf("SubmitCommand failed with %v: expected ErrBadParameter", err) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// SubmitCommand can handle a nil response buffer. |
| 74 |
func TestSubmitCommandNilResponse(t *testing.T) { |
| 75 |
ctx := getContext(t) |
| 76 |
defer ctx.Close() |
| 77 |
|
| 78 |
_, err := ctx.SubmitCommand(NormalPriority, getRandomRawCommand, nil) |
| 79 |
if err != ErrInvalidOutputPointer { |
| 80 |
t.Fatalf("SubmitCommand failed with %v: expected ErrInvalidOutputPointer", err) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// SubmitCommand can handle a response buffer that is shorter than necessary. |
| 85 |
func TestSubmitCommandShortResponse(t *testing.T) { |
| 86 |
ctx := getContext(t) |
| 87 |
defer ctx.Close() |
| 88 |
|
| 89 |
rawResponse := make([]byte, 1) |
| 90 |
_, err := ctx.SubmitCommand(NormalPriority, getRandomRawCommand, rawResponse) |
| 91 |
if err != ErrInsufficientBuffer { |
| 92 |
t.Fatalf("SubmitCommand failed with %v: expected ErrInsufficientBuffer", err) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// SubmitCommand can handle a response buffer that is longer than necessary. |
| 97 |
func TestSubmitCommandLongResponse(t *testing.T) { |
| 98 |
ctx := getContext(t) |
| 99 |
defer ctx.Close() |
| 100 |
|
| 101 |
rawResponse := make([]byte, os.Getpagesize()) |
| 102 |
responseLen, err := ctx.SubmitCommand(NormalPriority, getRandomRawCommand, rawResponse) |
| 103 |
if err != nil { |
| 104 |
t.Fatalf("SubmitCommand failed: %v", err) |
| 105 |
} |
| 106 |
rawResponse = rawResponse[:responseLen] |
| 107 |
|
| 108 |
// Expected response buffer for getRandomRawCommand |
| 109 |
expectedGetRandomRawResponse := []byte{128, 1, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0} |
| 110 |
if !bytes.Equal(rawResponse, expectedGetRandomRawResponse) { |
| 111 |
t.Fatalf("Got response of %v, expected %v", rawResponse, expectedGetRandomRawResponse) |
| 112 |
} |
| 113 |
} |