1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// Copyright 2017 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logid
import (
"crypto/sha256"
"encoding/base64"
"log"
"reflect"
"testing"
)
var (
wanted LogID = [32]byte{
'c', 't', 'l', 'o', 'g', '_', 'i', 'd', '_', 'f', 'o', 'r', '_', 't', 'e', 's',
't', 'i', 'n', 'g', '_', 'p', 'u', 'r', 'p', 'o', 's', 'e', 's', '!', '!', '1',
}
)
func toBytes(s string, l int) []byte {
bs := []byte(s)
if len(bs) != l {
log.Fatalf("string %q is %d bytes long, wanted length %d", s, len(bs), l)
}
return bs
}
func TestFromBytes(t *testing.T) {
tests := []struct {
in string
size int
want *LogID
wantErr bool
}{
{"ctlog_id_for_testing_purposes!!1", sha256.Size, &wanted, false},
// "invalid CT LogID - expected 32 bytes but got 16"
{"ctlog_id_for_tes", 16, nil, true},
// "invalid CT LogID - expected 32 bytes but got 37"
{"ctlog_id_for_testing_purposes!!1extra", 37, nil, true},
}
for _, test := range tests {
got, err := FromBytes(toBytes(test.in, test.size))
if gotErr := err != nil; gotErr != test.wantErr {
t.Errorf("FromBytes(%q): got err? %t, want? %t (err=%v)", test.in, gotErr, test.wantErr, err)
}
if err == nil && !reflect.DeepEqual(&got, test.want) {
t.Errorf("FromBytes(%q): got %v, wanted %v", test.in, got, test.want)
}
}
}
func TestFromB64(t *testing.T) {
tests := []struct {
in string
want *LogID
wantErr bool
}{
{"Y3Rsb2dfaWRfZm9yX3Rlc3RpbmdfcHVycG9zZXMhITE=", &wanted, false},
// "illegal base64 data at input byte 4"
{"garbage", nil, true},
// "invalid CT LogID - expected 32 bytes but got 1"
{"ab==", nil, true},
// "invalid CT LogID - expected 32 bytes but got 36"
{"this+input+decodes+to+36+bytes+which+is+too+long", nil, true},
}
for _, test := range tests {
got, err := FromB64(test.in)
if gotErr := err != nil; gotErr != test.wantErr {
t.Errorf("FromB64(%q): got err? %t, want? %t (err=%v)", test.in, gotErr, test.wantErr, err)
}
if err == nil && !reflect.DeepEqual(&got, test.want) {
t.Errorf("FromB64(%q): got %v, wanted %v", test.in, got, test.want)
}
}
}
func TestFromPubKeyB64(t *testing.T) {
tests := []struct {
desc string
b64 string
want string
wantErr bool
}{
{
desc: "not base64",
b64: "Not valid b64",
want: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
wantErr: true,
},
{
desc: "ECDSA",
b64: "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEfahLEimAoz2t01p3uMziiLOl/fHTDM0YDOhBRuiBARsV4UvxG2LdNgoIGLrtCzWE0J5APC2em4JlvR8EEEFMoA==",
want: "pLkJkLQYWBSHuxOizGdwCjw1mAT5G9+443fNDsgN3BA=",
wantErr: false,
},
{
desc: "RSA",
b64: "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAolpIHxdSlTXLo1s6H1OCdpSj/4DyHDc8wLG9wVmLqy1lk9fz4ATVmm+/1iN2Nk8jmctUKK2MFUtlWXZBSpym97M7frGlSaQXUWyA3CqQUEuIJOmlEjKTBEiQAvpfDjCHjlV2Be4qTM6jamkJbiWtgnYPhJL6ONaGTiSPm7Byy57iaz/hbckldSOIoRhYBiMzeNoA0DiRZ9KmfSeXZ1rB8y8X5urSW+iBzf2SaOfzBvDpcoTuAaWx2DPazoOl28fP1hZ+kHUYvxbcMjttjauCFx+JII0dmuZNIwjfeG/GBb9frpSX219k1O4Wi6OEbHEr8at/XQ0y7gTikOxBn/s5wQIDAQAB",
want: "rDua7X+pZ0dXFZ5tfVdWcvnZgQCUHpve/+yhMTt1eC0=",
wantErr: false,
},
}
for _, test := range tests {
logID, err := FromPubKeyB64(test.b64)
if gotErr := err != nil; gotErr != test.wantErr {
t.Errorf("%s: got? %t want? %t (err=%v)", test.desc, gotErr, test.wantErr, err)
continue
}
if got := base64.StdEncoding.EncodeToString(logID.Bytes()); got != test.want {
t.Errorf("%s: got? %s want? %s", test.desc, got, test.want)
}
}
}
|