github.com/zorawar87/trillian@v1.2.1/cmd/createtree/pkcs11_test.go (about)

     1  // +build pkcs11
     2  
     3  // Copyright 2017 Google Inc. All Rights Reserved.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package main
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/google/trillian/crypto/keyspb"
    25  )
    26  
    27  func TestRunPkcs11(t *testing.T) {
    28  	err := os.Chdir("../..")
    29  	if err != nil {
    30  		t.Fatalf("Unable to change working directory to ../..: %s", err)
    31  	}
    32  	defer os.Chdir("cmd/createtree")
    33  
    34  	pkcs11Tree := *defaultTree
    35  	pkcs11Tree.PrivateKey = mustMarshalAny(&keyspb.PKCS11Config{
    36  		TokenLabel: "log",
    37  		Pin:        "1234",
    38  		PublicKey: `-----BEGIN PUBLIC KEY-----
    39  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7/tWwqUXZJaNfnpvnqiaeNMkn
    40  hKusCsyAidrHxvuL+t54XFCHJwsB3wIlQZ4mMwb8mC/KRYhCqECBEoCAf/b0m3j/
    41  ASuEPLyYOrz/aEs3wP02IZQLGmihmjMk7T/ouNCuX7y1fTjX3GeVQ06U/EePwZFC
    42  xToc6NWBri0N3VVsswIDAQAB
    43  -----END PUBLIC KEY-----
    44  `,
    45  	})
    46  
    47  	runTest(t, []*testCase{
    48  		{
    49  			desc: "PKCS11ConfigFile",
    50  			setFlags: func() {
    51  				*privateKeyFormat = "PKCS11ConfigFile"
    52  				*pkcs11ConfigPath = "testdata/pkcs11-conf.json"
    53  			},
    54  			wantErr:  false,
    55  			wantTree: &pkcs11Tree,
    56  		},
    57  		{
    58  			desc: "emptyPKCS11Path",
    59  			setFlags: func() {
    60  				*privateKeyFormat = "PKCS11ConfigFile"
    61  				*pkcs11ConfigPath = ""
    62  			},
    63  			validateErr: errors.New("empty pkcs path"),
    64  			wantErr:     true,
    65  		},
    66  	})
    67  }