tox 0.0.5

Implementation of toxcore in pure Rust - P2P, distributed, encrypted, easy to use DHT-based network.
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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*! The implementation of onion announce
*/

use std::io::{ErrorKind, Error};
use std::net::{IpAddr, SocketAddr};
use std::time::{Duration, Instant, SystemTime};

use toxcore::binary_io::*;
use toxcore::crypto_core::*;
use toxcore::time::*;
use toxcore::onion::packet::*;
use toxcore::dht::kbucket::{Distance, Kbucket};

/// Number of secret random bytes to make onion ping id unique for each node.
pub const SECRET_BYTES_SIZE: usize = 32;

/// Maximum number of entries in onion announce list. When number of entries
/// exceeds this value farthest nodes are dropped using DHT distance function.
pub const ONION_ANNOUNCE_MAX_ENTRIES: usize = 160;

/// Number of seconds when onion ping id is valid after it was generated.
/// To be precise ping id will be valid `PING_ID_TIMEOUT` to
/// 2 * `PING_ID_TIMEOUT` seconds.
pub const PING_ID_TIMEOUT: u64 = 300;

/// Number of seconds that announce entry can be stored in onion announce list
/// without re-announcing.
pub const ONION_ANNOUNCE_TIMEOUT: u64 = 300;

/// Create onion ping id filled with zeros.
pub fn initial_ping_id() -> sha256::Digest {
    // can not fail since slice has enough length
    sha256::Digest::from_slice(&[0; sha256::DIGESTBYTES]).unwrap()
}

/** Entry that corresponds to announced onion node.

When node successfully announce itself this entry is added to announced nodes
list. It's considered expired after `ONION_ANNOUNCE_TIMEOUT` seconds.

*/
#[derive(Clone, Debug, Eq, PartialEq)]
struct OnionAnnounceEntry {
    /// Long term PublicKey of announced node
    pub pk: PublicKey,
    /// IP address of announced node
    pub ip_addr: IpAddr,
    /// Port of announced node
    pub port: u16,
    /// Onion return that should be used to send data packets to announced node
    pub onion_return: OnionReturn,
    /// PublicKey that should be used to encrypt data packets for announced node
    pub data_pk: PublicKey,
    /// Time when this entry was added to the list of announced nodes
    pub time: Instant
}

impl OnionAnnounceEntry {
    /// Create new `OnionAnnounceEntry` object using current unix time.
    pub fn new(pk: PublicKey, ip_addr: IpAddr, port: u16, onion_return: OnionReturn, data_pk: PublicKey) -> OnionAnnounceEntry {
        OnionAnnounceEntry {
            pk,
            ip_addr,
            port,
            onion_return,
            data_pk,
            time: clock_now()
        }
    }

    /** Check if this entry is timed out.

    Entry considered timed out after `ONION_ANNOUNCE_TIMEOUT` seconds since it
    was created.

    */
    pub fn is_timed_out(&self) -> bool {
        clock_elapsed(self.time) >= Duration::from_secs(ONION_ANNOUNCE_TIMEOUT)
    }
}

/// Size of serialized `OnionPingData` struct.
const ONION_PING_DATA_SIZE: usize =
    SECRET_BYTES_SIZE +
    /* time */ 8 +
    PUBLICKEYBYTES +
    /* ip_type */ 1 +
    /* ip_addr */ 8 +
    /* port */ 2;

/** Data on the basis of which onion ping id is calculated.

Format of this struct is not specified by tox protocol and can be different in
different implementations. That's possible because this struct is used for onion
ping id generation and only node that generated it can verify it.

Serialized form:

Length   | Content
-------- | ------
`32`     | Secret bytes of onion node
`8`      | Unix time in seconds divided by PING_ID_TIMEOUT
`32`     | `PublicKey` of sender
`1`      | IP type of sender
`8`      | `IpAddr` of sender
`2`      | Port of sender

*/
struct OnionPingData {
    /// Secret bytes of onion node to make onion ping id unique
    pub secret_bytes: [u8; SECRET_BYTES_SIZE],
    /// Can be any time but only current time or current time + `PING_ID_TIMEOUT`
    /// should be used.
    pub time: SystemTime,
    /// `PublicKey` of sender
    pub pk: PublicKey,
    /// `IpAddr` of sender
    pub ip_addr: IpAddr,
    /// Port of sender
    pub port: u16
}

