| 1 |
// Copyright (c) 2020 Nikos Filippakis |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package crypto |
| 8 |
|
| 9 |
import ( |
| 10 |
"database/sql" |
| 11 |
"os" |
| 12 |
"strconv" |
| 13 |
"testing" |
| 14 |
|
| 15 |
_ "github.com/mattn/go-sqlite3" |
| 16 |
|
| 17 |
"maunium.net/go/mautrix/crypto/olm" |
| 18 |
"maunium.net/go/mautrix/id" |
| 19 |
) |
| 20 |
|
| 21 |
const olmSessID = "sJlikQQKXp7UQjmS9/lyZCNUVJ2AmKyHbufPBaC7tpk" |
| 22 |
const olmPickled = "L6cdv3JYO9OzhXbcjNSwl7ldN5bDvwmGyin+hISePETE6bO71DIlhqTC9YIhg21RDqRPH2HNl1MCyCw0hEXICWQyeJ9S7JLie" + |
| 23 |
"5PYxhqSSaTYaybvlvw34jvuSgEx0iotM6WNuWu5ocrsOo5Ye/3Nz7lBvxaw2rpS0jZnn7eV1n9GbINZk4YEVWrHOn7OxYfaGECJHDeAk/ameStiy" + |
| 24 |
"o1Gru0a/cmR0O3oKMyYnlXir0jS7oETMCsWk59GeVlz++j4aK0FK4g8/3fCMmLDXSatFjE9hoWDmeRwal58Y+XwX76Te/PiWtrFrinvCDEQJcZTa" + |
| 25 |
"qcCwp6sZrgLbmfBUBb0zJCogCmYw8m2" |
| 26 |
const groupSession = "9ZbsRqJuETbjnxPpKv29n3dubP/m5PSLbr9I9CIWS2O86F/Og1JZXhqT+4fA5tovoPfdpk5QLh7PfDyjmgOcO9sSA37maJyzCy6Ap+uBZLAXp6VLJ0mjSvxi+PAbzGKDMqpn+pa+oeEIH6SFPG/2GGDSRoXVi5fttAClCIoav5RflWiMypKqnQRfkZR2Gx8glOaBiTzAd7m0X6XGfYIPol41JUIHfBLuJBfXQ0Uu5GScV4eKUWdJP2J6zzC2Hx8cZAhiBBzAza0CbGcnUK+YJXMYaJg92HiIo++l317LlsYUJ/P+gKOLafYR9/l8bAzxH7j5s31PnRs7mD1Bl6G1LFM+dPsGXUOLx6PlvlTlYYM/opai0uKKzT0Wk6zPoq9fN/smlXEPBtKlw2fqcytL4gOF0MrBPEca" |
| 27 |
|
| 28 |
func getCryptoStores(t *testing.T) (map[string]Store, func()) { |
| 29 |
db, err := sql.Open("sqlite3", ":memory:?_busy_timeout=5000") |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("Error opening db: %v", err) |
| 32 |
} |
| 33 |
sqlStore := NewSQLCryptoStore(db, "sqlite3", "accid", id.DeviceID("dev"), []byte("test"), emptyLogger{}) |
| 34 |
if err = sqlStore.CreateTables(); err != nil { |
| 35 |
t.Fatalf("Error creating tables: %v", err) |
| 36 |
} |
| 37 |
|
| 38 |
os.Remove("gob_store_test.gob") |
| 39 |
gobStore, err := NewGobStore("gob_store_test.gob") |
| 40 |
if err != nil { |
| 41 |
t.Fatalf("Error creating Gob store: %v", err) |
| 42 |
} |
| 43 |
|
| 44 |
return map[string]Store{ |
| 45 |
"sql": sqlStore, |
| 46 |
"gob": gobStore, |
| 47 |
}, func() { |
| 48 |
os.Remove("gob_store_test.gob") |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestPutNextBatch(t *testing.T) { |
| 53 |
stores, cleanup := getCryptoStores(t) |
| 54 |
defer cleanup() |
| 55 |
store := stores["sql"].(*SQLCryptoStore) |
| 56 |
store.PutNextBatch("batch1") |
| 57 |
if batch := store.GetNextBatch(); batch != "batch1" { |
| 58 |
t.Errorf("Expected batch1, got %v", batch) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestPutAccount(t *testing.T) { |
| 63 |
stores, cleanup := getCryptoStores(t) |
| 64 |
defer cleanup() |
| 65 |
for storeName, store := range stores { |
| 66 |
t.Run(storeName, func(t *testing.T) { |
| 67 |
acc := NewOlmAccount() |
| 68 |
store.PutAccount(acc) |
| 69 |
retrieved, err := store.GetAccount() |
| 70 |
if err != nil { |
| 71 |
t.Fatalf("Error retrieving account: %v", err) |
| 72 |
} |
| 73 |
if acc.IdentityKey() != retrieved.IdentityKey() { |
| 74 |
t.Errorf("Stored identity key %v, got %v", acc.IdentityKey(), retrieved.IdentityKey()) |
| 75 |
} |
| 76 |
if acc.SigningKey() != retrieved.SigningKey() { |
| 77 |
t.Errorf("Stored signing key %v, got %v", acc.SigningKey(), retrieved.SigningKey()) |
| 78 |
} |
| 79 |
}) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestValidateMessageIndex(t *testing.T) { |
| 84 |
stores, cleanup := getCryptoStores(t) |
| 85 |
defer cleanup() |
| 86 |
for storeName, store := range stores { |
| 87 |
t.Run(storeName, func(t *testing.T) { |
| 88 |
acc := NewOlmAccount() |
| 89 |
if !store.ValidateMessageIndex(acc.IdentityKey(), "sess1", "event1", 0, 1000) { |
| 90 |
t.Error("First message not validated successfully") |
| 91 |
} |
| 92 |
if store.ValidateMessageIndex(acc.IdentityKey(), "sess1", "event1", 0, 1001) { |
| 93 |
t.Error("First message validated successfully after changing timestamp") |
| 94 |
} |
| 95 |
if store.ValidateMessageIndex(acc.IdentityKey(), "sess1", "event2", 0, 1000) { |
| 96 |
t.Error("First message validated successfully after changing event ID") |
| 97 |
} |
| 98 |
if !store.ValidateMessageIndex(acc.IdentityKey(), "sess1", "event1", 0, 1000) { |
| 99 |
t.Error("First message not validated successfully for a second time") |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestStoreOlmSession(t *testing.T) { |
| 106 |
stores, cleanup := getCryptoStores(t) |
| 107 |
defer cleanup() |
| 108 |
for storeName, store := range stores { |
| 109 |
t.Run(storeName, func(t *testing.T) { |
| 110 |
if store.HasSession(olmSessID) { |
| 111 |
t.Error("Found Olm session before inserting it") |
| 112 |
} |
| 113 |
olmInternal, err := olm.SessionFromPickled([]byte(olmPickled), []byte("test")) |
| 114 |
if err != nil { |
| 115 |
t.Fatalf("Error creating internal Olm session: %v", err) |
| 116 |
} |
| 117 |
|
| 118 |
olmSess := OlmSession{ |
| 119 |
id: olmSessID, |
| 120 |
Internal: *olmInternal, |
| 121 |
} |
| 122 |
err = store.AddSession(olmSessID, &olmSess) |
| 123 |
if err != nil { |
| 124 |
t.Errorf("Error storing Olm session: %v", err) |
| 125 |
} |
| 126 |
if !store.HasSession(olmSessID) { |
| 127 |
t.Error("Not found Olm session after inserting it") |
| 128 |
} |
| 129 |
|
| 130 |
retrieved, err := store.GetLatestSession(olmSessID) |
| 131 |
if err != nil { |
| 132 |
t.Errorf("Failed retrieving Olm session: %v", err) |
| 133 |
} |
| 134 |
|
| 135 |
if retrieved.ID() != olmSessID { |
| 136 |
t.Errorf("Expected session ID to be %v, got %v", olmSessID, retrieved.ID()) |
| 137 |
} |
| 138 |
if pickled := string(retrieved.Internal.Pickle([]byte("test"))); pickled != olmPickled { |
| 139 |
t.Error("Pickled Olm session does not match original") |
| 140 |
} |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestStoreMegolmSession(t *testing.T) { |
| 146 |
stores, cleanup := getCryptoStores(t) |
| 147 |
defer cleanup() |
| 148 |
for storeName, store := range stores { |
| 149 |
t.Run(storeName, func(t *testing.T) { |
| 150 |
acc := NewOlmAccount() |
| 151 |
|
| 152 |
internal, err := olm.InboundGroupSessionFromPickled([]byte(groupSession), []byte("test")) |
| 153 |
if err != nil { |
| 154 |
t.Fatalf("Error creating internal inbound group session: %v", err) |
| 155 |
} |
| 156 |
|
| 157 |
igs := &InboundGroupSession{ |
| 158 |
Internal: *internal, |
| 159 |
SigningKey: acc.SigningKey(), |
| 160 |
SenderKey: acc.IdentityKey(), |
| 161 |
RoomID: "room1", |
| 162 |
} |
| 163 |
|
| 164 |
err = store.PutGroupSession("room1", acc.IdentityKey(), igs.ID(), igs) |
| 165 |
if err != nil { |
| 166 |
t.Errorf("Error storing inbound group session: %v", err) |
| 167 |
} |
| 168 |
|
| 169 |
retrieved, err := store.GetGroupSession("room1", acc.IdentityKey(), igs.ID()) |
| 170 |
if err != nil { |
| 171 |
t.Errorf("Error retrieving inbound group session: %v", err) |
| 172 |
} |
| 173 |
|
| 174 |
if pickled := string(retrieved.Internal.Pickle([]byte("test"))); pickled != groupSession { |
| 175 |
t.Error("Pickled inbound group session does not match original") |
| 176 |
} |
| 177 |
}) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func TestStoreOutboundMegolmSession(t *testing.T) { |
| 182 |
stores, cleanup := getCryptoStores(t) |
| 183 |
defer cleanup() |
| 184 |
for storeName, store := range stores { |
| 185 |
t.Run(storeName, func(t *testing.T) { |
| 186 |
sess, err := store.GetOutboundGroupSession("room1") |
| 187 |
if sess != nil { |
| 188 |
t.Error("Got outbound session before inserting") |
| 189 |
} |
| 190 |
if err != nil { |
| 191 |
t.Errorf("Error retrieving outbound session: %v", err) |
| 192 |
} |
| 193 |
|
| 194 |
outbound := NewOutboundGroupSession("room1", nil) |
| 195 |
err = store.AddOutboundGroupSession(outbound) |
| 196 |
if err != nil { |
| 197 |
t.Errorf("Error inserting outbound session: %v", err) |
| 198 |
} |
| 199 |
|
| 200 |
sess, err = store.GetOutboundGroupSession("room1") |
| 201 |
if sess == nil { |
| 202 |
t.Error("Did not get outbound session after inserting") |
| 203 |
} |
| 204 |
if err != nil { |
| 205 |
t.Errorf("Error retrieving outbound session: %v", err) |
| 206 |
} |
| 207 |
|
| 208 |
err = store.RemoveOutboundGroupSession("room1") |
| 209 |
if err != nil { |
| 210 |
t.Errorf("Error deleting outbound session: %v", err) |
| 211 |
} |
| 212 |
|
| 213 |
sess, err = store.GetOutboundGroupSession("room1") |
| 214 |
if sess != nil { |
| 215 |
t.Error("Got outbound session after deleting") |
| 216 |
} |
| 217 |
if err != nil { |
| 218 |
t.Errorf("Error retrieving outbound session: %v", err) |
| 219 |
} |
| 220 |
}) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
func TestStoreDevices(t *testing.T) { |
| 225 |
stores, cleanup := getCryptoStores(t) |
| 226 |
defer cleanup() |
| 227 |
for storeName, store := range stores { |
| 228 |
t.Run(storeName, func(t *testing.T) { |
| 229 |
deviceMap := make(map[id.DeviceID]*DeviceIdentity) |
| 230 |
for i := 0; i < 17; i++ { |
| 231 |
iStr := strconv.Itoa(i) |
| 232 |
acc := NewOlmAccount() |
| 233 |
deviceMap[id.DeviceID("dev"+iStr)] = &DeviceIdentity{ |
| 234 |
UserID: "user1", |
| 235 |
DeviceID: id.DeviceID("dev" + iStr), |
| 236 |
IdentityKey: acc.IdentityKey(), |
| 237 |
SigningKey: acc.SigningKey(), |
| 238 |
} |
| 239 |
} |
| 240 |
err := store.PutDevices("user1", deviceMap) |
| 241 |
if err != nil { |
| 242 |
t.Errorf("Error string devices: %v", err) |
| 243 |
} |
| 244 |
devs, err := store.GetDevices("user1") |
| 245 |
if err != nil { |
| 246 |
t.Errorf("Error getting devices: %v", err) |
| 247 |
} |
| 248 |
if len(devs) != 17 { |
| 249 |
t.Errorf("Stored 17 devices, got back %v", len(devs)) |
| 250 |
} |
| 251 |
if devs["dev0"].IdentityKey != deviceMap["dev0"].IdentityKey { |
| 252 |
t.Errorf("First device identity key does not match") |
| 253 |
} |
| 254 |
if devs["dev16"].IdentityKey != deviceMap["dev16"].IdentityKey { |
| 255 |
t.Errorf("Last device identity key does not match") |
| 256 |
} |
| 257 |
|
| 258 |
filtered := store.FilterTrackedUsers([]id.UserID{"user0", "user1", "user2"}) |
| 259 |
if len(filtered) != 1 || filtered[0] != "user1" { |
| 260 |
t.Errorf("Expected to get 'user1' from filter, got %v", filtered) |
| 261 |
} |
| 262 |
}) |
| 263 |
} |
| 264 |
} |