fx-torrent 0.10.3

FX Torrent is a feature rich Bittorrent protocol implementation written in rust supporting Linux, MacOS and Windows.
Documentation
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
use crate::{MagnetError, MagnetResult};
use itertools::Itertools;
use log::{trace, warn};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use url::Url;

/// The scheme identifier of a magnet
const MAGNET_SCHEME: &str = "magnet";

/// Represents a Magnet link.
#[derive(Debug, Clone, PartialEq)]
pub struct Magnet {
    pub exact_topics: Vec<String>,
    pub display_name: Option<String>,
    pub exact_length: Option<u64>,
    pub tracker_addresses: Vec<String>,
    pub web_seeds: Vec<String>,
    pub acceptable_source: Vec<String>,
    pub exact_source: Option<String>,
    pub keyword_topic: Option<String>,
    pub manifest_topic: Option<String>,
    pub select_only: Option<String>,
    pub peer: Option<String>,
}

impl Magnet {
    /// Get a builder instance for a magnet.
    pub fn builder() -> MagnetBuilder {
        MagnetBuilder::builder()
    }

    /// Gets the 'xt' (exact topic) value from the magnet link.
    pub fn xt(&self) -> Vec<&str> {
        self.exact_topics.iter().map(|e| e.as_str()).collect()
    }

    /// Gets the 'dn' (display name) value from the magnet link, if present.
    pub fn dn(&self) -> Option<&str> {
        self.display_name.as_ref().map(|e| e.as_str())
    }

    /// Gets the 'xl' (exact length) value from the magnet link, if present.
    pub fn xl(&self) -> Option<u64> {
        self.exact_length.clone()
    }

    /// Gets the 'tr' (address tracker) values from the magnet link.
    pub fn tr(&self) -> &[String] {
        self.tracker_addresses.as_slice()
    }

    /// Gets the 'tr' tracker values from the magnet link.
    pub fn trackers(&self) -> &[String] {
        self.tr()
    }

    /// Gets the 'ws' (web seed) values from the magnet link.
    pub fn ws(&self) -> &[String] {
        self.web_seeds.as_slice()
    }

    /// Gets the 'as' (acceptable source) values from the magnet link.
    pub fn as_(&self) -> &[String] {
        self.acceptable_source.as_slice()
    }

    /// Gets the 'xs' (exact source) value from the magnet link, if present.
    pub fn xs(&self) -> Option<&str> {
        self.exact_source.as_ref().map(|e| e.as_str())
    }

    /// Gets the 'kt' (keyword topic) value from the magnet link, if present.
    pub fn kt(&self) -> Option<&str> {
        self.keyword_topic.as_ref().map(|e| e.as_str())
    }

    /// Gets the 'mt' (manifest topic) value from the magnet link, if present.
    pub fn mt(&self) -> Option<&str> {
        self.manifest_topic.as_ref().map(|e| e.as_str())
    }

    /// Gets the 'so' (select only) value from the magnet link, if present.
    pub fn so(&self) -> Option<&str> {
        self.select_only.as_ref().map(|e| e.as_str())
    }

    /// Retrieve the select only indexes from the magnet link, if present.
    pub fn select_only(&self) -> MagnetResult<Option<Vec<u32>>> {
        if let Some(so) = self.so() {
            let mut indexes = Vec::new();
            let sections = so.split(",");

            for section in sections {
                if let Some((start, end)) = section.split_once("-") {
                    let start = start
                        .parse::<u32>()
                        .map_err(|e| MagnetError::InvalidValue(e.to_string()))?;
                    let end = end
                        .parse::<u32>()
                        .map_err(|e| MagnetError::InvalidValue(e.to_string()))?;
                    for i in start..=end {
                        indexes.push(i);
                    }
                } else {
                    indexes.push(
                        section
                            .parse::<u32>()
                            .map_err(|e| MagnetError::InvalidValue(e.to_string()))?,
                    );
                }
            }

            return Ok(Some(indexes));
        }

        Ok(None)
    }

    /// Gets the 'x.pe' (peer) value from the magnet link, if present.
    pub fn x_pe(&self) -> Option<&str> {
        self.peer.as_ref().map(|e| e.as_str())
    }

