aria2-protocol 0.3.10

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
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
//! Tests for AnnounceEvent, AnnounceTier, and AnnounceList.

use super::*;

// ------------------------------------------------------------------
// AnnounceEvent Tests
// ------------------------------------------------------------------

#[test]
fn test_announce_event_transitions() {
    // Started -> Downloading
    assert_eq!(
        AnnounceEvent::Started.next_event(),
        AnnounceEvent::Downloading
    );
    // StartedAfterCompletion -> Seeding
    assert_eq!(
        AnnounceEvent::StartedAfterCompletion.next_event(),
        AnnounceEvent::Seeding
    );
    // Stopped -> Halted
    assert_eq!(AnnounceEvent::Stopped.next_event(), AnnounceEvent::Halted);
    // Completed -> Seeding
    assert_eq!(
        AnnounceEvent::Completed.next_event(),
        AnnounceEvent::Seeding
    );
    // Stable states: Downloading, Seeding, Halted remain unchanged
    assert_eq!(
        AnnounceEvent::Downloading.next_event(),
        AnnounceEvent::Downloading
    );
    assert_eq!(AnnounceEvent::Seeding.next_event(), AnnounceEvent::Seeding);
    assert_eq!(AnnounceEvent::Halted.next_event(), AnnounceEvent::Halted);
}

#[test]
fn test_announce_event_next_if_after_started() {
    // Stopped -> Halted
    assert_eq!(
        AnnounceEvent::Stopped.next_event_if_after_started(),
        AnnounceEvent::Halted
    );
    // Completed -> Seeding
    assert_eq!(
        AnnounceEvent::Completed.next_event_if_after_started(),
        AnnounceEvent::Seeding
    );
    // Others remain unchanged
    assert_eq!(
        AnnounceEvent::Started.next_event_if_after_started(),
        AnnounceEvent::Started
    );
    assert_eq!(
        AnnounceEvent::StartedAfterCompletion.next_event_if_after_started(),
        AnnounceEvent::StartedAfterCompletion
    );
    assert_eq!(
        AnnounceEvent::Downloading.next_event_if_after_started(),
        AnnounceEvent::Downloading
    );
    assert_eq!(
        AnnounceEvent::Seeding.next_event_if_after_started(),
        AnnounceEvent::Seeding
    );
}

#[test]
fn test_announce_tier_accepts_events() {
    // Stopped event accepted by: Downloading, Stopped, Completed, Seeding
    assert!(AnnounceEvent::Downloading.accepts_stopped_event());
    assert!(AnnounceEvent::Stopped.accepts_stopped_event());
    assert!(AnnounceEvent::Completed.accepts_stopped_event());
    assert!(AnnounceEvent::Seeding.accepts_stopped_event());
    assert!(!AnnounceEvent::Started.accepts_stopped_event());
    assert!(!AnnounceEvent::StartedAfterCompletion.accepts_stopped_event());
    assert!(!AnnounceEvent::Halted.accepts_stopped_event());

    // Completed event accepted by: Downloading, Completed
    assert!(AnnounceEvent::Downloading.accepts_completed_event());
    assert!(AnnounceEvent::Completed.accepts_completed_event());
    assert!(!AnnounceEvent::Started.accepts_completed_event());
    assert!(!AnnounceEvent::StartedAfterCompletion.accepts_completed_event());
    assert!(!AnnounceEvent::Stopped.accepts_completed_event());
    assert!(!AnnounceEvent::Seeding.accepts_completed_event());
    assert!(!AnnounceEvent::Halted.accepts_completed_event());
}

#[test]
fn test_announce_event_string() {
    assert_eq!(AnnounceEvent::Started.as_event_string(), "started");
    assert_eq!(
        AnnounceEvent::StartedAfterCompletion.as_event_string(),
        "started"
    );
    assert_eq!(AnnounceEvent::Stopped.as_event_string(), "stopped");
    assert_eq!(AnnounceEvent::Completed.as_event_string(), "completed");
    assert_eq!(AnnounceEvent::Downloading.as_event_string(), "");
    assert_eq!(AnnounceEvent::Seeding.as_event_string(), "");
    assert_eq!(AnnounceEvent::Halted.as_event_string(), "");
}

#[test]
fn test_announce_events_convert_to_udp_tracker_events() {
    use crate::bittorrent::tracker::udp_tracker_protocol::UdpEvent;

    for (event, expected) in [
        (AnnounceEvent::Started, UdpEvent::Started),
        (AnnounceEvent::StartedAfterCompletion, UdpEvent::Started),
        (AnnounceEvent::Downloading, UdpEvent::None),
        (AnnounceEvent::Stopped, UdpEvent::Stopped),
        (AnnounceEvent::Completed, UdpEvent::Completed),
        (AnnounceEvent::Seeding, UdpEvent::None),
        (AnnounceEvent::Halted, UdpEvent::None),
    ] {
        assert_eq!(UdpEvent::from(event), expected);
    }
}

// ------------------------------------------------------------------
// AnnounceTier Tests
// ------------------------------------------------------------------

