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
// standard
use std::cmp::Ordering;
use std::option::Option;
use std::str::FromStr;
use std::string::ToString;

/// `LegacyTorVersion`-specific error type
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("{}", .0)]
    ParseError(String),
}

/// Type representing a legacy c-tor daemon's version number. This version conforms c-tor's [version-spec](https://spec.torproject.org/version-spec.htm).
#[derive(Clone)]
pub struct LegacyTorVersion {
    pub(crate) major: u32,
    pub(crate) minor: u32,
    pub(crate) micro: u32,
    pub(crate) patch_level: u32,
    pub(crate) status_tag: Option<String>,
}

impl LegacyTorVersion {
    fn status_tag_pattern_is_match(status_tag: &str) -> bool {
        if status_tag.is_empty() {
            return false;
        }

        for c in status_tag.chars() {
            if c.is_whitespace() {
                return false;
            }
        }
        true
    }

    /// Construct a new `LegacyTorVersion` object.
    pub fn new(
        major: u32,
        minor: u32,
        micro: u32,
        patch_level: Option<u32>,
        status_tag: Option<&str>,
    ) -> Result<LegacyTorVersion, Error> {
        let status_tag = if let Some(status_tag) = status_tag {
            if Self::status_tag_pattern_is_match(status_tag) {
                Some(status_tag.to_string())
            } else {
                return Err(Error::ParseError(
                    "tor version status tag may not be empty or contain white-space".to_string(),
                ));
            }
        } else {
            None
        };

        Ok(LegacyTorVersion {
            major,
            minor,
            micro,
            patch_level: patch_level.unwrap_or(0u32),
            status_tag,
        })
    }
}

impl FromStr for LegacyTorVersion {
    type Err = Error;

    fn from_str(s: &str) -> Result<LegacyTorVersion, Self::Err> {
        // MAJOR.MINOR.MICRO[.PATCHLEVEL][-STATUS_TAG][ (EXTRA_INFO)]*
        let mut tokens = s.split(' ');
        let (major, minor, micro, patch_level, status_tag) =
            if let Some(version_status_tag) = tokens.next() {
                let mut tokens = version_status_tag.split('-');
                let (major, minor, micro, patch_level) = if let Some(version) = tokens.next() {
                    let mut tokens = version.split('.');
                    let major: u32 = if let Some(major) = tokens.next() {
                        match major.parse() {
                            Ok(major) => major,
                            Err(_) => {
                                return Err(Error::ParseError(format!(
                                    "failed to parse '{}' as MAJOR portion of tor version",
                                    major
                                )))
                            }
                        }
                    } else {
                        return Err(Error::ParseError(
                            "failed to find MAJOR portion of tor version".to_string(),
                        ));
                    };
                    let minor: u32 = if let Some(minor) = tokens.next() {
                        match minor.parse() {
                            Ok(minor) => minor,
                            Err(_) => {
                                return Err(Error::ParseError(format!(
                                    "failed to parse '{}' as MINOR portion of tor version",
                                    minor
                                )))
                            }
                        }
                    } else {
                        return Err(Error::ParseError(
                            "failed to find MINOR portion of tor version".to_string(),
                        ));
                    };
                    let micro: u32 = if let Some(micro) = tokens.next() {
                        match micro.parse() {
                            Ok(micro) => micro,
                            Err(_) => {
                                return Err(Error::ParseError(format!(
                                    "failed to parse '{}' as MICRO portion of tor version",
                                    micro
                                )))
                            }
                        }
                    } else {
                        return Err(Error::ParseError(
                            "failed to find MICRO portion of tor version".to_string(),
                        ));
                    };
                    let patch_level: u32 = if let Some(patch_level) = tokens.next() {
                        match patch_level.parse() {
                            Ok(patch_level) => patch_level,
                            Err(_) => {
                                return Err(Error::ParseError(format!(
                                    "failed to parse '{}' as PATCHLEVEL portion of tor version",
                                    patch_level
                                )))
                            }
                        }
                    } else {
                        0u32
                    };
                    (major, minor, micro, patch_level)
                } else {
                    // if there were '-' the previous next() would have returned the enire string
                    unreachable!();
                };
                let status_tag = tokens.next().map(|status_tag| status_tag.to_string());

                (major, minor, micro, patch_level, status_tag)
            } else {
                // if there were no ' ' character the previou snext() would have returned the enire string
                unreachable!();
            };
        for extra_info in tokens {
            if !extra_info.starts_with('(') || !extra_info.ends_with(')') {
                return Err(Error::ParseError(format!(
                    "failed to parse '{}' as [ (EXTRA_INFO)]",
                    extra_info
                )));
            }
        }
        LegacyTorVersion::new(
            major,
            minor,
            micro,
            Some(patch_level),
            status_tag.as_deref(),
        )
    }
}

