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 <jamieliu@google.com>
Suggested-by: Andrei Vagin <avagin@google.com>
PiperOrigin-RevId: 574243850
This commit is contained in:
Ayush Ranjan
2023-10-17 13:15:22 -07:00
committed by gVisor bot
parent 669edba5d9
commit 6a25f2ebb2
4 changed files with 4 additions and 16 deletions
+1 -9
View File
@@ -24,7 +24,6 @@
#include <stack>
#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<char>(rand_r(&seed));
}
}
size_t CalculateUnixSockAddrLen(const char* sun_path) {
// Abstract addresses always return the full length.
if (sun_path[0] == 0) {
-3
View File
@@ -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<SocketPairKind> {
protected:
+2 -3
View File
@@ -185,13 +185,12 @@ PosixErrorOr<uint64_t> Links(const std::string& path) {
return static_cast<uint64_t>(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<uint32_t>(ts.tv_nsec);
char* const buf = static_cast<char*>(buffer);
for (size_t i = 0; i < len; i++) {
buf[i] = rand_r(&seed) % 255;
buffer[i] = rand_r(&seed) % 255;
}
}
+1 -1
View File
@@ -738,7 +738,7 @@ std::vector<T> 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 <typename T>
inline PosixErrorOr<T> Atoi(absl::string_view str) {