aboutsummaryrefslogtreecommitdiff
path: root/keyserver/storage/storage_test.go
blob: 9940eac60b6cc5a7f70432ed7f95aa06b0c0117b (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
package storage

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"reflect"
	"testing"

	"github.com/matrix-org/dendrite/keyserver/api"
	"github.com/matrix-org/dendrite/keyserver/types"
	"github.com/matrix-org/dendrite/setup/config"
)

var ctx = context.Background()

func MustCreateDatabase(t *testing.T) (Database, func()) {
	tmpfile, err := ioutil.TempFile("", "keyserver_storage_test")
	if err != nil {
		log.Fatal(err)
	}
	t.Logf("Database %s", tmpfile.Name())
	db, err := NewDatabase(nil, &config.DatabaseOptions{
		ConnectionString: config.DataSource(fmt.Sprintf("file://%s", tmpfile.Name())),
	})
	if err != nil {
		t.Fatalf("Failed to NewDatabase: %s", err)
	}
	return db, func() {
		os.Remove(tmpfile.Name())
	}
}

func MustNotError(t *testing.T, err error) {
	t.Helper()
	if err == nil {
		return
	}
	t.Fatalf("operation failed: %s", err)
}

func TestKeyChanges(t *testing.T) {
	db, clean := MustCreateDatabase(t)
	defer clean()
	_, err := db.StoreKeyChange(ctx, "@alice:localhost")
	MustNotError(t, err)
	deviceChangeIDB, err := db.StoreKeyChange(ctx, "@bob:localhost")
	MustNotError(t, err)
	deviceChangeIDC, err := db.StoreKeyChange(ctx, "@charlie:localhost")
	MustNotError(t, err)
	userIDs, latest, err := db.KeyChanges(ctx, deviceChangeIDB, types.OffsetNewest)
	if err != nil {
		t.Fatalf("Failed to KeyChanges: %s", err)
	}
	if latest != deviceChangeIDC {
		t.Fatalf("KeyChanges: got latest=%d want %d", latest, deviceChangeIDC)
	}
	if !reflect.DeepEqual(userIDs, []string{"@charlie:localhost"}) {
		t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
	}
}

func TestKeyChangesNoDupes(t *testing.T) {
	db, clean := MustCreateDatabase(t)
	defer clean()
	deviceChangeIDA, err := db.StoreKeyChange(ctx, "@alice:localhost")
	MustNotError(t, err)
	deviceChangeIDB, err := db.StoreKeyChange(ctx, "@alice:localhost")
	MustNotError(t, err)
	if deviceChangeIDA == deviceChangeIDB {
		t.Fatalf("Expected change ID to be different even when inserting key change for the same user, got %d for both changes", deviceChangeIDA)
	}
	deviceChangeID, err := db.StoreKeyChange(ctx, "@alice:localhost")
	MustNotError(t, err)
	userIDs, latest, err := db.KeyChanges(ctx, 0, types.OffsetNewest)
	if err != nil {
		t.Fatalf("Failed to KeyChanges: %s", err)
	}
	if latest != deviceChangeID {
		t.Fatalf("KeyChanges: got latest=%d want %d", latest, deviceChangeID)
	}
	if !reflect.DeepEqual(userIDs, []string{"@alice:localhost"}) {
		t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
	}
}

func TestKeyChangesUpperLimit(t *testing.T) {
	db, clean := MustCreateDatabase(t)
	defer clean()
	deviceChangeIDA, err := db.StoreKeyChange(ctx, "@alice:localhost")
	MustNotError(t, err)
	deviceChangeIDB, err := db.StoreKeyChange(ctx, "@bob:localhost")
	MustNotError(t, err)
	_, err = db.StoreKeyChange(ctx, "@charlie:localhost")
	MustNotError(t, err)
	userIDs, latest, err := db.KeyChanges(ctx, deviceChangeIDA, deviceChangeIDB)
	if err != nil {
		t.Fatalf("Failed to KeyChanges: %s", err)
	}
	if latest != deviceChangeIDB {
		t.Fatalf("KeyChanges: got latest=%d want %d", latest, deviceChangeIDB)
	}
	if !reflect.DeepEqual(userIDs, []string{"@bob:localhost"}) {
		t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
	}
}

// The purpose of this test is to make sure that the storage layer is generating sequential stream IDs per user,
// and that they are returned correctly when querying for device keys.
func TestDeviceKeysStreamIDGeneration(t *testing.T) {
	var err error
	db, clean := MustCreateDatabase(t)
	defer clean()
	alice := "@alice:TestDeviceKeysStreamIDGeneration"
	bob := "@bob:TestDeviceKeysStreamIDGeneration"
	msgs := []api.DeviceMessage{
		{
			Type: api.TypeDeviceKeyUpdate,
			DeviceKeys: &api.DeviceKeys{
				DeviceID: "AAA",
				UserID:   alice,
				KeyJSON:  []byte(`{"key":"v1"}`),
			},
			// StreamID: 1
		},
		{
			Type: api.TypeDeviceKeyUpdate,
			DeviceKeys: &api.DeviceKeys{
				DeviceID: "AAA",
				UserID:   bob,
				KeyJSON:  []byte(`{"key":"v1"}`),
			},
			// StreamID: 1 as this is a different user
		},
		{
			Type: api.TypeDeviceKeyUpdate,
			DeviceKeys: &api.DeviceKeys{
				DeviceID: "another_device",
				UserID:   alice,
				KeyJSON:  []byte(`{"key":"v1"}`),
			},
			// StreamID: 2 as this is a 2nd device key
		},
	}
	MustNotError(t, db.StoreLocalDeviceKeys(ctx, msgs))
	if msgs[0].StreamID != 1 {
		t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=1 but got %d", msgs[0].StreamID)
	}
	if msgs[1].StreamID != 1 {
		t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=1 (different user) but got %d", msgs[1].StreamID)
	}
	if msgs[2].StreamID != 2 {
		t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=2 (another device) but got %d", msgs[2].StreamID)
	}

	// updating a device sets the next stream ID for that user
	msgs = []api.DeviceMessage{
		{
			Type: api.TypeDeviceKeyUpdate,
			DeviceKeys: &api.DeviceKeys{
				DeviceID: "AAA",
				UserID:   alice,
				KeyJSON:  []byte(`{"key":"v2"}`),
			},
			// StreamID: 3
		},
	}
	MustNotError(t, db.StoreLocalDeviceKeys(ctx, msgs))
	if msgs[0].StreamID != 3 {
		t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=3 (new key same device) but got %d", msgs[0].StreamID)
	}

	// Querying for device keys returns the latest stream IDs
	msgs, err = db.DeviceKeysForUser(ctx, alice, []string{"AAA", "another_device"}, false)
	if err != nil {
		t.Fatalf("DeviceKeysForUser returned error: %s", err)
	}
	wantStreamIDs := map[string]int64{
		"AAA":            3,
		"another_device": 2,
	}
	if len(msgs) != len(wantStreamIDs) {
		t.Fatalf("DeviceKeysForUser: wrong number of devices, got %d want %d", len(msgs), len(wantStreamIDs))
	}
	for _, m := range msgs {
		if m.StreamID != wantStreamIDs[m.DeviceID] {
			t.Errorf("DeviceKeysForUser: wrong returned stream ID for key, got %d want %d", m.StreamID, wantStreamIDs[m.DeviceID])
		}
	}
}