sntp 0.1.2

A Simple Network Time Protocol (SNTP) client implementation using smoltcp.
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
//! Wire protocol definitions for the Simple Network Time Protocol v4 (SNTPv4).
//!
//! See https://tools.ietf.org/html/rfc4330 for the SNTPv4 specification.

use byteorder::{ByteOrder, NetworkEndian};
use core::convert;
use smoltcp::{Error, Result};

/// The SNTP leap indicator field.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum LeapIndicator {
    NoWarning,
    LastMinute61Sec,
    LastMinute59Sec,
    AlarmCondition,
    Unknown(u8),
}

impl convert::From<u8> for LeapIndicator {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::NoWarning,
            1 => Self::LastMinute61Sec,
            2 => Self::LastMinute59Sec,
            3 => Self::AlarmCondition,
            _ => Self::Unknown(value),
        }
    }
}

impl convert::From<LeapIndicator> for u8 {
    fn from(value: LeapIndicator) -> Self {
        match value {
            LeapIndicator::NoWarning => 0,
            LeapIndicator::LastMinute61Sec => 1,
            LeapIndicator::LastMinute59Sec => 2,
            LeapIndicator::AlarmCondition => 3,
            LeapIndicator::Unknown(value) => value,
        }
    }
}

/// The SNTP protocol mode.
///
/// Only unicast mode is supported at the time.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ProtocolMode {
    Reserved,
    SymmetricActive,
    SymmetricPassive,
    Client,
    Server,
    Broadcast,
    NtpControlMessage,
    Private,
    Unknown(u8),
}

impl convert::From<u8> for ProtocolMode {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Reserved,
            1 => Self::SymmetricActive,
            2 => Self::SymmetricPassive,
            3 => Self::Client,
            4 => Self::Server,
            5 => Self::Broadcast,
            6 => Self::NtpControlMessage,
            7 => Self::Private,
            _ => Self::Unknown(value),
        }
    }
}

impl convert::From<ProtocolMode> for u8 {
    fn from(value: ProtocolMode) -> Self {
        match value {
            ProtocolMode::Reserved => 0,
            ProtocolMode::SymmetricActive => 1,
            ProtocolMode::SymmetricPassive => 2,
            ProtocolMode::Client => 3,
            ProtocolMode::Server => 4,
            ProtocolMode::Broadcast => 5,
            ProtocolMode::NtpControlMessage => 6,
            ProtocolMode::Private => 7,
            ProtocolMode::Unknown(value) => value,
        }
    }
}

/// The SNTP stratum.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Stratum {
    KissOfDeath,
    Primary,
    Secondary(u8),
    Reserved(u8),
}

impl From<u8> for Stratum {
    fn from(s: u8) -> Self {
        match s {
            0 => Stratum::KissOfDeath,
            1 => Stratum::Primary,
            2..=15 => Stratum::Secondary(s),
            _ => Stratum::Reserved(s),
        }
    }
}

impl Into<u8> for Stratum {
    fn into(self) -> u8 {
        match self {
            Stratum::KissOfDeath => 0,
            Stratum::Primary => 1,
            Stratum::Secondary(s) | Stratum::Reserved(s) => s,
        }
    }
}

/// An SNTP timestamp, represented as integer and fractional part.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct Timestamp {
    pub(crate) sec: u32,
    pub(crate) frac: u32,
}

impl Timestamp {
    fn parse(buffer: &[u8]) -> Result<Timestamp> {
        let sec = NetworkEndian::read_u32(buffer.get(0..4).ok_or(Error::Truncated)?);
        let frac = NetworkEndian::read_u32(buffer.get(4..8).ok_or(Error::Truncated)?);
        Ok(Timestamp { sec, frac })
    }

    fn emit(&self, buffer: &mut [u8]) {
        NetworkEndian::write_u32(&mut buffer[0..4], self.sec);
        NetworkEndian::write_u32(&mut buffer[4..8], self.frac);
    }
}

/// A read/write wrapper around a Simple Network Time Protocol v4 packet buffer.
#[derive(Debug, PartialEq)]
pub struct Packet<T: AsRef<[u8]>> {
    buffer: T,
}

pub(crate) mod field {
    #![allow(non_snake_case)]
    #![allow(unused)]

    use core::ops;

    type Field = ops::Range<usize>;

