wol 0.5.1

Wake up remote hosts with Wake On LAN magic packets
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
// Copyright Sebastian Wiesner <sebastian@swsnr.de>
//
// Licensed under the EUPL
//
// See https://interoperable-europe.ec.europa.eu/collection/eupl/eupl-text-eupl-12

//! Parse "wakeup files".
//!
//! A "wakeup file" is a file containing lines denoting systems to wake up.
//! Each line is a whitespace-separated sequence of hardware address, and
//! optionally packet destination, port, and SecureON token. See
//! [`WakeUpTarget`] for documentation for details.
//!
//! Blank lines and lines starting with `#` are ignored.
//!
//! Use [`from_lines`] or [`from_reader`] to read wakeup files.

use std::fmt::Display;
use std::io::{BufRead, Error, ErrorKind};
use std::net::IpAddr;
use std::num::ParseIntError;
use std::str::FromStr;

use crate::{MacAddress, ParseError, SecureOn};

/// A destination to send a magic packet to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MagicPacketDestination {
    /// A DNS name to be resolved into an IP address.
    Dns(String),
    /// An IP address.
    Ip(IpAddr),
}

impl Display for MagicPacketDestination {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MagicPacketDestination::Dns(name) => write!(f, "{name}"),
            MagicPacketDestination::Ip(ip_addr) => write!(f, "{ip_addr}"),
        }
    }
}

impl From<String> for MagicPacketDestination {
    fn from(value: String) -> Self {
        IpAddr::from_str(&value)
            .ok()
            .map_or_else(|| Self::Dns(value), Self::Ip)
    }
}

/// A single target to wake up.
///
/// # String format
///
/// Wake up targets can be parsed from strings in the following format:
///
/// ```text
/// <hardware-address> [<IP/DNS name>] [<port>] [<secure-on>]
/// ```
///
/// Except for the hardware address all other fields are optional.
///
/// The MAC address is given as six hexadecimal bytes separated by dashes or
/// colons, e.g `XX-XX-XX-XX-XX-XX` or `XX:XX:XX:XX:XX:XX`.
///
/// The SecureON is given in the same format.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WakeUpTarget {
    hardware_address: MacAddress,
    packet_destination: Option<MagicPacketDestination>,
    port: Option<u16>,
    secure_on: Option<SecureOn>,
}

impl WakeUpTarget {
    /// Create a new wake up target for the given `hardware_address`.
    #[must_use]
    pub fn new(hardware_address: MacAddress) -> Self {
        Self {
            hardware_address,
            packet_destination: None,
            port: None,
            secure_on: None,
        }
    }

    /// Get the hardware address.
    #[must_use]
    pub fn hardware_address(&self) -> MacAddress {
        self.hardware_address
    }

    /// Get the host to send the magic packet to if any.
    ///
    /// This is usually not the same host as the one to wake up; rather it
    /// should be a broadcast address.
    #[must_use]
    pub fn packet_destination(&self) -> Option<&MagicPacketDestination> {
        self.packet_destination.as_ref()
    }

    /// Get the port to send the magic packet to.
    #[must_use]
    pub fn port(&self) -> Option<u16> {
        self.port
    }

    /// Get the SecureON token to include in the packet if any.
    #[must_use]
    pub fn secure_on(&self) -> Option<SecureOn> {
        self.secure_on
    }

    /// Change the hardware address.
    #[must_use]
    pub fn with_hardware_address(mut self, hardware_address: MacAddress) -> Self {
        self.hardware_address = hardware_address;
        self
    }

    /// Change the packet destination.
    #[must_use]
    pub fn with_packet_destination(
        mut self,
        packet_destination: Option<MagicPacketDestination>,
    ) -> Self {
        self.packet_destination = packet_destination;
        self
    }

    /// Change the packet destination.
    #[must_use]
    pub fn with_dns_packet_destination(mut self, dns: String) -> Self {
        self.packet_destination = Some(MagicPacketDestination::Dns(dns));
        self
    }

    /// Change the packet destination.
    #[must_use]
    pub fn with_ip_packet_destination(mut self, ip: IpAddr) -> Self {
        self.packet_destination = Some(MagicPacketDestination::Ip(ip));
        self
    }

    /// Change the destination port for the magic packet.
    #[must_use]
    pub fn with_port(mut self, port: Option<u16>) -> Self {
        self.port = port;
        self
    }

    /// Change the SecureON token for this target.
    #[must_use]
    pub fn with_secure_on(mut self, secure_on: Option<SecureOn>) -> Self {
        self.secure_on = secure_on;
        self
    }
}