    /// Get the magnet as an url.
    pub fn as_url(&self) -> Url {
        let topics = self
            .exact_topics
            .iter()
            .map(|topic| format!("xt={}", topic))
            .join("&");
        let mut url = Url::from_str(format!("{}:?{}", MAGNET_SCHEME, topics).as_str())
            .expect("expected the magnet scheme to be valid");
        let mut params = url.query_pairs_mut();

        if let Some(dn) = self.display_name.as_ref() {
            params.append_pair("dn", dn.as_str());
        }
        if let Some(xl) = self.exact_length.as_ref() {
            params.append_pair("xl", xl.to_string().as_str());
        }
        for tr in self.tracker_addresses.iter() {
            params.append_pair("tr", tr.as_str());
        }
        for ws in self.web_seeds.iter() {
            params.append_pair("ws", ws.as_str());
        }
        for mas in self.acceptable_source.iter() {
            params.append_pair("as", mas.as_str());
        }
        if let Some(xs) = self.exact_source.as_ref() {
            params.append_pair("xs", xs.as_str());
        }
        if let Some(kt) = self.keyword_topic.as_ref() {
            params.append_pair("kt", kt.as_str());
        }
        if let Some(mt) = self.manifest_topic.as_ref() {
            params.append_pair("mt", mt.as_str());
        }
        if let Some(so) = self.select_only.as_ref() {
            params.append_pair("so", so.as_str());
        }
        if let Some(xpe) = self.peer.as_ref() {
            params.append_pair("x.pe", xpe.as_str());
        }

        params.finish();
        drop(params);

        url
    }

    /// Verify if the given topic (xt) is valid or not.
    ///
    /// # Returns
    ///
    /// It returns an error when the topic is invalid.
    pub fn is_valid_topic(xt: &str) -> MagnetResult<()> {
        let sections = xt.split(":").collect::<Vec<&str>>();
        if sections.len() != 3 {
            return Err(MagnetError::InvalidValue(
                "xt requires format \"urn:XXX:XXX\"".to_string(),
            ));
        }
        if sections[0] != "urn" {
            return Err(MagnetError::InvalidValue(
                "xt requires the first section to be \"urn\"".to_string(),
            ));
        }
        if sections[1].len() != 4 {
            return Err(MagnetError::InvalidValue(
                "xt requires the second section to have a size of 4 chars".to_string(),
            ));
        }

        Ok(())
    }

    /// Verify if the given uri contains the magnet scheme.
    /// It returns `true` if the uri has the magnet scheme, `false` otherwise.
    pub fn has_magnet_scheme<S: AsRef<str>>(uri: S) -> bool {
        Url::parse(uri.as_ref())
            .map(|e| e.scheme() == MAGNET_SCHEME)
            .unwrap_or(false)
    }

    /// Check if the given uri contains an encoded `&` as `&amp`.
    fn contains_encoded_ampersand(uri: &str) -> bool {
        uri.contains("&amp;")
    }
}

impl FromStr for Magnet {
    type Err = MagnetError;

    fn from_str(uri: &str) -> MagnetResult<Self> {
        let mut uri = uri.to_string();

        // replace any encoded ampersands
        if Self::contains_encoded_ampersand(uri.as_str()) {
            uri = uri.replace("&amp;", "&");
        }

        let uri = Url::parse(&uri).map_err(|e| MagnetError::Parse(e.to_string()))?;
        let scheme = uri.scheme();

        // verify if the given scheme is supported
        if scheme != MAGNET_SCHEME {
            return Err(MagnetError::UnsupportedScheme(scheme.to_string()));
        }

        let query = uri.query_pairs();
        let mut builder = MagnetBuilder::builder();

        for (key, value) in query {
            let key = key.to_lowercase();

            match key.as_str() {
                "xt" => {
                    builder.exact_topic(value);
                }
                "dn" => {
                    builder.display_name(value);
                }
                "xl" => {
                    builder.exact_length(u64::from_str(value.as_ref()).map_err(|_| {
                        trace!("Value {} is not a valid number", value);
                        MagnetError::Parse("xl is invalid".to_string())
                    })?);
                }
                "tr" => {
                    builder.address_tracker(value);
                }
                "ws" => {
                    builder.web_seed(value);
                }
                "as" => {
                    builder.acceptable_source(value);
                }
                "xs" => {
                    builder.exact_source(value);
                }
                "kt" => {
                    builder.keyword_topic(value);
                }
                "mt" => {
                    builder.manifest_topic(value);
                }
                "so" => {
                    builder.select_only(value);
                }
                "x.pe" => {
                    builder.peer(value);
                }
                _ => warn!("Unsupported magnet parameter {}", key),
            }
        }

        builder.build()
    }
}

