Clean up no_std and use of std in test code

This fixes a wedge of instances of:

```
warning: the item `String` is imported redundantly
  --> rustls/src/msgs/handshake.rs:27:5
   |
27 | use alloc::string::String;
   |     ^^^^^^^^^^^^^^^^^^^^^
```

Where `String` is present from the std prelude when built
for testing.  Like we just did in webpki, _always_ opt-in
to no_std, and then import the std prelude in tests where
necessary.
This commit is contained in:
Joseph Birr-Pixton 2024-02-27 14:37:39 +00:00 committed by Joe Birr-Pixton
parent d022089471
commit 2590759da7
27 changed files with 42 additions and 7 deletions

View File

@ -42,6 +42,8 @@ impl<'a> fmt::Debug for BsDebug<'a> {
#[cfg(test)]
mod tests {
use super::BsDebug;
use std::format;
use std::prelude::v1::*;
#[test]
fn debug() {

View File

@ -229,6 +229,7 @@ mod tests {
use crate::msgs::persist::Tls13ClientSessionValue;
use crate::suites::SupportedCipherSuite;
use crate::test_provider::cipher_suite;
use alloc::vec::Vec;
use pki_types::{ServerName, UnixTime};

View File

@ -103,6 +103,8 @@ impl ActiveKeyExchange for KeyExchange {
#[cfg(test)]
mod tests {
use std::format;
#[test]
fn kxgroup_fmt_yields_name() {
assert_eq!("X25519", format!("{:?}", super::X25519));

View File

@ -187,6 +187,7 @@ mod tests {
use crate::test_provider::tls13::{
TLS13_AES_128_GCM_SHA256_INTERNAL, TLS13_CHACHA20_POLY1305_SHA256_INTERNAL,
};
use std::dbg;
fn test_short_packet(version: Version, expected: &[u8]) {
const PN: u64 = 654360564;

View File

@ -243,6 +243,7 @@ pub struct OutputLengthError;
mod tests {
use super::{expand, Hkdf, HkdfUsingHmac};
use crate::test_provider::hmac;
use std::prelude::v1::*;
struct ByteArray<const N: usize>([u8; N]);

View File

@ -580,6 +580,8 @@ impl StdError for OtherError {
mod tests {
use super::{Error, InvalidMessage};
use crate::error::{CertRevocationListError, OtherError};
use std::prelude::v1::*;
use std::{println, vec};
#[test]
fn certificate_error_equality() {

View File

@ -318,7 +318,7 @@
#![cfg_attr(read_buf, feature(read_buf))]
#![cfg_attr(read_buf, feature(core_io_borrowed_buf))]
#![cfg_attr(bench, feature(test))]
#![cfg_attr(not(test), no_std)]
#![no_std]
extern crate alloc;
// This `extern crate` plus the `#![no_std]` attribute changes the default prelude from
@ -326,7 +326,6 @@ extern crate alloc;
// is in `std::prelude` but not in `core::prelude`. This helps maintain no-std support as even
// developers that are not interested in, or aware of, no-std support and / or that never run
// `cargo build --no-default-features` locally will get errors when they rely on `std::prelude` API.
#[cfg(not(test))]
extern crate std;
// Import `test` sysroot crate for `Bencher` definitions.

View File

@ -117,6 +117,8 @@ where
#[cfg(test)]
mod tests {
use std::prelude::v1::*;
type Test = super::LimitedCache<String, usize>;
#[test]

View File

@ -317,6 +317,8 @@ impl<'a> Drop for LengthPrefixedBuffer<'a> {
#[cfg(test)]
mod tests {
use super::*;
use std::prelude::v1::*;
use std::vec;
#[test]
fn interrupted_length_prefixed_buffer_leaves_maximum_length() {

View File

@ -577,7 +577,8 @@ const READ_SIZE: usize = 4096;
#[cfg(test)]
mod tests {
use std::io;
use std::prelude::v1::*;
use std::vec;
use crate::msgs::message::Message;

View File

@ -339,7 +339,7 @@ pub(crate) mod tests {
//! check panic-safety of relatively unused values.
use super::*;
use crate::msgs::codec::Codec;
use std::prelude::v1::*;
#[test]
fn test_enums() {

View File

@ -72,6 +72,7 @@ mod tests {
use crate::enums::ProtocolVersion;
use crate::msgs::base::Payload;
use crate::msgs::message::{BorrowedPlainMessage, PlainMessage};
use std::prelude::v1::*;
fn msg_eq(
m: &BorrowedPlainMessage,

View File

@ -21,6 +21,9 @@ use crate::verify::DigitallySignedStruct;
use pki_types::{CertificateDer, DnsName};
use std::prelude::v1::*;
use std::{format, println, vec};
#[test]
fn rejects_short_random() {
let bytes = [0x01; 31];

View File

@ -6,9 +6,10 @@ use super::codec::Reader;
use super::enums::AlertLevel;
use super::message::{Message, OpaqueMessage, PlainMessage};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::prelude::v1::*;
use std::{format, fs, println, vec};
#[test]
fn test_read_fuzz_corpus() {

View File

@ -411,6 +411,7 @@ mod tests {
#[test]
fn serversessionvalue_is_debug() {
use std::{println, vec};
let ssv = ServerSessionValue::new(
None,
ProtocolVersion::TLSv1_3,

View File

@ -897,9 +897,9 @@ impl Default for Version {
#[cfg(test)]
mod tests {
use crate::quic::HeaderProtectionKey;
use super::PacketKey;
use crate::quic::HeaderProtectionKey;
use std::prelude::v1::*;
#[test]
fn auto_traits() {

View File

@ -234,6 +234,7 @@ pub(crate) struct Decrypted {
#[cfg(test)]
mod tests {
use super::*;
use std::vec;
#[test]
fn test_has_decrypted() {

View File

@ -207,6 +207,7 @@ mod tests {
use crate::server::ProducesTickets;
use crate::server::ResolvesServerCert;
use crate::server::StoresServerSessions;
use std::vec;
#[test]
fn test_noserversessionstorage_drops_put() {

View File

@ -866,6 +866,7 @@ impl crate::conn::SideData for ServerConnectionData {}
#[cfg(test)]
mod tests {
use super::*;
use std::format;
// these branches not reachable externally, unless something else goes wrong.
#[test]

View File

@ -252,6 +252,7 @@ mod tests {
use super::*;
use crate::enums::CipherSuite;
use crate::test_provider::tls13::*;
use std::{println, vec};
#[test]
fn test_client_pref() {

View File

@ -831,6 +831,8 @@ where
#[cfg(all(test, any(feature = "ring", feature = "aws_lc_rs")))]
mod tests {
use core::fmt::Debug;
use std::prelude::v1::*;
use std::vec;
use super::{derive_traffic_iv, derive_traffic_key, KeySchedule, SecretKind};
use crate::test_provider::ring_like::aead;

View File

@ -337,6 +337,8 @@ impl Codec for DigitallySignedStruct {
#[test]
fn assertions_are_debug() {
use std::format;
assert_eq!(
format!("{:?}", ClientCertVerified::assertion()),
"ClientCertVerified(())"

View File

@ -7,6 +7,8 @@
#![cfg(feature = "ring")]
use core::time::Duration;
use std::prelude::v1::*;
use std::println;
use std::time::Instant;
use crate::crypto::ring;

View File

@ -413,7 +413,9 @@ mod tests {
use pki_types::{CertificateDer, CertificateRevocationListDer};
use std::prelude::v1::*;
use std::sync::Arc;
use std::{format, println, vec};
fn load_crls(crls_der: &[&[u8]]) -> Vec<CertificateRevocationListDer<'static>> {
crls_der

View File

@ -282,7 +282,9 @@ impl ServerCertVerifier for WebPkiServerVerifier {
#[cfg(all(test, any(feature = "ring", feature = "aws_lc_rs")))]
mod tests {
use std::prelude::v1::*;
use std::sync::Arc;
use std::{println, vec};
use pki_types::{CertificateDer, CertificateRevocationListDer};

View File

@ -220,6 +220,7 @@ pub(crate) fn verify_server_cert_signed_by_trust_anchor_impl(
#[cfg(test)]
mod tests {
use super::*;
use std::format;
#[test]
fn certificate_debug() {

View File

@ -43,6 +43,7 @@ const DER_SEQUENCE_TAG: u8 = 0x30;
#[cfg(test)]
mod tests {
use super::*;
use std::vec;
#[test]
fn test_empty() {