    pub const LI_VN_MODE: usize = 0;
    pub const STRATUM: usize = 1;
    pub const POLL: usize = 2;
    pub const PRECISION: usize = 3;
    pub const ROOT_DELAY: Field = 4..8;
    pub const ROOT_DISPERSION: Field = 8..12;
    pub const REFERENCE_IDENTIFIER: Field = 12..16;
    pub const REFERENCE_TIMESTAMP: Field = 16..24;
    pub const ORIGINATE_TIMESTAMP: Field = 24..32;
    pub const RECEIVE_TIMESTAMP: Field = 32..40;
    pub const TRANSMIT_TIMESTAMP: Field = 40..48;
    pub const KEY_IDENTIFIER: Field = 48..52;
    pub const MESSAGE_DIGEST: Field = 52..68;

    // Offsets and masks for LI_VN_MODE bitfield
    pub const LI_MASK: u8 = 0xc0;
    pub const LI_SHIFT: u8 = 6;
    pub const VN_MASK: u8 = 0x38;
    pub const VN_SHIFT: u8 = 3;
    pub const MODE_MASK: u8 = 0x07;
    pub const MODE_SHIFT: u8 = 0x00;
}

impl<T: AsRef<[u8]>> Packet<T> {
    /// Imbue a raw octet buffer with SNTP packet structure.
    pub fn new_unchecked(buffer: T) -> Packet<T> {
        Packet { buffer }
    }

    /// Shorthand for a combination of [new_unchecked] and [check_len].
    ///
    /// [new_unchecked]: #method.new_unchecked
    /// [check_len]: #method.check_len
    pub fn new_checked(buffer: T) -> Result<Packet<T>> {
        let packet = Self::new_unchecked(buffer);
        packet.check_len()?;
        Ok(packet)
    }

    /// Ensure that no accessor method will panic if called.
    /// Returns `Err(Error::Truncated)` if the buffer is too short.
    ///
    /// [set_header_len]: #method.set_header_len
    pub fn check_len(&self) -> Result<()> {
        let len = self.buffer.as_ref().len();
        if len < field::TRANSMIT_TIMESTAMP.end {
            Err(Error::Truncated)
        } else {
            Ok(())
        }
    }

    /// Returns the leap indicator of this packet.
    pub fn leap_indicator(&self) -> LeapIndicator {
        let data = self.buffer.as_ref();
        LeapIndicator::from((data[field::LI_VN_MODE] & field::LI_MASK) >> field::LI_SHIFT)
    }

    /// Returns the version of this packet.
    pub fn version(&self) -> u8 {
        let data = self.buffer.as_ref();
        (data[field::LI_VN_MODE] & field::VN_MASK) >> field::VN_SHIFT
    }

    /// Returns the protocol mode of this packet.
    pub fn protocol_mode(&self) -> ProtocolMode {
        let data = self.buffer.as_ref();
        ProtocolMode::from((data[field::LI_VN_MODE] & field::MODE_MASK) >> field::MODE_SHIFT)
    }

    /// Returns the stratum of this packet.
    pub fn stratum(&self) -> Stratum {
        self.buffer.as_ref()[field::STRATUM].into()
    }

    /// Returns the poll interval of this packet.
    pub fn poll_interval(&self) -> u8 {
        self.buffer.as_ref()[field::POLL]
    }

    /// Returns the precision of this packet.
    pub fn precision(&self) -> i8 {
        self.buffer.as_ref()[field::PRECISION] as i8
    }

    /// Returns the root delay of this packet.
    pub fn root_delay(&self) -> i32 {
        let data = self.buffer.as_ref();
        NetworkEndian::read_i32(&data[field::ROOT_DELAY])
    }

    /// Returns the root dispersion of this packet.
    pub fn root_dispersion(&self) -> u32 {
        let data = self.buffer.as_ref();
        NetworkEndian::read_u32(&data[field::ROOT_DISPERSION])
    }

    /// Returns the reference identifier of this packet.
    pub fn ref_identifier(&self) -> [u8; 4] {
        let d = &self.buffer.as_ref()[field::REFERENCE_IDENTIFIER];
        [d[0], d[1], d[2], d[3]]
    }

    /// Returns the reference timestamp of this packet.
    pub fn ref_timestamp(&self) -> Result<Timestamp> {
        let data = self.buffer.as_ref();
        Timestamp::parse(&data[field::REFERENCE_TIMESTAMP])
    }

