[libc++] Refactor the tests for std::random_device

That will make it easier to change the behavior of the arc4random()
based implementation. Note that in particular, the eval.pass.cpp test
used to work with non "/dev/random" based implementations because we'd
throw an exception upon constructing the random_device. This patch makes
the intent of the test clearer.
This commit is contained in:
Louis Dionne 2022-01-10 16:30:28 -05:00
parent 914fffc7f2
commit 84654f2733
2 changed files with 18 additions and 34 deletions

View File

@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
// See https://llvm.org/PR20183
//
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
// UNSUPPORTED: libcpp-has-no-random-device
@ -38,15 +37,6 @@
#include "test_convertible.h"
#endif
bool is_valid_random_device(const std::string &token) {
#if defined(_LIBCPP_USING_DEV_RANDOM)
// Not an exhaustive list: they're the only tokens that are tested below.
return token == "/dev/urandom" || token == "/dev/random";
#else
return token == "/dev/urandom";
#endif
}
void check_random_device_valid(const std::string &token) {
std::random_device r(token);
}
@ -67,24 +57,18 @@ int main(int, char**) {
{
std::random_device r;
}
// Check the validity of various tokens
{
std::string token = "wrong file";
check_random_device_invalid(token);
}
{
std::string token = "/dev/urandom";
if (is_valid_random_device(token))
check_random_device_valid(token);
else
check_random_device_invalid(token);
}
{
std::string token = "/dev/random";
if (is_valid_random_device(token))
check_random_device_valid(token);
else
check_random_device_invalid(token);
check_random_device_invalid("wrong file");
check_random_device_invalid("/dev/whatever");
check_random_device_valid("/dev/urandom");
#if defined(_LIBCPP_USING_DEV_RANDOM)
check_random_device_valid("/dev/random");
#else
check_random_device_invalid("/dev/random");
#endif
}
#if !defined(_WIN32)
// Test that random_device(const string&) properly handles getting
// a file descriptor with the value '0'. Do this by closing the standard

View File

@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
// See https://llvm.org/PR20183
//
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
// UNSUPPORTED: libcpp-has-no-random-device
@ -32,15 +31,16 @@ int main(int, char**)
((void)e); // Prevent unused warning
}
#ifndef TEST_HAS_NO_EXCEPTIONS
try
// When using the `/dev/urandom` implementation, make sure that we throw
// an exception when we hit EOF while reading the custom-provided file.
#if !defined(TEST_HAS_NO_EXCEPTIONS) && defined(_LIBCPP_USING_DEV_RANDOM)
{
std::random_device r("/dev/null");
(void)r();
LIBCPP_ASSERT(false);
}
catch (const std::system_error&)
{
try {
(void)r();
LIBCPP_ASSERT(false);
} catch (const std::system_error&) {
}
}
#endif