impl ToBytes for OnionPingData {
    fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
        do_gen!(buf,
            gen_slice!(&self.secret_bytes) >>
            gen_be_u64!(unix_time(self.time) / PING_ID_TIMEOUT) >>
            gen_slice!(self.pk.as_ref()) >>
            gen_be_u8!(self.ip_addr.is_ipv4() as u8) >>
            gen_call!(|buf, ip_addr| IpAddr::to_bytes(ip_addr, buf), &self.ip_addr) >>
            gen_be_u16!(self.port)
        )
    }
}

impl OnionPingData {
    /** Calculate onion ping id using sha256 hash of stored data.

    Time is divided by `PING_ID_TIMEOUT` so this hash remains unchanged for
    `PING_ID_TIMEOUT` seconds.

    */
    pub fn ping_id(&self) -> sha256::Digest {
        let mut buf = [0; ONION_PING_DATA_SIZE];
        // can not fail since buf has enough length
        self.to_bytes((&mut buf, 0)).unwrap();
        sha256::hash(&buf)
    }
}

/** Holds list of announced onion nodes and process announce requests.
*/
#[derive(Clone, Debug)]
pub struct OnionAnnounce {
    /// Secret bytes of onion node to make onion ping id unique
    secret_bytes: [u8; SECRET_BYTES_SIZE],
    /// List of announced onion nodes
    entries: Vec<OnionAnnounceEntry>,
    /// Short term DHT `PublicKey`
    dht_pk: PublicKey
}

impl OnionAnnounce {
    /// Create new `OnionAnnounce` instance.
    pub fn new(dht_pk: PublicKey) -> OnionAnnounce {
        let mut secret_bytes = [0; SECRET_BYTES_SIZE];
        randombytes_into(&mut secret_bytes);
        OnionAnnounce {
            secret_bytes,
            entries: Vec::with_capacity(ONION_ANNOUNCE_MAX_ENTRIES),
            dht_pk
        }
    }

    /** Calculate onion ping id using sha256 hash of arguments together with
    secret bytes stored in this struct.

    Time is divided by `PING_ID_TIMEOUT` so this hash remains unchanged for
    `PING_ID_TIMEOUT` seconds.

    */
    fn ping_id(&self, time: SystemTime, pk: PublicKey, ip_addr: IpAddr, port: u16) -> sha256::Digest {
        let data = OnionPingData {
            secret_bytes: self.secret_bytes,
            time,
            pk,
            ip_addr,
            port
        };
        data.ping_id()
    }

    /// Find entry by its `PublicKey` ignoring timed out entries
    fn find_in_entries(&self, pk: PublicKey) -> Option<&OnionAnnounceEntry> {
        match self.entries.binary_search_by(|e| self.dht_pk.distance(&e.pk, &pk)) {
            //TODO: use Option::filter when it's stabilized
            Ok(idx) => if self.entries[idx].is_timed_out() { None } else { self.entries.get(idx) },
            Err(_) => None
        }
    }

    /** Try to add announce entry to onion announce list.

    Firstly we remove all timed out entries. Then if:
    - announce list already contains entry with such `PublicKey` then update
      entry and return it
    - announce list with new entry does not exceed `ONION_ANNOUNCE_MAX_ENTRIES`
      length add entry to the list and return it
    - the farthest entry from DHT `PublicKey` is farther than new entry then
      replace it with new entry

    Also we keep onion announce list sorted by distance to DHT `PublicKey` so
    we can easily find the farthest entry.

    */
    fn add_to_entries(&mut self, entry: OnionAnnounceEntry) -> Option<&OnionAnnounceEntry> {
        //TODO: remove timed out entries by timer?
        self.entries.retain(|e| !e.is_timed_out());
        match self.entries.binary_search_by(|e| self.dht_pk.distance(&e.pk, &entry.pk)) {
            Ok(idx) => {
                // node with such pk already announced - just update the entry
                self.entries[idx].clone_from(&entry);
                self.entries.get(idx)
            },
            Err(idx) => {
                if self.entries.len() < ONION_ANNOUNCE_MAX_ENTRIES {
                    // adding new entry does not exceed the limit - just add it
                    self.entries.insert(idx, entry);
                    self.entries.get(idx)
                } else if idx < ONION_ANNOUNCE_MAX_ENTRIES {
                    // the farthest entry is farther than new entry - replace it
                    self.entries.pop();
                    self.entries.insert(idx, entry);
                    self.entries.get(idx)
                } else {
                    None
                }
            }
        }
    }

