Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
env:
# The commit the expectations were recorded against. The test is given
# it too and refuses to run against another one.
LIBRESPEED_GO_COMMIT: 8aefd77bd4bde428cd86d3622102f0ac0443d96e
LIBRESPEED_GO_COMMIT: 0bf0ae364f11782e543a045b6604859a8d1b69db
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ deliberately different.
on Linux only. `--fwmark` stays Linux-only (`SO_MARK`).
- **Numbers the Go client cannot act on are refused.** A negative value,
a duration or timeout past the `time.Duration` range, and a count, size or
mark past 32 bits are out of range. Go panics on `--upload-size -1`.
A negative `--server` or `--exclude` is still taken, so `--server -1` tests
every server as it does in Go.
mark past 32 bits are out of range. A negative `--server` or `--exclude`
is still taken, so `--server -1` tests every server as it does in Go.
- **CSV quoting follows the csv crate.** Only the delimiter, a quote or a line
break causes quoting. Go also quotes a field that starts with a space or is
exactly `\.`.
Expand Down
2 changes: 0 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub struct Cli {
pub version: bool,

/// Force IPv4 only
// Both may be given; --ipv4 wins, the precedence the Go client applies.
#[arg(long = "ipv4", short = '4')]
pub ipv4: bool,

Expand Down Expand Up @@ -161,7 +160,6 @@ pub struct Cli {

/// Force HTTPS for every test server, whichever scheme the server list
/// gives. Does not affect how the server list itself is fetched
// Both may be given; --secure wins, as it does in the Go client.
#[arg(long)]
pub secure: bool,

Expand Down
6 changes: 4 additions & 2 deletions src/defs/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,10 @@ impl Server {

let mut counter = BytesCounter::new();
counter.set_mebi(opts.use_mebi);
counter.set_upload_size(opts.upload_size);
// The body counts frames as hyper takes them, which runs ahead of what
// the connections have written; this is what keeps the total from
// reporting the difference as sent.
counter.set_wire(client.wire());
let counter = Arc::new(counter);

// Pre-allocating one random blob and reusing it keeps the CPU out of the
// measurement; --no-pre-allocate streams endless random data instead.
Expand All @@ -480,8 +478,12 @@ impl Server {
);
None
} else {
// Only a payload uses the size. Without one it is not checked, and
// scaling it to bytes could overflow a 32-bit usize.
counter.set_upload_size(opts.upload_size);
Some(Bytes::from(random_data(counter.upload_size())))
};
let counter = Arc::new(counter);

let url = url_join_path(&self.get_url()?, &self.upload_url);

Expand Down
38 changes: 25 additions & 13 deletions src/http/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mod imp {
let certs = rustls_pki_types::CertificateDer::pem_slice_iter(&pem)
.collect::<Result<Vec<_>, _>>()?;
if certs.is_empty() {
bail!("no certificates found in {}", path.display());
bail!("no usable PEM certificate found in {}", path.display());
}
Ok(certs)
}
Expand All @@ -274,9 +274,9 @@ mod imp {
if tls.skip_verify {
// Say so rather than ignoring the bundle in silence: a run that
// named a CA file and verified nothing looks like a run that
// verified against that file.
// verified against that file. The bundle is not parsed, so one
// without a certificate is no error here, as in the Go client.
if let Some(path) = tls.ca_cert {
read_ca_bundle(path)?;
crate::write_error!(
"--skip-cert-verify overrides --ca-cert: {} is not used\n",
path.display()
Expand Down Expand Up @@ -362,18 +362,30 @@ mod imp {
builder.danger_accept_invalid_hostnames(true);
}

if let Some(path) = tls.ca_cert {
let pem = std::fs::read(path)
.with_context(|| format!("cannot read CA certificate bundle {}", path.display()))?;
let certs = split_pem(&pem);
if certs.is_empty() {
bail!("no certificates found in {}", path.display());
match tls.ca_cert {
// As in the rustls build, the bundle is named and not parsed, so
// one without a certificate is no error here, as in the Go client.
Some(path) if tls.skip_verify => {
crate::write_error!(
"--skip-cert-verify overrides --ca-cert: {} is not used\n",
path.display()
);
}
for cert in certs {
builder.add_root_certificate(native_tls::Certificate::from_pem(&cert)?);
Some(path) => {
let pem = std::fs::read(path).with_context(|| {
format!("cannot read CA certificate bundle {}", path.display())
})?;
let certs = split_pem(&pem);
if certs.is_empty() {
bail!("no usable PEM certificate found in {}", path.display());
}
for cert in certs {
builder.add_root_certificate(native_tls::Certificate::from_pem(&cert)?);
}
// `--ca-cert` replaces the system trust store, as it does in the Go version.
builder.disable_built_in_roots(true);
}
// `--ca-cert` replaces the system trust store, as it does in the Go version.
builder.disable_built_in_roots(true);
None => {}
}

if cfg!(feature = "http2") && tls.http2 {
Expand Down
42 changes: 40 additions & 2 deletions src/speedtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const PING_WORKERS: usize = 10;
/// exhaust memory on a router.
const MAX_LOCAL_JSON: u64 = 8 * 1024 * 1024;

/// The largest `--upload-size`, in KiB: the Go client's limit, the most whose
/// byte count fits an `i32`, just under 2 GiB. The same limit holds on every
/// system, so a value that works on one works on another.
const MAX_UPLOAD_SIZE: u32 = i32::MAX as u32 / 1024;

/// Which URL scheme the server list entries are forced to.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ForceScheme {
Expand Down Expand Up @@ -59,6 +64,21 @@ pub async fn run(cli: &Cli) -> anyhow::Result<()> {
anyhow::bail!("incompatible options 'source' and 'interface'");
}

// These pairs contradict each other: only one address family and one
// scheme can be forced, and --simple would print its lines into the JSON,
// the CSV or the event stream.
for (first, second, both) in [
("ipv4", "ipv6", cli.ipv4 && cli.ipv6),
("secure", "insecure", cli.secure && cli.insecure),
("simple", "json", cli.simple && cli.json),
("simple", "csv", cli.simple && cli.csv),
("simple", "json-stream", cli.simple && cli.json_stream),
] {
if both {
anyhow::bail!("incompatible options '{first}' and '{second}'");
}
}

if cli.http2 && !cfg!(feature = "http2") {
anyhow::bail!("this build has no HTTP/2 support, so --http2 cannot be used");
}
Expand All @@ -78,11 +98,29 @@ pub async fn run(cli: &Cli) -> anyhow::Result<()> {
anyhow::bail!("invalid concurrent requests setting");
}

// The upload payload is allocated in one piece of that many KiB. The size
// is not used where there is no upload or no payload to allocate, and
// neither where the run only lists the servers.
if !cli.no_upload && !cli.no_pre_allocate && !cli.list {
let size = cli.upload_size;
if size < 1 {
write_error!("Upload size cannot be lower than 1 KiB: {size} is given\n");
anyhow::bail!("invalid upload size setting");
}
if size > MAX_UPLOAD_SIZE {
write_error!(
"Upload size cannot be more than {MAX_UPLOAD_SIZE} KiB: {size} is given\n"
);
anyhow::bail!("invalid upload size setting");
}
}

// Read here rather than left to the TLS backend, so an unreadable bundle
// is reported the way the Go client reports it: the error on its own.
// is reported the way the Go client reports it: the error from opening
// the file, after the prefix every other error gets.
if let Some(path) = &cli.ca_cert {
if let Err(e) = read_file(path, u64::MAX) {
output::fatal(e);
anyhow::bail!(e);
}
}

Expand Down
Loading
Loading