File: config_test.go

package info (click to toggle)
golang-github-google-certificate-transparency 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,764 kB
  • sloc: sh: 606; makefile: 103; sql: 16
file content (130 lines) | stat: -rw-r--r-- 3,900 bytes parent folder | download | duplicates (3)
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
129
130
// Copyright 2018 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 core

import (
	"encoding/pem"
	"strings"
	"testing"

	"github.com/google/certificate-transparency-go/trillian/ctfe/testonly"
	"github.com/google/certificate-transparency-go/trillian/migrillian/configpb"
	"github.com/google/trillian/crypto/keyspb"
)

const ctURI = "https://ct.googleapis.com/testtube"

func TestLoadConfigFromFileValid(t *testing.T) {
	for _, tc := range []struct {
		desc      string
		filename  string
		wantItems int
	}{
		{desc: "text proto", filename: "../testdata/config.textproto", wantItems: 2},
		{desc: "binary proto", filename: "../testdata/config.pb", wantItems: 1},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			cfg, err := LoadConfigFromFile(tc.filename)
			if err != nil {
				t.Fatalf("LoadConfigFromFile(): %v", err)
			}
			if cfg == nil {
				t.Fatal("Config is nil")
			}
			if err := ValidateConfig(cfg); err != nil {
				t.Fatalf("Loaded invalid config: %v", err)
			}
			if got, want := len(cfg.MigrationConfigs.Config), tc.wantItems; got != want {
				t.Errorf("Wrong number of migration configs %d, want %d", got, want)
			}
		})
	}
}

func TestLoadConfigFromFileErrors(t *testing.T) {
	for _, tc := range []struct {
		desc     string
		filename string
		wantErr  string
	}{
		{desc: "no-such-file", filename: "does-not-exist", wantErr: "no such file"},
		{desc: "wrong-format", filename: "../testdata/not-config.textproto", wantErr: "failed to parse"},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			cfg, err := LoadConfigFromFile(tc.filename)
			if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
				t.Errorf("Expected error containing %q", tc.wantErr)
			}
			if cfg != nil {
				t.Error("Expected nil config")
			}
		})
	}
}

func TestValidateMigrationConfig(t *testing.T) {
	block, _ := pem.Decode([]byte(testonly.CTLogPublicKeyPEM))
	pubKey := &keyspb.PublicKey{Der: block.Bytes}

	for _, tc := range []struct {
		desc    string
		cfg     *configpb.MigrationConfig
		wantErr string
	}{
		{
			desc:    "missing-source-uri",
			cfg:     &configpb.MigrationConfig{},
			wantErr: "missing CT log URI",
		},
		{
			desc:    "missing-pub-key",
			cfg:     &configpb.MigrationConfig{SourceUri: ctURI},
			wantErr: "missing public key",
		},
		{
			desc:    "wrong-log-ID",
			cfg:     &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey},
			wantErr: "log ID must be positive",
		},
		{
			desc: "wrong-batch-size",
			cfg: &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey,
				LogId: 10},
			wantErr: "batch size must be positive",
		},
		{
			desc: "unknown-identity-function",
			cfg: &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey,
				LogId: 10, BatchSize: 100},
			wantErr: "unknown identity function",
		},
		{
			desc: "ok",
			cfg: &configpb.MigrationConfig{SourceUri: ctURI, PublicKey: pubKey,
				LogId: 10, BatchSize: 100,
				IdentityFunction: configpb.IdentityFunction_SHA256_CERT_DATA},
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			err := ValidateMigrationConfig(tc.cfg)
			if len(tc.wantErr) == 0 && err != nil {
				t.Errorf("ValidateMigrationConfig()=%v, want nil", err)
			}
			if len(tc.wantErr) > 0 && (err == nil || !strings.Contains(err.Error(), tc.wantErr)) {
				t.Errorf("ValidateMigrationConfig()=%v, want err containing %q", err, tc.wantErr)
			}
		})
	}
}