/// An invalid wake up target.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WakeUpTargetParseError {
    /// The string was empty or consistent only of whitespace,
    Empty,
    /// The hardware address in field 1 was invalid.
    InvalidHardwareAddress(ParseError),
    /// The port number in the given field was invalid.
    InvalidPort(u8, ParseIntError),
    /// The SecureON token in the given was invalid.
    InvalidSecureOn(u8, ParseError),
    /// The line had more than the expected number of fields.
    TooManyFields(usize),
}

impl Display for WakeUpTargetParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Empty => write!(f, "Line empty"),
            Self::InvalidHardwareAddress(parse_error) => {
                // The hardware address is always in the first field
                write!(f, "Field 1: Invalid hardware address: {parse_error}")
            }
            Self::InvalidPort(field, error) => {
                write!(f, "Field {field}: Invalid port number: {error}")
            }
            Self::InvalidSecureOn(field, error) => {
                write!(f, "Field {field}: Invalid SecureON token: {error}")
            }

            Self::TooManyFields(fields) => write!(f, "Expected 4 fields, got {fields}"),
        }
    }
}

impl std::error::Error for WakeUpTargetParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::InvalidHardwareAddress(parse_error) => Some(parse_error),
            Self::InvalidPort(_, error) => Some(error),
            Self::InvalidSecureOn(_, error) => Some(error),
            Self::TooManyFields(_) | Self::Empty => None,
        }
    }
}

impl FromStr for WakeUpTarget {
    type Err = WakeUpTargetParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts = s.split_ascii_whitespace().collect::<Vec<_>>();
        match parts[..] {
            [] => Err(Self::Err::Empty),
            [field_1] => MacAddress::from_str(field_1)
                .map_err(Self::Err::InvalidHardwareAddress)
                .map(Self::new),
            [field_1, field_2] => {
                let mut line = MacAddress::from_str(field_1)
                    .map_err(Self::Err::InvalidHardwareAddress)
                    .map(Self::new)?;
                if let Ok(secure_on) = SecureOn::from_str(field_2) {
                    line.secure_on = Some(secure_on);
                } else if let Ok(port) = u16::from_str(field_2) {
                    line.port = Some(port);
                } else {
                    line.packet_destination =
                        Some(MagicPacketDestination::from(field_2.to_owned()));
                }
                Ok(line)
            }
            [field_1, field_2, field_3] => {
                let mut line = MacAddress::from_str(field_1)
                    .map_err(Self::Err::InvalidHardwareAddress)
                    .map(Self::new)?;
                match SecureOn::from_str(field_3) {
                    Ok(secure_on) => {
                        line.secure_on = Some(secure_on);
                        if let Ok(port) = u16::from_str(field_2) {
                            line.port = Some(port);
                        } else {
                            line.packet_destination =
                                Some(MagicPacketDestination::from(field_2.to_owned()));
                        }
                        Ok(line)
                    }
                    Err(error) if field_3.contains(['.', ':', '-']) => {
                        // If the 3rd field contains MAC address separators, it definitely can't be a valid numeric port,
                        // and is likely just an invalid SecureON password.
                        Err(Self::Err::InvalidSecureOn(3, error))
                    }
                    Err(_) => {
                        // If field 3 is not a SecureON password, then field 3 must be a port
                        line.packet_destination =
                            Some(MagicPacketDestination::from(field_2.to_owned()));
                        line.port = Some(
                            u16::from_str(field_3).map_err(|err| Self::Err::InvalidPort(3, err))?,
                        );
                        Ok(line)
                    }
                }
            }
            [field_1, field_2, field_3, field_4] => Ok(MacAddress::from_str(field_1)
                .map_err(Self::Err::InvalidHardwareAddress)
                .map(Self::new)?
                .with_packet_destination(Some(MagicPacketDestination::from(field_2.to_owned())))
                .with_port(Some(
                    u16::from_str(field_3).map_err(|err| Self::Err::InvalidPort(3, err))?,
                ))
                .with_secure_on(Some(
                    SecureOn::from_str(field_4)
                        .map_err(|error| Self::Err::InvalidSecureOn(4, error))?,
                ))),
            _ => Err(Self::Err::TooManyFields(parts.len())),
        }
    }
}

/// An invalid [`WakeUpTarget`] in an iterator over lines.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseLineError(usize, WakeUpTargetParseError);

impl ParseLineError {
    /// Create a new error.
    ///
    /// `line_no` denotes the 1-based number of the faulty line, and `error` is
    /// the error which occurred while parsing that line.
    #[must_use]
    pub fn new(line_no: usize, error: WakeUpTargetParseError) -> Self {
        Self(line_no, error)
    }