    /// Returns the originate timestamp of this packet.
    pub fn orig_timestamp(&self) -> Result<Timestamp> {
        let data = self.buffer.as_ref();
        Timestamp::parse(&data[field::ORIGINATE_TIMESTAMP])
    }

    /// Returns the receive timestamp of this packet.
    pub fn recv_timestamp(&self) -> Result<Timestamp> {
        let data = self.buffer.as_ref();
        Timestamp::parse(&data[field::RECEIVE_TIMESTAMP])
    }

    /// Returns the transmit timestamp of this packet.
    pub fn xmit_timestamp(&self) -> Result<Timestamp> {
        let data = self.buffer.as_ref();
        Timestamp::parse(&data[field::TRANSMIT_TIMESTAMP])
    }
}

impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
    /// Sets the leap indicator for this packet.
    pub fn set_leap_indicator(&mut self, li: LeapIndicator) {
        let data = self.buffer.as_mut();
        let li: u8 = li.into();
        data[field::LI_VN_MODE] &= !field::LI_MASK;
        data[field::LI_VN_MODE] |= li << field::LI_SHIFT;
    }

    /// Sets the version number for this packet.
    pub fn set_version(&mut self, vn: u8) {
        let data = self.buffer.as_mut();
        data[field::LI_VN_MODE] &= !field::VN_MASK;
        data[field::LI_VN_MODE] |= vn << field::VN_SHIFT;
    }

    /// Sets the protocol mode for this packet.
    pub fn set_protocol_mode(&mut self, mode: ProtocolMode) {
        let data = self.buffer.as_mut();
        let mode: u8 = mode.into();
        data[field::LI_VN_MODE] &= !field::MODE_MASK;
        data[field::LI_VN_MODE] |= mode << field::MODE_SHIFT;
    }

    /// Sets the stratum for this packet.
    pub fn set_stratum(&mut self, stratum: Stratum) {
        self.buffer.as_mut()[field::STRATUM] = stratum.into();
    }

    /// Sets the poll interval for this packet.
    pub fn set_poll_interval(&mut self, poll: u8) {
        self.buffer.as_mut()[field::POLL] = poll;
    }

    /// Sets the precision for this packet.
    pub fn set_precision(&mut self, precision: i8) {
        self.buffer.as_mut()[field::PRECISION] = precision as u8;
    }

    /// Sets the root delay for this packet.
    pub fn set_root_delay(&mut self, delay: i32) {
        let data = &mut self.buffer.as_mut()[field::ROOT_DELAY];
        NetworkEndian::write_i32(data, delay);
    }

    /// Sets the root dispersion for this packet.
    pub fn set_root_dispersion(&mut self, disp: u32) {
        let data = &mut self.buffer.as_mut()[field::ROOT_DISPERSION];
        NetworkEndian::write_u32(data, disp);
    }

    /// Sets the reference identifier for this packet.
    pub fn set_ref_identifier(&mut self, id: [u8; 4]) {
        self.buffer.as_mut()[field::REFERENCE_IDENTIFIER].copy_from_slice(&id[..]);
    }

    /// Sets the reference timestamp for this packet.
    pub fn set_ref_timestamp(&mut self, ts: Timestamp) {
        let field = &mut self.buffer.as_mut()[field::REFERENCE_TIMESTAMP];
        ts.emit(field);
    }

    /// Sets the originate timestamp for this packet.
    pub fn set_orig_timestamp(&mut self, ts: Timestamp) {
        let field = &mut self.buffer.as_mut()[field::ORIGINATE_TIMESTAMP];
        ts.emit(field);
    }

    /// Sets the receive timestamp for this packet.
    pub fn set_recv_timestamp(&mut self, ts: Timestamp) {
        let field = &mut self.buffer.as_mut()[field::RECEIVE_TIMESTAMP];
        ts.emit(field);
    }
    /// Sets the transmit timestamp for this packet.
    pub fn set_xmit_timestamp(&mut self, ts: Timestamp) {
        let field = &mut self.buffer.as_mut()[field::TRANSMIT_TIMESTAMP];
        ts.emit(field);
    }
}

