rust/chg/src/message.rs
author Yuya Nishihara <yuya@tcha.org>
Sat, 11 Apr 2020 00:21:37 +0900
changeset 44693 61fda2dbc522
parent 44685 90e05b304902
child 45620 426294d06ddc
permissions -rw-r--r--
rust-chg: leverage impl trait at argument position Differential Revision: https://phab.mercurial-scm.org/D8401
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     1
// Copyright 2018 Yuya Nishihara <yuya@tcha.org>
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     2
//
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     3
// This software may be used and distributed according to the terms of the
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     4
// GNU General Public License version 2 or any later version.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     5
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     6
//! Utility for parsing and building command-server messages.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     7
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
     8
use bytes::{BufMut, Bytes, BytesMut};
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     9
use std::error;
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    10
use std::ffi::{OsStr, OsString};
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    11
use std::io;
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    12
use std::os::unix::ffi::OsStrExt;
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    13
use std::path::PathBuf;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    14
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    15
pub use tokio_hglib::message::*; // re-exports
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    16
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    17
/// Shell command type requested by the server.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    18
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    19
pub enum CommandType {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    20
    /// Pager should be spawned.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    21
    Pager,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    22
    /// Shell command should be executed to send back the result code.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    23
    System,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    24
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    25
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    26
/// Shell command requested by the server.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    27
#[derive(Clone, Debug, Eq, PartialEq)]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    28
pub struct CommandSpec {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    29
    pub command: OsString,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    30
    pub current_dir: OsString,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    31
    pub envs: Vec<(OsString, OsString)>,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    32
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    33
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    34
/// Parses "S" channel request into command type and spec.
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    35
pub fn parse_command_spec(data: Bytes) -> io::Result<(CommandType, CommandSpec)> {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    36
    let mut split = data.split(|&c| c == b'\0');
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    37
    let ctype = parse_command_type(split.next().ok_or(new_parse_error("missing type"))?)?;
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    38
    let command = split.next().ok_or(new_parse_error("missing command"))?;
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    39
    let current_dir = split.next().ok_or(new_parse_error("missing current dir"))?;
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    40
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    41
    let mut envs = Vec::new();
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    42
    for l in split {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    43
        let mut s = l.splitn(2, |&c| c == b'=');
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    44
        let k = s.next().unwrap();
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    45
        let v = s.next().ok_or(new_parse_error("malformed env"))?;
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    46
        envs.push((
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    47
            OsStr::from_bytes(k).to_owned(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    48
            OsStr::from_bytes(v).to_owned(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    49
        ));
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    50
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    51
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    52
    let spec = CommandSpec {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    53
        command: OsStr::from_bytes(command).to_owned(),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    54
        current_dir: OsStr::from_bytes(current_dir).to_owned(),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    55
        envs: envs,
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    56
    };
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    57
    Ok((ctype, spec))
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    58
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    59
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    60
fn parse_command_type(value: &[u8]) -> io::Result<CommandType> {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    61
    match value {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    62
        b"pager" => Ok(CommandType::Pager),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    63
        b"system" => Ok(CommandType::System),
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    64
        _ => Err(new_parse_error(format!(
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    65
            "unknown command type: {}",
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    66
            decode_latin1(value)
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
    67
        ))),
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    68
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    69
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    70
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    71
/// Client-side instruction requested by the server.
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    72
#[derive(Clone, Debug, Eq, PartialEq)]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    73
pub enum Instruction {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    74
    Exit(i32),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    75
    Reconnect,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    76
    Redirect(PathBuf),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    77
    Unlink(PathBuf),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    78
}
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    79
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    80
/// Parses validation result into instructions.
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    81
pub fn parse_instructions(data: Bytes) -> io::Result<Vec<Instruction>> {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    82
    let mut instructions = Vec::new();
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    83
    for l in data.split(|&c| c == b'\0') {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    84
        if l.is_empty() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    85
            continue;
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    86
        }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    87
        let mut s = l.splitn(2, |&c| c == b' ');
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    88
        let inst = match (s.next().unwrap(), s.next()) {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    89
            (b"exit", Some(arg)) => decode_latin1(arg)
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    90
                .parse()
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    91
                .map(Instruction::Exit)
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    92
                .map_err(|_| new_parse_error(format!("invalid exit code: {:?}", arg)))?,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    93
            (b"reconnect", None) => Instruction::Reconnect,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    94
            (b"redirect", Some(arg)) => {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    95
                Instruction::Redirect(OsStr::from_bytes(arg).to_owned().into())
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    96
            }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    97
            (b"unlink", Some(arg)) => Instruction::Unlink(OsStr::from_bytes(arg).to_owned().into()),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    98
            _ => {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
    99
                return Err(new_parse_error(format!("unknown command: {:?}", l)));
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   100
            }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   101
        };
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   102
        instructions.push(inst);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   103
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   104
    Ok(instructions)
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   105
}
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   106
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   107
// allocate large buffer as environment variables can be quite long
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   108
const INITIAL_PACKED_ENV_VARS_CAPACITY: usize = 4096;
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   109
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   110
/// Packs environment variables of platform encoding into bytes.
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   111
///
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   112
/// # Panics
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   113
///
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   114
/// Panics if key or value contains `\0` character, or key contains '='
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   115
/// character.
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
   116
pub fn pack_env_vars_os(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
   117
    vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
   118
) -> Bytes {
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   119
    let mut vars_iter = vars.into_iter();
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   120
    if let Some((k, v)) = vars_iter.next() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   121
        let mut dst = BytesMut::with_capacity(INITIAL_PACKED_ENV_VARS_CAPACITY);
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   122
        pack_env_into(&mut dst, k.as_ref(), v.as_ref());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   123
        for (k, v) in vars_iter {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   124
            dst.reserve(1);
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   125
            dst.put_u8(b'\0');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   126
            pack_env_into(&mut dst, k.as_ref(), v.as_ref());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   127
        }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   128
        dst.freeze()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   129
    } else {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   130
        Bytes::new()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   131
    }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   132
}
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   133
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   134
fn pack_env_into(dst: &mut BytesMut, k: &OsStr, v: &OsStr) {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   135
    assert!(!k.as_bytes().contains(&0), "key shouldn't contain NUL");
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   136
    assert!(!k.as_bytes().contains(&b'='), "key shouldn't contain '='");
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   137
    assert!(!v.as_bytes().contains(&0), "value shouldn't contain NUL");
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   138
    dst.reserve(k.as_bytes().len() + 1 + v.as_bytes().len());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   139
    dst.put_slice(k.as_bytes());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   140
    dst.put_u8(b'=');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   141
    dst.put_slice(v.as_bytes());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   142
}
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   143
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
   144
fn decode_latin1(s: impl AsRef<[u8]>) -> String {
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   145
    s.as_ref().iter().map(|&c| c as char).collect()
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   146
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   147
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
   148
fn new_parse_error(error: impl Into<Box<dyn error::Error + Send + Sync>>) -> io::Error {
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   149
    io::Error::new(io::ErrorKind::InvalidData, error)
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   150
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   151
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   152
#[cfg(test)]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   153
mod tests {
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   154
    use super::*;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   155
    use std::os::unix::ffi::OsStringExt;
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   156
    use std::panic;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   157
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   158
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   159
    fn parse_command_spec_good() {
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   160
        let src = [
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   161
            b"pager".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   162
            b"less -FRX".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   163
            b"/tmp".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   164
            b"LANG=C".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   165
            b"HGPLAIN=".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   166
        ]
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   167
        .join(&0);
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   168
        let spec = CommandSpec {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   169
            command: os_string_from(b"less -FRX"),
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   170
            current_dir: os_string_from(b"/tmp"),
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   171
            envs: vec![
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   172
                (os_string_from(b"LANG"), os_string_from(b"C")),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   173
                (os_string_from(b"HGPLAIN"), os_string_from(b"")),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   174
            ],
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   175
        };
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   176
        assert_eq!(
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   177
            parse_command_spec(Bytes::from(src)).unwrap(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   178
            (CommandType::Pager, spec)
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   179
        );
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   180
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   181
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   182
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   183
    fn parse_command_spec_too_short() {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   184
        assert!(parse_command_spec(Bytes::from_static(b"")).is_err());
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   185
        assert!(parse_command_spec(Bytes::from_static(b"pager")).is_err());
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   186
        assert!(parse_command_spec(Bytes::from_static(b"pager\0less")).is_err());
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   187
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   188
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   189
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   190
    fn parse_command_spec_malformed_env() {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   191
        assert!(parse_command_spec(Bytes::from_static(b"pager\0less\0/tmp\0HOME")).is_err());
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   192
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   193
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   194
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   195
    fn parse_command_spec_unknown_type() {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   196
        assert!(parse_command_spec(Bytes::from_static(b"paper\0less")).is_err());
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   197
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   198
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   199
    #[test]
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   200
    fn parse_instructions_good() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   201
        let src = [
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   202
            b"exit 123".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   203
            b"reconnect".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   204
            b"redirect /whatever".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   205
            b"unlink /someother".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   206
        ]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   207
        .join(&0);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   208
        let insts = vec![
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   209
            Instruction::Exit(123),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   210
            Instruction::Reconnect,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   211
            Instruction::Redirect(path_buf_from(b"/whatever")),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   212
            Instruction::Unlink(path_buf_from(b"/someother")),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   213
        ];
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   214
        assert_eq!(parse_instructions(Bytes::from(src)).unwrap(), insts);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   215
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   216
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   217
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   218
    fn parse_instructions_empty() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   219
        assert_eq!(parse_instructions(Bytes::new()).unwrap(), vec![]);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   220
        assert_eq!(
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   221
            parse_instructions(Bytes::from_static(b"\0")).unwrap(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   222
            vec![]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   223
        );
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   224
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   225
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   226
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   227
    fn parse_instructions_malformed_exit_code() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   228
        assert!(parse_instructions(Bytes::from_static(b"exit foo")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   229
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   230
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   231
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   232
    fn parse_instructions_missing_argument() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   233
        assert!(parse_instructions(Bytes::from_static(b"exit")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   234
        assert!(parse_instructions(Bytes::from_static(b"redirect")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   235
        assert!(parse_instructions(Bytes::from_static(b"unlink")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   236
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   237
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   238
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   239
    fn parse_instructions_unknown_command() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   240
        assert!(parse_instructions(Bytes::from_static(b"quit 0")).is_err());
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   241
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   242
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   243
    #[test]
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   244
    fn pack_env_vars_os_good() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   245
        assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   246
            pack_env_vars_os(vec![] as Vec<(OsString, OsString)>),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   247
            Bytes::new()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   248
        );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   249
        assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   250
            pack_env_vars_os(vec![os_string_pair_from(b"FOO", b"bar")]),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   251
            Bytes::from_static(b"FOO=bar")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   252
        );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   253
        assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   254
            pack_env_vars_os(vec![
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   255
                os_string_pair_from(b"FOO", b""),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   256
                os_string_pair_from(b"BAR", b"baz")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   257
            ]),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   258
            Bytes::from_static(b"FOO=\0BAR=baz")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   259
        );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   260
    }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   261
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   262
    #[test]
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   263
    fn pack_env_vars_os_large_key() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   264
        let mut buf = vec![b'A'; INITIAL_PACKED_ENV_VARS_CAPACITY];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   265
        let envs = vec![os_string_pair_from(&buf, b"")];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   266
        buf.push(b'=');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   267
        assert_eq!(pack_env_vars_os(envs), Bytes::from(buf));
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   268
    }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   269
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   270
    #[test]
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   271
    fn pack_env_vars_os_large_value() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   272
        let mut buf = vec![b'A', b'='];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   273
        buf.resize(INITIAL_PACKED_ENV_VARS_CAPACITY + 1, b'a');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   274
        let envs = vec![os_string_pair_from(&buf[..1], &buf[2..])];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   275
        assert_eq!(pack_env_vars_os(envs), Bytes::from(buf));
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   276
    }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   277
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   278
    #[test]
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   279
    fn pack_env_vars_os_nul_eq() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   280
        assert!(panic::catch_unwind(|| {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   281
            pack_env_vars_os(vec![os_string_pair_from(b"\0", b"")])
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   282
        })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   283
        .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   284
        assert!(panic::catch_unwind(|| {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   285
            pack_env_vars_os(vec![os_string_pair_from(b"FOO", b"\0bar")])
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   286
        })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   287
        .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   288
        assert!(panic::catch_unwind(|| {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   289
            pack_env_vars_os(vec![os_string_pair_from(b"FO=", b"bar")])
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   290
        })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   291
        .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   292
        assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   293
            pack_env_vars_os(vec![os_string_pair_from(b"FOO", b"=ba")]),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   294
            Bytes::from_static(b"FOO==ba")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   295
        );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   296
    }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   297
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   298
    fn os_string_from(s: &[u8]) -> OsString {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   299
        OsString::from_vec(s.to_vec())
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   300
    }
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   301
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   302
    fn os_string_pair_from(k: &[u8], v: &[u8]) -> (OsString, OsString) {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   303
        (os_string_from(k), os_string_from(v))
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   304
    }
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   305
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   306
    fn path_buf_from(s: &[u8]) -> PathBuf {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   307
        os_string_from(s).into()
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   308
    }
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   309
}