github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/support_libraries/protected_objects/protected_obj_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 protected_objects 16 17 import ( 18 "container/list" 19 "fmt" 20 "testing" 21 "time" 22 // "github.com/jlmucb/cloudproxy/go/tpm2" 23 // "github.com/golang/protobuf/proto" 24 ) 25 26 func TestTime(t *testing.T) { 27 ttb := time.Now() 28 ttn := time.Now() 29 tta := time.Now() 30 tb, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", ttb.String()) 31 if err != nil { 32 t.Fatal("Can't parse time before\n") 33 } 34 ta, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", tta.String()) 35 if err != nil { 36 t.Fatal("Can't parse time after\n") 37 } 38 fmt.Printf("tb: %s, tn: %s, ta: %s\n", tb.String(), ttn.String(), ta.String()) 39 if tb.After(ttn) { 40 t.Fatal("Time after fails\n") 41 } 42 if ta.Before(ttn) { 43 t.Fatal("Time before fails\n") 44 } 45 } 46 47 func TestBasicObject(t *testing.T) { 48 49 // Add three objects: a file and two keys 50 obj_type := "file" 51 status := "active" 52 notBefore := time.Now() 53 validFor := 365 * 24 * time.Hour 54 notAfter := notBefore.Add(validFor) 55 56 obj_1, err := CreateObject("/jlm/file/file1", 1, 57 &obj_type, &status, ¬Before, ¬After, nil) 58 if err != nil { 59 t.Fatal("Can't create object") 60 } 61 fmt.Printf("Obj: %s\n", *obj_1.NotBefore) 62 obj_type = "key" 63 obj_2, _ := CreateObject("/jlm/key/key1", 1, 64 &obj_type, &status, ¬Before, ¬After, nil) 65 obj_3, _ := CreateObject("/jlm/key/key2", 1, 66 &obj_type, &status, ¬Before, ¬After, nil) 67 68 // add them to object list 69 obj_list := list.New() 70 err = AddObject(obj_list, *obj_1) 71 if err != nil { 72 t.Fatal("Can't add object") 73 } 74 _ = AddObject(obj_list, *obj_2) 75 _ = AddObject(obj_list, *obj_3) 76 77 // Find object test 78 o3 := FindObject(obj_list, *obj_1.ObjId.ObjName, *obj_1.ObjId.ObjEpoch, nil, nil) 79 if o3 == nil { 80 t.Fatal("Can't find object") 81 } 82 fmt.Printf("Found object\n") 83 PrintObject(o3) 84 85 // Make a protected object 86 protectorKeys := []byte{ 87 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 88 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 89 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 90 } 91 p_obj_1, err := MakeProtectedObject(*obj_1, "/jlm/key/key1", 1, protectorKeys) 92 if err != nil { 93 t.Fatal("Can't make protected object") 94 } 95 if p_obj_1 == nil { 96 t.Fatal("Bad protected object") 97 } 98 PrintProtectedObject(p_obj_1) 99 100 p_obj_2, err := MakeProtectedObject(*obj_2, "/jlm/key/key2", 1, protectorKeys) 101 if err != nil { 102 t.Fatal("Can't make protected object") 103 } 104 if p_obj_2 == nil { 105 t.Fatal("Bad protected object") 106 } 107 PrintProtectedObject(p_obj_2) 108 109 p_obj_3, err := RecoverProtectedObject(p_obj_1, protectorKeys) 110 if err != nil { 111 t.Fatal("Can't recover protected object") 112 } 113 if *obj_1.ObjId.ObjName != *p_obj_3.ObjId.ObjName { 114 t.Fatal("objects don't match") 115 } 116 117 protected_obj_list := list.New() 118 _ = AddProtectedObject(protected_obj_list, *p_obj_1) 119 _ = AddProtectedObject(protected_obj_list, *p_obj_2) 120 121 pr_list1 := FindProtectorObjects(protected_obj_list, "/jlm/key/key1", 1) 122 if pr_list1 == nil { 123 t.Fatal("FindProtectorObjects fails") 124 } 125 fmt.Printf("Protecting:\n") 126 for e := pr_list1.Front(); e != nil; e = e.Next() { 127 o := e.Value.(ProtectedObjectMessage) 128 PrintProtectedObject(&o) 129 } 130 fmt.Printf("\n") 131 fmt.Printf("Protected:\n") 132 pr_list2 := FindProtectedObjects(protected_obj_list, "/jlm/key/key1", 1) 133 if pr_list2 == nil { 134 t.Fatal("FindProtectedObjects fails") 135 } 136 for e := pr_list2.Front(); e != nil; e = e.Next() { 137 o := e.Value.(ProtectedObjectMessage) 138 PrintProtectedObject(&o) 139 } 140 fmt.Printf("\n") 141 } 142 143 func TestEarliestandLatest(t *testing.T) { 144 145 // Add three objects: a file and two keys 146 obj_type := "key" 147 status := "active" 148 notBefore := time.Now() 149 validFor := 365 * 24 * time.Hour 150 notAfter := notBefore.Add(validFor) 151 152 obj_1, _ := CreateObject("/jlm/key/key1", 1, 153 &obj_type, &status, ¬Before, ¬After, nil) 154 obj_2, _ := CreateObject("/jlm/key/key1", 2, 155 &obj_type, &status, ¬Before, ¬After, nil) 156 157 // add them to object list 158 obj_list := list.New() 159 err := AddObject(obj_list, *obj_1) 160 if err != nil { 161 t.Fatal("Can't add object") 162 } 163 _ = AddObject(obj_list, *obj_2) 164 165 statuses := []string{"active"} 166 result := GetEarliestEpoch(obj_list, "/jlm/key/key1", statuses) 167 if result == nil { 168 t.Fatal("Can't get earliest epoch") 169 } 170 if *result.ObjId.ObjName != "/jlm/key/key1" || 171 result.ObjId.ObjEpoch == nil || *result.ObjId.ObjEpoch != 1 { 172 t.Fatal("Earliest epoch failed") 173 } 174 175 result = GetLatestEpoch(obj_list, "/jlm/key/key1", statuses) 176 if result == nil { 177 t.Fatal("Can't get latest epoch") 178 } 179 if *result.ObjId.ObjName != "/jlm/key/key1" || 180 result.ObjId.ObjEpoch == nil || *result.ObjId.ObjEpoch != 2 { 181 PrintObject(result) 182 t.Fatal("Latest epoch failed") 183 } 184 } 185 186 func TestSaveAndRestore(t *testing.T) { 187 188 // Add three objects: a file and two keys 189 obj_type := "file" 190 status := "active" 191 notBefore := time.Now() 192 validFor := 365 * 24 * time.Hour 193 notAfter := notBefore.Add(validFor) 194 195 obj_1, err := CreateObject("/jlm/file/file1", 1, 196 &obj_type, &status, ¬Before, ¬After, nil) 197 if err != nil { 198 t.Fatal("Can't create object") 199 } 200 fmt.Printf("Obj: %s\n", *obj_1.NotBefore) 201 obj_type = "key" 202 obj_2, _ := CreateObject("/jlm/key/key1", 1, 203 &obj_type, &status, ¬Before, ¬After, nil) 204 obj_3, _ := CreateObject("/jlm/key/key2", 1, 205 &obj_type, &status, ¬Before, ¬After, nil) 206 207 // add them to object list 208 obj_list := list.New() 209 err = AddObject(obj_list, *obj_1) 210 if err != nil { 211 t.Fatal("Can't add object") 212 } 213 _ = AddObject(obj_list, *obj_2) 214 _ = AddObject(obj_list, *obj_3) 215 216 err = SaveObjects(obj_list, "tmptest/s1") 217 if err != nil { 218 t.Fatal("Can't save objects") 219 } 220 r := LoadObjects("tmptest/s1") 221 if r == nil { 222 t.Fatal("Can't Load objects") 223 } 224 225 if obj_list.Len() != r.Len() { 226 t.Fatal("recovered object list has different size") 227 } 228 229 er := obj_list.Front() 230 for eo := obj_list.Front(); eo != nil; eo = eo.Next() { 231 oo := eo.Value.(ObjectMessage) 232 or := er.Value.(ObjectMessage) 233 if *oo.ObjId.ObjName != *or.ObjId.ObjName { 234 t.Fatal("recovered name doesn't match") 235 } 236 if *oo.ObjId.ObjEpoch != *or.ObjId.ObjEpoch { 237 t.Fatal("recovered object doesn't match") 238 } 239 er = er.Next() 240 } 241 } 242 243 func TestConstructChain(t *testing.T) { 244 245 // Add three objects: a file and two keys 246 obj_type := "file" 247 status := "active" 248 notBefore := time.Now() 249 validFor := 365 * 24 * time.Hour 250 notAfter := notBefore.Add(validFor) 251 252 obj_1, err := CreateObject("/jlm/file/file1", 1, 253 &obj_type, &status, ¬Before, ¬After, nil) 254 if err != nil { 255 t.Fatal("Can't create object") 256 } 257 fmt.Printf("Obj: %s\n", *obj_1.NotBefore) 258 obj_type = "key" 259 obj_2, _ := CreateObject("/jlm/key/key1", 1, 260 &obj_type, &status, ¬Before, ¬After, nil) 261 obj_3, _ := CreateObject("/jlm/key/key2", 1, 262 &obj_type, &status, ¬Before, ¬After, nil) 263 264 // add them to object list 265 obj_list := list.New() 266 err = AddObject(obj_list, *obj_1) 267 if err != nil { 268 t.Fatal("Can't add object") 269 } 270 _ = AddObject(obj_list, *obj_2) 271 _ = AddObject(obj_list, *obj_3) 272 273 protected_obj_list := list.New() 274 275 // Make a protected object 276 protectorKeys := []byte{ 277 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 278 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 279 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 280 } 281 p_obj_1, err := MakeProtectedObject(*obj_1, "/jlm/key/key1", 1, protectorKeys) 282 if err != nil { 283 t.Fatal("Can't make protected object") 284 } 285 if p_obj_1 == nil { 286 t.Fatal("Bad protected object") 287 } 288 PrintProtectedObject(p_obj_1) 289 290 p_obj_2, err := MakeProtectedObject(*obj_2, "/jlm/key/key2", 1, protectorKeys) 291 if err != nil { 292 t.Fatal("Can't make protected object") 293 } 294 if p_obj_2 == nil { 295 t.Fatal("Bad protected object") 296 } 297 PrintProtectedObject(p_obj_2) 298 299 _ = AddProtectedObject(protected_obj_list, *p_obj_1) 300 _ = AddProtectedObject(protected_obj_list, *p_obj_2) 301 302 fmt.Printf("\n\nProtected Object list:\n") 303 for e := protected_obj_list.Front(); e != nil; e = e.Next() { 304 o := e.Value.(ProtectedObjectMessage) 305 PrintProtectedObject(&o) 306 } 307 fmt.Printf("\n\n") 308 309 statuses := []string{"active"} 310 311 seen_list := list.New() 312 chain, err := ConstructProtectorChain(obj_list, 313 "/jlm/file/file1", 1, nil, nil, statuses, nil, seen_list, protected_obj_list) 314 if err != nil { 315 fmt.Printf("err: %s\n", err) 316 t.Fatal("Can't ConstructProtectorChain ") 317 } 318 fmt.Printf("Protector Chain:\n") 319 for e := chain.Front(); e != nil; e = e.Next() { 320 o := e.Value.(ObjectMessage) 321 PrintObject(&o) 322 } 323 324 base_list := list.New() 325 target := new(ObjectIdMessage) 326 if target == nil { 327 t.Fatal("Can't make ObjectId --- ConstructProtectorChainFromBase") 328 } 329 330 base_name := "/jlm/key/key2" 331 base_epoch := int32(1) 332 seen_list_base := list.New() 333 target.ObjName = &base_name 334 target.ObjEpoch = &base_epoch 335 AddObjectId(base_list, *target) 336 337 chain, err = ConstructProtectorChainFromBase(obj_list, 338 "/jlm/file/file1", 1, statuses, nil, base_list, seen_list_base, protected_obj_list) 339 if err != nil { 340 fmt.Printf("err: %s\n", err) 341 t.Fatal("Can't ConstructProtectorChainFromBase") 342 } 343 fmt.Printf("\nBase chain:\n") 344 for e := chain.Front(); e != nil; e = e.Next() { 345 o := e.Value.(ObjectMessage) 346 PrintObject(&o) 347 } 348 349 base_name = "/jlm/key/key4" 350 base_epoch = int32(1) 351 seen_list_base = list.New() 352 target.ObjName = &base_name 353 target.ObjEpoch = &base_epoch 354 AddObjectId(base_list, *target) 355 chain, err = ConstructProtectorChainFromBase(obj_list, 356 "/jlm/file/file1", 1, statuses, nil, base_list, seen_list_base, protected_obj_list) 357 if err == nil { 358 fmt.Printf("shouldn't have found any satisfying objects") 359 } 360 }