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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// Copyright 2023 The Matrix.org Foundation C.I.C.
//
// 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.

//! Types for the `m.room_key.withheld` events.

use std::collections::BTreeMap;

use ruma::{
    exports::ruma_macros::AsStrAsRefStr,
    serde::{AsRefStr, DebugAsRefStr, DeserializeFromCowStr, FromString, SerializeAsRefStr},
    OwnedDeviceId, OwnedRoomId,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use vodozemac::Curve25519PublicKey;

use super::{EventType, ToDeviceEvent};
use crate::types::{
    deserialize_curve_key, serialize_curve_key, EventEncryptionAlgorithm, PrivOwnedStr,
};

/// The `m.room_key_request` to-device event.
pub type RoomKeyWithheldEvent = ToDeviceEvent<RoomKeyWithheldContent>;

impl Clone for RoomKeyWithheldEvent {
    fn clone(&self) -> Self {
        Self {
            sender: self.sender.clone(),
            content: self.content.clone(),
            other: self.other.clone(),
        }
    }
}

/// The `m.room_key.withheld` event content.
///
/// This is an enum over the different room key algorithms we support.
///
/// Devices that purposely do not send megolm keys to a device may instead send
/// an m.room_key.withheld event as a to-device message to the device to
/// indicate that it should not expect to receive keys for the message.
#[derive(Clone, Debug, Deserialize)]
#[serde(try_from = "WithheldHelper")]
pub enum RoomKeyWithheldContent {
    /// The `m.megolm.v1.aes-sha2` variant of the `m.room_key.withheld` content.
    MegolmV1AesSha2(MegolmV1AesSha2WithheldContent),
    /// The `m.megolm.v2.aes-sha2` variant of the `m.room_key.withheld` content.
    #[cfg(feature = "experimental-algorithms")]
    MegolmV2AesSha2(MegolmV2AesSha2WithheldContent),
    /// An unknown and unsupported variant of the `m.room_key.withheld` content.
    Unknown(UnknownRoomKeyWithHeld),
}

macro_rules! construct_withheld_content {
    ($algorithm:ident, $code:ident, $room_id:ident, $session_id:ident, $sender_key:ident, $from_device:ident) => {
        match $code {
            WithheldCode::Blacklisted
            | WithheldCode::Unverified
            | WithheldCode::Unauthorised
            | WithheldCode::Unavailable => {
                let content = CommonWithheldCodeContent {
                    $room_id,
                    $session_id,
                    $sender_key,
                    $from_device,
                    other: Default::default(),
                };

                RoomKeyWithheldContent::$algorithm(
                    MegolmV1AesSha2WithheldContent::from_code_and_content($code, content),
                )
            }
            WithheldCode::NoOlm => {
                RoomKeyWithheldContent::$algorithm(MegolmV1AesSha2WithheldContent::NoOlm(
                    NoOlmWithheldContent { $sender_key, $from_device, other: Default::default() }
                        .into(),
                ))
            }
            _ => unreachable!("Can't create an unknown withheld code content"),
        }
    };
}

impl RoomKeyWithheldContent {
    /// Creates a withheld content from the given info
    ///
    /// # Panics
    ///
    /// The method will panic if a unsupported algorithm is given. The only
    /// supported algorithm as of now is `m.megolm.v1.aes-sha2`.
    pub fn new(
        algorithm: EventEncryptionAlgorithm,
        code: WithheldCode,
        room_id: OwnedRoomId,
        session_id: String,
        sender_key: Curve25519PublicKey,
        from_device: OwnedDeviceId,
    ) -> Self {
        let from_device = Some(from_device);

        match algorithm {
            EventEncryptionAlgorithm::MegolmV1AesSha2 => {
                construct_withheld_content!(
                    MegolmV1AesSha2,
                    code,
                    room_id,
                    session_id,
                    sender_key,
                    from_device
                )
            }
            #[cfg(feature = "experimental-algorithms")]
            EventEncryptionAlgorithm::MegolmV2AesSha2 => {
                construct_withheld_content!(
                    MegolmV2AesSha2,
                    code,
                    room_id,
                    session_id,
                    sender_key,
                    from_device
                )
            }
            _ => unreachable!("Unsupported algorithm {algorithm}"),
        }
    }

    /// Get the withheld code of this event.
    pub fn withheld_code(&self) -> WithheldCode {
        match self {
            RoomKeyWithheldContent::MegolmV1AesSha2(c) => c.withheld_code(),
            #[cfg(feature = "experimental-algorithms")]
            RoomKeyWithheldContent::MegolmV2AesSha2(c) => c.withheld_code(),
            RoomKeyWithheldContent::Unknown(c) => c.code.to_owned(),
        }
    }

    /// Get the algorithm of the room key withheld.
    pub fn algorithm(&self) -> EventEncryptionAlgorithm {
        match &self {
            RoomKeyWithheldContent::MegolmV1AesSha2(_) => EventEncryptionAlgorithm::MegolmV1AesSha2,
            #[cfg(feature = "experimental-algorithms")]
            RoomKeyWithheldContent::MegolmV2AesSha2(_) => EventEncryptionAlgorithm::MegolmV2AesSha2,
            RoomKeyWithheldContent::Unknown(c) => c.algorithm.to_owned(),
        }
    }
}

impl EventType for RoomKeyWithheldContent {
    const EVENT_TYPE: &'static str = "m.room_key.withheld";
}

/// A machine-readable code for why the megolm key was not sent.
#[derive(
    Clone,
    PartialEq,
    Eq,
    Hash,
    AsStrAsRefStr,
    AsRefStr,
    FromString,
    DebugAsRefStr,
    SerializeAsRefStr,
    DeserializeFromCowStr,
)]
#[non_exhaustive]
pub enum WithheldCode {
    /// the user/device was blacklisted.
    #[ruma_enum(rename = "m.blacklisted")]
    Blacklisted,

