aboutsummaryrefslogtreecommitdiff
path: root/setup/config/config_test.go
blob: ffbf4c3c501895258899be37a13625457bb03ad4 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2017 Vector Creations Ltd
//
// 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 config

import (
	"fmt"
	"reflect"
	"testing"

	"github.com/matrix-org/gomatrixserverlib"
	"github.com/sirupsen/logrus"
	"gopkg.in/yaml.v2"
)

func TestLoadConfigRelative(t *testing.T) {
	cfg, err := loadConfig("/my/config/dir", []byte(testConfig),
		mockReadFile{
			"/my/config/dir/matrix_key.pem": testKey,
			"/my/config/dir/tls_cert.pem":   testCert,
		}.readFile,
		false,
	)
	if err != nil {
		t.Error("failed to load config:", err)
	}

	configErrors := &ConfigErrors{}
	cfg.Verify(configErrors, false)
	if len(*configErrors) > 0 {
		for _, err := range *configErrors {
			logrus.Errorf("Configuration error: %s", err)
		}
		t.Error("configuration verification failed")
	}
}

const testConfig = `
version: 2
global:
  server_name: localhost
  private_key: matrix_key.pem
  key_id: ed25519:auto
  key_validity_period: 168h0m0s
  well_known_server_name: "localhost:443"
  well_known_client_name: "localhost:443"
  trusted_third_party_id_servers:
  - matrix.org
  - vector.im
  kafka:
    addresses:
    - localhost:2181
    topic_prefix: Dendrite
    use_naffka: true
    naffka_database:
      connection_string: file:naffka.db
      max_open_conns: 100
      max_idle_conns: 2
      conn_max_lifetime: -1
  metrics:
    enabled: false
    basic_auth:
      username: metrics
      password: metrics
  server_notices:
    local_part: "_server"
    display_name: "Server alerts"
    avatar: ""
    room_name: "Server Alerts"	
  jetstream:
    addresses: ["test"]
app_service_api:
  internal_api:
    listen: http://localhost:7777
    connect: http://localhost:7777
  database:
    connection_string: file:appservice.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
  config_files: []
client_api:
  internal_api:
    listen: http://localhost:7771
    connect: http://localhost:7771
  external_api:
    listen: http://[::]:8071
  registration_disabled: true
  registration_shared_secret: ""
  enable_registration_captcha: false
  recaptcha_public_key: ""
  recaptcha_private_key: ""
  recaptcha_bypass_secret: ""
  recaptcha_siteverify_api: ""
  turn:
    turn_user_lifetime: ""
    turn_uris: []
    turn_shared_secret: ""
    turn_username: ""
    turn_password: ""
current_state_server:
  internal_api:
    listen: http://localhost:7782
    connect: http://localhost:7782
  database:
    connection_string: file:currentstate.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
federation_api:
  internal_api:
    listen: http://localhost:7772
    connect: http://localhost:7772
  external_api:
    listen: http://[::]:8072
  database:
    connection_string: file:federationapi.db
key_server:
  internal_api:
    listen: http://localhost:7779
    connect: http://localhost:7779
  database:
    connection_string: file:keyserver.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
media_api:
  internal_api:
    listen: http://localhost:7774
    connect: http://localhost:7774
  external_api:
    listen: http://[::]:8074
  database:
    connection_string: file:mediaapi.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
  base_path: ./media_store
  max_file_size_bytes: 10485760
  dynamic_thumbnails: false
  max_thumbnail_generators: 10
  thumbnail_sizes:
  - width: 32
    height: 32
    method: crop
  - width: 96
    height: 96
    method: crop
  - width: 640
    height: 480
    method: scale
room_server:
  internal_api:
    listen: http://localhost:7770
    connect: http://localhost:7770
  database:
    connection_string: file:roomserver.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
server_key_api:
  internal_api:
    listen: http://localhost:7780
    connect: http://localhost:7780
  database:
    connection_string: file:serverkeyapi.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
  key_perspectives:
  - server_name: matrix.org
    keys:
    - key_id: ed25519:auto
      public_key: Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw
    - key_id: ed25519:a_RXGa
      public_key: l8Hft5qXKn1vfHrg3p4+W8gELQVo8N13JkluMfmn2sQ
sync_api:
  internal_api:
    listen: http://localhost:7773
    connect: http://localhost:7773
  database:
    connection_string: file:syncapi.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
user_api:
  internal_api:
    listen: http://localhost:7781
    connect: http://localhost:7781
  account_database:
    connection_string: file:userapi_accounts.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
  pusher_database:
    connection_string: file:pushserver.db
    max_open_conns: 100
    max_idle_conns: 2
    conn_max_lifetime: -1
relay_api:
  internal_api:
    listen: http://localhost:7775
    connect: http://localhost:7775
  external_api:
    listen: http://[::]:8075
  database:
    connection_string: file:relayapi.db
mscs:
  database:
    connection_string: file:mscs.db
tracing:
  enabled: false
  jaeger:
    serviceName: ""
    disabled: false
    rpc_metrics: false
    tags: []
    sampler: null
    reporter: null
    headers: null
    baggage_restrictions: null
    throttler: null
logging:
- type: file
  level: info
  params:
    path: /var/log/dendrite
`

