rust/chg/src/message.rs
author Yuya Nishihara <yuya@tcha.org>
Tue, 31 Mar 2020 23:25:26 +0900
changeset 44685 90e05b304902
parent 44679 82adc720c0a3
child 44693 61fda2dbc522
permissions -rw-r--r--
rust-chg: silence warning about dated coding style Differential Revision: https://phab.mercurial-scm.org/D8384
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.
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   116
pub fn pack_env_vars_os<I, P>(vars: I) -> Bytes
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   117
where
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   118
    I: IntoIterator<Item = (P, P)>,
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   119
    P: AsRef<OsStr>,
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   120
{
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   121
    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
   122
    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
   123
        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
   124
        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
   125
        for (k, v) in vars_iter {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   126
            dst.reserve(1);
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   127
            dst.put_u8(b'\0');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   128
            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
   129
        }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   130
        dst.freeze()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   131
    } else {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   132
        Bytes::new()
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
}
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   135
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   136
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
   137
    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
   138
    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
   139
    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
   140
    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
   141
    dst.put_slice(k.as_bytes());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   142
    dst.put_u8(b'=');
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   143
    dst.put_slice(v.as_bytes());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   144
}
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   145
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   146
fn decode_latin1<S>(s: S) -> String
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   147
where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   148
    S: AsRef<[u8]>,
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   149
{
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   150
    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
   151
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   152
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   153
fn new_parse_error<E>(error: E) -> io::Error
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   154
where
44685
90e05b304902 rust-chg: silence warning about dated coding style
Yuya Nishihara <yuya@tcha.org>
parents: 44679
diff changeset
   155
    E: Into<Box<dyn error::Error + Send + Sync>>,
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   156
{
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   157
    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
   158
}
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   159
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   160
#[cfg(test)]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   161
mod tests {
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   162
    use super::*;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   163
    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
   164
    use std::panic;
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   165
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   166
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   167
    fn parse_command_spec_good() {
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   168
        let src = [
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   169
            b"pager".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   170
            b"less -FRX".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   171
            b"/tmp".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   172
            b"LANG=C".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   173
            b"HGPLAIN=".as_ref(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   174
        ]
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   175
        .join(&0);
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   176
        let spec = CommandSpec {
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   177
            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
   178
            current_dir: os_string_from(b"/tmp"),
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   179
            envs: vec![
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   180
                (os_string_from(b"LANG"), os_string_from(b"C")),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   181
                (os_string_from(b"HGPLAIN"), os_string_from(b"")),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   182
            ],
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   183
        };
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   184
        assert_eq!(
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   185
            parse_command_spec(Bytes::from(src)).unwrap(),
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   186
            (CommandType::Pager, spec)
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39971
diff changeset
   187
        );
39971
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
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   190
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   191
    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
   192
        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
   193
        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
   194
        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
   195
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   196
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   197
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   198
    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
   199
        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
   200
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   201
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   202
    #[test]
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   203
    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
   204
        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
   205
    }
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   206
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   207
    #[test]
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   208
    fn parse_instructions_good() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   209
        let src = [
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   210
            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
   211
            b"reconnect".as_ref(),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   212
            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
   213
            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
   214
        ]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   215
        .join(&0);
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   216
        let insts = vec![
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   217
            Instruction::Exit(123),
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   218
            Instruction::Reconnect,
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   219
            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
   220
            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
   221
        ];
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   222
        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
   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
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   226
    fn parse_instructions_empty() {
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   227
        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
   228
        assert_eq!(
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   229
            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
   230
            vec![]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   231
        );
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   232
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   233
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   234
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   235
    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
   236
        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
   237
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   238
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   239
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   240
    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
   241
        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
   242
        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
   243
        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
   244
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   245
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   246
    #[test]
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   247
    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
   248
        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
   249
    }
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   250
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   251
    #[test]
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   252
    fn pack_env_vars_os_good() {
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![] as Vec<(OsString, OsString)>),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   255
            Bytes::new()
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   256
        );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   257
        assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   258
            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
   259
            Bytes::from_static(b"FOO=bar")
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
        assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   262
            pack_env_vars_os(vec![
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   263
                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
   264
                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
   265
            ]),
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   266
            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
   267
        );
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_key() {
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'; INITIAL_PACKED_ENV_VARS_CAPACITY];
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   273
        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
   274
        buf.push(b'=');
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_large_value() {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   280
        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
   281
        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
   282
        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
   283
        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
   284
    }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   285
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   286
    #[test]
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   287
    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
   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"\0", b"")])
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!(panic::catch_unwind(|| {
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"\0bar")])
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   294
        })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   295
        .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   296
        assert!(panic::catch_unwind(|| {
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   297
            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
   298
        })
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   299
        .is_err());
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   300
        assert_eq!(
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   301
            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
   302
            Bytes::from_static(b"FOO==ba")
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   303
        );
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   304
    }
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   305
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   306
    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
   307
        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
   308
    }
44674
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   309
8a7beeea655f rust-chg: add helper to pack environment variables
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
   310
    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
   311
        (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
   312
    }
44679
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   313
82adc720c0a3 rust-chg: add helper to parse instructions sent from server
Yuya Nishihara <yuya@tcha.org>
parents: 44674
diff changeset
   314
    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
   315
        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
   316
    }
39971
b1d8acd82d60 rust-chg: add parser for request messages sent to "S" channel
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   317
}