firetrap 0.1.0

Modern, safe and extensible FTP server library for Rust
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
extern crate std;
extern crate bytes;

use failure::*;

use std::{fmt, result};
use self::bytes::{Bytes};

/// The parameter the can be given to the `STRU` command. It is used to set the file `STRU`cture to
/// the given structure given. This stems from a time where it was common for some operating
/// systems to address i.e. particular records in files, but isn't used a lot these days. We
/// support the command itself for legacy reasons, but will only support the `File` structure.
// Unfortunately Rust doesn't support anonymous enums for now, so we'll have to do with explicit
// command parameter enums for the commands that take mutually exclusive parameters.
#[derive(Debug, PartialEq, Clone)]
pub enum StruParam {
    /// "Regular" file structure.
    File,
    /// Files are structured in "Records".
    Record,
    /// Files are structured in "Pages".
    Page,
}

/// The parameter that can be given to the `MODE` command. The `MODE` command is obsolete, and we
/// only support the `Stream` mode. We still have to support the command itself for compatibility
/// reasons, though.
#[derive(Debug, PartialEq, Clone)]
pub enum ModeParam {
    /// Data is sent in a continuous stream of bytes.
    Stream,
    /// Data is sent as a series of blocks preceded by one or more header bytes.
    Block,
    /// Some round-about way of sending compressed data.
    Compressed,
}

/// The parameter that can be given to the `OPTS` command, specifying the option the client wants
/// to set.
#[derive(Debug, PartialEq, Clone)]
pub enum Opt {
    /// The client wants us to enable UTF-8 encoding for file paths and such.
    UTF8,
}

#[derive(Debug, PartialEq, Clone)]
/// The FTP commands.
// TODO: Write a short description of what the command should do according to the FTP spec in the
// docstring.
pub enum Command {
    /// The `USER` command
    User {
        /// The bytes making up the actual username.
        // Ideally I'd like to immediately convert the username to a valid UTF8 `&str`, because
        // that's part of the semantics of the `User` struct, and thus should be part of parsing.
        // Unfortunately though, that would mean the `Command` enum would become generic over
        // lifetimes and for ergonomic reasons I want to avoid that ATM.
        // TODO: Reconsider when NLL have been merged into stable.
        username: Bytes,
    },
    /// The `PASS` command
    Pass {
        /// The bytes making up the actual password.
        password: Bytes,
    },
    /// The `ACCT` command
    Acct {
        /// The bytes making up the account about which information is requested.
        account: Bytes,
    },
    /// The `SYST` command
    Syst,
    /// The `STAT` command
    Stat {
        /// The bytes making up the path about which information is requested, if given.
        path: Option<Bytes>,
    },
    /// The `TYPE` command
    Type,
    /// The `STRU` command
    Stru {
        /// The structure to which the client would like to switch. Only the `File` structure is
        /// supported by us.
        structure: StruParam,
    },
    /// The `MODE` command
    Mode {
        /// The transfer mode to which the client would like to switch. Only the `Stream` mode is
        /// supported by us.
        mode: ModeParam,
    },
    /// The `HELP` command
    Help,
    /// The `NOOP` command
    Noop,
    /// The `PASSV` command
    Pasv,
    /// The `PORT` command
    Port,
    /// The `RETR` command
    Retr {
        /// The path to the file the client would like to retrieve.
        path: String,
    },
    /// The `STOR` command
    Stor {
        /// The path to the file the client would like to store.
        path: String
    },
    /// The `LIST` command
    List {
        /// The path of the file/directory the clients wants to list
        path: Option<String>,
    },
    /// The `NLST` command
    Nlst {
        /// The path of the file/directory the clients wants to list.
        path: Option<String>
    },
    /// The `FEAT` command
    Feat,
    /// The `PWD` command
    Pwd,
    /// The `CWD` command
    Cwd {
        /// The path the client would like to change directory to.
        path: std::path::PathBuf,
    },
    /// The `CDUP` command
    Cdup,
    /// The `OPTS` command
    Opts {
        /// The option the client wants to set
        option: Opt
    },
    /// The `DELE` command
    Dele {
        /// The (regular) file to delete.
        path: String,
    },
    /// The `QUIT` command
    Quit,
    /// The `MKD` command
    Mkd {
        /// The path to the directory the client wants to create.
        path: std::path::PathBuf,
    },
}

