github.com/zorawar87/trillian@v1.2.1/cmd/createtree/pem_test.go (about) 1 // Copyright 2017 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 main 16 17 import ( 18 "errors" 19 "testing" 20 21 "github.com/google/trillian/crypto/keys/der" 22 "github.com/google/trillian/crypto/keys/pem" 23 "github.com/google/trillian/crypto/keyspb" 24 ) 25 26 func TestWithPEMKeyFile(t *testing.T) { 27 pemPath, pemPassword := "../../testdata/log-rpc-server.privkey.pem", "towel" 28 29 wantTree := *defaultTree 30 wantTree.PrivateKey = mustMarshalAny(&keyspb.PEMKeyFile{ 31 Path: pemPath, 32 Password: pemPassword, 33 }) 34 35 runTest(t, []*testCase{ 36 { 37 desc: "empty pemKeyPath", 38 setFlags: func() { 39 *privateKeyFormat = "PEMKeyFile" 40 *pemKeyPath = "" 41 *pemKeyPass = pemPassword 42 }, 43 validateErr: errors.New("empty pem_key_path"), 44 wantErr: true, 45 }, 46 { 47 desc: "empty pemKeyPass", 48 setFlags: func() { 49 *privateKeyFormat = "PEMKeyFile" 50 *pemKeyPath = pemPath 51 *pemKeyPass = "" 52 }, 53 validateErr: errors.New("pemfile: empty password for file"), 54 wantErr: true, 55 }, 56 { 57 desc: "valid pemKeyPath and pemKeyPass", 58 setFlags: func() { 59 *privateKeyFormat = "PEMKeyFile" 60 *pemKeyPath = pemPath 61 *pemKeyPass = pemPassword 62 }, 63 wantTree: &wantTree, 64 }, 65 }) 66 } 67 68 func TestWithPrivateKey(t *testing.T) { 69 pemPath, pemPassword := "../../testdata/log-rpc-server.privkey.pem", "towel" 70 71 key, err := pem.ReadPrivateKeyFile(pemPath, pemPassword) 72 if err != nil { 73 t.Fatalf("Error reading test private key file: %v", err) 74 } 75 76 keyDER, err := der.MarshalPrivateKey(key) 77 if err != nil { 78 t.Fatalf("Error marshaling test private key to DER: %v", err) 79 } 80 81 wantTree := *defaultTree 82 wantTree.PrivateKey = mustMarshalAny(&keyspb.PrivateKey{ 83 Der: keyDER, 84 }) 85 86 runTest(t, []*testCase{ 87 { 88 desc: "empty pemKeyPath", 89 setFlags: func() { 90 *privateKeyFormat = "PrivateKey" 91 *pemKeyPath = "" 92 *pemKeyPass = pemPassword 93 }, 94 validateErr: errors.New("empty pem_key_path"), 95 wantErr: true, 96 }, 97 { 98 desc: "empty pemKeyPass", 99 setFlags: func() { 100 *privateKeyFormat = "PrivateKey" 101 *pemKeyPath = pemPath 102 *pemKeyPass = "" 103 }, 104 validateErr: errors.New("pemfile: empty password for file"), 105 wantErr: true, 106 }, 107 { 108 desc: "valid pemKeyPath and pemKeyPass", 109 setFlags: func() { 110 *privateKeyFormat = "PrivateKey" 111 *pemKeyPath = pemPath 112 *pemKeyPass = pemPassword 113 }, 114 wantTree: &wantTree, 115 }, 116 }) 117 }