Debian Codegraph @

tpm/pcrs_test.go

raw
1 // Copyright (c) 2014, Google LLC All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tpm
16
17 import (
18 "testing"
19 )
20
21 func TestPCRMask(t *testing.T) {
22 var mask pcrMask
23 if err := mask.setPCR(-1); err == nil {
24 t.Fatal("Incorrectly allowed non-existent PCR -1 to be set")
25 }
26
27 if err := mask.setPCR(24); err == nil {
28 t.Fatal("Incorrectly allowed non-existent PCR 24 to be set")
29 }
30
31 if err := mask.setPCR(0); err != nil {
32 t.Fatal("Couldn't set PCR 0 in the mask:", err)
33 }
34
35 set, err := mask.isPCRSet(0)
36 if err != nil {
37 t.Fatal("Couldn't check to see if PCR 0 was set:", err)
38 }
39
40 if !set {
41 t.Fatal("Incorrectly said PCR wasn't set when it should have been")
42 }
43
44 if err := mask.setPCR(18); err != nil {
45 t.Fatal("Couldn't set PCR 18 in the mask:", err)
46 }
47
48 set, err = mask.isPCRSet(18)
49 if err != nil {
50 t.Fatal("Couldn't check to see if PCR 18 was set:", err)
51 }
52
53 if !set {
54 t.Fatal("Incorrectly said PCR wasn't set when it should have been")
55 }
56
57 if _, err := mask.isPCRSet(-1); err == nil {
58 t.Fatal("Incorrectly permitted a check for PCR -1")
59 }
60
61 if _, err := mask.isPCRSet(400); err == nil {
62 t.Fatal("Incorrectly permitted a check for PCR 400")
63 }
64 }
65
66 func TestNewPCRSelection(t *testing.T) {
67 pcrs, err := newPCRSelection([]int{17, 18})
68 if err != nil {
69 t.Fatal("Couldn't set up a PCR selection with PCRs 17 and 18")
70 }
71
72 if pcrs.Size != 3 {
73 t.Fatal("Incorrectly size in a PCR selection")
74 }
75
76 set, err := pcrs.Mask.isPCRSet(17)
77 if err != nil {
78 t.Fatal("Couldn't check a PCR on a mask in a PCR selection")
79 }
80
81 if !set {
82 t.Fatal("PCR 17 wasn't set in a PCR selection after setting it")
83 }
84
85 set, err = pcrs.Mask.isPCRSet(20)
86 if err != nil {
87 t.Fatal("Couldn't check an unset PCR on a mask in a PCR selection")
88 }
89
90 if set {
91 t.Fatal("PCR 20 was incorrectly set in a PCR mask in a PCR selection")
92 }
93 }
94
95 func TestIncorrectCreatePCRComposite(t *testing.T) {
96 pcrs, err := newPCRSelection([]int{17, 18})
97 if err != nil {
98 t.Fatal("Couldn't set up a PCR selection with PCRs 17 and 18")
99 }
100
101 // This byte array is far too long and isn't a multiple of PCRSize, since it
102 // is of prime size.
103 pcrValues := make([]byte, 541)
104 if _, err := createPCRComposite(pcrs.Mask, pcrValues); err == nil {
105 t.Fatal("Incorrectly created a PCR composite with wrong PCR length")
106 }
107 }
108
109 func TestWrongCreatePCRInfoLong(t *testing.T) {
110 pcrs, err := newPCRSelection([]int{17, 18})
111 if err != nil {
112 t.Fatal("Couldn't set up a PCR selection with PCRs 17 and 18")
113 }
114
115 // This byte array is far too long and isn't a multiple of PCRSize, since it
116 // is of prime size.
117 pcrValues := make([]byte, 541)
118 if _, err := createPCRInfoLong(0, pcrs.Mask, pcrValues); err == nil {
119 t.Fatal("Incorrectly created a PCR composite with wrong PCR length")
120 }
121 }
122
123 func TestWrongNewPCRInfoLong(t *testing.T) {
124 rwc := openTPMOrSkip(t)
125 defer rwc.Close()
126
127 if _, err := newPCRInfoLong(rwc, 0, []int{400}); err == nil {
128 t.Fatal("Incorrectly created a pcrInfoLong for PCR 400")
129 }
130
131 // This case uses a reasonable PCR value but a nil file.
132 if _, err := newPCRInfoLong(nil, 0, []int{17}); err == nil {
133 t.Fatal("Incorrectly created a pcrInfoLong using a nil file")
134 }
135 }
136
137 func TestNewPCRInfoLongWithHashes(t *testing.T) {
138 pcrMap := make(map[int][]byte)
139 pcrMap[23] = make([]byte, 20)
140 pcrMap[16] = make([]byte, 20)
141
142 if _, err := newPCRInfoLongWithHashes(LocZero, pcrMap); err != nil {
143 t.Fatal("Couldn't create pcrInfoLong structure")
144 }
145 }