    /// the user/devices is unverified.
    #[ruma_enum(rename = "m.unverified")]
    Unverified,

    /// The user/device is not allowed have the key. For example, this would
    /// usually be sent in response to a key request if the user was not in
    /// the room when the message was sent.
    #[ruma_enum(rename = "m.unauthorised")]
    Unauthorised,

    /// Sent in reply to a key request if the device that the key is requested
    /// from does not have the requested key.
    #[ruma_enum(rename = "m.unavailable")]
    Unavailable,

    /// An olm session could not be established.
    /// This may happen, for example, if the sender was unable to obtain a
    /// one-time key from the recipient.
    #[ruma_enum(rename = "m.no_olm")]
    NoOlm,

    #[doc(hidden)]
    _Custom(PrivOwnedStr),
}

impl std::fmt::Display for WithheldCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        let string = match self {
            WithheldCode::Blacklisted => "The sender has blocked you.",
            WithheldCode::Unverified => "The sender has disabled encrypting to unverified devices.",
            WithheldCode::Unauthorised => "You are not authorised to read the message.",
            WithheldCode::Unavailable => "The requested key was not found.",
            WithheldCode::NoOlm => "Unable to establish a secure channel.",
            _ => self.as_str(),
        };

        f.write_str(string)
    }
}

#[derive(Debug, Deserialize, Serialize)]
struct WithheldHelper {
    pub algorithm: EventEncryptionAlgorithm,
    pub reason: Option<String>,
    pub code: WithheldCode,
    #[serde(flatten)]
    other: Value,
}

/// Content for the `m.room_key.withheld` event for the `m.megolm.v1.aes-sha2`
/// algorithm.
#[derive(Clone, Debug)]
pub enum MegolmV1AesSha2WithheldContent {
    /// The `m.blacklisted` variant of the withheld code content.
    BlackListed(Box<CommonWithheldCodeContent>),
    /// The `m.unverified` variant of the withheld code content.
    Unverified(Box<CommonWithheldCodeContent>),
    /// The `m.unauthorised` variant of the withheld code content.
    Unauthorised(Box<CommonWithheldCodeContent>),
    /// The `m.unavailable` variant of the withheld code content.
    Unavailable(Box<CommonWithheldCodeContent>),
    /// The `m.no_olm` variant of the withheld code content.
    NoOlm(Box<NoOlmWithheldContent>),
}