    /** Handle `OnionAnnounceRequest` packet and return `OnionAnnounceResponse`.

    If `OnionAnnounceRequest` packet contains valid onion ping id it's
    considered as announce request. Otherwise it's considered as search request.
    In case of announce request we try to add new entry to announce list and if
    succeed return status `AnnounceStatus::Announced`. In case of search request
    we try to find entry by `search_pk` key from request and if succeed return
    status `AnnounceStatus::Found`. If announce or search failed we return
    status `AnnounceStatus::Failed`.

    If request is a search request and we found requested node then
    `ping_id_or_pk` field in response packet will contain `PublicKey` that
    should be used to send data packets to requested node. Otherwise it will
    contain valid onion ping id that should be used to send announce requests
    to this node.

    Also response packet will contain up to 4 closest to `search_pk` nodes from
    kbucket. They are used to search closest to long term `PublicKey` nodes to
    announce.

    */
    pub fn handle_onion_announce_request(&mut self, request: OnionAnnounceRequest, dht_sk: &SecretKey, kbucket: &Kbucket, addr: SocketAddr) -> Result<OnionAnnounceResponse, Error> {
        let shared_secret = precompute(&request.inner.pk, dht_sk);
        let payload = request.inner.get_payload(&shared_secret)?;

        let time = SystemTime::now();
        let ping_id_1 = self.ping_id(
            time,
            request.inner.pk,
            addr.ip(),
            addr.port()
        );
        let ping_id_2 = self.ping_id(
            time + Duration::from_secs(PING_ID_TIMEOUT),
            request.inner.pk,
            addr.ip(),
            addr.port()
        );

        let entry_opt = if payload.ping_id == ping_id_1 || payload.ping_id == ping_id_2 {
            let entry = OnionAnnounceEntry::new(request.inner.pk, addr.ip(), addr.port(), request.onion_return, payload.data_pk);
            self.add_to_entries(entry)
        } else {
            self.find_in_entries(payload.search_pk)
        };

        let (announce_status, ping_id_or_pk) = if let Some(entry) = entry_opt {
            if entry.pk == request.inner.pk {
                if entry.data_pk != payload.data_pk {
                    // failed to find ourselves with same long term pk but different data pk
                    // weird case, should we remove it?
                    (AnnounceStatus::Failed, ping_id_2)
                } else {
                    // successfully announced ourselves
                    (AnnounceStatus::Announced, ping_id_2)
                }
            } else {
                // requested node is found by its long term pk
                (AnnounceStatus::Found, pk_as_digest(entry.data_pk))
            }
        } else {
            // requested node not found or failed to announce
            (AnnounceStatus::Failed, ping_id_2)
        };

        let response_payload = OnionAnnounceResponsePayload {
            announce_status,
            ping_id_or_pk,
            nodes: kbucket.get_closest(&payload.search_pk)
        };
        let response = OnionAnnounceResponse::new(&shared_secret, payload.sendback_data, response_payload);

        Ok(response)
    }

