bdrck 0.22.5

Generic common foundational utilities.
Documentation
// Copyright 2015 Axel Rasmussen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// These tests exercise `src/crypto/compat.rs`. The `compat` module itself is
// a private module inside `crypto`, so we drive it indirectly through its
// public wrappers `digest::Salt` and `key::Nonce`.

use crate::crypto::digest::Salt;
use crate::crypto::key::Nonce;

#[test]
fn test_nonce_new_is_zero() {
    crate::init().unwrap();

    let n = Nonce::new();
    assert!(n.as_bytes().iter().all(|b| *b == 0));
}

#[test]
fn test_nonce_debug_format_is_hex() {
    crate::init().unwrap();

    // The inner compat::Nonce formats as a bare lowercase-hex string of its
    // bytes; the outer struct's derived Debug wraps that with
    // `Nonce { nonce: ... }`. A zeroed nonce therefore stringifies to the
    // expected prefix + NONCE_BYTES * 2 '0' characters + suffix.
    let n = Nonce::new();
    let s = format!("{:?}", n);
    assert!(s.starts_with("Nonce { nonce: "), "got {:?}", s);
    assert!(s.ends_with(" }"), "got {:?}", s);
    // 24-byte NONCE_BYTES => 48 hex characters, all zero.
    assert!(
        s.contains(&"0".repeat(48)),
        "expected 48 zero hex chars in {:?}",
        s
    );
}

#[test]
fn test_salt_serde_round_trip() {
    crate::init().unwrap();

    let salt = Salt::default();
    let bytes = rmp_serde::to_vec(&salt).unwrap();
    let back: Salt = rmp_serde::from_slice(&bytes).unwrap();
    assert_eq!(salt, back);
}

#[test]
fn test_salt_deserialize_wrong_length_bytes() {
    crate::init().unwrap();

    // A MessagePack bin whose length doesn't match SALT_BYTES hits the
    // compat visit_bytes error arm.
    let too_short: Vec<u8> = vec![0u8; 5];
    let bytes = rmp_serde::to_vec(&too_short).unwrap();
    assert!(rmp_serde::from_slice::<Salt>(&bytes).is_err());

    let too_long: Vec<u8> = vec![0u8; 100];
    let bytes = rmp_serde::to_vec(&too_long).unwrap();
    assert!(rmp_serde::from_slice::<Salt>(&bytes).is_err());
}

#[test]
fn test_salt_deserialize_wrong_length_array() {
    crate::init().unwrap();

    // Hand-craft a MessagePack array16 of 31 u8 zeros. SALT_BYTES is 32, so
    // this drives the visit_seq short-length regression added in A8.
    let mut buf: Vec<u8> = Vec::new();
    buf.push(0xdc);
    buf.extend_from_slice(&31u16.to_be_bytes());
    buf.extend(std::iter::repeat(0u8).take(31));
    assert!(rmp_serde::from_slice::<Salt>(&buf).is_err());

    // Likewise for an overlong array (33 elements).
    let mut buf: Vec<u8> = Vec::new();
    buf.push(0xdc);
    buf.extend_from_slice(&33u16.to_be_bytes());
    buf.extend(std::iter::repeat(0u8).take(33));
    assert!(rmp_serde::from_slice::<Salt>(&buf).is_err());
}