github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/tao/rollback_support_test.go (about)

     1  // Copyright (c) 2014-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 tao
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  func TestProtectUnprotect(t *testing.T) {
    23  	symKeys := []byte{
    24  		0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
    25  		0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
    26  		0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
    27  	}
    28  	plaintext := []byte{
    29  		0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
    30  	}
    31  	ciphertext, err := Protect(symKeys, plaintext)
    32  	if err != nil {
    33  		t.Fatal("Protect failed " + err.Error() + "\n")
    34  	}
    35  	recoveredtext, err := Unprotect(symKeys, ciphertext)
    36  	if err != nil {
    37  		t.Fatal("Unprotect failed " + err.Error() + "\n")
    38  	}
    39  	fmt.Printf("Plaintext: %x\n", plaintext)
    40  	fmt.Printf("CipherText: %x\n", ciphertext)
    41  	fmt.Printf("RecoveredText: %x\n", recoveredtext)
    42  }
    43  
    44  func TestRollback(t *testing.T) {
    45  	tableKey := []byte{
    46  		0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
    47  		0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
    48  		0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
    49  	}
    50  	t1 := ReadRollbackTable("/tmp/testtable", tableKey)
    51  	if t1 == nil {
    52  		t.Fatal("ReadRollbackTable failed")
    53  	}
    54  	programName := "testProgram"
    55  	entryName := "firstEntry"
    56  	e := t1.LookupRollbackEntry(programName, entryName)
    57  	if e != nil {
    58  		t.Fatal("LookupRollbackEntry should have failed")
    59  	}
    60  	c := int64(1)
    61  	e = t1.UpdateRollbackEntry(programName, entryName, &c)
    62  	if e == nil {
    63  		t.Fatal("UpdateRollbackEntry failed")
    64  	}
    65  	e = t1.UpdateRollbackEntry(programName, "secondEntry", &c)
    66  	if e == nil {
    67  		t.Fatal("UpdateRollbackEntry (2) failed")
    68  	}
    69  	ok := WriteRollbackTable(t1, "/tmp/testtable2", tableKey)
    70  	if !ok {
    71  		t.Fatal("WriteRollbackTable failed")
    72  	}
    73  	t2 := ReadRollbackTable("/tmp/testtable2", tableKey)
    74  	if t2 == nil {
    75  		t.Fatal("ReadRollbackTable failed")
    76  	}
    77  	e = t2.LookupRollbackEntry(programName, entryName)
    78  	if e == nil {
    79  		t.Fatal("LookupRollbackEntry after reading table failed")
    80  	}
    81  	// e.PrintRollbackEntry()
    82  }