    /** Handle data request and build `OnionResponse3` packet that should be
    sent to determined address.

    When onion node handles `OnionDataRequest` it checks if onion entry list
    contains destination node and when entry exists sends `OnionDataResponse`
    to this node through its onion path.

    */
    pub fn handle_data_request(&self, request: OnionDataRequest) -> Result<(OnionResponse3, SocketAddr), Error> {
        if let Some(entry) = self.find_in_entries(request.inner.destination_pk) {
            let response_payload = OnionDataResponse {
                nonce: request.inner.nonce,
                temporary_pk: request.inner.temporary_pk,
                payload: request.inner.payload
            };
            let response = OnionResponse3 {
                onion_return: entry.onion_return.clone(),
                payload: InnerOnionResponse::OnionDataResponse(response_payload)
            };
            let saddr = SocketAddr::new(entry.ip_addr, entry.port);
            Ok((response, saddr))
        } else {
            Err(Error::new(
                ErrorKind::Other,
                format!("No announced node with public key {:?}", request.inner.destination_pk)
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate rand;

    use super::*;

    use quickcheck::{Arbitrary, StdGen};
    use tokio_executor;
    use tokio_timer::clock::*;

    use toxcore::time::ConstNow;

    const ONION_RETURN_3_PAYLOAD_SIZE: usize = ONION_RETURN_3_SIZE - secretbox::NONCEBYTES;

    #[test]
    fn announce_entry_valid() {
        let entry = OnionAnnounceEntry::new(
            PublicKey::from_slice(&[1; 32]).unwrap(),
            "1.2.3.4".parse().unwrap(),
            12345,
            OnionReturn {
                nonce: secretbox::gen_nonce(),
                payload: vec![42; 42]
            },
            gen_keypair().0
        );
        assert!(!entry.is_timed_out());
    }

    #[test]
    fn announce_entry_expired() {
        let entry = OnionAnnounceEntry::new(
            PublicKey::from_slice(&[1; 32]).unwrap(),
            "1.2.3.4".parse().unwrap(),
            12345,
            OnionReturn {
                nonce: secretbox::gen_nonce(),
                payload: vec![42; 42]
            },
            gen_keypair().0
        );

        let mut enter = tokio_executor::enter().unwrap();
        // time when entry is timed out
        let clock = Clock::new_with_now(ConstNow(
            entry.time + Duration::from_secs(ONION_ANNOUNCE_TIMEOUT + 1)
        ));

        with_default(&clock, &mut enter, |_| {
            assert!(entry.is_timed_out());
        });
    }

    #[test]
    fn ping_id_respects_timeout_gap() {
        let onion_announce = OnionAnnounce::new(gen_keypair().0);

        let time = SystemTime::now();
        let time_1 = time - Duration::from_secs(unix_time(time) % PING_ID_TIMEOUT);
        let time_2 = time_1 + Duration::from_secs(PING_ID_TIMEOUT - 1);
        let pk = gen_keypair().0;
        let ip_addr = "1.2.3.4".parse().unwrap();
        let port = 12345;

        let ping_id_1 = onion_announce.ping_id(time_1, pk, ip_addr, port);
        let ping_id_2 = onion_announce.ping_id(time_2, pk, ip_addr, port);

        assert_eq!(ping_id_1, ping_id_2);
    }

    #[test]
    fn ping_id_depends_on_all_args() {
        let onion_announce = OnionAnnounce::new(gen_keypair().0);

        let time_1 = SystemTime::now();
        let time_2 = time_1 + Duration::from_secs(PING_ID_TIMEOUT);

        let pk_1 = gen_keypair().0;
        let pk_2 = gen_keypair().0;

        let ip_addr_1 = "1.2.3.4".parse().unwrap();
        let ip_addr_2 = "3.4.5.6".parse().unwrap();

        let port_1 = 12345;
        let port_2 = 54321;

        let ping_id = onion_announce.ping_id(time_1, pk_1, ip_addr_1, port_1);

        let ping_id_1 = onion_announce.ping_id(time_1, pk_1, ip_addr_1, port_1);
        let ping_id_2 = onion_announce.ping_id(time_2, pk_1, ip_addr_1, port_1);
        let ping_id_3 = onion_announce.ping_id(time_1, pk_2, ip_addr_1, port_1);
        let ping_id_4 = onion_announce.ping_id(time_1, pk_1, ip_addr_2, port_1);
        let ping_id_5 = onion_announce.ping_id(time_1, pk_1, ip_addr_1, port_2);

        assert_eq!(ping_id, ping_id_1);
        assert!(ping_id != ping_id_2);
        assert!(ping_id != ping_id_3);
        assert!(ping_id != ping_id_4);
        assert!(ping_id != ping_id_5);
    }

    fn create_random_entry() -> OnionAnnounceEntry {
        let mut gen = StdGen::new(rand::thread_rng(), 1024);
        OnionAnnounceEntry::new(
            gen_keypair().0,
            IpAddr::arbitrary(&mut gen),
            u16::arbitrary(&mut gen),
            OnionReturn {
                nonce: secretbox::gen_nonce(),
                payload: vec![42; 42]
            },
            gen_keypair().0
        )
    }

    #[test]
    fn onion_announce_is_clonable() {
        let dht_pk = gen_keypair().0;
        let mut onion_announce = OnionAnnounce::new(dht_pk);
        let entry = create_random_entry();
        onion_announce.add_to_entries(entry);
        let _cloned = onion_announce.clone();
        // that's all.
    }

    #[test]
    fn expired_entry_not_in_entries() {
        let dht_pk = gen_keypair().0;
        let mut onion_announce = OnionAnnounce::new(dht_pk);

        let entry = create_random_entry();
        let entry_pk = entry.pk;
        let entry_time = entry.time;

        onion_announce.entries.push(entry);

        let mut enter = tokio_executor::enter().unwrap();
        // time when entry is timed out
        let clock = Clock::new_with_now(ConstNow(
            entry_time + Duration::from_secs(ONION_ANNOUNCE_TIMEOUT + 1)
        ));

        with_default(&clock, &mut enter, |_| {
            assert!(onion_announce.find_in_entries(entry_pk).is_none());
        });
    }

    ////////////////////////////////////////////////////////////////////////////////////////
    // Tests for OnionAnnounce::add_to_entries
    #[test]
    fn add_to_entries_when_limit_is_not_reached() {
        let dht_pk = gen_keypair().0;
        let mut onion_announce = OnionAnnounce::new(dht_pk);

        let mut pks = Vec::new();

        for _ in 0..ONION_ANNOUNCE_MAX_ENTRIES {
            let entry = create_random_entry();
            pks.push(entry.pk);
            assert!(onion_announce.add_to_entries(entry).is_some());
        }

        // check that announce list contains all added entries
        for pk in pks {
            assert!(onion_announce.find_in_entries(pk).is_some());
        }

        assert_eq!(onion_announce.entries.len(), ONION_ANNOUNCE_MAX_ENTRIES);
    }

    #[test]
    fn add_to_entries_should_update_existent_entry() {
        let dht_pk = gen_keypair().0;
        let mut onion_announce = OnionAnnounce::new(dht_pk);

        let mut pks = Vec::new();

        for _ in 0..ONION_ANNOUNCE_MAX_ENTRIES {
            let entry = create_random_entry();
            pks.push(entry.pk);
            assert!(onion_announce.add_to_entries(entry).is_some());
        }

        // choose one of entries to update
        let mut entry_to_update = onion_announce.entries[ONION_ANNOUNCE_MAX_ENTRIES / 2].clone();

        entry_to_update.time += Duration::from_secs(7);
        entry_to_update.ip_addr = "1.2.3.4".parse().unwrap();
        entry_to_update.port = 12345;

        let mut enter = tokio_executor::enter().unwrap();
        // time when entry will be updated
        let clock = Clock::new_with_now(ConstNow(entry_to_update.time));

        with_default(&clock, &mut enter, |_| {
            // update entry
            assert!(onion_announce.add_to_entries(entry_to_update.clone()).is_some());

            assert_eq!(onion_announce.find_in_entries(entry_to_update.pk), Some(&entry_to_update));

            // check that announce list contains all added entries
            for pk in pks {
                assert!(onion_announce.find_in_entries(pk).is_some());
            }

            assert_eq!(onion_announce.entries.len(), ONION_ANNOUNCE_MAX_ENTRIES);
        });
    }

    #[test]
    fn add_to_entries_should_replace_timed_out_entries() {
        let dht_pk = gen_keypair().0;
        let mut onion_announce = OnionAnnounce::new(dht_pk);

        let mut pks = Vec::new();

        let now = Instant::now();

        let mut enter = tokio_executor::enter().unwrap();
        // time when all entries except one will be created
        let clock_1 = Clock::new_with_now(ConstNow(
            now + Duration::from_secs(ONION_ANNOUNCE_TIMEOUT)
        ));
        // time when one entry will be timed out
        let clock_2 = Clock::new_with_now(ConstNow(
            now + Duration::from_secs(ONION_ANNOUNCE_TIMEOUT + 1)
        ));

        with_default(&clock_1, &mut enter, |_| {
            for _ in 0..ONION_ANNOUNCE_MAX_ENTRIES {
                let entry = create_random_entry();
                pks.push(entry.pk);
                assert!(onion_announce.add_to_entries(entry).is_some());
            }
        });

        // make one of entries timed out
        let timed_out_pk = onion_announce.entries[ONION_ANNOUNCE_MAX_ENTRIES / 2].pk;
        onion_announce.entries[ONION_ANNOUNCE_MAX_ENTRIES / 2].time = now;

        with_default(&clock_2, &mut enter, |_| {
            let entry = create_random_entry();
            let entry_pk = entry.pk;
            assert!(onion_announce.add_to_entries(entry).is_some());

            // check that announce list contains new entry
            assert!(onion_announce.find_in_entries(entry_pk).is_some());

            // check that announce list contains all old entries except timed out
            for pk in pks.into_iter().filter(|&pk| pk != timed_out_pk) {
                assert!(onion_announce.find_in_entries(pk).is_some());
            }

            assert_eq!(onion_announce.entries.len(), ONION_ANNOUNCE_MAX_ENTRIES);
        });
    }

    #[test]
    fn add_to_entries_should_replace_the_farthest_entry() {
        let dht_pk = PublicKey::from_slice(&[0; 32]).unwrap();
        let mut onion_announce = OnionAnnounce::new(dht_pk);

        // add one entry with farthest pk
        let mut entry = create_random_entry();
        entry.pk = PublicKey::from_slice(&[255; 32]).unwrap();
        assert!(onion_announce.add_to_entries(entry).is_some());

        let mut pks = Vec::new();

        for _ in 0..ONION_ANNOUNCE_MAX_ENTRIES - 1 {
            let entry = create_random_entry();
            pks.push(entry.pk);
            assert!(onion_announce.add_to_entries(entry).is_some());
        }

        // add new entry that should replace the farthest one
        let entry = create_random_entry();
        let entry_pk = entry.pk;
        assert!(onion_announce.add_to_entries(entry).is_some());

        // check that announce list contains new entry
        assert!(onion_announce.find_in_entries(entry_pk).is_some());

        // check that announce list contains all old entries except farthest
        for pk in pks {
            assert!(onion_announce.find_in_entries(pk).is_some());
        }

        assert_eq!(onion_announce.entries.len(), ONION_ANNOUNCE_MAX_ENTRIES);
    }

    #[test]
    fn add_to_entries_should_should_not_add_the_farthest_entry() {
        let dht_pk = PublicKey::from_slice(&[0; 32]).unwrap();
        let mut onion_announce = OnionAnnounce::new(dht_pk);

        let mut pks = Vec::new();

        for _ in 0..ONION_ANNOUNCE_MAX_ENTRIES {
            let entry = create_random_entry();
            pks.push(entry.pk);
            assert!(onion_announce.add_to_entries(entry).is_some());
        }

        // try to add new farthest entry
        let mut entry = create_random_entry();
        let entry_pk = PublicKey::from_slice(&[255; 32]).unwrap();
        entry.pk = entry_pk;
        assert!(onion_announce.add_to_entries(entry).is_none());

        // check that announce list does not contain new entry
        assert!(onion_announce.find_in_entries(entry_pk).is_none());

        // check that announce list contains all old entries
        for pk in pks {
            assert!(onion_announce.find_in_entries(pk).is_some());
        }

        assert_eq!(onion_announce.entries.len(), ONION_ANNOUNCE_MAX_ENTRIES);
    }

    ////////////////////////////////////////////////////////////////////////////////////////
    // Tests for OnionAnnounce::handle_onion_announce_request
    #[test]
    fn handle_announce_failed_to_find_node() {
        let (dht_pk, dht_sk) = gen_keypair();
        let search_pk = gen_keypair().0;
        let data_pk = gen_keypair().0;
        let (packet_pk, packet_sk) = gen_keypair();
        let shared_secret = precompute(&dht_pk, &packet_sk);

        let mut onion_announce = OnionAnnounce::new(dht_pk);

        // insert random entry
        let entry = create_random_entry();
        assert!(onion_announce.add_to_entries(entry).is_some());

        // create request packet
        let sendback_data = 42;
        let payload = OnionAnnounceRequestPayload {
            ping_id: initial_ping_id(),
            search_pk,
            data_pk,
            sendback_data
        };
        let inner = InnerOnionAnnounceRequest::new(&shared_secret, &packet_pk, payload);
        let onion_return = OnionReturn {
            nonce: secretbox::gen_nonce(),
            payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
        };
        let request = OnionAnnounceRequest {
            inner,
            onion_return
        };

        let kbucket = Kbucket::new(&dht_pk);

        let addr = "127.0.0.1:12345".parse().unwrap();

        let response = onion_announce.handle_onion_announce_request(request, &dht_sk, &kbucket, addr).unwrap();

        let response_payload = response.get_payload(&shared_secret).unwrap();

        assert_eq!(response.sendback_data, sendback_data);
        assert_eq!(response_payload.announce_status, AnnounceStatus::Failed);
    }

    #[test]
    fn handle_announce_node_is_found() {
        let (dht_pk, dht_sk) = gen_keypair();
        let data_pk = gen_keypair().0;
        let (packet_pk, packet_sk) = gen_keypair();
        let shared_secret = precompute(&dht_pk, &packet_sk);

        let mut onion_announce = OnionAnnounce::new(dht_pk);

        // insert random entry
        let entry = create_random_entry();
        let search_pk = entry.pk;
        let entry_data_pk = entry.data_pk;
        assert!(onion_announce.add_to_entries(entry).is_some());

        // create request packet
        let sendback_data = 42;
        let payload = OnionAnnounceRequestPayload {
            ping_id: initial_ping_id(),
            search_pk,
            data_pk,
            sendback_data
        };
        let inner = InnerOnionAnnounceRequest::new(&shared_secret, &packet_pk, payload);
        let onion_return = OnionReturn {
            nonce: secretbox::gen_nonce(),
            payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
        };
        let request = OnionAnnounceRequest {
            inner,
            onion_return
        };

        let kbucket = Kbucket::new(&dht_pk);

        let addr = "127.0.0.1:12345".parse().unwrap();

        let response = onion_announce.handle_onion_announce_request(request, &dht_sk, &kbucket, addr).unwrap();

        let response_payload = response.get_payload(&shared_secret).unwrap();

        assert_eq!(response.sendback_data, sendback_data);
        assert_eq!(response_payload.announce_status, AnnounceStatus::Found);
        assert_eq!(digest_as_pk(response_payload.ping_id_or_pk), entry_data_pk);
    }

    #[test]
    fn handle_announce_successfully_announced() {
        let (dht_pk, dht_sk) = gen_keypair();
        let search_pk = gen_keypair().0;
        let data_pk = gen_keypair().0;
        let (packet_pk, packet_sk) = gen_keypair();
        let shared_secret = precompute(&dht_pk, &packet_sk);

        let mut onion_announce = OnionAnnounce::new(dht_pk);

        // insert random entry
        let entry = create_random_entry();
        assert!(onion_announce.add_to_entries(entry).is_some());

        let addr: SocketAddr = "127.0.0.1:12345".parse().unwrap();
        let time = SystemTime::now();
        let ping_id = onion_announce.ping_id(time, packet_pk, addr.ip(), addr.port());

        // create request packet
        let sendback_data = 42;
        let payload = OnionAnnounceRequestPayload {
            ping_id,
            search_pk,
            data_pk,
            sendback_data
        };
        let inner = InnerOnionAnnounceRequest::new(&shared_secret, &packet_pk, payload);
        let onion_return = OnionReturn {
            nonce: secretbox::gen_nonce(),
            payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
        };
        let request = OnionAnnounceRequest {
            inner,
            onion_return
        };

        let kbucket = Kbucket::new(&dht_pk);

        let response = onion_announce.handle_onion_announce_request(request, &dht_sk, &kbucket, addr).unwrap();

        let response_payload = response.get_payload(&shared_secret).unwrap();

        assert_eq!(response.sendback_data, sendback_data);
        assert_eq!(response_payload.announce_status, AnnounceStatus::Announced);
        assert!(onion_announce.find_in_entries(packet_pk).is_some());
    }

    #[test]
    fn handle_announce_failed_to_find_ourselves_with_different_data_pk() { // weird case, should we remove it?
        let (dht_pk, dht_sk) = gen_keypair();
        let data_pk = gen_keypair().0;
        let (packet_pk, packet_sk) = gen_keypair();
        let shared_secret = precompute(&dht_pk, &packet_sk);

        let mut onion_announce = OnionAnnounce::new(dht_pk);

        // insert ourselves
        let mut entry = create_random_entry();
        entry.pk = packet_pk;
        assert!(onion_announce.add_to_entries(entry).is_some());

        // create request packet
        let sendback_data = 42;
        let payload = OnionAnnounceRequestPayload {
            ping_id: initial_ping_id(),
            search_pk: packet_pk,
            data_pk,
            sendback_data
        };
        let inner = InnerOnionAnnounceRequest::new(&shared_secret, &packet_pk, payload);
        let onion_return = OnionReturn {
            nonce: secretbox::gen_nonce(),
            payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
        };
        let request = OnionAnnounceRequest {
            inner,
            onion_return
        };

        let kbucket = Kbucket::new(&dht_pk);

        let addr = "127.0.0.1:12345".parse().unwrap();

        let response = onion_announce.handle_onion_announce_request(request, &dht_sk, &kbucket, addr).unwrap();

        let response_payload = response.get_payload(&shared_secret).unwrap();

        assert_eq!(response.sendback_data, sendback_data);
        assert_eq!(response_payload.announce_status, AnnounceStatus::Failed);
    }

    ////////////////////////////////////////////////////////////////////////////////////////
    // Tests for OnionAnnounce::handle_onion_announce_request
    #[test]
    fn handle_data_request() {
        let (dht_pk, _dht_sk) = gen_keypair();

        let mut onion_announce = OnionAnnounce::new(dht_pk);

        // insert random entry
        let entry = create_random_entry();
        let entry_pk = entry.pk;
        let entry_addr = entry.ip_addr;
        let entry_port = entry.port;
        let entry_onion_return = entry.onion_return.clone();
        assert!(onion_announce.add_to_entries(entry).is_some());

        let nonce = gen_nonce();
        let temporary_pk = gen_keypair().0;
        let payload = vec![42; 123];
        let onion_return = OnionReturn {
            nonce: secretbox::gen_nonce(),
            payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
        };
        let inner = InnerOnionDataRequest {
            destination_pk: entry_pk,
            nonce,
            temporary_pk,
            payload: payload.clone()
        };
        let request = OnionDataRequest {
            inner,
            onion_return
        };

        let (response, saddr) = onion_announce.handle_data_request(request).unwrap();

        assert_eq!(saddr.ip(), entry_addr);
        assert_eq!(saddr.port(), entry_port);
        assert_eq!(response.onion_return, entry_onion_return);
        assert_eq!(response.payload, InnerOnionResponse::OnionDataResponse(OnionDataResponse {
            nonce,
            temporary_pk,
            payload
        }));
    }

    #[test]
    fn handle_data_request_unknown_destination() {
        let (dht_pk, _dht_sk) = gen_keypair();

        let onion_announce = OnionAnnounce::new(dht_pk);

        let onion_return = OnionReturn {
            nonce: secretbox::gen_nonce(),
            payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
        };
        let inner = InnerOnionDataRequest {
            destination_pk: gen_keypair().0,
            nonce: gen_nonce(),
            temporary_pk: gen_keypair().0,
            payload: vec![42; 123]
        };
        let request = OnionDataRequest {
            inner,
            onion_return
        };

        assert!(onion_announce.handle_data_request(request).is_err());
    }
}