github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/newfileproxy/resourcemanager/resource_test.go (about) 1 // Copyright (c) 2016, 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 resourcemanager 16 17 import ( 18 "crypto/ecdsa" 19 "crypto/elliptic" 20 "crypto/rand" 21 "crypto/x509" 22 "fmt" 23 "math/big" 24 "testing" 25 "sync" 26 "time" 27 28 "github.com/jlmucb/cloudproxy/go/apps/newfileproxy/common" 29 ) 30 31 func TestTimeEncode(t *testing.T) { 32 now := time.Now() 33 s, err := EncodeTime(now) 34 if err != nil { 35 t.Fatal("EncodeTime fails\n") 36 } 37 fmt.Printf("Encoded time: %s\n", s) 38 tt, err := DecodeTime(s) 39 if err != nil { 40 t.Fatal("DecodeTime fails\n") 41 } 42 if !now.Equal(*tt) { 43 t.Fatal("TestTimeEncode not equal\n") 44 } 45 fmt.Printf("TestTimeEncode succeeds") 46 } 47 48 func StringIntoPointer(s1 string) *string { 49 return &s1 50 } 51 52 func IntIntoPointer(i1 int) *int32 { 53 i := int32(i1) 54 return &i 55 } 56 57 func TestTableFunctions(t *testing.T) { 58 mutex := new(sync.RWMutex) 59 60 // Generate Certificates and keys for test. 61 notBefore := time.Now() 62 validFor := 365 * 24 * time.Hour 63 notAfter := notBefore.Add(validFor) 64 65 serialNumber := new(big.Int).SetInt64(1) 66 67 policyKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 68 if err != nil { 69 t.Fatal("TestTableFunctions: ecdsa.GenerateKey fails\n") 70 } 71 var policyPriv interface{} 72 var policyPub interface{} 73 policyPriv = policyKey 74 policyPub = policyKey.Public() 75 policyCert, err := common.CreateKeyCertificate(*serialNumber, "Google", "Google", 76 "US", policyPriv, nil, "", "TestPolicyCert", "US", 77 policyPub, notBefore, notAfter, true, 78 x509.KeyUsageCertSign|x509.KeyUsageKeyAgreement|x509.KeyUsageDigitalSignature) 79 if err != nil { 80 t.Fatal("TestTableFunctions: CreateKeyCertificate fails\n") 81 } 82 policyCertificate, err := x509.ParseCertificate(policyCert) 83 if err != nil { 84 t.Fatal("TestTableFunctions: ParseCertificate fails\n") 85 } 86 fmt.Printf("\nPolicyCert: %x\n", policyCert) 87 fmt.Printf("\nPolicyCertificate: %x\n", policyCertificate) 88 89 programKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 90 if err != nil { 91 t.Fatal("TestTableFunctions: ecdsa.GenerateKey fails\n") 92 } 93 var programPub interface{} 94 programPub = programKey.Public() 95 programCert, err := common.CreateKeyCertificate(*serialNumber, "Google", "Google", 96 "US", policyPriv, policyCertificate, "", "TestProgramCert", "US", 97 programPub, notBefore, notAfter, false, 98 x509.KeyUsageCertSign|x509.KeyUsageKeyAgreement|x509.KeyUsageDigitalSignature) 99 if err != nil { 100 t.Fatal("TestTableFunctions: CreateKeyCertificate fails\n") 101 } 102 fmt.Printf("\nProgramCert: %x\n", programCert) 103 104 user1Key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 105 if err != nil { 106 t.Fatal("TestTableFunctions: ecdsa.GenerateKey fails\n") 107 } 108 var user1Pub interface{} 109 user1Pub = user1Key.Public() 110 user1Cert, err := common.CreateKeyCertificate(*serialNumber, "Google", "Google", 111 "US", policyKey, policyCertificate, "", "TestUserCert1", "US", 112 user1Pub, notBefore, notAfter, true, 113 x509.KeyUsageCertSign|x509.KeyUsageKeyAgreement|x509.KeyUsageDigitalSignature) 114 if err != nil { 115 t.Fatal("TestTableFunctions: CreateKeyCertificate fails\n") 116 } 117 fmt.Printf("\nUserCert 1: %x\n", user1Cert) 118 119 user2Key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 120 if err != nil { 121 t.Fatal("TestTableFunctions: ecdsa.GenerateKey fails\n") 122 } 123 var user2Pub interface{} 124 user2Pub = user2Key.Public() 125 user2Cert, err := common.CreateKeyCertificate(*serialNumber, "Google", "Google", 126 "US", policyKey, policyCertificate, "", "TestUserCert2", "US", 127 user2Pub, notBefore, notAfter, false, 128 x509.KeyUsageCertSign|x509.KeyUsageKeyAgreement|x509.KeyUsageDigitalSignature) 129 if err != nil { 130 t.Fatal("TestTableFunctions: CreateKeyCertificate fails\n") 131 } 132 fmt.Printf("\nUserCert 2: %x\n", user2Cert) 133 134 programPrincipal := new(PrincipalInfo) 135 user1Principal := new(PrincipalInfo) 136 user2Principal := new(PrincipalInfo) 137 138 programPrincipal.Name = StringIntoPointer("TestProgramCert") 139 programPrincipal.Cert = programCert 140 user1Principal.Name = StringIntoPointer("TestUser1Cert") 141 user1Principal.Cert = user1Cert 142 user2Principal.Name = StringIntoPointer("TestUser2Cert") 143 user2Principal.Cert = user2Cert 144 145 resourceMaster := new(ResourceMasterInfo) 146 resourceMaster.ServiceName = StringIntoPointer("TestService") 147 resourceMaster.PolicyCert = policyCert 148 resourceMaster.BaseDirectoryName = StringIntoPointer("./tmptest") 149 150 cp1 := MakeCombinedPrincipalFromTwo(programPrincipal, user1Principal) 151 cp2 := MakeCombinedPrincipalFromTwo(programPrincipal, user2Principal) 152 if cp1 == nil || cp2 == nil { 153 t.Fatal("Can't make combined principal") 154 } 155 156 str_time1, err := EncodeTime(time.Now()) 157 if err != nil { 158 t.Fatal("Can't EncodeTime") 159 } 160 str_time2, err := EncodeTime(time.Now()) 161 if err != nil { 162 t.Fatal("Can't EncodeTime") 163 } 164 165 // Resource 1 166 res1 := new(ResourceInfo) 167 res1.Name = StringIntoPointer("TestFile1") 168 res1.Type = IntIntoPointer(int(ResourceType_FILE)) 169 res1.DateCreated = &str_time1 170 res1.DateModified = &str_time1 171 res1.Size = IntIntoPointer(0) 172 res1.Keys = nil 173 err = res1.AddOwner(*cp1, mutex) 174 if err != nil { 175 t.Fatal("AddOwner fails") 176 } 177 err = res1.AddReader(*cp2, mutex) 178 if err != nil { 179 t.Fatal("AddReader fails") 180 } 181 err = res1.AddWriter(*cp2, mutex) 182 if err != nil { 183 t.Fatal("AddWriter fails") 184 } 185 186 err = resourceMaster.InsertResource(res1, mutex) 187 if err != nil { 188 t.Fatal("InsertResource fails") 189 } 190 191 // Resource 2 192 res2 := new(ResourceInfo) 193 res2.Name = StringIntoPointer("TestFile2") 194 res2.Type = IntIntoPointer(int(ResourceType_FILE)) 195 res2.DateCreated = &str_time1 196 res2.DateModified = &str_time2 197 res2.Size = IntIntoPointer(0) 198 res2.Keys = nil 199 err = res2.AddOwner(*cp2, mutex) 200 if err != nil { 201 t.Fatal("AddOwner fails") 202 } 203 err = res2.AddReader(*cp1, mutex) 204 if err != nil { 205 t.Fatal("AddReader fails") 206 } 207 err = res2.AddWriter(*cp1, mutex) 208 if err != nil { 209 t.Fatal("AddWriter fails") 210 } 211 err = res2.AddReader(*cp2, mutex) 212 if err != nil { 213 t.Fatal("AddReader fails") 214 } 215 err = res2.AddWriter(*cp2, mutex) 216 if err != nil { 217 t.Fatal("AddReader fails") 218 } 219 220 err = resourceMaster.InsertResource(res2, mutex) 221 if err != nil { 222 t.Fatal("InsertResource fails") 223 } 224 225 fmt.Printf("\nReaders list: \n") 226 PrintPrincipalList(res2.Readers) 227 228 n := FindCombinedPrincipalPosition(*cp2, res2.Readers) 229 if n < 0 { 230 t.Fatal("FindCombinedPrincipalPosition fails") 231 } 232 x := resourceMaster.FindResource("TestFile1", mutex) 233 if x == nil { 234 t.Fatal("resourceMaster.FindResource TestFile1 fails") 235 } 236 fmt.Printf("\n") 237 x.PrintResource(*resourceMaster.BaseDirectoryName, true) 238 239 fmt.Printf("\n") 240 y := resourceMaster.FindResource("TestFile2", mutex) 241 if y == nil { 242 t.Fatal("resourceMaster.FindResource TestFile2 fails") 243 } 244 245 fileContents1 := []byte{1, 3, 5} 246 fileContents2 := []byte{2, 4, 6} 247 err = res1.Write(*resourceMaster.BaseDirectoryName, fileContents1) 248 if err != nil { 249 t.Fatal("res1.Write fails") 250 } 251 err = res2.Write(*resourceMaster.BaseDirectoryName, fileContents2) 252 if err != nil { 253 t.Fatal("res2.Write fails") 254 } 255 out1, err := res1.Read(*resourceMaster.BaseDirectoryName) 256 if err != nil { 257 t.Fatal("res1.Read fails") 258 } 259 fmt.Printf("out1: %x\n", out1) 260 out2, err := res2.Read(*resourceMaster.BaseDirectoryName) 261 if err != nil { 262 t.Fatal("res2.Read fails") 263 } 264 fmt.Printf("out2: %x\n", out2) 265 fmt.Printf("\n") 266 // TODO(jlm): consider removing 267 fmt.Printf("\n") 268 res1.PrintResource(*resourceMaster.BaseDirectoryName, true) 269 fmt.Printf("\n") 270 res2.PrintResource(*resourceMaster.BaseDirectoryName, true) 271 } 272 273 func TestResourceInfo(t *testing.T) { 274 return 275 }