impl Command {
    /// Parse the given bytes into a [`Command`].
    ///
    /// [`Command`]: ./enum.Command.html
    pub fn parse<T: AsRef<[u8]> + Into<Bytes>>(buf: T) -> Result<Command> {
        let vec = buf.into().to_vec();
        let mut iter = vec.splitn(2, |&b| b == b' ' || b == b'\r' || b == b'\n');
        let cmd_token = iter.next().unwrap();
        let cmd_params = iter.next().unwrap_or(&[]);

        // TODO: Make command parsing case insensitive (consider using "nom")
        let cmd = match cmd_token {
            b"USER" | b"user" => {
                let username = parse_to_eol(cmd_params)?;
                Command::User{
                    username,
                }
            },
            b"PASS" | b"pass" => {
                let password = parse_to_eol(cmd_params)?;
                Command::Pass{
                    password,
                }
            }
            b"ACCT" | b"acct" => {
                let account = parse_to_eol(cmd_params)?;
                Command::Acct{
                    account,
                }
            }
            b"SYST" | b"syst" => Command::Syst,
            b"STAT" => {
                let params = parse_to_eol(cmd_params)?;
                let path = if !params.is_empty() { Some(params) } else { None };
                Command::Stat{path}
            },
            b"TYPE" | b"type" => {
                // We don't care about text format conversion, so we'll ignore the params and we're
                // just always in binary mode.
                Command::Type
            },
            b"STRU" | b"stru" => {
                let params = parse_to_eol(cmd_params)?;
                if params.len() > 1 {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                match params.first() {
                    Some(b'F') => Command::Stru{structure: StruParam::File},
                    Some(b'R') => Command::Stru{structure: StruParam::Record},
                    Some(b'P') => Command::Stru{structure: StruParam::Page},
                    _ => return Err(ParseErrorKind::InvalidCommand)?,
                }
            },
            b"MODE" | b"mode" => {
                let params = parse_to_eol(cmd_params)?;
                if params.len() > 1 {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                match params.first() {
                    Some(b'S') => Command::Mode{mode: ModeParam::Stream},
                    Some(b'B') => Command::Mode{mode: ModeParam::Block},
                    Some(b'C') => Command::Mode{mode: ModeParam::Compressed},
                    _ => return Err(ParseErrorKind::InvalidCommand)?,
                }
            },
            b"HELP" | b"help" => Command::Help,
            b"NOOP" | b"noop" => {
                let params = parse_to_eol(cmd_params)?;
                if !params.is_empty() {
                    // NOOP params are prohibited
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                Command::Noop
            },
            b"PASV" | b"pasv" => {
                let params = parse_to_eol(cmd_params)?;
                if !params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                Command::Pasv
            },
            b"PORT" | b"port" => {
                let params = parse_to_eol(cmd_params)?;
                if params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                Command::Port
            },
            b"RETR" | b"retr" => {
                let path = parse_to_eol(cmd_params)?;
                if path.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                let path = String::from_utf8_lossy(&path);
                // TODO: Can we do this without allocation?
                Command::Retr{path: path.to_string()}
            },
            b"STOR" | b"stor" => {
                let path = parse_to_eol(cmd_params)?;
                if path.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                // TODO:: Can we do this without allocation?
                let path = String::from_utf8_lossy(&path);
                Command::Stor{path: path.to_string()}
            },
            b"LIST" | b"list" => {
                let path = parse_to_eol(cmd_params)?;
                let path = if path.is_empty() { None } else { Some(String::from_utf8_lossy(&path).to_string()) };
                Command::List{path: path}
            },
            b"NLST" | b"nlst" => {
                let path = parse_to_eol(cmd_params)?;
                let path = if path.is_empty() { None } else { Some(String::from_utf8_lossy(&path).to_string()) };
                Command::Nlst{path: path}
            },
            b"FEAT" |b"feat" => {
                let params = parse_to_eol(cmd_params)?;
                if !params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                Command::Feat
            },
            b"PWD" | b"XPWD" | b"pwd" | b"xpwd" => {
                let params = parse_to_eol(cmd_params)?;
                if !params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                Command::Pwd
            },
            b"CWD" | b"XCWD" | b"cwd" | b"xcwd" => {
                let path = parse_to_eol(cmd_params)?;
                if path.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                let path = String::from_utf8_lossy(&path).to_string();
                let path = path.into();
                Command::Cwd{path}
            },
            b"CDUP" | b"cdup" => {
                let params = parse_to_eol(cmd_params)?;
                if !params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?;
                }
                Command::Cdup
            },
            b"OPTS" | b"opts" => {
                let params = parse_to_eol(cmd_params)?;
                if params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?
                }

                match &params[..] {
                    b"UTF8"  => Command::Opts{option: Opt::UTF8},
                    _       => return Err(ParseErrorKind::InvalidCommand)?,
                }
            },
            b"DELE" | b"dele" => {
                let path = parse_to_eol(cmd_params)?;
                if path.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?
                }

                let path = String::from_utf8_lossy(&path).to_string();
                let path = path.into();
                Command::Dele{path}

            },
            b"QUIT" | b"quit" => {
                let params = parse_to_eol(cmd_params)?;
                if !params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?
                }

                Command::Quit
            },
            b"MKD" | b"XMKD" => {
                let params = parse_to_eol(cmd_params)?;
                if params.is_empty() {
                    return Err(ParseErrorKind::InvalidCommand)?
                }

                let path = String::from_utf8_lossy(&params).to_string();
                let path = path.into();
                Command::Mkd{path}
            },
            _ => return Err(ParseErrorKind::UnknownCommand{command: std::str::from_utf8(cmd_token).context(ParseErrorKind::InvalidUTF8)?.to_string()})?,
        };

