From 6a25f2ebb2c8d4d317115598babb3f585a4c8515 Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Tue, 17 Oct 2023 13:12:23 -0700 Subject: [PATCH] Remove duplicate RandomizeBuffer implementation from socket util. Instead use the one from test_util, which uses a more random seed for rand_r(). Suggested-by: Jamie Liu Suggested-by: Andrei Vagin PiperOrigin-RevId: 574243850 --- test/util/socket_util.cc | 10 +--------- test/util/socket_util.h | 3 --- test/util/test_util.cc | 5 ++--- test/util/test_util.h | 2 +- 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/test/util/socket_util.cc b/test/util/socket_util.cc index 7ffbc6379..c148dcf85 100644 --- a/test/util/socket_util.cc +++ b/test/util/socket_util.cc @@ -24,7 +24,6 @@ #include #include "gtest/gtest.h" -#include "absl/memory/memory.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" #include "absl/time/clock.h" @@ -32,6 +31,7 @@ #include "test/util/file_descriptor.h" #include "test/util/posix_error.h" #include "test/util/temp_path.h" +#include "test/util/test_util.h" #include "test/util/thread_util.h" namespace gvisor { @@ -598,14 +598,6 @@ void TransferTest(int fd1, int fd2) { EXPECT_EQ(0, memcmp(buf1, buf2, sizeof(buf1))); } -// Initializes the given buffer with random data. -void RandomizeBuffer(char* ptr, size_t len) { - uint32_t seed = time(nullptr); - for (size_t i = 0; i < len; ++i) { - ptr[i] = static_cast(rand_r(&seed)); - } -} - size_t CalculateUnixSockAddrLen(const char* sun_path) { // Abstract addresses always return the full length. if (sun_path[0] == 0) { diff --git a/test/util/socket_util.h b/test/util/socket_util.h index 68acba0d8..fcf167097 100644 --- a/test/util/socket_util.h +++ b/test/util/socket_util.h @@ -401,9 +401,6 @@ SocketPairKind NoOp(SocketPairKind const& base); // ASSERT_NO_FATAL_FAILURE(). void TransferTest(int fd1, int fd2); -// Fills [buf, buf+len) with random bytes. -void RandomizeBuffer(char* buf, size_t len); - // Base test fixture for tests that operate on pairs of connected sockets. class SocketPairTest : public ::testing::TestWithParam { protected: diff --git a/test/util/test_util.cc b/test/util/test_util.cc index 1c55eeb15..d26f273af 100644 --- a/test/util/test_util.cc +++ b/test/util/test_util.cc @@ -185,13 +185,12 @@ PosixErrorOr Links(const std::string& path) { return static_cast(st.st_nlink); } -void RandomizeBuffer(void* buffer, size_t len) { +void RandomizeBuffer(char* buffer, size_t len) { struct timespec ts = {}; clock_gettime(CLOCK_MONOTONIC, &ts); uint32_t seed = static_cast(ts.tv_nsec); - char* const buf = static_cast(buffer); for (size_t i = 0; i < len; i++) { - buf[i] = rand_r(&seed) % 255; + buffer[i] = rand_r(&seed) % 255; } } diff --git a/test/util/test_util.h b/test/util/test_util.h index 65c2cee3f..7421c0e0c 100644 --- a/test/util/test_util.h +++ b/test/util/test_util.h @@ -738,7 +738,7 @@ std::vector VecCat(Args&&... args) { } while (false) // Fill the given buffer with random bytes. -void RandomizeBuffer(void* buffer, size_t len); +void RandomizeBuffer(char* buffer, size_t len); template inline PosixErrorOr Atoi(absl::string_view str) {