impl Display for Magnet {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let url = self.as_url();
        write!(f, "{}", url)
    }
}

/// A builder for constructing a `Magnet` struct.
#[derive(Debug, Clone, Default)]
pub struct MagnetBuilder {
    exact_topics: Option<Vec<String>>,
    display_name: Option<String>,
    exact_length: Option<u64>,
    address_tracker: Option<Vec<String>>,
    web_seed: Option<Vec<String>>,
    acceptable_source: Vec<String>,
    exact_source: Option<String>,
    keyword_topic: Option<String>,
    manifest_topic: Option<String>,
    select_only: Option<String>,
    peer: Option<String>,
}

impl MagnetBuilder {
    /// Creates a new `MagnetBuilder` instance.
    pub fn builder() -> Self {
        Self::default()
    }

    /// Add the given exact topic to the magnet builder.
    pub fn exact_topic<S>(&mut self, exact_topic: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.exact_topics
            .get_or_insert(Vec::new())
            .push(exact_topic.into());
        self
    }

    /// Set the exact topics for the magnet builder.
    pub fn exact_topics(&mut self, exact_topics: Vec<String>) -> &mut Self {
        self.exact_topics
            .get_or_insert(Vec::new())
            .extend(exact_topics);
        self
    }

    /// Sets the display name for the magnet link.
    pub fn display_name<S>(&mut self, display_name: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.display_name = Some(display_name.into());
        self
    }

    /// Sets the exact length for the magnet link.
    pub fn exact_length(&mut self, exact_length: u64) -> &mut Self {
        self.exact_length = Some(exact_length);
        self
    }

    /// Add a tracker address to the magnet link.
    pub fn address_tracker<S>(&mut self, address_tracker: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.address_tracker
            .get_or_insert(Vec::new())
            .push(address_tracker.into());
        self
    }

    /// Set the tracker addresses of the magnet link.
    pub fn address_trackers(&mut self, address_trackers: Vec<String>) -> &mut Self {
        self.address_tracker
            .get_or_insert(Vec::new())
            .extend(address_trackers);
        self
    }

    /// Add a web seed to the magnet link.
    pub fn web_seed<S>(&mut self, web_seed: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.web_seed
            .get_or_insert(Vec::new())
            .push(web_seed.into());
        self
    }

    /// Set the web seed addresses of the magnet link.
    pub fn web_seeds(&mut self, web_seeds: Vec<String>) -> &mut Self {
        self.web_seed.get_or_insert(Vec::new()).extend(web_seeds);
        self
    }

    /// Adds an acceptable source to the magnet link.
    pub fn acceptable_source<S>(&mut self, acceptable_source: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.acceptable_source.push(acceptable_source.into());
        self
    }

    /// Sets the exact source for the magnet link.
    pub fn exact_source<S>(&mut self, exact_source: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.exact_source = Some(exact_source.into());
        self
    }

    /// Sets the keyword topic for the magnet link.
    pub fn keyword_topic<S>(&mut self, keyword_topic: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.keyword_topic = Some(keyword_topic.into());
        self
    }

    /// Sets the manifest topic for the magnet link.
    pub fn manifest_topic<S>(&mut self, manifest_topic: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.manifest_topic = Some(manifest_topic.into());
        self
    }

    /// Sets the select only for the magnet link.
    pub fn select_only<S>(&mut self, select_only: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.select_only = Some(select_only.into());
        self
    }