/// Struct for most of the withheld code content variants.
#[derive(Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct CommonWithheldCodeContent {
    /// The room where the key is used.
    pub room_id: OwnedRoomId,

    /// The ID of the session.
    pub session_id: String,

    /// The device curve25519 key of the session creator.
    #[serde(deserialize_with = "deserialize_curve_key", serialize_with = "serialize_curve_key")]
    pub sender_key: Curve25519PublicKey,

    /// The device ID of the device sending the m.room_key.withheld message
    /// MSC3735.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_device: Option<OwnedDeviceId>,

    #[serde(flatten)]
    other: BTreeMap<String, Value>,
}

impl CommonWithheldCodeContent {
    /// Create a new common withheld code content.
    pub fn new(
        room_id: OwnedRoomId,
        session_id: String,
        sender_key: Curve25519PublicKey,
        device_id: OwnedDeviceId,
    ) -> Self {
        Self {
            room_id,
            session_id,
            sender_key,
            from_device: Some(device_id),
            other: Default::default(),
        }
    }
}

impl MegolmV1AesSha2WithheldContent {
    /// Get the withheld code for this content
    pub fn withheld_code(&self) -> WithheldCode {
        match self {
            MegolmV1AesSha2WithheldContent::BlackListed(_) => WithheldCode::Blacklisted,
            MegolmV1AesSha2WithheldContent::Unverified(_) => WithheldCode::Unverified,
            MegolmV1AesSha2WithheldContent::Unauthorised(_) => WithheldCode::Unauthorised,
            MegolmV1AesSha2WithheldContent::Unavailable(_) => WithheldCode::Unavailable,
            MegolmV1AesSha2WithheldContent::NoOlm(_) => WithheldCode::NoOlm,
        }
    }

    fn from_code_and_content(code: WithheldCode, content: CommonWithheldCodeContent) -> Self {
        let content = content.into();

        match code {
            WithheldCode::Blacklisted => Self::BlackListed(content),
            WithheldCode::Unverified => Self::Unverified(content),
            WithheldCode::Unauthorised => Self::Unauthorised(content),
            WithheldCode::Unavailable => Self::Unavailable(content),
            _ => unreachable!("This constructor requires one of the common withheld codes"),
        }
    }
}

/// No olm content (no room_id nor session_id)
#[derive(Clone, Deserialize, Serialize)]
pub struct NoOlmWithheldContent {
    #[serde(deserialize_with = "deserialize_curve_key", serialize_with = "serialize_curve_key")]
    /// The device curve25519 key of the session creator.
    pub sender_key: Curve25519PublicKey,

    /// The device ID of the device sending the m.room_key.withheld message
    /// MSC3735.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from_device: Option<OwnedDeviceId>,

    #[serde(flatten)]
    other: BTreeMap<String, Value>,
}

#[cfg(not(tarpaulin_include))]
impl std::fmt::Debug for CommonWithheldCodeContent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AnyWithheldContent")
            .field("room_id", &self.room_id)
            .field("session_id", &self.session_id)
            .field("sender_key", &self.sender_key)
            .field("from_device", &self.from_device)
            .finish_non_exhaustive()
    }
}

#[cfg(not(tarpaulin_include))]
impl std::fmt::Debug for NoOlmWithheldContent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NoOlmWithheldContent")
            .field("sender_key", &self.sender_key)
            .field("from_device", &self.from_device)
            .finish_non_exhaustive()
    }
}

/// Megolm V2 variant of withheld content
pub type MegolmV2AesSha2WithheldContent = MegolmV1AesSha2WithheldContent;

/// Withheld info for unknown/unsupported encryption alhg
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UnknownRoomKeyWithHeld {
    /// The algorithm of the unknown room key.
    pub algorithm: EventEncryptionAlgorithm,
    /// The withheld code
    pub code: WithheldCode,
    /// A human-readable reason for why the key was not sent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    /// The other data of the unknown room key.
    #[serde(flatten)]
    other: BTreeMap<String, Value>,
}

impl TryFrom<WithheldHelper> for RoomKeyWithheldContent {
    type Error = serde_json::Error;