/// A high-level representation of a Simple Network Time Protocol v4 packet.
///
/// SNTPv4 messages have the following layout
/// (see [RFC 4330](https://tools.ietf.org/html/rfc4330) for details):
///
/// ```no_rust
///                      1                   2                   3
///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |LI | VN  |Mode |    Stratum    |     Poll      |   Precision    |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                          Root  Delay                           |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                       Root  Dispersion                         |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                     Reference Identifier                       |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                                |
/// |                    Reference Timestamp (64)                    |
/// |                                                                |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                                |
/// |                    Originate Timestamp (64)                    |
/// |                                                                |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                                |
/// |                     Receive Timestamp (64)                     |
/// |                                                                |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                                |
/// |                     Transmit Timestamp (64)                    |
/// |                                                                |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                 Key Identifier (optional) (32)                 |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                                |
/// |                                                                |
/// |                 Message Digest (optional) (128)                |
/// |                                                                |
/// |                                                                |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ```
///
/// Note that most of these fields are ignored right now, as only unicast mode
/// is supported, without any advanced features (delays, response checks, etc.).
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Repr {
    /// Leap indicator for leap second insertion/deletion.
    pub leap_indicator: LeapIndicator,
    /// Version number.
    pub version: u8,
    /// Protocol mode. Only unicast mode is supported.
    pub protocol_mode: ProtocolMode,
    /// Stratum of the server in an SNTP response.
    pub stratum: Stratum,
    /// Maximum interval between successive messages.
    pub poll_interval: u8,
    /// Precision of the system clock in seconds.
    pub precision: i8,
    /// Total roundtrip delay to the primary reference source.
    /// Signed fixed-point in 16.16 format.
    pub root_delay: i32,
    /// Maximum error due to clock frequency tolerances.
    /// Unsigned fixed-point in 16.16 format.
    pub root_dispersion: u32,
    /// Bitstring identifying the particular reference source.
    pub ref_identifier: [u8; 4],
    /// The time at which the system clock was last set or corrected.
    pub ref_timestamp: Timestamp,
    /// The time at which the request departed the client for the server.
    pub orig_timestamp: Timestamp,
    /// The time at which the request arrived at the server
    /// or the reply arrived at the client.
    pub recv_timestamp: Timestamp,
    /// The time at which the request departed the client
    /// or the reply departed the server.
    pub xmit_timestamp: Timestamp,
}

impl Repr {
    /// Return the length of a packet that will be emitted
    /// from this high-level representation.
    pub fn buffer_len(&self) -> usize {
        field::KEY_IDENTIFIER.start
    }

    /// Parse an SNTP packet and return a high-level representation.
    pub fn parse<T>(packet: &Packet<&T>) -> Result<Self>
    where
        T: AsRef<[u8]> + ?Sized,
    {
        Ok(Repr {
            leap_indicator: packet.leap_indicator(),
            version: packet.version(),
            protocol_mode: packet.protocol_mode(),
            stratum: packet.stratum(),
            poll_interval: packet.poll_interval(),
            precision: packet.precision(),
            root_delay: packet.root_delay(),
            root_dispersion: packet.root_dispersion(),
            ref_identifier: packet.ref_identifier(),
            ref_timestamp: packet.ref_timestamp()?,
            orig_timestamp: packet.orig_timestamp()?,
            recv_timestamp: packet.recv_timestamp()?,
            xmit_timestamp: packet.xmit_timestamp()?,
        })
    }