        Ok(cmd)
    }
}

/// Try to parse a buffer of bytes, upto end of line into a `&str`.
fn parse_to_eol<T: AsRef<[u8]> + Into<Bytes>>(bytes: T) -> Result<Bytes> {
    let mut pos: usize = 0;
    let mut bytes: Bytes = bytes.into();
    let copy = bytes.clone();
    let mut iter = copy.as_ref().iter();

    loop {
        let b = match iter.next() {
            Some(b) => b,
            _ => return Err(ParseErrorKind::InvalidEOL)?,
        };

        if *b == b'\r' {
            match iter.next() {
                Some(b'\n') => return Ok(bytes.split_to(pos)),
                _ => return Err(ParseErrorKind::InvalidEOL)?,
            }
        }

        if *b == b'\n' {
            return Ok(bytes.split_to(pos));
        }

        if !is_valid_token_char(*b) {
            return Err(ParseErrorKind::InvalidToken{token: *b})?;
        }

        // We don't have to be afraid of an overflow here, since a `Bytes` can never be bigger than
        // `std::usize::MAX`
        pos += 1;
    }
}

fn is_valid_token_char(b: u8) -> bool {
    b > 0x1F && b < 0x7F
}

/// The error type returned by the [Command::parse] method.
///
/// [Command::parse]: ./enum.Command.html#method.parse
#[derive(Debug)]
pub struct ParseError {
    inner: Context<ParseErrorKind>,
}

impl PartialEq for ParseError {
    #[inline]
    fn eq(&self, other: &ParseError) -> bool {
        self.kind() == other.kind()
    }
}

/// A list specifying categories of Parse errors. It is meant to be used with the [ParseError]
/// type.
///
/// [ParseError]: ./struct.ParseError.html
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum ParseErrorKind {
    /// The client issued a command that we don't know about.
    #[fail(display = "Unknown command: {}", command)]
    UnknownCommand {
        /// The command that we don't know about.
        command: String,
    },
    /// The client issued an invalid command (e.g. required parameters are missing).
    #[fail(display = "Invalid command")]
    InvalidCommand,
    /// An invalid token (e.g. not UTF-8) was encountered while parsing the command.
    #[fail(display = "Invalid token while parsing: {}", token)]
    InvalidToken{
        /// The Token that is not UTF-8 encoded.
        token: u8,
    },
    /// Non-UTF8 character encountered.
    #[fail(display = "Non-UTF8 character while parsing")]
    InvalidUTF8,
    /// Invalid end-of-line character.
    #[fail(display = "Invalid end-of-line")]
    InvalidEOL,
}