type mockReadFile map[string]string

func (m mockReadFile) readFile(path string) ([]byte, error) {
	data, ok := m[path]
	if !ok {
		return nil, fmt.Errorf("no such file %q", path)
	}
	return []byte(data), nil
}

func TestReadKey(t *testing.T) {
	keyID, _, err := readKeyPEM("path/to/key", []byte(testKey), true)
	if err != nil {
		t.Error("failed to load private key:", err)
	}
	wantKeyID := testKeyID
	if wantKeyID != string(keyID) {
		t.Errorf("wanted key ID to be %q, got %q", wantKeyID, keyID)
	}
}

const testKeyID = "ed25519:c8NsuQ"

const testKey = `
-----BEGIN MATRIX PRIVATE KEY-----
Key-ID: ` + testKeyID + `
7KRZiZ2sTyRR8uqqUjRwczuwRXXkUMYIUHq4Mc3t4bE=
-----END MATRIX PRIVATE KEY-----
`

const testCert = `
-----BEGIN CERTIFICATE-----
MIIE0zCCArugAwIBAgIJAPype3u24LJeMA0GCSqGSIb3DQEBCwUAMAAwHhcNMTcw
NjEzMTQyODU4WhcNMTgwNjEzMTQyODU4WjAAMIICIjANBgkqhkiG9w0BAQEFAAOC
Ag8AMIICCgKCAgEA3vNSr7lCh/alxPFqairp/PYohwdsqPvOD7zf7dJCNhy0gbdC
9/APwIbPAPL9nU+o9ud1ACNCKBCQin/9LnI5vd5pa/Ne+mmRADDLB/BBBoywSJWG
NSfKJ9n3XY1bjgtqi53uUh+RDdQ7sXudDqCUxiiJZmS7oqK/mp88XXAgCbuXUY29
GmzbbDz37vntuSxDgUOnJ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A5UXoORQZfJ+b41llq1B
GcSF6iaVbAFKnsUyyr1i9uHz/6Muqflphv/SfZxGheIn5u3PnhXrzDagvItjw0NS
n0Xq64k7fc42HXJpF8CGBkSaIhtlzcruO+vqR80B9r62+D0V7VmHOnP135MT6noU
8F0JQfEtP+I8NII5jHSF/khzSgP5g80LS9tEc2ILnIHK1StkInAoRQQ+/HsQsgbz
ANAf5kxmMsM0zlN2hkxl0H6o7wKlBSw3RI3cjfilXiMWRPJrzlc4
-----END CERTIFICATE-----
`

func TestUnmarshalDataUnit(t *testing.T) {
	target := struct {
		Got DataUnit `yaml:"value"`
	}{}
	for input, expect := range map[string]DataUnit{
		"value: 0.6tb": 659706976665,
		"value: 1.2gb": 1288490188,
		"value: 256mb": 268435456,
		"value: 128kb": 131072,
		"value: 128":   128,
	} {
		if err := yaml.Unmarshal([]byte(input), &target); err != nil {
			t.Fatal(err)
		} else if target.Got != expect {
			t.Fatalf("expected value %d but got %d", expect, target.Got)
		}
	}
}

func Test_SigningIdentityFor(t *testing.T) {
	tests := []struct {
		name         string
		virtualHosts []*VirtualHost
		serverName   gomatrixserverlib.ServerName
		want         *gomatrixserverlib.SigningIdentity
		wantErr      bool
	}{
		{
			name:    "no virtual hosts defined",
			wantErr: true,
		},
		{
			name:       "no identity found",
			serverName: gomatrixserverlib.ServerName("doesnotexist"),
			wantErr:    true,
		},
		{
			name:       "found identity",
			serverName: gomatrixserverlib.ServerName("main"),
			want:       &gomatrixserverlib.SigningIdentity{ServerName: "main"},
		},
		{
			name:       "identity found on virtual hosts",
			serverName: gomatrixserverlib.ServerName("vh2"),
			virtualHosts: []*VirtualHost{
				{SigningIdentity: gomatrixserverlib.SigningIdentity{ServerName: "vh1"}},
				{SigningIdentity: gomatrixserverlib.SigningIdentity{ServerName: "vh2"}},
			},
			want: &gomatrixserverlib.SigningIdentity{ServerName: "vh2"},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			c := &Global{
				VirtualHosts: tt.virtualHosts,
				SigningIdentity: gomatrixserverlib.SigningIdentity{
					ServerName: "main",
				},
			}
			got, err := c.SigningIdentityFor(tt.serverName)
			if (err != nil) != tt.wantErr {
				t.Errorf("SigningIdentityFor() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("SigningIdentityFor() got = %v, want %v", got, tt.want)
			}
		})
	}
}