    /// The line number at which the error occurred.
    #[must_use]
    pub fn line_no(&self) -> usize {
        self.0
    }

    /// The error at this line.
    #[must_use]
    pub fn error(&self) -> &WakeUpTargetParseError {
        &self.1
    }
}

impl Display for ParseLineError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Line {}: {}", self.0, self.1)
    }
}

impl std::error::Error for ParseLineError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.1)
    }
}

fn parse_line(i: usize, line: &str) -> Option<Result<WakeUpTarget, ParseLineError>> {
    if line.trim().is_empty() || line.trim().starts_with('#') {
        None
    } else {
        Some(WakeUpTarget::from_str(line).map_err(|error| ParseLineError(i + 1, error)))
    }
}

/// Parse targets from an iterator over lines.
///
/// Ignore empty lines, or lines starting with `#`, and try to parse all other
/// lines as [`WakeUpTarget`]s.
///
/// Return an iterator over results from parsing lines, after ignoring empty
/// or comment lines.  Each item is either a parsed target, or an error which
/// occurred while parsing a line.
pub fn from_lines<I, S>(lines: I) -> impl Iterator<Item = Result<WakeUpTarget, ParseLineError>>
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    lines
        .into_iter()
        .enumerate()
        .filter_map(|(i, line)| parse_line(i, line.as_ref()))
}

/// Parse targets from lines read from a reader.
///
/// See [`from_lines`] for more information.
///
/// Return an iterator over results from parsing lines, after ignoring empty
/// or comment lines.  Each item is either a parsed target, or an error which
/// occurring while reading or parsing a line.
///
/// If a line fails to parse the [`ParseLineError`] is wrapped in an
/// [`std::io::Error`], with [`ErrorKind::InvalidData`].
pub fn from_reader<R: BufRead>(reader: R) -> impl Iterator<Item = Result<WakeUpTarget, Error>> {
    reader.lines().enumerate().filter_map(|(i, line)| {
        line.and_then(|line| {
            parse_line(i, &line)
                .transpose()
                .map_err(|error| Error::new(ErrorKind::InvalidData, error))
        })
        .transpose()
    })
}

#[cfg(test)]
mod tests {
    use std::{io::BufReader, net::IpAddr, str::FromStr};

    use crate::ParseErrorKind;

    use super::*;

    #[test]
    fn test_target_from_string_empty() {
        assert!(WakeUpTarget::from_str("").is_err());
        assert!(WakeUpTarget::from_str("        ").is_err());
        assert!(WakeUpTarget::from_str("\t").is_err());
    }