#[test]
fn test_announce_tier_next_event() {
    let mut tier = AnnounceTier::from_urls(vec!["http://tracker.test/announce".to_string()]);
    assert_eq!(tier.event, AnnounceEvent::Started);

    tier.next_event();
    assert_eq!(tier.event, AnnounceEvent::Downloading);

    // Downloading is stable
    tier.next_event();
    assert_eq!(tier.event, AnnounceEvent::Downloading);
}

#[test]
fn test_announce_tier_next_event_if_after_started() {
    let mut tier = AnnounceTier::from_urls(vec!["http://tracker.test/announce".to_string()]);
    tier.event = AnnounceEvent::Stopped;
    tier.next_event_if_after_started();
    assert_eq!(tier.event, AnnounceEvent::Halted);

    tier.event = AnnounceEvent::Completed;
    tier.next_event_if_after_started();
    assert_eq!(tier.event, AnnounceEvent::Seeding);

    // Started should NOT transition via nextEventIfAfterStarted
    tier.event = AnnounceEvent::Started;
    tier.next_event_if_after_started();
    assert_eq!(tier.event, AnnounceEvent::Started);
}

// ------------------------------------------------------------------
// AnnounceList Tests
// ------------------------------------------------------------------

#[test]
fn test_announce_list_creation() {
    // Test from announce string
    let list = AnnounceList::new(&[], &Some("http://tracker1.com/announce".to_string()));
    assert_eq!(list.tier_count(), 1);
    assert_eq!(list.get_announce(), Some("http://tracker1.com/announce"));

    // Test from multi-tier list
    let multi_tier = vec![
        vec![
            "http://tier1-1.com/announce".to_string(),
            "http://tier1-2.com/announce".to_string(),
        ],
        vec!["http://tier2-1.com/announce".to_string()],
    ];
    let list2 = AnnounceList::new(&multi_tier, &None);
    assert_eq!(list2.tier_count(), 2);
    assert_eq!(list2.get_announce(), Some("http://tier1-1.com/announce"));

    // Test empty case
    let list3 = AnnounceList::new(&[], &None);
    assert_eq!(list3.tier_count(), 0);
    assert!(list3.get_announce().is_none());
}

#[test]
fn test_announce_list_success_resets_to_first_tier() {
    let multi_tier = vec![
        vec!["http://t1.com/announce".to_string()],
        vec!["http://t2.com/announce".to_string()],
    ];
    let mut list = AnnounceList::new(&multi_tier, &None);

    // Initially at tier 0
    assert_eq!(list.get_announce(), Some("http://t1.com/announce"));

    // Advance to tier 1 via failure
    list.announce_failure();

    // Now at tier 1
    assert_eq!(list.get_announce(), Some("http://t2.com/announce"));

    // Success resets to first tier and advances event
    list.announce_success();
    // C++ behavior: announceSuccess on current tier (tier 1):
    // 1. Calls nextEvent on tier 1 (Started -> Downloading)
    // 2. Removes current URL and pushes to front of tier 1
    // 3. Resets currentTier to begin (tier 0)
    // So we should be back at tier 0, tracker 0 = t1
    assert_eq!(list.get_announce(), Some("http://t1.com/announce"));
}

#[test]
fn test_announce_list_failure_advances_tracker() {
    let urls = vec![
        "http://t1.com/announce".to_string(),
        "http://t2.com/announce".to_string(),
    ];
    let mut list = AnnounceList::new(&[urls], &None);
    // Initially at tracker 0
    assert_eq!(list.get_announce(), Some("http://t1.com/announce"));

    // Failure advances to next tracker in same tier
    list.announce_failure();
    assert_eq!(list.get_announce(), Some("http://t2.com/announce"));
}

#[test]
fn test_announce_list_failure_advances_tier_on_last_url() {
    let multi_tier = vec![
        vec!["http://t1.com/announce".to_string()],
        vec!["http://t2.com/announce".to_string()],
    ];
    let mut list = AnnounceList::new(&multi_tier, &None);

    // Tier 0 has only 1 URL, so failure should advance to tier 1
    list.announce_failure();
    assert_eq!(list.get_announce(), Some("http://t2.com/announce"));
}

#[test]
fn test_announce_list_all_tiers_failed() {
    let multi_tier = vec![
        vec!["http://t1.com/announce".to_string()],
        vec!["http://t2.com/announce".to_string()],
    ];
    let mut list = AnnounceList::new(&multi_tier, &None);

    assert!(!list.all_tiers_failed());

    // Fail tier 0
    list.announce_failure();
    assert!(!list.all_tiers_failed());

    // Fail tier 1
    list.announce_failure();
    assert!(list.all_tiers_failed());
    assert!(list.get_announce().is_none());
}