    fn try_from(value: WithheldHelper) -> Result<Self, Self::Error> {
        let unknown = |value: WithheldHelper| -> Result<RoomKeyWithheldContent, _> {
            Ok(Self::Unknown(UnknownRoomKeyWithHeld {
                algorithm: value.algorithm,
                code: value.code,
                reason: value.reason,
                other: serde_json::from_value(value.other)?,
            }))
        };

        Ok(match value.algorithm {
            EventEncryptionAlgorithm::MegolmV1AesSha2 => match value.code {
                WithheldCode::NoOlm => {
                    let content: NoOlmWithheldContent = serde_json::from_value(value.other)?;
                    Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::NoOlm(content.into()))
                }
                WithheldCode::Blacklisted
                | WithheldCode::Unverified
                | WithheldCode::Unauthorised
                | WithheldCode::Unavailable => {
                    let content: CommonWithheldCodeContent = serde_json::from_value(value.other)?;

                    Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::from_code_and_content(
                        value.code, content,
                    ))
                }
                _ => unknown(value)?,
            },
            #[cfg(feature = "experimental-algorithms")]
            EventEncryptionAlgorithm::MegolmV2AesSha2 => match value.code {
                WithheldCode::NoOlm => {
                    let content: NoOlmWithheldContent = serde_json::from_value(value.other)?;
                    Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::NoOlm(content.into()))
                }
                WithheldCode::Blacklisted
                | WithheldCode::Unverified
                | WithheldCode::Unauthorised
                | WithheldCode::Unavailable => {
                    let content: CommonWithheldCodeContent = serde_json::from_value(value.other)?;

                    Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::from_code_and_content(
                        value.code, content,
                    ))
                }
                _ => unknown(value)?,
            },
            _ => unknown(value)?,
        })
    }
}

impl Serialize for RoomKeyWithheldContent {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let algorithm = self.algorithm();

        let helper = match self {
            Self::MegolmV1AesSha2(r) => {
                let code = r.withheld_code();
                let reason = Some(code.to_string());

                match r {
                    MegolmV1AesSha2WithheldContent::BlackListed(content)
                    | MegolmV1AesSha2WithheldContent::Unverified(content)
                    | MegolmV1AesSha2WithheldContent::Unauthorised(content)
                    | MegolmV1AesSha2WithheldContent::Unavailable(content) => WithheldHelper {
                        algorithm,
                        code,
                        reason,
                        other: serde_json::to_value(content).map_err(serde::ser::Error::custom)?,
                    },
                    MegolmV1AesSha2WithheldContent::NoOlm(content) => WithheldHelper {
                        algorithm,
                        code,
                        reason,
                        other: serde_json::to_value(content).map_err(serde::ser::Error::custom)?,
                    },
                }
            }
            #[cfg(feature = "experimental-algorithms")]
            Self::MegolmV2AesSha2(r) => {
                let code = r.withheld_code();
                let reason = Some(code.to_string());

                match r {
                    MegolmV1AesSha2WithheldContent::BlackListed(content)
                    | MegolmV1AesSha2WithheldContent::Unverified(content)
                    | MegolmV1AesSha2WithheldContent::Unauthorised(content)
                    | MegolmV1AesSha2WithheldContent::Unavailable(content) => WithheldHelper {
                        algorithm,
                        code,
                        reason,
                        other: serde_json::to_value(content).map_err(serde::ser::Error::custom)?,
                    },
                    MegolmV1AesSha2WithheldContent::NoOlm(content) => WithheldHelper {
                        algorithm,
                        code,
                        reason,
                        other: serde_json::to_value(content).map_err(serde::ser::Error::custom)?,
                    },
                }
            }
            Self::Unknown(r) => WithheldHelper {
                algorithm: r.algorithm.to_owned(),
                code: r.code.to_owned(),
                reason: r.reason.to_owned(),
                other: serde_json::to_value(r.other.clone()).map_err(serde::ser::Error::custom)?,
            },
        };

        helper.serialize(serializer)
    }
}

#[cfg(test)]
pub(super) mod tests {
    use std::collections::BTreeMap;

    use assert_matches::assert_matches;
    use assert_matches2::assert_let;
    use ruma::{device_id, room_id, serde::Raw, to_device::DeviceIdOrAllDevices, user_id};
    use serde_json::{json, Value};
    use vodozemac::Curve25519PublicKey;

