C++11: Also convert NULL to nullptr in unit tests (!117)

Closes !117 - Require C++11 compilation
This commit is contained in:
Mike Fleetwood 2023-09-12 14:37:09 +01:00 committed by Curtis Gedak
parent 40665913bf
commit 3b469273de
5 changed files with 27 additions and 27 deletions

View File

@ -69,10 +69,10 @@ std::string binary_string_to_print(size_t offset, const char* s, size_t len)
// Re-execute current executable using xvfb-run so that it provides a virtual X11 display. // Re-execute current executable using xvfb-run so that it provides a virtual X11 display.
static void exec_using_xvfb_run(int argc, char** argv) static void exec_using_xvfb_run(int argc, char** argv)
{ {
// argc+2 = Space for "xvfb-run" command, existing argc strings plus NULL pointer. // argc+2 = Space for "xvfb-run" command, existing argc strings plus nullptr.
size_t size = sizeof(char*) * (argc+2); size_t size = sizeof(char*) * (argc+2);
char** new_argv = (char**)malloc(size); char** new_argv = (char**)malloc(size);
if (new_argv == NULL) if (new_argv == nullptr)
{ {
fprintf(stderr, "Failed to allocate %lu bytes of memory. errno=%d,%s\n", fprintf(stderr, "Failed to allocate %lu bytes of memory. errno=%d,%s\n",
(unsigned long)size, errno, strerror(errno)); (unsigned long)size, errno, strerror(errno));
@ -80,14 +80,14 @@ static void exec_using_xvfb_run(int argc, char** argv)
} }
new_argv[0] = strdup("xvfb-run"); new_argv[0] = strdup("xvfb-run");
if (new_argv[0] == NULL) if (new_argv[0] == nullptr)
{ {
fprintf(stderr, "Failed to allocate %lu bytes of memory. errno=%d,%s\n", fprintf(stderr, "Failed to allocate %lu bytes of memory. errno=%d,%s\n",
(unsigned long)strlen(new_argv[0])+1, errno, strerror(errno)); (unsigned long)strlen(new_argv[0])+1, errno, strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Copy argv pointers including final NULL pointer. // Copy argv pointers including final nullptr.
for (size_t i = 0; i <= (unsigned)argc; i++) for (size_t i = 0; i <= (unsigned)argc; i++)
new_argv[i+1] = argv[i]; new_argv[i+1] = argv[i];
@ -102,7 +102,7 @@ static void exec_using_xvfb_run(int argc, char** argv)
void ensure_x11_display(int argc, char** argv) void ensure_x11_display(int argc, char** argv)
{ {
const char* display = getenv("DISPLAY"); const char* display = getenv("DISPLAY");
if (display == NULL) if (display == nullptr)
{ {
printf("DISPLAY environment variable unset. Executing 'xvfb-run %s ...'\n", argv[0]); printf("DISPLAY environment variable unset. Executing 'xvfb-run %s ...'\n", argv[0]);
exec_using_xvfb_run(argc, argv); exec_using_xvfb_run(argc, argv);

View File

@ -148,7 +148,7 @@ static std::string get_block_name( unsigned want )
static std::string get_link_name() static std::string get_link_name()
{ {
DIR * dir = opendir( "/dev/disk/by-id" ); DIR * dir = opendir( "/dev/disk/by-id" );
if ( dir == NULL ) if (dir == nullptr)
{ {
ADD_FAILURE() << __func__ << "(): Failed to open directory '/dev/disk/by-id'"; ADD_FAILURE() << __func__ << "(): Failed to open directory '/dev/disk/by-id'";
return ""; return "";
@ -180,8 +180,8 @@ static std::string get_link_name()
// Follow symbolic link return real path. // Follow symbolic link return real path.
static std::string follow_link_name( std::string link ) static std::string follow_link_name( std::string link )
{ {
char * rpath = realpath( link.c_str(), NULL ); char* rpath = realpath(link.c_str(), nullptr);
if ( rpath == NULL ) if (rpath == nullptr)
{ {
ADD_FAILURE() << __func__ << "(): Failed to resolve symbolic link '" << link << "'"; ADD_FAILURE() << __func__ << "(): Failed to resolve symbolic link '" << link << "'";
return ""; return "";

View File

@ -86,7 +86,7 @@ void EraseFileSystemSignaturesTest::create_image_file(Byte_Value size)
m_partition.Reset(); m_partition.Reset();
PedDevice* lp_device = ped_device_get(s_image_name); PedDevice* lp_device = ped_device_get(s_image_name);
ASSERT_TRUE(lp_device != NULL); ASSERT_TRUE(lp_device != nullptr);
m_partition.set_unpartitioned(s_image_name, m_partition.set_unpartitioned(s_image_name,
lp_device->path, lp_device->path,
@ -96,7 +96,7 @@ void EraseFileSystemSignaturesTest::create_image_file(Byte_Value size)
false); false);
ped_device_destroy(lp_device); ped_device_destroy(lp_device);
lp_device = NULL; lp_device = nullptr;
} }
@ -155,7 +155,7 @@ const char* first_non_zero_byte(const char* buf, size_t size)
buf++; buf++;
size--; size--;
} }
return NULL; return nullptr;
} }
@ -183,7 +183,7 @@ bool EraseFileSystemSignaturesTest::image_contains_all_zeros()
return false; return false;
} }
const char* p = first_non_zero_byte(buf, bytes_read); const char* p = first_non_zero_byte(buf, bytes_read);
if (p != NULL) if (p != nullptr)
{ {
ADD_FAILURE() << __func__ << "(): First non-zero bytes:\n" ADD_FAILURE() << __func__ << "(): First non-zero bytes:\n"
<< binary_string_to_print(offset + (p - buf), p, buf + bytes_read - p); << binary_string_to_print(offset + (p - buf), p, buf + bytes_read - p);

View File

@ -79,7 +79,7 @@ static bool mem_is_zero( const char * mem, size_t len )
class PasswordRAMStoreTest : public ::testing::Test class PasswordRAMStoreTest : public ::testing::Test
{ {
protected: protected:
PasswordRAMStoreTest() : looked_up_pw( NULL ) {}; PasswordRAMStoreTest() : looked_up_pw(nullptr) {};
static void SetUpTestCase(); static void SetUpTestCase();
@ -93,7 +93,7 @@ protected:
}; };
// Initialise test case class static member. // Initialise test case class static member.
const char * PasswordRAMStoreTest::protected_mem = NULL; const char * PasswordRAMStoreTest::protected_mem = nullptr;
const size_t ProtectedMemSize = 4096; // [Implementation knowledge: size] const size_t ProtectedMemSize = 4096; // [Implementation knowledge: size]
@ -101,7 +101,7 @@ const size_t ProtectedMemSize = 4096; // [Implementation knowledge: size]
void PasswordRAMStoreTest::SetUpTestCase() void PasswordRAMStoreTest::SetUpTestCase()
{ {
protected_mem = PasswordRAMStore::get_protected_mem(); protected_mem = PasswordRAMStore::get_protected_mem();
ASSERT_TRUE( protected_mem != NULL ) << __func__ << "(): No locked virtual memory for password RAM store"; ASSERT_TRUE(protected_mem != nullptr) << __func__ << "(): No locked virtual memory for password RAM store";
} }
TEST_F( PasswordRAMStoreTest, Initialisation ) TEST_F( PasswordRAMStoreTest, Initialisation )
@ -114,7 +114,7 @@ TEST_F( PasswordRAMStoreTest, UnknownPasswordLookup )
{ {
// Test lookup of non-existent password fails. // Test lookup of non-existent password fails.
looked_up_pw = PasswordRAMStore::lookup( "key-unknown" ); looked_up_pw = PasswordRAMStore::lookup( "key-unknown" );
EXPECT_TRUE( looked_up_pw == NULL ); EXPECT_TRUE(looked_up_pw == nullptr);
} }
TEST_F( PasswordRAMStoreTest, UnknownPasswordErasure ) TEST_F( PasswordRAMStoreTest, UnknownPasswordErasure )
@ -270,7 +270,7 @@ TEST_F( PasswordRAMStoreTest, TooLongPassword )
EXPECT_TRUE( mem_is_zero( protected_mem, ProtectedMemSize ) ); EXPECT_TRUE( mem_is_zero( protected_mem, ProtectedMemSize ) );
looked_up_pw = PasswordRAMStore::lookup( "key-too-long" ); looked_up_pw = PasswordRAMStore::lookup( "key-too-long" );
EXPECT_TRUE( looked_up_pw == NULL ); EXPECT_TRUE(looked_up_pw == nullptr);
EXPECT_FALSE(PasswordRAMStore::erase("key-too-long")); EXPECT_FALSE(PasswordRAMStore::erase("key-too-long"));
EXPECT_TRUE( mem_is_zero( protected_mem, ProtectedMemSize ) ); EXPECT_TRUE( mem_is_zero( protected_mem, ProtectedMemSize ) );

View File

@ -194,12 +194,12 @@ protected:
}; };
SupportedFileSystems* SupportedFileSystemsTest::s_supported_filesystems = NULL; SupportedFileSystems* SupportedFileSystemsTest::s_supported_filesystems = nullptr;
const char* SupportedFileSystemsTest::s_image_name = "test_SupportedFileSystems.img"; const char* SupportedFileSystemsTest::s_image_name = "test_SupportedFileSystems.img";
SupportedFileSystemsTest::SupportedFileSystemsTest() SupportedFileSystemsTest::SupportedFileSystemsTest()
: m_fstype(GetParam()), m_fs_object(NULL), m_require_loopdev(false), : m_fstype(GetParam()), m_fs_object(nullptr), m_require_loopdev(false),
// Initialise top-level operation detail object with description ... // Initialise top-level operation detail object with description ...
m_operation_detail("Operation details:", STATUS_NONE) m_operation_detail("Operation details:", STATUS_NONE)
{ {
@ -208,12 +208,12 @@ SupportedFileSystemsTest::SupportedFileSystemsTest()
void SupportedFileSystemsTest::SetUp() void SupportedFileSystemsTest::SetUp()
{ {
ASSERT_TRUE(s_supported_filesystems != NULL) << __func__ << "(): TEST_BUG: File system interfaces not loaded"; ASSERT_TRUE(s_supported_filesystems != nullptr) << __func__ << "(): TEST_BUG: File system interfaces not loaded";
// Lookup file system interface object. // Lookup file system interface object.
m_fs_object = s_supported_filesystems->get_fs_object(m_fstype); m_fs_object = s_supported_filesystems->get_fs_object(m_fstype);
ASSERT_TRUE(m_fs_object != NULL) << __func__ << "(): TEST_BUG: Interface object not found for file system " ASSERT_TRUE(m_fs_object != nullptr) << __func__ << "(): TEST_BUG: Interface object not found for file system "
<< test_fsname(m_fstype); << test_fsname(m_fstype);
} }
@ -243,7 +243,7 @@ void SupportedFileSystemsTest::TearDown()
unlink(s_image_name); unlink(s_image_name);
m_fs_object = NULL; m_fs_object = nullptr;
} }
@ -279,7 +279,7 @@ std::vector<FSType> SupportedFileSystemsTest::get_supported_fstypes()
// Create the supported file system interface object. // Create the supported file system interface object.
void SupportedFileSystemsTest::setup_supported_filesystems() void SupportedFileSystemsTest::setup_supported_filesystems()
{ {
if (s_supported_filesystems == NULL) if (s_supported_filesystems == nullptr)
{ {
s_supported_filesystems = new SupportedFileSystems(); s_supported_filesystems = new SupportedFileSystems();
@ -294,7 +294,7 @@ void SupportedFileSystemsTest::setup_supported_filesystems()
void SupportedFileSystemsTest::teardown_supported_filesystems() void SupportedFileSystemsTest::teardown_supported_filesystems()
{ {
delete s_supported_filesystems; delete s_supported_filesystems;
s_supported_filesystems = NULL; s_supported_filesystems = nullptr;
} }
@ -361,7 +361,7 @@ void SupportedFileSystemsTest::reload_partition()
// Use libparted to get the sector size etc. of the image file. // Use libparted to get the sector size etc. of the image file.
PedDevice* lp_device = ped_device_get(m_dev_name.c_str()); PedDevice* lp_device = ped_device_get(m_dev_name.c_str());
ASSERT_TRUE(lp_device != NULL); ASSERT_TRUE(lp_device != nullptr);
// Prepare partition object spanning whole of the image file. // Prepare partition object spanning whole of the image file.
m_partition.set_unpartitioned(m_dev_name, m_partition.set_unpartitioned(m_dev_name,
@ -372,7 +372,7 @@ void SupportedFileSystemsTest::reload_partition()
false); false);
ped_device_destroy(lp_device); ped_device_destroy(lp_device);
lp_device = NULL; lp_device = nullptr;
} }