[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [Git][tpo/applications/tor-browser][tor-browser-156.0a1-17.0-2] fixup! TB 44806: Implement the tor integration in Rust.



Title: GitLab

Pier Angelo Vendrame pushed to branch tor-browser-156.0a1-17.0-2 at The Tor Project / Applications / Tor Browser

Commits:

  • 2da1bad7
    by Elena at 2026-11-17T10:56:27+01:00
    fixup! TB 44806: Implement the tor integration in Rust.
    
    TB 44930: Implement the commands on the Rust control port
    
    Implementing async notification parsing.
    

3 changed files:

Changes:

  • toolkit/components/tor-integration/tor_provider/src/ctor/controller/controller.rs
    ... ... @@ -4,12 +4,13 @@
    4 4
     // copied, modified, or distributed except according to those terms.
    
    5 5
     
    
    6 6
     use bytes::Bytes;
    
    7
    -use std::net::IpAddr;
    
    7
    +use std::{net::IpAddr, rc::Rc};
    
    8 8
     use tinystr::TinyAsciiStr;
    
    9 9
     
    
    10 10
     use super::{
    
    11 11
         commands::{self, Command},
    
    12 12
         error::ControllerError,
    
    13
    +    notification::{AsyncNotification, NotificationListener},
    
    13 14
         types::*,
    
    14 15
     };
    
    15 16
     use crate::ctor::{
    
    ... ... @@ -72,6 +73,21 @@ impl<CP: ControlPortInterface> TorController<CP> {
    72 73
             );
    
    73 74
         }
    
    74 75
     
    
    76
    +    pub fn set_notification_listener(&self, listener: Option<Rc<dyn NotificationListener>>) {
    
    77
    +        self.0.set_async_handler(listener.as_ref().map(|l| {
    
    78
    +            let l = l.clone();
    
    79
    +            Box::new(move |notification| {
    
    80
    +                if let Some(notification) = AsyncNotification::parse(notification) {
    
    81
    +                    notification.dispatch(l.as_ref());
    
    82
    +                }
    
    83
    +            }) as Box<dyn Fn(Reply)>
    
    84
    +        }));
    
    85
    +    }
    
    86
    +
    
    87
    +    pub fn set_close_handler(&self, cb: Box<dyn FnOnce()>) {
    
    88
    +        self.0.set_close_handler(cb);
    
    89
    +    }
    
    90
    +
    
    75 91
         // Setup
    
    76 92
     
    
    77 93
         /// Authenticate to the tor daemon.
    
    ... ... @@ -216,9 +232,4 @@ impl<CP: ControlPortInterface> TorController<CP> {
    216 232
         pub fn set_async_handler(&self, cb: Option<Box<dyn Fn(Reply)>>) {
    
    217 233
             self.0.set_async_handler(cb);
    
    218 234
         }
    
    219
    -
    
    220
    -    #[inline]
    
    221
    -    pub fn set_close_handler(&self, cb: Box<dyn FnOnce()>) {
    
    222
    -        self.0.set_close_handler(cb);
    
    223
    -    }
    
    224 235
     }

  • toolkit/components/tor-integration/tor_provider/src/ctor/controller/mod.rs
    ... ... @@ -6,9 +6,11 @@
    6 6
     mod commands;
    
    7 7
     mod controller;
    
    8 8
     mod error;
    
    9
    +mod notification;
    
    9 10
     mod parsers;
    
    10 11
     mod types;
    
    11 12
     
    
    12 13
     pub use controller::*;
    
    13 14
     pub use error::*;
    
    15
    +pub use notification::NotificationListener;
    
    14 16
     pub use types::*;

  • toolkit/components/tor-integration/tor_provider/src/ctor/controller/notification.rs
    1
    +// Licensed under the Apache License, Version 2.0,
    
    2
    +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license
    
    3
    +// <http://opensource.org/licenses/MIT>, at your option. This file may not be
    
    4
    +// copied, modified, or distributed except according to those terms.
    
    5
    +
    
    6
    +use bytes::{Bytes, BytesMut};
    
    7
    +
    
    8
    +use super::{
    
    9
    +    parsers::{parse_bootstrap_status, parse_circuit_built, parse_stream_sentconnect},
    
    10
    +    types::{BootstrapStatus, Circuit, CircuitID, Stream},
    
    11
    +};
    
    12
    +use crate::ctor::reply_parser::{DetailReplyLine, Reply};
    
    13
    +
    
    14
    +pub trait NotificationListener {
    
    15
    +    fn log_message(&self, severity: String, message: Bytes);
    
    16
    +    fn bootstrap_status(&self, status: BootstrapStatus);
    
    17
    +    fn circuit_built(&self, circuit: Circuit);
    
    18
    +    fn circuit_closed(&self, id: CircuitID);
    
    19
    +    fn stream_sent_connect(&self, stream: Stream);
    
    20
    +}
    
    21
    +
    
    22
    +#[derive(Debug, Clone, PartialEq, Eq)]
    
    23
    +pub enum AsyncNotification {
    
    24
    +    LogMessage { severity: String, message: Bytes },
    
    25
    +    BootstrapStatus(BootstrapStatus),
    
    26
    +    CircuitBuilt(Circuit),
    
    27
    +    CircuitClosed(CircuitID),
    
    28
    +    StreamSentConnect(Stream),
    
    29
    +}
    
    30
    +
    
    31
    +impl AsyncNotification {
    
    32
    +    /// Try to parse a reply as an async notification.
    
    33
    +    /// Async notifications do not have an error handler, so any parsing error
    
    34
    +    /// will cause the reply to be just dropped silently.
    
    35
    +    pub fn parse(reply: Reply) -> Option<Self> {
    
    36
    +        if !reply.is_async() {
    
    37
    +            return None;
    
    38
    +        }
    
    39
    +        let first_line = match reply.details().first() {
    
    40
    +            Some(DetailReplyLine::MidReplyLine { line, .. }) => line,
    
    41
    +            Some(DetailReplyLine::DataReplyLine { lines, .. }) => {
    
    42
    +                if lines.is_empty() {
    
    43
    +                    debug_assert!(false, "DataReplyLine must always have at least one line.");
    
    44
    +                    return None;
    
    45
    +                }
    
    46
    +                &lines[0]
    
    47
    +            }
    
    48
    +            None => &reply.end_line().line,
    
    49
    +        };
    
    50
    +        let (keyword, data) = get_type(first_line)?;
    
    51
    +        match keyword.as_str() {
    
    52
    +            "STATUS_CLIENT" => Self::parse_bootstrap_status(data),
    
    53
    +            "CIRC" => Self::parse_circuit_event(data),
    
    54
    +            "STREAM" => Self::parse_stream_event(data),
    
    55
    +            "DEBUG" | "INFO" | "NOTICE" | "WARN" | "ERR" => Self::parse_log_message(keyword, reply),
    
    56
    +            _ => {
    
    57
    +                let mut buf = Vec::new();
    
    58
    +                let _ = reply.write_to(&mut buf);
    
    59
    +                log::debug!("Unknown notification: {}", String::from_utf8_lossy(&buf));
    
    60
    +                None
    
    61
    +            }
    
    62
    +        }
    
    63
    +    }
    
    64
    +
    
    65
    +    /// Try to parse a `STATUS_CLIENT ... BOOTSTRAP` event.
    
    66
    +    /// We will silently drop any other kind of STATUS_CLIENT event.
    
    67
    +    fn parse_bootstrap_status(data: &[u8]) -> Option<Self> {
    
    68
    +        parse_bootstrap_status(data)
    
    69
    +            .map(|status| AsyncNotification::BootstrapStatus(status))
    
    70
    +            .ok()
    
    71
    +    }
    
    72
    +
    
    73
    +    /// Try to parse CIRC BUILT/CLOSED events.
    
    74
    +    fn parse_circuit_event(data: &[u8]) -> Option<Self> {
    
    75
    +        let (status, id) = circuit_stream_status(data)?;
    
    76
    +        match status.as_str() {
    
    77
    +            "BUILT" => parse_circuit_built(data)
    
    78
    +                .ok()
    
    79
    +                .and_then(|c| c)
    
    80
    +                .map(AsyncNotification::CircuitBuilt),
    
    81
    +            "CLOSED" => Some(AsyncNotification::CircuitClosed(id)),
    
    82
    +            _ => None,
    
    83
    +        }
    
    84
    +    }
    
    85
    +
    
    86
    +    /// Try to parse a STREAM SENTCONNECT event.
    
    87
    +    fn parse_stream_event(data: &[u8]) -> Option<Self> {
    
    88
    +        let (status, _) = circuit_stream_status(data)?;
    
    89
    +        match status.as_str() {
    
    90
    +            "SENTCONNECT" => parse_stream_sentconnect(data)
    
    91
    +                .ok()
    
    92
    +                .and_then(|stream| stream.map(|s| AsyncNotification::StreamSentConnect(s))),
    
    93
    +            _ => None,
    
    94
    +        }
    
    95
    +    }
    
    96
    +
    
    97
    +    /// Try to parse a log message.
    
    98
    +    fn parse_log_message(severity: String, reply: Reply) -> Option<Self> {
    
    99
    +        match reply.details().first() {
    
    100
    +            Some(DetailReplyLine::DataReplyLine { lines, .. }) => {
    
    101
    +                if lines.len() == 1 {
    
    102
    +                    None
    
    103
    +                } else {
    
    104
    +                    // No need to join the last line, as it is supposed to be a
    
    105
    +                    // `650 OK`.
    
    106
    +                    Some(AsyncNotification::LogMessage {
    
    107
    +                        severity,
    
    108
    +                        message: join_lines(&lines[1..]),
    
    109
    +                    })
    
    110
    +                }
    
    111
    +            }
    
    112
    +            None if !reply.has_details() => {
    
    113
    +                if reply.end_line().line.len() <= (severity.len() + 1) {
    
    114
    +                    None
    
    115
    +                } else {
    
    116
    +                    let message = reply.end_line().line.slice(severity.len() + 1..);
    
    117
    +                    Some(AsyncNotification::LogMessage { severity, message })
    
    118
    +                }
    
    119
    +            }
    
    120
    +            _ => {
    
    121
    +                // The logs use data lines, as per the specs.
    
    122
    +                None
    
    123
    +            }
    
    124
    +        }
    
    125
    +    }
    
    126
    +
    
    127
    +    /// Consume self by sending it to a NotificationListener.
    
    128
    +    pub fn dispatch(self, listener: &dyn NotificationListener) {
    
    129
    +        match self {
    
    130
    +            AsyncNotification::LogMessage { severity, message } => {
    
    131
    +                listener.log_message(severity, message)
    
    132
    +            }
    
    133
    +            AsyncNotification::BootstrapStatus(status) => listener.bootstrap_status(status),
    
    134
    +            AsyncNotification::CircuitBuilt(circuit) => listener.circuit_built(circuit),
    
    135
    +            AsyncNotification::CircuitClosed(id) => listener.circuit_closed(id),
    
    136
    +            AsyncNotification::StreamSentConnect(stream) => listener.stream_sent_connect(stream),
    
    137
    +        }
    
    138
    +    }
    
    139
    +}
    
    140
    +
    
    141
    +/// Split a reply line into keyword and data.
    
    142
    +fn get_type<'a>(data: &'a [u8]) -> Option<(String, &'a [u8])> {
    
    143
    +    if data.is_empty() {
    
    144
    +        return None;
    
    145
    +    }
    
    146
    +    let mut parts = data.splitn(2, |b| *b == b' ');
    
    147
    +    let keyword = make_keyword(parts.next())?;
    
    148
    +    let data = parts.next().unwrap_or(&[]);
    
    149
    +    Some((keyword, data))
    
    150
    +}
    
    151
    +
    
    152
    +/// Validate a CIRC/STREAM event.
    
    153
    +/// We handle them together as they have the same structure:
    
    154
    +/// `<id> <event-type> ...` (with compatible formats).
    
    155
    +///
    
    156
    +/// Notice that we have both a CircuitID and a StreamID type, but for stream we
    
    157
    +/// drop the ID, so as a matter of fact we only use the CircuitID variant.
    
    158
    +/// Therefore, we thought it was not worth to make this a generic function.
    
    159
    +fn circuit_stream_status(data: &[u8]) -> Option<(String, CircuitID)> {
    
    160
    +    let mut parts = data.split(|b| *b == b' ');
    
    161
    +    let id = parts.next().and_then(|t| CircuitID::new(t));
    
    162
    +    make_keyword(parts.next()).zip(id)
    
    163
    +}
    
    164
    +
    
    165
    +/// Make a keyword (uppercase string).
    
    166
    +/// We use only ASCII keywords for now, but this function accepts anything as
    
    167
    +/// long as it is valid UTF-8 (including ASCII).
    
    168
    +///
    
    169
    +/// Notice that we need to return an owned string as we are doing the case
    
    170
    +/// conversion.
    
    171
    +fn make_keyword(keyword: Option<&[u8]>) -> Option<String> {
    
    172
    +    // Our keywords are ASCII, so ignore this error silently, we are not
    
    173
    +    // interested in those anyway...
    
    174
    +    keyword.and_then(|kw| String::from_utf8(kw.to_ascii_uppercase()).ok())
    
    175
    +}
    
    176
    +
    
    177
    +/// Join log message lines.
    
    178
    +fn join_lines(lines: &[Bytes]) -> Bytes {
    
    179
    +    let mut result = BytesMut::new();
    
    180
    +    for (i, line) in lines.iter().enumerate() {
    
    181
    +        result.extend_from_slice(line);
    
    182
    +        if i < lines.len() - 1 {
    
    183
    +            result.extend_from_slice(b"\r\n");
    
    184
    +        }
    
    185
    +    }
    
    186
    +    result.freeze()
    
    187
    +}
    
    188
    +
    
    189
    +#[cfg(test)]
    
    190
    +mod tests {
    
    191
    +    use super::*;
    
    192
    +    use crate::ctor::reply_parser::make_reply;
    
    193
    +
    
    194
    +    #[test]
    
    195
    +    fn test_make_keyword() {
    
    196
    +        assert_eq!(make_keyword(Some(&b"TEST"[..])).unwrap(), "TEST");
    
    197
    +        assert_eq!(make_keyword(Some(&b"test"[..])).unwrap(), "TEST");
    
    198
    +        assert_eq!(make_keyword(Some(&b"TesT"[..])).unwrap(), "TEST");
    
    199
    +        assert_eq!(make_keyword(Some(&b"Te_sT"[..])).unwrap(), "TE_ST");
    
    200
    +
    
    201
    +        assert!(make_keyword(None).is_none());
    
    202
    +
    
    203
    +        let invalid_utf8 = [0xFF, 0xFE];
    
    204
    +        assert!(make_keyword(Some(&invalid_utf8)).is_none());
    
    205
    +    }
    
    206
    +
    
    207
    +    #[test]
    
    208
    +    fn test_get_type() {
    
    209
    +        {
    
    210
    +            let (keyword, data) = get_type(b"STATUS_CLIENT BOOTSTRAP PROGRESS=50").unwrap();
    
    211
    +            assert_eq!(keyword, "STATUS_CLIENT");
    
    212
    +            assert_eq!(data, b"BOOTSTRAP PROGRESS=50");
    
    213
    +        }
    
    214
    +        {
    
    215
    +            let (keyword, data) = get_type(b"CIRC").unwrap();
    
    216
    +            assert_eq!(keyword, "CIRC");
    
    217
    +            assert_eq!(data, &b""[..]);
    
    218
    +        }
    
    219
    +        {
    
    220
    +            let (keyword, _) = get_type(b"debug message").unwrap();
    
    221
    +            assert_eq!(keyword, "DEBUG");
    
    222
    +        }
    
    223
    +        {
    
    224
    +            let (keyword, _) = get_type(b"StAtUs_ClIeNt data").unwrap();
    
    225
    +            assert_eq!(keyword, "STATUS_CLIENT");
    
    226
    +        }
    
    227
    +        {
    
    228
    +            let (keyword, data) = get_type(b"CIRC   id BUILT").unwrap();
    
    229
    +            assert_eq!(keyword, "CIRC");
    
    230
    +            assert_eq!(data, b"  id BUILT"); // Splits on first space only
    
    231
    +        }
    
    232
    +        assert!(get_type(b"").is_none());
    
    233
    +    }
    
    234
    +
    
    235
    +    #[test]
    
    236
    +    fn test_circuit_stream_status() {
    
    237
    +        {
    
    238
    +            let (status, id) = circuit_stream_status(b"123 BUILT").unwrap();
    
    239
    +            assert_eq!(status, "BUILT");
    
    240
    +            assert_eq!(id, CircuitID::new(b"123").unwrap());
    
    241
    +        }
    
    242
    +        {
    
    243
    +            let (status, id) = circuit_stream_status(b"456 CLOSED").unwrap();
    
    244
    +            assert_eq!(status, "CLOSED");
    
    245
    +            assert_eq!(id, CircuitID::new(b"456").unwrap());
    
    246
    +        }
    
    247
    +        {
    
    248
    +            let (status, _) = circuit_stream_status(b"1 built").unwrap();
    
    249
    +            assert_eq!(status, "BUILT");
    
    250
    +        }
    
    251
    +        {
    
    252
    +            let (status, _) = circuit_stream_status(b"1 bUIlt").unwrap();
    
    253
    +            assert_eq!(status, "BUILT");
    
    254
    +        }
    
    255
    +
    
    256
    +        // Missing ID
    
    257
    +        assert!(circuit_stream_status(b"BUILT").is_none());
    
    258
    +        // Missing status
    
    259
    +        assert!(circuit_stream_status(b"123").is_none());
    
    260
    +        // Invalid ID
    
    261
    +        assert!(circuit_stream_status(b"ab+c BUILT").is_none());
    
    262
    +        // Empty
    
    263
    +        assert!(circuit_stream_status(b"").is_none());
    
    264
    +    }
    
    265
    +
    
    266
    +    #[test]
    
    267
    +    fn test_join_lines() {
    
    268
    +        {
    
    269
    +            let lines = vec![Bytes::from_static(b"single line")];
    
    270
    +            let result = join_lines(&lines);
    
    271
    +            assert_eq!(result, "single line");
    
    272
    +        }
    
    273
    +        {
    
    274
    +            let lines = vec![
    
    275
    +                Bytes::from_static(b"aaaaaaaaaaa 1234"),
    
    276
    +                Bytes::from_static(b"b b"),
    
    277
    +                Bytes::from_static(b"cccc cc"),
    
    278
    +            ];
    
    279
    +            let result = join_lines(&lines);
    
    280
    +            assert_eq!(result, "aaaaaaaaaaa 1234\r\nb b\r\ncccc cc");
    
    281
    +        }
    
    282
    +        {
    
    283
    +            let lines: Vec<Bytes> = vec![];
    
    284
    +            let result = join_lines(&lines);
    
    285
    +            assert_eq!(result, "");
    
    286
    +        }
    
    287
    +        {
    
    288
    +            let lines = vec![
    
    289
    +                Bytes::from_static(b"second line is"),
    
    290
    +                Bytes::from_static(b""),
    
    291
    +                Bytes::from_static(b"empty"),
    
    292
    +            ];
    
    293
    +            let result = join_lines(&lines);
    
    294
    +            assert_eq!(result, "second line is\r\n\r\nempty");
    
    295
    +        }
    
    296
    +    }
    
    297
    +
    
    298
    +    #[test]
    
    299
    +    fn debug_log() {
    
    300
    +        {
    
    301
    +            let reply = make_reply(b"650 DEBUG Some debug message\r\n");
    
    302
    +            let notification = AsyncNotification::parse(reply).unwrap();
    
    303
    +            match notification {
    
    304
    +                AsyncNotification::LogMessage { severity, message } => {
    
    305
    +                    assert_eq!(severity, "DEBUG");
    
    306
    +                    assert_eq!(message, "Some debug message");
    
    307
    +                }
    
    308
    +                _ => unreachable!("Expected LogMessage"),
    
    309
    +            }
    
    310
    +        }
    
    311
    +        {
    
    312
    +            let reply = make_reply(b"650+DEBUG\r\nSome debug message\r\n.\r\n650 OK\r\n");
    
    313
    +            let notification = AsyncNotification::parse(reply).unwrap();
    
    314
    +            match notification {
    
    315
    +                AsyncNotification::LogMessage { severity, message } => {
    
    316
    +                    assert_eq!(severity, "DEBUG");
    
    317
    +                    assert_eq!(message, "Some debug message");
    
    318
    +                }
    
    319
    +                _ => unreachable!("Expected LogMessage"),
    
    320
    +            }
    
    321
    +        }
    
    322
    +        {
    
    323
    +            let reply = make_reply(b"650-DEBUG Some debug message\r\n650 OK\r\n");
    
    324
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    325
    +        }
    
    326
    +    }
    
    327
    +
    
    328
    +    #[test]
    
    329
    +    fn info_log() {
    
    330
    +        let reply = make_reply(b"650 INFO Connection established\r\n");
    
    331
    +        let notification = AsyncNotification::parse(reply).unwrap();
    
    332
    +        match notification {
    
    333
    +            AsyncNotification::LogMessage { severity, message } => {
    
    334
    +                assert_eq!(severity, "INFO");
    
    335
    +                assert_eq!(message, "Connection established");
    
    336
    +            }
    
    337
    +            _ => unreachable!("Expected LogMessage"),
    
    338
    +        }
    
    339
    +    }
    
    340
    +
    
    341
    +    #[test]
    
    342
    +    fn warn_log() {
    
    343
    +        let reply = make_reply(b"650 WARN Something suspicious happened\r\n");
    
    344
    +        let notification = AsyncNotification::parse(reply).unwrap();
    
    345
    +        match notification {
    
    346
    +            AsyncNotification::LogMessage { severity, message } => {
    
    347
    +                assert_eq!(severity, "WARN");
    
    348
    +                assert_eq!(message, "Something suspicious happened");
    
    349
    +            }
    
    350
    +            _ => unreachable!("Expected LogMessage"),
    
    351
    +        }
    
    352
    +    }
    
    353
    +
    
    354
    +    #[test]
    
    355
    +    fn err_log() {
    
    356
    +        let reply = make_reply(b"650 ERR Critical error occurred\r\n");
    
    357
    +        let notification = AsyncNotification::parse(reply).unwrap();
    
    358
    +        match notification {
    
    359
    +            AsyncNotification::LogMessage { severity, message } => {
    
    360
    +                assert_eq!(severity, "ERR");
    
    361
    +                assert_eq!(message, "Critical error occurred")
    
    362
    +            }
    
    363
    +            _ => unreachable!("Expected LogMessage"),
    
    364
    +        }
    
    365
    +    }
    
    366
    +
    
    367
    +    #[test]
    
    368
    +    fn notice_log() {
    
    369
    +        let reply = make_reply(b"650 NOTICE Important notice\r\n");
    
    370
    +        let notification = AsyncNotification::parse(reply).unwrap();
    
    371
    +        match notification {
    
    372
    +            AsyncNotification::LogMessage { severity, message } => {
    
    373
    +                assert_eq!(severity, "NOTICE");
    
    374
    +                assert_eq!(message, "Important notice");
    
    375
    +            }
    
    376
    +            _ => unreachable!("Expected LogMessage"),
    
    377
    +        }
    
    378
    +    }
    
    379
    +
    
    380
    +    #[test]
    
    381
    +    fn empty_log() {
    
    382
    +        assert!(AsyncNotification::parse(make_reply(b"650 NOTICE\r\n")).is_none());
    
    383
    +        assert!(AsyncNotification::parse(make_reply(b"650 NOTICE \r\n")).is_none());
    
    384
    +        // At the moment, we do not trim spaces.
    
    385
    +        assert!(AsyncNotification::parse(make_reply(b"650 NOTICE    \r\n")).is_some());
    
    386
    +        assert!(AsyncNotification::parse(make_reply(b"650+NOTICE\r\n.\r\n650 OK\r\n")).is_none());
    
    387
    +        assert_eq!(
    
    388
    +            AsyncNotification::parse(make_reply(b"650+NOTICE\r\n\r\n.\r\n650 OK\r\n")).unwrap(),
    
    389
    +            AsyncNotification::LogMessage {
    
    390
    +                severity: String::from("NOTICE"),
    
    391
    +                message: Bytes::new(),
    
    392
    +            }
    
    393
    +        );
    
    394
    +    }
    
    395
    +
    
    396
    +    #[test]
    
    397
    +    fn bootstrap_status() {
    
    398
    +        let reply = make_reply(b"650 STATUS_CLIENT NOTICE BOOTSTRAP PROGRESS=10 TAG=conn_done SUMMARY=\"Connected to a relay\"\r\n");
    
    399
    +        let notification = AsyncNotification::parse(reply).unwrap();
    
    400
    +        match notification {
    
    401
    +            AsyncNotification::BootstrapStatus(_) => {
    
    402
    +                // Actual parsing delegated to parse_bootstrap_status
    
    403
    +            }
    
    404
    +            _ => unreachable!("Expected BootstrapStatus"),
    
    405
    +        }
    
    406
    +    }
    
    407
    +
    
    408
    +    #[test]
    
    409
    +    fn circuit_built() {
    
    410
    +        let reply = make_reply(b"650 CIRC 42 BUILT $1111111111111111111111111111111111111111\r\n");
    
    411
    +        let notification = AsyncNotification::parse(reply).unwrap();
    
    412
    +        match notification {
    
    413
    +            AsyncNotification::CircuitBuilt(_) => {}
    
    414
    +            _ => unreachable!("Expected CircuitBuilt"),
    
    415
    +        }
    
    416
    +    }
    
    417
    +
    
    418
    +    #[test]
    
    419
    +    fn circuit_closed() {
    
    420
    +        {
    
    421
    +            let reply = make_reply(b"650 CIRC 42 CLOSED\r\n");
    
    422
    +            let notification = AsyncNotification::parse(reply).unwrap();
    
    423
    +            match notification {
    
    424
    +                AsyncNotification::CircuitClosed(id) => {
    
    425
    +                    assert_eq!(id, CircuitID::new(b"42").unwrap());
    
    426
    +                }
    
    427
    +                _ => unreachable!("Expected CircuitClosed"),
    
    428
    +            }
    
    429
    +        }
    
    430
    +        {
    
    431
    +            let reply = make_reply(b"650-CIRC 42 CLOSED\r\n650 Extended data\r\n");
    
    432
    +            let notification = AsyncNotification::parse(reply).unwrap();
    
    433
    +            match notification {
    
    434
    +                AsyncNotification::CircuitClosed(id) => {
    
    435
    +                    assert_eq!(id, CircuitID::new(b"42").unwrap());
    
    436
    +                }
    
    437
    +                _ => unreachable!("Expected CircuitClosed"),
    
    438
    +            }
    
    439
    +        }
    
    440
    +    }
    
    441
    +
    
    442
    +    #[test]
    
    443
    +    fn stream_sentconnect() {
    
    444
    +        let reply = make_reply(b"650 STREAM 1 SENTCONNECT 1 1.2.3.4:443\r\n");
    
    445
    +        let notification = AsyncNotification::parse(reply).unwrap();
    
    446
    +        match notification {
    
    447
    +            AsyncNotification::StreamSentConnect(_) => {}
    
    448
    +            _ => unreachable!("Expected StreamSentConnect"),
    
    449
    +        }
    
    450
    +    }
    
    451
    +
    
    452
    +    #[test]
    
    453
    +    fn parse_errors() {
    
    454
    +        {
    
    455
    +            let reply = make_reply(b"650 UNKNOWN_EVENT data\r\n");
    
    456
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    457
    +        }
    
    458
    +        {
    
    459
    +            // Empty keyword
    
    460
    +            let reply = make_reply(b"650 \r\n");
    
    461
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    462
    +        }
    
    463
    +        {
    
    464
    +            let reply = make_reply(b"250 Not an async reply\r\n");
    
    465
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    466
    +        }
    
    467
    +        // Unkown statuses
    
    468
    +        {
    
    469
    +            let reply = make_reply(b"650 CIRC 42 EXTENDED\r\n");
    
    470
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    471
    +        }
    
    472
    +        {
    
    473
    +            let reply = make_reply(b"650 STREAM 1 ATTACHED\r\n");
    
    474
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    475
    +        }
    
    476
    +        // Invalid IDs
    
    477
    +        {
    
    478
    +            let reply = make_reply(b"650 CIRC a_b_c BUILT\r\n");
    
    479
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    480
    +        }
    
    481
    +        {
    
    482
    +            let reply = make_reply(b"650 STREAM 123 SENTCONNECT a_b_c 1.2.3.4:443\r\n");
    
    483
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    484
    +        }
    
    485
    +        {
    
    486
    +            let reply = make_reply(b"650 STREAM 12_3 SENTCONNECT abc 1.2.3.4:443\r\n");
    
    487
    +            assert!(AsyncNotification::parse(reply).is_none());
    
    488
    +        }
    
    489
    +    }
    
    490
    +}

  • _______________________________________________
    tor-commits mailing list -- tor-commits@xxxxxxxxxxxxxxxxxxxx
    To unsubscribe send an email to tor-commits-leave@xxxxxxxxxxxxxxxxxxxx