    use super::RoomKeyWithheldEvent;
    use crate::types::{
        events::room_key_withheld::{
            MegolmV1AesSha2WithheldContent, RoomKeyWithheldContent, WithheldCode,
        },
        EventEncryptionAlgorithm,
    };

    pub fn json(code: &WithheldCode) -> Value {
        json!({
            "sender": "@alice:example.org",
            "content": {
                "room_id": "!DwLygpkclUAfQNnfva:localhost:8481",
                "session_id": "0ZcULv8j1nqVWx6orFjD6OW9JQHydDPXfaanA+uRyfs",
                "algorithm": "m.megolm.v1.aes-sha2",
                "sender_key": "9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA",
                "code": code.to_owned(),
                "reason": code.to_string(),
                "org.matrix.msgid": "8836f2f0-635d-4f0e-9228-446c63ba3ea3"
            },
            "type": "m.room_key.withheld",
            "m.custom.top": "something custom in the top",
        })
    }

    pub fn no_olm_json() -> Value {
        json!({
            "sender": "@alice:example.org",
            "content": {
                "algorithm": "m.megolm.v1.aes-sha2",
                "sender_key": "9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA",
                "code": "m.no_olm",
                "reason": "Unable to establish a secure channel.",
                "org.matrix.msgid": "8836f2f0-635d-4f0e-9228-446c63ba3ea3"
            },
            "type": "m.room_key.withheld",
            "m.custom.top": "something custom in the top",
        })
    }

    pub fn unknown_alg_json() -> Value {
        json!({
            "sender": "@alice:example.org",
            "content": {
                "algorithm": "caesar.cipher",
                "sender_key": "9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA",
                "code": "m.brutus",
                "reason": "Tu quoque fili",
                "org.matrix.msgid": "8836f2f0-635d-4f0e-9228-446c63ba3ea3"
            },
            "type": "m.room_key.withheld",
            "m.custom.top": "something custom in the top",
        })
    }

    pub fn unknown_code_json() -> Value {
        json!({
            "sender": "@alice:example.org",
            "content": {
                "room_id": "!DwLygpkclUAfQNnfva:localhost:8481",
                "session_id": "0ZcULv8j1nqVWx6orFjD6OW9JQHydDPXfaanA+uRyfs",
                "algorithm": "m.megolm.v1.aes-sha2",
                "sender_key": "9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA",
                "code": "org.mscXXX.new_code",
                "reason": "Unable to establish a secure channel.",
                "org.matrix.msgid": "8836f2f0-635d-4f0e-9228-446c63ba3ea3"
            },
            "type": "m.room_key.withheld",
            "m.custom.top": "something custom in the top",
        })
    }

    #[test]
    fn deserialization() -> Result<(), serde_json::Error> {
        let codes = [
            WithheldCode::Unverified,
            WithheldCode::Blacklisted,
            WithheldCode::Unauthorised,
            WithheldCode::Unavailable,
        ];
        for code in codes {
            let json = json(&code);
            let event: RoomKeyWithheldEvent = serde_json::from_value(json.clone())?;

            assert_let!(RoomKeyWithheldContent::MegolmV1AesSha2(content) = &event.content);
            assert_eq!(code, content.withheld_code());

            assert_eq!(event.content.algorithm(), EventEncryptionAlgorithm::MegolmV1AesSha2);
            let serialized = serde_json::to_value(event)?;
            assert_eq!(json, serialized);
        }
        Ok(())
    }

    #[test]
    fn deserialization_no_olm() -> Result<(), serde_json::Error> {
        let json = no_olm_json();
        let event: RoomKeyWithheldEvent = serde_json::from_value(json.clone())?;
        assert_matches!(
            event.content,
            RoomKeyWithheldContent::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::NoOlm(_))
        );
        let serialized = serde_json::to_value(event)?;
        assert_eq!(json, serialized);