impl ToString for LegacyTorVersion {
    fn to_string(&self) -> String {
        match &self.status_tag {
            Some(status_tag) => format!(
                "{}.{}.{}.{}-{}",
                self.major, self.minor, self.micro, self.patch_level, status_tag
            ),
            None => format!(
                "{}.{}.{}.{}",
                self.major, self.minor, self.micro, self.patch_level
            ),
        }
    }
}

impl PartialEq for LegacyTorVersion {
    fn eq(&self, other: &Self) -> bool {
        self.major == other.major
            && self.minor == other.minor
            && self.micro == other.micro
            && self.patch_level == other.patch_level
            && self.status_tag == other.status_tag
    }
}

impl PartialOrd for LegacyTorVersion {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if let Some(order) = self.major.partial_cmp(&other.major) {
            if order != Ordering::Equal {
                return Some(order);
            }
        }

        if let Some(order) = self.minor.partial_cmp(&other.minor) {
            if order != Ordering::Equal {
                return Some(order);
            }
        }

        if let Some(order) = self.micro.partial_cmp(&other.micro) {
            if order != Ordering::Equal {
                return Some(order);
            }
        }

        if let Some(order) = self.patch_level.partial_cmp(&other.patch_level) {
            if order != Ordering::Equal {
                return Some(order);
            }
        }

        // version-spect.txt *does* say that we should compare tags lexicgraphically
        // if all of the version numbers are the same when comparing, but we are
        // going to diverge here and say we can only compare tags for equality.
        //
        // In practice we will be comparing tor daemon tags against tagless (stable)
        // versions so this shouldn't be an issue

        if self.status_tag == other.status_tag {
            return Some(Ordering::Equal);
        }

        None
    }
}

#[test]
fn test_version() -> anyhow::Result<()> {
    assert!(LegacyTorVersion::from_str("1.2.3")? == LegacyTorVersion::new(1, 2, 3, None, None)?);
    assert!(
        LegacyTorVersion::from_str("1.2.3.4")? == LegacyTorVersion::new(1, 2, 3, Some(4), None)?
    );
    assert!(
        LegacyTorVersion::from_str("1.2.3-test")?
            == LegacyTorVersion::new(1, 2, 3, None, Some("test"))?
    );
    assert!(
        LegacyTorVersion::from_str("1.2.3.4-test")?
            == LegacyTorVersion::new(1, 2, 3, Some(4), Some("test"))?
    );
    assert!(
        LegacyTorVersion::from_str("1.2.3 (extra_info)")?
            == LegacyTorVersion::new(1, 2, 3, None, None)?
    );
    assert!(
        LegacyTorVersion::from_str("1.2.3.4 (extra_info)")?
            == LegacyTorVersion::new(1, 2, 3, Some(4), None)?
    );
    assert!(
        LegacyTorVersion::from_str("1.2.3.4-tag (extra_info)")?
            == LegacyTorVersion::new(1, 2, 3, Some(4), Some("tag"))?
    );

    assert!(
        LegacyTorVersion::from_str("1.2.3.4-tag (extra_info) (extra_info)")?
            == LegacyTorVersion::new(1, 2, 3, Some(4), Some("tag"))?
    );

    assert!(LegacyTorVersion::new(1, 2, 3, Some(4), Some("spaced tag")).is_err());
    assert!(LegacyTorVersion::new(1, 2, 3, Some(4), Some("" /* empty tag */)).is_err());
    assert!(LegacyTorVersion::from_str("").is_err());
    assert!(LegacyTorVersion::from_str("1.2").is_err());
    assert!(LegacyTorVersion::from_str("1.2-foo").is_err());
    assert!(LegacyTorVersion::from_str("1.2.3.4-foo bar").is_err());
    assert!(LegacyTorVersion::from_str("1.2.3.4-foo bar (extra_info)").is_err());
    assert!(LegacyTorVersion::from_str("1.2.3.4-foo (extra_info) badtext").is_err());
    assert!(
        LegacyTorVersion::new(0, 0, 0, Some(0), None)?
            < LegacyTorVersion::new(1, 0, 0, Some(0), None)?
    );
    assert!(
        LegacyTorVersion::new(0, 0, 0, Some(0), None)?
            < LegacyTorVersion::new(0, 1, 0, Some(0), None)?
    );
    assert!(
        LegacyTorVersion::new(0, 0, 0, Some(0), None)?
            < LegacyTorVersion::new(0, 0, 1, Some(0), None)?
    );

    // ensure status tags make comparison between equal versions (apart from
    // tags) unknowable
    let zero_version = LegacyTorVersion::new(0, 0, 0, Some(0), None)?;
    let zero_version_tag = LegacyTorVersion::new(0, 0, 0, Some(0), Some("tag"))?;

    assert!(!(zero_version < zero_version_tag));
    assert!(!(zero_version <= zero_version_tag));
    assert!(!(zero_version > zero_version_tag));
    assert!(!(zero_version >= zero_version_tag));

    Ok(())
}