github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/tao/host_test.go (about) 1 // Copyright (c) 2014, Google Inc. 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 tao 16 17 import ( 18 "bytes" 19 "testing" 20 21 "github.com/jlmucb/cloudproxy/go/tao/auth" 22 ) 23 24 var testChild = auth.SubPrin{auth.PrinExt{Name: "TestChild"}} 25 26 func testNewTaoRootHost(t *testing.T) Host { 27 th, err := NewTaoRootHost() 28 if err != nil { 29 t.Fatal("Couldn't create a new RootHost:", err) 30 } 31 32 if err := th.AddedHostedProgram(testChild); err != nil { 33 t.Fatal("Couldn't add a test child program:", err) 34 } 35 36 return th 37 } 38 39 func testNewTaoStackedHost(t *testing.T) Host { 40 ft, err := NewSoftTao("", nil) 41 if err != nil { 42 t.Fatal("Couldn't set up a SoftTao for the StackedHost") 43 } 44 45 th, err := NewTaoStackedHost(ft) 46 if err != nil { 47 t.Fatal("Couldn't set up a StackedHost over a SoftTao") 48 } 49 50 return th 51 } 52 53 func testTaoHostRandomBytes(t *testing.T, th Host) { 54 b, err := th.GetRandomBytes(testChild, 10) 55 if err != nil { 56 t.Fatal("Couldn't get random bytes from the Host:", err) 57 } 58 59 if len(b) != 10 { 60 t.Fatal("The length of the returned random bytes is not 10") 61 } 62 } 63 64 func testTaoHostSharedSecretFailure(t *testing.T, th Host) { 65 tag := "test tag" 66 _, err := th.GetSharedSecret(tag, 10) 67 if err == nil { 68 t.Fatal("A Host that doesn't support shared secrets created one") 69 } 70 } 71 72 func testTaoHostAttest(t *testing.T, th Host) { 73 a, err := th.Attest(testChild, nil, nil, nil, auth.Const(true)) 74 if err != nil { 75 t.Fatal("Couldn't attest to a trival statement:", err) 76 } 77 78 if a == nil { 79 t.Fatal("Incorrectly returned an empty attestation from a successful Attest") 80 } 81 } 82 83 func testTaoHostEncryption(t *testing.T, th Host) { 84 data := []byte{1, 2, 3, 4, 5, 6, 7} 85 e, err := th.Encrypt(data) 86 if err != nil { 87 t.Fatal("Couldn't encrypt data") 88 } 89 90 d, err := th.Decrypt(e) 91 if err != nil { 92 t.Fatal("Couldn't decrypt encrypted data") 93 } 94 95 if !bytes.Equal(d, data) { 96 t.Fatal("Decrypted data didn't match original data") 97 } 98 } 99 100 func testHostName(t *testing.T, th Host) { 101 n := th.HostName() 102 if n.KeyHash == nil { 103 t.Fatal("HostName returned an invalid Host name") 104 } 105 } 106 107 func testTaoHostRemovedHostedProgram(t *testing.T, th Host) { 108 if err := th.RemovedHostedProgram(testChild); err != nil { 109 t.Fatal("Couldn't remove an existing hosted program") 110 } 111 } 112 113 func TestTaoRootHostRandomBytes(t *testing.T) { 114 testTaoHostRandomBytes(t, testNewTaoRootHost(t)) 115 } 116 117 func TestTaoRootHostSharedSecretFailure(t *testing.T) { 118 testTaoHostSharedSecretFailure(t, testNewTaoRootHost(t)) 119 } 120 121 func TestTaoRootHostAttest(t *testing.T) { 122 testTaoHostAttest(t, testNewTaoRootHost(t)) 123 } 124 125 func TestTaoRootHostEncryption(t *testing.T) { 126 testTaoHostEncryption(t, testNewTaoRootHost(t)) 127 } 128 129 func TestTaoRootHostName(t *testing.T) { 130 testHostName(t, testNewTaoRootHost(t)) 131 } 132 133 func TestTaoRootHostRemovedHostedProgram(t *testing.T) { 134 testTaoHostRemovedHostedProgram(t, testNewTaoRootHost(t)) 135 } 136 137 func TestTaoStackedHostRandomBytes(t *testing.T) { 138 testTaoHostRandomBytes(t, testNewTaoStackedHost(t)) 139 } 140 141 func TestTaoStackedHostSharedSecretFailure(t *testing.T) { 142 testTaoHostSharedSecretFailure(t, testNewTaoStackedHost(t)) 143 } 144 145 func TestTaoStackedHostAttest(t *testing.T) { 146 testTaoHostAttest(t, testNewTaoStackedHost(t)) 147 } 148 149 func TestTaoStackedHostEncryption(t *testing.T) { 150 testTaoHostEncryption(t, testNewTaoStackedHost(t)) 151 } 152 153 func TestTaoStackedHostName(t *testing.T) { 154 testHostName(t, testNewTaoStackedHost(t)) 155 } 156 157 func TestTaoStackedHostRemovedHostedProgram(t *testing.T) { 158 testTaoHostRemovedHostedProgram(t, testNewTaoStackedHost(t)) 159 }