1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package core
16
17 import (
18 "encoding/pem"
19 "strings"
20 "testing"
21
22 "github.com/google/certificate-transparency-go/trillian/ctfe/testonly"
23 "github.com/google/certificate-transparency-go/trillian/migrillian/configpb"
24 "github.com/google/trillian/crypto/keyspb"
25 )
26
27 const ctURI = "https://ct.googleapis.com/testtube"
28
29 func TestLoadConfigFromFileValid(t *testing.T) {
30 for _, tc := range []struct {
31 desc string
32 filename string
33 wantItems int
34 }{
35 {desc: "text proto", filename: "../testdata/config.textproto", wantItems: 2},
36 {desc: "binary proto", filename: "../testdata/config.pb", wantItems: 1},
37 } {
38 t.Run(tc.desc, func(t *testing.T) {
39 cfg, err := LoadConfigFromFile(tc.filename)
40 if err != nil {
41 t.Fatalf("LoadConfigFromFile(): %v", err)
42 }
43 if cfg == nil {
44 t.Fatal("Config is nil")
45 }
46 if err := ValidateConfig(cfg); err != nil {
47 t.Fatalf("Loaded invalid config: %v", err)
48 }
49 if got, want := len(cfg.MigrationConfigs.Config), tc.wantItems; got != want {
50 t.Errorf("Wrong number of migration configs %d, want %d", got, want)
51 }
52 })
53 }
54 }
55
56 func TestLoadConfigFromFileErrors(t *testing.T) {
57 for _, tc := range []struct {
58 desc string
59 filename string
60 wantErr string
61 }{
62 {desc: "no-such-file", filename: "does-not-exist", wantErr: "no such file"},
63 {desc: "wrong-format", filename: "../testdata/not-config.textproto", wantErr: "failed to parse"},
64 } {
65 t.Run(tc.desc, func(t *testing.T) {
66 cfg, err := LoadConfigFromFile(tc.filename)
67 if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
68 t.Errorf("Expected error containing %q", tc.wantErr)
69 }
70 if cfg != nil {
71 t.Error("Expected nil config")
72 }
73 })
74 }
75 }
76
77 func TestValidateMigrationConfig(t *testing.T) {
78 block, _ := pem.Decode([]byte(testonly.CTLogPublicKeyPEM))
79 pubKey := &keyspb.PublicKey{Der: block.Bytes}
80
81 for _, tc := range []struct {
82 desc string
83 cfg *configpb.MigrationConfig
84 wantErr string
85 }{
86 {
87 desc: "missing-source-uri",
88 cfg: &configpb.MigrationConfig{},
89 wantErr: "missing CT log URI",
90 },
91 {
92 desc: "missing-pub-key",
93 cfg: &configpb.MigrationConfig{SourceUri: ctURI},
94 wantErr: "missing public key",
95 },
96 {
97 desc: "wrong-log-ID",
98 cfg: &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey},
99 wantErr: "log ID must be positive",
100 },
101 {
102 desc: "wrong-batch-size",
103 cfg: &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey,
104 LogId: 10},
105 wantErr: "batch size must be positive",
106 },
107 {
108 desc: "unknown-identity-function",
109 cfg: &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey,
110 LogId: 10, BatchSize: 100},
111 wantErr: "unknown identity function",
112 },
113 {
114 desc: "ok",
115 cfg: &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey,
116 LogId: 10, BatchSize: 100,
117 IdentityFunction: configpb.IdentityFunction_SHA256_CERT_DATA},
118 },
119 } {
120 t.Run(tc.desc, func(t *testing.T) {
121 err := ValidateMigrationConfig(tc.cfg)
122 if len(tc.wantErr) == 0 && err != nil {
123 t.Errorf("ValidateMigrationConfig()=%v, want nil", err)
124 }
125 if len(tc.wantErr) > 0 && (err == nil || !strings.Contains(err.Error(), tc.wantErr)) {
126 t.Errorf("ValidateMigrationConfig()=%v, want err containing %q", err, tc.wantErr)
127 }
128 })
129 }
130 }
131
View as plain text