    /// Emit a high-level representation into an SNTP packet.
    pub fn emit<T>(&self, packet: &mut Packet<&mut T>) -> Result<()>
    where
        T: AsRef<[u8]> + AsMut<[u8]> + ?Sized,
    {
        packet.set_leap_indicator(self.leap_indicator);
        packet.set_version(self.version);
        packet.set_protocol_mode(self.protocol_mode);
        packet.set_stratum(self.stratum);
        packet.set_poll_interval(self.poll_interval);
        packet.set_precision(self.precision);
        packet.set_root_delay(self.root_delay);
        packet.set_root_dispersion(self.root_dispersion);
        packet.set_ref_identifier(self.ref_identifier);
        packet.set_ref_timestamp(self.ref_timestamp);
        packet.set_orig_timestamp(self.orig_timestamp);
        packet.set_recv_timestamp(self.recv_timestamp);
        packet.set_xmit_timestamp(self.xmit_timestamp);

        Ok(())
    }
}

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

    static PACKET_BYTES: [u8; 48] = [
        0x24, 0x02, 0x00, 0xe6, 0x00, 0x00, 0x01, 0x20, 0x00, 0x00, 0x00, 0x6f, 0x50, 0x42, 0xe0,
        0x02, 0xe2, 0x6c, 0x32, 0xf1, 0x0e, 0xd5, 0xfe, 0xa9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0xe2, 0x6c, 0x35, 0x11, 0x6a, 0x8c, 0xe6, 0x47, 0xe2, 0x6c, 0x35, 0x11, 0x6a,
        0x8d, 0xf8, 0x8f,
    ];

    #[test]
    fn test_deconstruct() {
        let packet = Packet::new_unchecked(&PACKET_BYTES[..]);
        assert_eq!(packet.leap_indicator(), LeapIndicator::NoWarning);
        assert_eq!(packet.version(), 4);
        assert_eq!(packet.protocol_mode(), ProtocolMode::Server);
        assert_eq!(packet.stratum(), Stratum::Secondary(2));
        assert_eq!(packet.poll_interval(), 0);
        assert_eq!(packet.precision(), -26);
        assert_eq!(packet.root_delay(), 0x120);
        assert_eq!(packet.root_dispersion(), 0x6f);
        assert_eq!(packet.ref_identifier(), [80, 66, 224, 2]);
        assert_eq!(
            packet.ref_timestamp(),
            Ok(Timestamp {
                sec: 0xe26c32f1,
                frac: 0x0ed5fea9,
            })
        );
        assert_eq!(
            packet.orig_timestamp(),
            Ok(Timestamp {
                sec: 0x00000000,
                frac: 0x00000000
            })
        );
        assert_eq!(
            packet.recv_timestamp(),
            Ok(Timestamp {
                sec: 0xe26c3511,
                frac: 0x6a8ce647,
            })
        );
        assert_eq!(
            packet.xmit_timestamp(),
            Ok(Timestamp {
                sec: 0xe26c3511,
                frac: 0x6a8df88f
            })
        )
    }

    #[test]
    fn test_construct() {
        let mut bytes = vec![0xa5; 48];
        let mut packet = Packet::new_unchecked(&mut bytes);
        packet.set_leap_indicator(LeapIndicator::NoWarning);
        packet.set_version(4);
        packet.set_protocol_mode(ProtocolMode::Server);
        packet.set_stratum(Stratum::Secondary(2));
        packet.set_poll_interval(0);
        packet.set_precision(-26);
        packet.set_root_delay(0x120);
        packet.set_root_dispersion(0x6f);
        packet.set_ref_identifier([80, 66, 224, 2]);
        packet.set_ref_timestamp(Timestamp {
            sec: 0xe26c32f1,
            frac: 0x0ed5fea9,
        });
        packet.set_orig_timestamp(Timestamp {
            sec: 0x00000000,
            frac: 0x00000000,
        });
        packet.set_recv_timestamp(Timestamp {
            sec: 0xe26c3511,
            frac: 0x6a8ce647,
        });
        packet.set_xmit_timestamp(Timestamp {
            sec: 0xe26c3511,
            frac: 0x6a8df88f,
        });
        assert_eq!(&packet.buffer[..], &PACKET_BYTES[..]);
    }

    fn packet_repr() -> Repr {
        Repr {
            leap_indicator: LeapIndicator::NoWarning,
            version: 4,
            protocol_mode: ProtocolMode::Server,
            stratum: Stratum::Secondary(2),
            poll_interval: 0,
            precision: -26,
            root_delay: 0x120,
            root_dispersion: 0x6f,
            ref_identifier: [80, 66, 224, 2],
            ref_timestamp: Timestamp {
                sec: 0xe26c32f1,
                frac: 0x0ed5fea9,
            },
            orig_timestamp: Timestamp {
                sec: 0x00000000,
                frac: 0x00000000,
            },
            recv_timestamp: Timestamp {
                sec: 0xe26c3511,
                frac: 0x6a8ce647,
            },
            xmit_timestamp: Timestamp {
                sec: 0xe26c3511,
                frac: 0x6a8df88f,
            },
        }
    }

    #[test]
    fn test_parse() {
        let packet = Packet::new_unchecked(&PACKET_BYTES[..]);
        let repr = Repr::parse(&packet).unwrap();
        assert_eq!(repr, packet_repr());
    }

    #[test]
    fn test_emit() {
        let mut bytes = vec![0xa5; 48];
        let mut packet = Packet::new_unchecked(&mut bytes);
        packet_repr().emit(&mut packet).unwrap();
        assert_eq!(&packet.buffer[..], &PACKET_BYTES[..]);
    }
}