#[test]
fn test_announce_list_event_management() {
    let mut list = AnnounceList::new(&[vec!["http://t.com/announce".to_string()]], &None);

    // Initial event is Started
    assert_eq!(list.get_event(), AnnounceEvent::Started);
    assert_eq!(list.get_event_string(), "started");

    // Set event to Completed
    list.set_event(AnnounceEvent::Completed);
    assert_eq!(list.get_event(), AnnounceEvent::Completed);
    assert_eq!(list.get_event_string(), "completed");

    // After success, event advances: Completed -> Seeding
    list.announce_success();
    // Success resets to first tier; event on that tier is now Downloading
    // (since the first tier's event was Started, and nextEvent makes it Downloading)
    // Wait - we set it to Completed on the first tier, then announceSuccess
    // calls nextEvent on that tier: Completed -> Seeding
    // Then resets to first tier
    assert_eq!(list.get_event(), AnnounceEvent::Seeding);
}

#[test]
fn test_announce_list_stopped_allowed_tiers() {
    let mut list = AnnounceList::new(
        &[
            vec!["http://t1.com/announce".to_string()],
            vec!["http://t2.com/announce".to_string()],
        ],
        &None,
    );

    // Both tiers start with Started event - does NOT accept stopped
    assert_eq!(list.count_stopped_allowed_tier(), 0);

    // Advance tier 0 to Downloading
    list.announce_success(); // tier 0: Started -> Downloading, reset to tier 0
    assert_eq!(list.get_event(), AnnounceEvent::Downloading);
    assert_eq!(list.count_stopped_allowed_tier(), 1);

    // Advance tier 1 too - need to fail through to it
    list.announce_failure(); // move to tier 1
    list.announce_success(); // tier 1: Started -> Downloading
    // Now both tiers should be Downloading
    assert_eq!(list.count_stopped_allowed_tier(), 2);
}

#[test]
fn test_announce_list_completed_allowed_tiers() {
    let list = AnnounceList::new(
        &[
            vec!["http://t1.com/announce".to_string()],
            vec!["http://t2.com/announce".to_string()],
        ],
        &None,
    );

    // Both tiers start with Started - does NOT accept completed
    assert_eq!(list.count_completed_allowed_tier(), 0);
}

#[test]
fn test_announce_list_move_to_stopped_allowed_tier() {
    let mut list = AnnounceList::new(
        &[
            vec!["http://t1.com/announce".to_string()],
            vec!["http://t2.com/announce".to_string()],
        ],
        &None,
    );

    // Set tier 1 to Downloading (accepts stopped)
    list.tiers[1].event = AnnounceEvent::Downloading;

    // Current tier is 0 (Started, doesn't accept stopped)
    assert!(!list.current_tier_accepts_stopped_event());

    // Move to stopped-allowed tier
    list.move_to_stopped_allowed_tier();
    assert!(list.current_tier_accepts_stopped_event());
    assert_eq!(list.get_announce(), Some("http://t2.com/announce"));
}

#[test]
fn test_announce_list_reset_tier() {
    let multi_tier = vec![
        vec!["http://t1.com/announce".to_string()],
        vec!["http://t2.com/announce".to_string()],
    ];
    let mut list = AnnounceList::new(&multi_tier, &None);

    // Advance through failures
    list.announce_failure();
    assert_eq!(list.get_announce(), Some("http://t2.com/announce"));

    // Reset should go back to beginning
    list.reset_tier();
    assert_eq!(list.get_announce(), Some("http://t1.com/announce"));
}

#[test]
fn test_announce_list_shuffle() {
    let urls: Vec<String> = (0..20)
        .map(|i| format!("http://tracker{}.com/announce", i))
        .collect();
    let mut list = AnnounceList::new(std::slice::from_ref(&urls), &None);

    let _original_first = list.get_announce().unwrap().to_string();
    list.shuffle();

    // After shuffle, the list should still contain all URLs
    // (it's very unlikely shuffle produces the same order for 20 items)
    assert_eq!(list.tier_count(), 1);
    let tier_urls: Vec<&str> = list.tiers[0].urls.iter().map(|s| s.as_str()).collect();
    assert_eq!(tier_urls.len(), 20);
}

#[test]
fn test_announce_list_appends_new_trackers_without_duplicates() {
    let mut list = AnnounceList::new(
        &[
            vec!["http://torrent.test/announce".to_string()],
            vec!["udp://torrent.test:6969/announce".to_string()],
        ],
        &None,
    );

    let added = list.append_tiers(&[
        vec![
            "http://torrent.test/announce".to_string(),
            "http://public.test/announce".to_string(),
        ],
        vec!["http://public.test/announce".to_string()],
    ]);

    assert_eq!(added, vec!["http://public.test/announce"]);
    assert_eq!(list.tier_count(), 3);
    assert_eq!(
        list.get_tracker_url(0, 0).unwrap(),
        "http://torrent.test/announce"
    );
    assert_eq!(
        list.get_tracker_url(2, 0).unwrap(),
        "http://public.test/announce"
    );
}

#[test]
fn test_announce_list_appends_after_all_tiers_failed() {
    let mut list = AnnounceList::new(&[vec!["http://torrent.test/announce".to_string()]], &None);
    list.announce_failure();
    assert!(list.all_tiers_failed());

    let added = list.append_tiers(&[vec!["http://public.test/announce".to_string()]]);

    assert_eq!(added, vec!["http://public.test/announce"]);
    assert_eq!(list.get_announce(), Some("http://public.test/announce"));
    assert!(!list.all_tiers_failed());
}