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

     1  //  Copyright (c) 2015, 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  	"math/rand"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/jlmucb/cloudproxy/go/tao/auth"
    23  )
    24  
    25  func TestInMemoryInit(t *testing.T) {
    26  	_, err := NewSoftTao("", nil)
    27  	if err != nil {
    28  		t.Fatal("Couldn't initialize a SoftTao in memory:", err)
    29  	}
    30  }
    31  
    32  func TestSoftTaoRandom(t *testing.T) {
    33  	ft, err := NewSoftTao("", nil)
    34  	if err != nil {
    35  		t.Fatal("Couldn't initialize a SoftTao in memory:", err)
    36  	}
    37  
    38  	if _, err := ft.GetRandomBytes(10); err != nil {
    39  		t.Fatal("Couldn't get 10 random bytes:", err)
    40  	}
    41  }
    42  
    43  func TestSoftTaoSeal(t *testing.T) {
    44  	ft, err := NewSoftTao("", nil)
    45  	if err != nil {
    46  		t.Fatal("Couldn't initialize a SoftTao in memory:", err)
    47  	}
    48  
    49  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
    50  	b := make([]byte, 33)
    51  	for i := range b {
    52  		b[i] = byte(r.Intn(256))
    53  	}
    54  
    55  	_, err = ft.Seal(b, SealPolicyDefault)
    56  	if err != nil {
    57  		t.Fatal("Couldn't seal data in the SoftTao under the default policy:", err)
    58  	}
    59  }
    60  
    61  func TestSoftTaoUnseal(t *testing.T) {
    62  	ft, err := NewSoftTao("", nil)
    63  	if err != nil {
    64  		t.Fatal("Couldn't initialize a SoftTao in memory:", err)
    65  	}
    66  
    67  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
    68  	b := make([]byte, 33)
    69  	for i := range b {
    70  		b[i] = byte(r.Intn(256))
    71  	}
    72  
    73  	s, err := ft.Seal(b, SealPolicyDefault)
    74  	if err != nil {
    75  		t.Fatal("Couldn't seal data in the SoftTao under the default policyL", err)
    76  	}
    77  
    78  	u, p, err := ft.Unseal(s)
    79  	if string(p) != SealPolicyDefault {
    80  		t.Fatal("Invalid policy returned by Unseal")
    81  	}
    82  
    83  	if len(u) != len(b) {
    84  		t.Fatal("Invalid unsealed length")
    85  	}
    86  
    87  	for i, v := range u {
    88  		if v != b[i] {
    89  			t.Fatalf("Incorrect byte at position %d", i)
    90  		}
    91  	}
    92  }
    93  
    94  func TestSoftTaoAttest(t *testing.T) {
    95  	ft, err := NewSoftTao("", nil)
    96  	if err != nil {
    97  		t.Fatal("Couldn't initialize a SoftTao in memory:", err)
    98  	}
    99  
   100  	self, err := ft.GetTaoName()
   101  	if err != nil {
   102  		t.Fatal("Couldn't get own name:", err)
   103  	}
   104  
   105  	kb, err := ft.(*SoftTao).keys.SigningKey.CanonicalKeyBytesFromSigner()
   106  	if err != nil {
   107  		t.Fatal("Couldn't get universal name from signingkey\n")
   108  	}
   109  
   110  	stmt := auth.Speaksfor{
   111  		// Delegate:  auth.NewKeyPrin([]byte("BogusKeyBytes1")),
   112  		Delegate:  auth.NewKeyPrin(kb),
   113  		Delegator: self,
   114  		// Delegator: self,
   115  	}
   116  
   117  	a, err := ft.Attest(nil, nil, nil, stmt)
   118  	if err != nil {
   119  		t.Fatal("Couldn't attest to a statement in the SoftTao:", err)
   120  	}
   121  	if a== nil {
   122  	}
   123  
   124  	// Make sure the attestation passes basic sanity checks.
   125  	_, err = a.Validate()
   126  	if err != nil {
   127  		t.Fatalf("The attestation produced by the SoftTao didn't pass validation: %s", err)
   128  	}
   129  }