base: net: use tempfile::tempdir() in tests

Avoid the possibility of tests reusing the same socket path by using a
known unique directory from tempfile::tempdir().

BUG=b:274145919
TEST=tools/dev_container tools/presubmit

Change-Id: I883e223617fb465cdfbff5a45509098487b62b3b
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4348699
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2023-03-17 12:35:08 -07:00 committed by crosvm LUCI
parent cddc85e857
commit 875a1d7d30

View file

@ -2,19 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::env;
use std::io::ErrorKind;
use std::path::PathBuf;
use std::time::Duration;
use base::AsRawDescriptor;
use base::UnixSeqpacket;
use base::UnixSeqpacketListener;
use base::UnlinkUnixSeqpacketListener;
fn tmpdir() -> PathBuf {
env::temp_dir()
}
use tempfile::tempdir;
#[test]
fn unix_seqpacket_path_not_exists() {
@ -24,8 +19,8 @@ fn unix_seqpacket_path_not_exists() {
#[test]
fn unix_seqpacket_listener_path() {
let mut socket_path = tmpdir();
socket_path.push("unix_seqpacket_listener_path");
let temp_dir = tempdir().expect("failed to create tempdir");
let socket_path = temp_dir.path().join("unix_seqpacket_listener_path");
let listener = UnlinkUnixSeqpacketListener(
UnixSeqpacketListener::bind(&socket_path).expect("failed to create UnixSeqpacketListener"),
);
@ -35,8 +30,8 @@ fn unix_seqpacket_listener_path() {
#[test]
fn unix_seqpacket_listener_from_fd() {
let mut socket_path = tmpdir();
socket_path.push("unix_seqpacket_listener_from_fd");
let temp_dir = tempdir().expect("failed to create tempdir");
let socket_path = temp_dir.path().join("unix_seqpacket_listener_from_fd");
let listener = UnlinkUnixSeqpacketListener(
UnixSeqpacketListener::bind(&socket_path).expect("failed to create UnixSeqpacketListener"),
);
@ -59,8 +54,8 @@ fn unix_seqpacket_listener_from_fd() {
#[test]
fn unix_seqpacket_path_exists_pass() {
let mut socket_path = tmpdir();
socket_path.push("path_to_socket");
let temp_dir = tempdir().expect("failed to create tempdir");
let socket_path = temp_dir.path().join("path_to_socket");
let _listener = UnlinkUnixSeqpacketListener(
UnixSeqpacketListener::bind(&socket_path).expect("failed to create UnixSeqpacketListener"),
);
@ -70,8 +65,8 @@ fn unix_seqpacket_path_exists_pass() {
#[test]
fn unix_seqpacket_path_listener_accept_with_timeout() {
let mut socket_path = tmpdir();
socket_path.push("path_listerner_accept_with_timeout");
let temp_dir = tempdir().expect("failed to create tempdir");
let socket_path = temp_dir.path().join("path_listerner_accept_with_timeout");
let listener = UnlinkUnixSeqpacketListener(
UnixSeqpacketListener::bind(&socket_path).expect("failed to create UnixSeqpacketListener"),
);
@ -103,8 +98,8 @@ fn unix_seqpacket_path_listener_accept_with_timeout() {
#[test]
fn unix_seqpacket_path_listener_accept() {
let mut socket_path = tmpdir();
socket_path.push("path_listerner_accept");
let temp_dir = tempdir().expect("failed to create tempdir");
let socket_path = temp_dir.path().join("path_listerner_accept");
let listener = UnlinkUnixSeqpacketListener(
UnixSeqpacketListener::bind(&socket_path).expect("failed to create UnixSeqpacketListener"),
);