    #[test]
    fn test_target_from_string_hardware_address_only() {
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
        );
        assert_eq!(
            WakeUpTarget::from_str("12-13-14-15-16-17").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
        );
        assert_eq!(
            WakeUpTarget::from_str("  12:13:14:15:16:17  ").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
        );
        assert_eq!(
            WakeUpTarget::from_str("  jj:13:14:15:16:17  ").unwrap_err(),
            WakeUpTargetParseError::InvalidHardwareAddress(ParseError {
                kind: ParseErrorKind::InvalidByteLiteral
            })
        );
        assert_eq!(
            WakeUpTarget::from_str("  12:13:14:15:16:17:18  ").unwrap_err(),
            WakeUpTargetParseError::InvalidHardwareAddress(ParseError {
                kind: ParseErrorKind::TrailingBytes
            })
        );
    }

    #[test]
    fn test_target_from_string_hardware_address_and_destination() {
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 foo.example.com").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_dns_packet_destination("foo.example.com".into())
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 192.0.2.4").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_ip_packet_destination(IpAddr::from_str("192.0.2.4").unwrap())
        );
    }

    #[test]
    fn test_target_from_string_hardware_address_and_port() {
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 9").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_port(Some(9))
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 09").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_port(Some(9))
        );
    }

    #[test]
    fn test_target_from_string_hardware_address_and_secure_on() {
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 aa-bb-cc-dd-ee-ff").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_secure_on(Some(SecureOn::from([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])))
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 aa-bb-cc-dd-ee-f").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_dns_packet_destination("aa-bb-cc-dd-ee-f".into())
        );
    }

    #[test]
    fn test_target_from_string_hardware_address_and_host_and_port() {
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 foo.example.com 23").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_dns_packet_destination("foo.example.com".into())
                .with_port(Some(23))
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 192.0.2.4 23").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_ip_packet_destination(IpAddr::from_str("192.0.2.4").unwrap())
                .with_port(Some(23))
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 192.0.2.4 foo").unwrap_err(),
            WakeUpTargetParseError::InvalidPort(3, u16::from_str("foo").unwrap_err())
        );
    }

    #[test]
    fn test_target_from_string_hardware_address_and_host_and_secure_on() {
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 foo.example.com aa-bb-cc-dd-ee-ff").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_dns_packet_destination("foo.example.com".into())
                .with_secure_on(Some(SecureOn::from([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])))
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 192.0.2.4 aa-bb-cc-dd-ee-ff").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_ip_packet_destination(IpAddr::from_str("192.0.2.4").unwrap())
                .with_secure_on(Some(SecureOn::from([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])))
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 192.0.2.4 aa-bb-cc-dd-ee-f").unwrap_err(),
            WakeUpTargetParseError::InvalidSecureOn(
                3,
                ParseError {
                    kind: ParseErrorKind::InvalidByteLiteral
                }
            )
        );
    }

    #[test]
    fn test_target_from_string_hardware_address_and_port_and_secure_on() {
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 42 aa-bb-cc-dd-ee-ff").unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_port(Some(42))
                .with_secure_on(Some(SecureOn::from([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])))
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 42 aa-bb-cc-dd-ee-f").unwrap_err(),
            WakeUpTargetParseError::InvalidSecureOn(
                3,
                ParseError {
                    kind: ParseErrorKind::InvalidByteLiteral
                }
            )
        );
    }

    #[test]
    fn test_target_from_string_full() {
        let line =
            WakeUpTarget::from_str("12:13:14:15:16:17 192.0.2.42 42 aa-bb-cc-dd-ee-ff").unwrap();
        assert_eq!(
            line.hardware_address(),
            MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17])
        );
        assert_eq!(
            line.packet_destination(),
            Some(&MagicPacketDestination::Ip(
                IpAddr::from_str("192.0.2.42").unwrap()
            ))
        );
        assert_eq!(line.port(), Some(42));
        assert_eq!(
            line.secure_on(),
            Some(SecureOn([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]))
        );
    }

    #[test]
    fn test_line_from_string_too_many_fields() {
        assert_eq!(
            WakeUpTarget::from_str("a b c d e f g   ").unwrap_err(),
            WakeUpTargetParseError::TooManyFields(7)
        );
        assert_eq!(
            WakeUpTarget::from_str("12:13:14:15:16:17 192.0.2.42 42 aa-bb-cc-dd-ee-ff extra")
                .unwrap_err(),
            WakeUpTargetParseError::TooManyFields(5)
        );
    }

    #[test]
    fn test_from_lines() {
        let file = "# A test file

  # A bad line
12:13:14:15:16:17 192.0.2.42 42 aa-bb-cc-dd-ee-ff extra

# A good line
12:13:14:15:16:17 192.0.2.42 42 aa-bb-cc-dd-ee-ff

# A short line
12:13:14:15:16:17 23";
        let targets = from_lines(file.lines()).collect::<Vec<_>>();
        assert_eq!(
            targets,
            vec![
                Err(ParseLineError::new(
                    4,
                    WakeUpTargetParseError::TooManyFields(5)
                )),
                Ok(
                    WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                        .with_ip_packet_destination(IpAddr::from_str("192.0.2.42").unwrap())
                        .with_port(Some(42))
                        .with_secure_on(Some(SecureOn::from([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])))
                ),
                Ok(
                    WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                        .with_port(Some(23))
                )
            ]
        );
    }

    #[test]
    fn test_from_reader() {
        let file = "# A test file

  # A bad line
12:13:14:15:16:17 192.0.2.42 42 aa-bb-cc-dd-ee-ff extra

# A good line
12:13:14:15:16:17 192.0.2.42 42 aa-bb-cc-dd-ee-ff

# A short line
12:13:14:15:16:17 23";
        let reader = BufReader::new(file.as_bytes());
        let mut targets = from_reader(reader);
        let error = targets.next().unwrap().unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
        assert_eq!(
            *error
                .into_inner()
                .unwrap()
                .downcast::<ParseLineError>()
                .unwrap(),
            (ParseLineError::new(4, WakeUpTargetParseError::TooManyFields(5)))
        );
        assert_eq!(
            targets.next().unwrap().unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_ip_packet_destination(IpAddr::from_str("192.0.2.42").unwrap())
                .with_port(Some(42))
                .with_secure_on(Some(SecureOn::from([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])))
        );
        assert_eq!(
            targets.next().unwrap().unwrap(),
            WakeUpTarget::new(MacAddress::from([0x12, 0x13, 0x14, 0x15, 0x16, 0x17]))
                .with_port(Some(23))
        );
        assert!(targets.next().is_none());
    }
}