    /// Sets the peer for the magnet link.
    pub fn peer<S>(&mut self, peer: S) -> &mut Self
    where
        S: Into<String>,
    {
        self.peer = Some(peer.into());
        self
    }

    /// Builds a `Magnet` instance from the builder's configuration.
    ///
    /// # Returns
    ///
    /// - `Ok(Magnet)`: A `Magnet` instance with the specified configuration.
    /// - `Err(MagnetError::InvalidUri)`: If the exact topic is not set, indicating an invalid magnet link.
    pub fn build(&mut self) -> MagnetResult<Magnet> {
        let exact_topics = self.exact_topics.take().ok_or(MagnetError::InvalidUri)?;

        // validate the topics
        for topic in exact_topics.iter() {
            Magnet::is_valid_topic(topic.as_str())?;
        }

        let tracker_addresses = self.address_tracker.take().unwrap_or(Vec::new());
        let web_seeds = self.web_seed.take().unwrap_or(Vec::new());

        Ok(Magnet {
            exact_topics,
            display_name: self.display_name.take(),
            exact_length: self.exact_length.take(),
            tracker_addresses,
            web_seeds,
            acceptable_source: std::mem::take(&mut self.acceptable_source),
            exact_source: self.exact_source.take(),
            keyword_topic: self.keyword_topic.take(),
            manifest_topic: self.manifest_topic.take(),
            select_only: self.select_only.take(),
            peer: self.peer.take(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_magnet_from_str() {
        init_logger!();
        let uri = "magnet:?xt=urn:btih:EADAF0EFEA39406914414D359E0EA16416409BD7&dn=debian-12.4.0-amd64-DVD-1.iso&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.bittor.pw%3A1337%2Fannounce&tr=udp%3A%2F%2Fpublic.popcorn-tracker.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce";
        let expected_result = create_expected_magnet_from_str();

        let result = Magnet::from_str(uri).unwrap();

        assert_eq!(expected_result, result);
    }

    #[test]
    fn test_magnet_from_str_encoded_url() {
        init_logger!();
        let uri = "magnet:?xt=urn:btih:EADAF0EFEA39406914414D359E0EA16416409BD7&amp;dn=debian-12.4.0-amd64-DVD-1.iso&amp;tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&amp;tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&amp;tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&amp;tr=udp%3A%2F%2Ftracker.bittor.pw%3A1337%2Fannounce&amp;tr=udp%3A%2F%2Fpublic.popcorn-tracker.org%3A6969%2Fannounce&amp;tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&amp;tr=udp%3A%2F%2Fexodus.desync.com%3A6969&amp;tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce";
        let expected_result = create_expected_magnet_from_str();

        let result = Magnet::from_str(uri).unwrap();

        assert_eq!(expected_result, result);
    }

    #[test]
    fn test_magnet_from_str_invalid_scheme() {
        init_logger!();
        let uri = "custom:?xt=urn:btih:EADAF0EFEA39406914414D359E0EA1641640007";

        let result = Magnet::from_str(uri);

        assert_eq!(
            Err(MagnetError::UnsupportedScheme("custom".to_string())),
            result
        );
    }

    #[test]
    fn test_magnet_xt() {
        init_logger!();
        let expected_result = vec!["urn:btih:EADAF0EFEA39406914414D359E0EA16416409BD7".to_string()];
        let uri = "magnet:?xt=urn:btih:EADAF0EFEA39406914414D359E0EA16416409BD7&dn=debian-12.4.0-amd64-DVD-1.iso&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.bittor.pw%3A1337%2Fannounce&tr=udp%3A%2F%2Fpublic.popcorn-tracker.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce";

        let magnet = Magnet::from_str(uri).unwrap();

        let result = magnet.xt();
        assert_eq!(expected_result, result, "expected the exact topic to match");
    }

    #[test]
    fn test_magnet_dn() {
        init_logger!();
        let display_name = "Example File Name";
        let magnet = Magnet {
            exact_topics: vec!["urn:btih:6b0cd35c4a6b724".to_string()],
            display_name: Some(display_name.to_string()),
            exact_length: Some(8455000),
            tracker_addresses: vec!["http://tracker.example.com:12345/announce".to_string()],
            web_seeds: vec![],
            acceptable_source: vec![],
            exact_source: None,
            keyword_topic: None,
            manifest_topic: None,
            select_only: None,
            peer: None,
        };

        let result = magnet.dn();

        assert_eq!(Some(display_name), result)
    }

    #[test]
    fn test_magnet_tr() {
        init_logger!();
        let expected_result = vec![
            "http://tracker1.example.com:12345/announce".to_string(),
            "http://tracker2.example.com:23456/announce".to_string(),
        ];
        let magnet = Magnet {
            exact_topics: vec!["urn:btih:6b0cd35c4a6b724".to_string()],
            display_name: None,
            exact_length: Some(8455000),
            tracker_addresses: expected_result.clone(),
            web_seeds: vec![],
            acceptable_source: vec![],
            exact_source: None,
            keyword_topic: None,
            manifest_topic: None,
            select_only: None,
            peer: None,
        };

        let result = magnet.tr();

        assert_eq!(expected_result.as_slice(), result);
        assert_eq!(expected_result.as_slice(), magnet.trackers());
    }

    #[test]
    fn test_magnet_select_only() {
        let expected_result: Vec<u32> = vec![0, 2, 4, 6, 7, 8];
        let magnet = Magnet {
            exact_topics: vec!["urn:btih:6b0cd35c4a6b724".to_string()],
            display_name: None,
            exact_length: None,
            tracker_addresses: vec![],
            web_seeds: vec![],
            acceptable_source: vec![],
            exact_source: None,
            keyword_topic: None,
            manifest_topic: None,
            select_only: Some("0,2,4,6-8".to_string()),
            peer: None,
        };

        let result = magnet
            .select_only()
            .expect("expected the so to be valid")
            .expect("expected the so to be present");

        assert_eq!(expected_result, result)
    }

    #[test]
    fn test_magnet_as_uri() {
        init_logger!();
        let uri = "magnet:?xt=urn:btih:EADAF0EFEA39406914414D359E0EA16416409BD7&dn=debian-12.4.0-amd64-DVD-1.iso&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.bittor.pw%3A1337%2Fannounce&tr=udp%3A%2F%2Fpublic.popcorn-tracker.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce";
        let expected_result = Url::from_str(uri).unwrap();
        let magnet = Magnet::from_str(uri).unwrap();

        let result = magnet.as_url();

        assert_eq!(expected_result, result);
    }

    #[test]
    fn test_magnet_to_string() {
        init_logger!();
        let uri = "magnet:?xt=urn:btih:EADAF0EFEA39406914414D359E0EA16416409BD7&dn=debian-12.4.0-amd64-DVD-1.iso&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Ftracker.bittor.pw%3A1337%2Fannounce&tr=udp%3A%2F%2Fpublic.popcorn-tracker.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Fopen.demonii.com%3A1337%2Fannounce";
        let magnet = Magnet::from_str(uri).unwrap();

        let result = magnet.to_string();

        assert_eq!(uri, result);
    }

    fn create_expected_magnet_from_str() -> Magnet {
        Magnet {
            exact_topics: vec!["urn:btih:EADAF0EFEA39406914414D359E0EA16416409BD7".to_string()],
            display_name: Some("debian-12.4.0-amd64-DVD-1.iso".to_string()),
            exact_length: None,
            tracker_addresses: vec![
                "udp://tracker.opentrackr.org:1337".to_string(),
                "udp://open.stealth.si:80/announce".to_string(),
                "udp://tracker.torrent.eu.org:451/announce".to_string(),
                "udp://tracker.bittor.pw:1337/announce".to_string(),
                "udp://public.popcorn-tracker.org:6969/announce".to_string(),
                "udp://tracker.dler.org:6969/announce".to_string(),
                "udp://exodus.desync.com:6969".to_string(),
                "udp://open.demonii.com:1337/announce".to_string(),
            ],
            web_seeds: vec![],
            acceptable_source: vec![],
            exact_source: None,
            keyword_topic: None,
            manifest_topic: None,
            select_only: None,
            peer: None,
        }
    }
}