impl Fail for ParseError {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl ParseError {
    /// Returns the corresponding `ParseErrorKind` for this error.
    pub fn kind(&self) -> &ParseErrorKind {
        self.inner.get_context()
    }
}

impl From<ParseErrorKind> for ParseError {
    fn from(kind: ParseErrorKind) -> ParseError {
        ParseError { inner: Context::new(kind) }
    }
}

impl From<Context<ParseErrorKind>> for ParseError {
    fn from(inner: Context<ParseErrorKind>) -> ParseError {
        ParseError { inner: inner }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

/// The Result type used in this module.
pub type Result<T> = result::Result<T, ParseError>;

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

    #[test]
    fn parse_user_cmd_crnl() {
        let input = "USER Dolores\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::User{username: "Dolores".into()});
    }

    #[test]
    // TODO: According to RFC 959, verbs should be interpreted without regards to case
    fn parse_user_cmd_mixed_case() {
        let input = "uSeR Dolores\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::UnknownCommand{command: "uSeR".into() })}));
    }

    #[test]
    fn parse_user_lowercase() {
        let input = "user Dolores\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::User{username: "Dolores".into()});
    }

    #[test]
    // Not all clients include the (actually mandatory) '\r'
    fn parse_user_cmd_nl(){
        let input = "USER Dolores\n";
        assert_eq!(Command::parse(input).unwrap(), Command::User{username: "Dolores".into()});
    }

    #[test]
    // Although we accept requests ending in only '\n', we won't accept requests ending only in '\r'
    fn parse_user_cmd_cr() {
        let input = "USER Dolores\r";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidEOL)}));
    }

    #[test]
    // We should fail if the request does not end in '\n' or '\r'
    fn parse_user_cmd_no_eol() {
        let input = "USER Dolores";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidEOL)}));
    }

    #[test]
    // We should skip only one space after a token, to allow for tokens starting with a space.
    fn parse_user_cmd_double_space(){
        let input = "USER  Dolores\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::User{username: " Dolores".into()});
    }

    #[test]
    fn parse_user_cmd_whitespace() {
        let input = "USER Dolores Abernathy\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::User{username: "Dolores Abernathy".into()});
    }

    #[test]
    fn parse_pass_cmd_crnl() {
        let input = "PASS s3cr3t\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Pass{password: "s3cr3t".into()});
    }

    #[test]
    fn parse_pass_cmd_whitespace() {
        let input = "PASS s3cr#t p@S$w0rd\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Pass{password: "s3cr#t p@S$w0rd".into()});
    }

    #[test]
    fn parse_acct() {
        let input = "ACCT Teddy\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Acct{account: "Teddy".into()});
    }

    #[test]
    fn parse_stru_no_params() {
        let input = "STRU\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_stru_f() {
        let input = "STRU F\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Stru{structure: StruParam::File});
    }

    #[test]
    fn parse_stru_r() {
        let input = "STRU R\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Stru{structure: StruParam::Record});
    }

    #[test]
    fn parse_stru_p() {
        let input = "STRU P\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Stru{structure: StruParam::Page});
    }

    #[test]
    fn parse_stru_garbage() {
        let input = "STRU FSK\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "STRU F lskdjf\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "STRU\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_mode_s() {
        let input = "MODE S\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Mode{mode: ModeParam::Stream});
    }

    #[test]
    fn parse_mode_b() {
        let input = "MODE B\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Mode{mode: ModeParam::Block});
    }

    #[test]
    fn parse_mode_c() {
        let input = "MODE C\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Mode{mode: ModeParam::Compressed});
    }

    #[test]
    fn parse_mode_garbage() {
        let input = "MODE SKDJF\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "MODE\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "MODE S D\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_help() {
        let input = "HELP\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Help);

        let input = "HELP bla\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Help);
    }

    #[test]
    fn parse_noop() {
        let input = "NOOP\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Noop);

        let input = "NOOP bla\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_pasv() {
        let input = "PASV\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Pasv);

        let input = "PASV bla\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_port() {
        let input = "PORT\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "PORT a1,a2,a3,a4,p1,p2\r\n";
        assert_eq!(Command::parse(input).unwrap(), Command::Port);
    }

    #[test]
    fn parse_list() {
        let input = "LIST\r\n";
        assert_eq!(Command::parse(input), Ok(Command::List{path: None}));

        let input = "LIST tmp\r\n";
        let expected_path = Some("tmp".to_string());
        assert_eq!(Command::parse(input), Ok(Command::List{path: expected_path}));
    }

    #[test]
    fn parse_feat() {
        let input = "FEAT\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Feat));

        let input = "FEAT bla\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_pwd() {
        let input = "PWD\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Pwd));

        let input = "PWD bla\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_cwd() {
        let input = "CWD\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "CWD /tmp\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Cwd{path: "/tmp".into()}));

        let input = "CWD public\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Cwd{path: "public".into()}));
    }

    #[test]
    fn parse_cdup() {
        let input = "CDUP\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Cdup));

        let input = "CDUP bla\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_opts() {
        let input = "OPTS\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "OPTS bla\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "OPTS UTF8\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Opts{option: Opt::UTF8}));
    }

    #[test]
    fn parse_dele() {
        let input = "DELE\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "DELE some_file\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Dele{path: "some_file".into()}));
    }

    #[test]
    fn parse_quit() {
        let input = "QUIT\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Quit));

        let input = "QUIT NOW\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));
    }

    #[test]
    fn parse_mkd() {
        let input = "MKD\r\n";
        assert_eq!(Command::parse(input), Err(ParseError{inner: Context::new(ParseErrorKind::InvalidCommand)}));

        let input = "MKD bla\r\n";
        assert_eq!(Command::parse(input), Ok(Command::Mkd{path: "bla".into()}));
    }
}