        Ok(())
    }

    #[test]
    fn deserialization_unknown_code() -> Result<(), serde_json::Error> {
        let json = unknown_code_json();
        let event: RoomKeyWithheldEvent = serde_json::from_value(json.clone())?;
        assert_matches!(event.content, RoomKeyWithheldContent::Unknown(_));

        assert_let!(RoomKeyWithheldContent::Unknown(content) = &event.content);
        assert_eq!(content.code.as_str(), "org.mscXXX.new_code");

        let serialized = serde_json::to_value(event)?;
        assert_eq!(json, serialized);

        Ok(())
    }

    #[test]
    fn deserialization_unknown_alg() -> Result<(), serde_json::Error> {
        let json = unknown_alg_json();
        let event: RoomKeyWithheldEvent = serde_json::from_value(json.clone())?;
        assert_matches!(event.content, RoomKeyWithheldContent::Unknown(_));

        assert_let!(RoomKeyWithheldContent::Unknown(content) = &event.content);
        assert_matches!(&content.code, WithheldCode::_Custom(_));
        let serialized = serde_json::to_value(event)?;
        assert_eq!(json, serialized);

        Ok(())
    }

    #[test]
    fn serialization_to_device() {
        let mut messages = BTreeMap::new();

        let room_id = room_id!("!DwLygpkclUAfQNnfva:localhost:8481");
        let user_id = user_id!("@alice:example.org");
        let device_id = device_id!("DEV001");
        let sender_key =
            Curve25519PublicKey::from_base64("9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA")
                .unwrap();

        let content = RoomKeyWithheldContent::new(
            EventEncryptionAlgorithm::MegolmV1AesSha2,
            WithheldCode::Unverified,
            room_id.to_owned(),
            "0ZcULv8j1nqVWx6orFjD6OW9JQHydDPXfaanA+uRyfs".to_owned(),
            sender_key,
            device_id.to_owned(),
        );
        let content: Raw<RoomKeyWithheldContent> =
            Raw::new(&content).expect("We can always serialize a withheld content info").cast();

        messages
            .entry(user_id.to_owned())
            .or_insert_with(BTreeMap::new)
            .insert(DeviceIdOrAllDevices::DeviceId(device_id.to_owned()), content);

        let serialized = serde_json::to_value(messages).unwrap();

        let expected: Value = json!({
            "@alice:example.org":{
                "DEV001":{
                    "algorithm":"m.megolm.v1.aes-sha2",
                    "code":"m.unverified",
                    "from_device":"DEV001",
                    "reason":"The sender has disabled encrypting to unverified devices.",
                    "room_id":"!DwLygpkclUAfQNnfva:localhost:8481",
                    "sender_key":"9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA",
                    "session_id":"0ZcULv8j1nqVWx6orFjD6OW9JQHydDPXfaanA+uRyfs"
                }
            }
        });
        assert_eq!(serialized, expected);
    }

    #[test]
    fn no_olm_should_not_have_room_and_session() {
        let room_id = room_id!("!DwLygpkclUAfQNnfva:localhost:8481");
        let device_id = device_id!("DEV001");
        let sender_key =
            Curve25519PublicKey::from_base64("9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA")
                .unwrap();

        let content = RoomKeyWithheldContent::new(
            EventEncryptionAlgorithm::MegolmV1AesSha2,
            WithheldCode::NoOlm,
            room_id.to_owned(),
            "0ZcULv8j1nqVWx6orFjD6OW9JQHydDPXfaanA+uRyfs".to_owned(),
            sender_key,
            device_id.to_owned(),
        );

        assert_let!(
            RoomKeyWithheldContent::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::NoOlm(
                content
            )) = content
        );
        assert_eq!(content.sender_key, sender_key);

        let content = RoomKeyWithheldContent::new(
            EventEncryptionAlgorithm::MegolmV1AesSha2,
            WithheldCode::Unverified,
            room_id.to_owned(),
            "0ZcULv8j1nqVWx6orFjD6OW9JQHydDPXfaanA+uRyfs".to_owned(),
            sender_key,
            device_id.to_owned(),
        );

        assert_let!(
            RoomKeyWithheldContent::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::Unverified(
                content
            )) = content
        );
        assert_eq!(content.session_id, "0ZcULv8j1nqVWx6orFjD6OW9JQHydDPXfaanA+uRyfs");
    }
}