fix: bugprone-unchecked-optional-access warnings in tests (#4672)

This commit is contained in:
Charles Kerr
2023-01-26 10:50:42 -06:00
committed by GitHub
parent 331e1699bc
commit 862abe909c
11 changed files with 111 additions and 54 deletions

View File

@@ -44,7 +44,10 @@ TEST_F(AnnouncerTest, parseHttpAnnounceResponseNoPeers)
EXPECT_EQ(3, response.seeders);
EXPECT_EQ(0, response.leechers);
EXPECT_EQ(2, response.downloads);
EXPECT_EQ(*tr_address::from_string("1.2.3.4"), response.external_ip);
auto addr = tr_address::from_string("1.2.3.4");
EXPECT_TRUE(addr.has_value());
assert(addr.has_value());
EXPECT_EQ(*addr, response.external_ip);
EXPECT_EQ(0U, std::size(response.pex));
EXPECT_EQ(0U, std::size(response.pex6));
EXPECT_EQ(""sv, response.errmsg);

View File

@@ -338,7 +338,8 @@ TEST_F(AnnouncerUdpTest, canScrape)
EXPECT_TRUE(announcer->handleMessage(std::data(arr), response_size));
// confirm that announcer processed the response
EXPECT_TRUE(response);
EXPECT_TRUE(response.has_value());
assert(response.has_value());
expectEqual(expected_response, *response);
// Now scrape again.
@@ -421,7 +422,8 @@ TEST_F(AnnouncerUdpTest, canMultiScrape)
EXPECT_TRUE(announcer->handleMessage(std::data(arr), response_size));
// Confirm that announcer processed the response
EXPECT_TRUE(response);
EXPECT_TRUE(response.has_value());
assert(response.has_value());
expectEqual(expected_response, *response);
}
@@ -470,7 +472,8 @@ TEST_F(AnnouncerUdpTest, canHandleScrapeError)
EXPECT_TRUE(sendError(*announcer, scrape_transaction_id, expected_response.errmsg));
// confirm that announcer processed the response
EXPECT_TRUE(response);
EXPECT_TRUE(response.has_value());
assert(response.has_value());
expectEqual(expected_response, *response);
}
@@ -510,7 +513,8 @@ TEST_F(AnnouncerUdpTest, canHandleConnectError)
EXPECT_TRUE(sendError(*announcer, transaction_id, expected_response.errmsg));
// Confirm that announcer processed the response
EXPECT_TRUE(response);
EXPECT_TRUE(response.has_value());
assert(response.has_value());
expectEqual(expected_response, *response);
}
@@ -643,6 +647,7 @@ TEST_F(AnnouncerUdpTest, canAnnounce)
EXPECT_TRUE(announcer->handleMessage(std::data(arr), response_size));
// Confirm that announcer processed the response
EXPECT_TRUE(response);
EXPECT_TRUE(response.has_value());
assert(response.has_value());
expectEqual(expected_response, *response);
}

View File

@@ -206,12 +206,14 @@ TEST(Crypto, sha1FromString)
// lowercase hex
auto const baseline = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"sv;
auto const lc = tr_sha1_from_string(baseline);
EXPECT_TRUE(lc);
EXPECT_TRUE(lc.has_value());
assert(lc.has_value());
EXPECT_EQ(baseline, tr_sha1_to_string(*lc));
// uppercase hex should yield the same result
auto const uc = tr_sha1_from_string(tr_strupper(baseline));
EXPECT_TRUE(uc);
EXPECT_TRUE(uc.has_value());
assert(uc.has_value());
EXPECT_EQ(*lc, *uc);
}
@@ -230,12 +232,14 @@ TEST(Crypto, sha256FromString)
// lowercase hex
auto const baseline = "05d58dfd14ed21d33add137eb7a2c5d4ef5aaa4a945e654363d32b7c4bf5c929"sv;
auto const lc = tr_sha256_from_string(baseline);
EXPECT_TRUE(lc);
EXPECT_TRUE(lc.has_value());
assert(lc.has_value());
EXPECT_EQ(baseline, tr_sha256_to_string(*lc));
// uppercase hex should yield the same result
auto const uc = tr_sha256_from_string(tr_strupper(baseline));
EXPECT_TRUE(uc);
EXPECT_TRUE(uc.has_value());
assert(uc.has_value());
EXPECT_EQ(*lc, *uc);
}

View File

@@ -166,6 +166,7 @@ protected:
int ping_node(struct sockaddr const* sa, int /*salen*/) override
{
auto addrport = tr_address::from_sockaddr(sa);
assert(addrport);
auto const [addr, port] = *addrport;
pinged_.push_back(Pinged{ addr, port, tr_time() });
return 0;
@@ -584,9 +585,11 @@ TEST_F(DhtTest, pingsAddedNodes)
EXPECT_EQ(0U, std::size(mediator.mock_dht_.pinged_));
auto const addr = *tr_address::from_string("10.10.10.1");
auto const addr = tr_address::from_string("10.10.10.1");
EXPECT_TRUE(addr.has_value());
assert(addr.has_value());
auto constexpr Port = tr_port::fromHost(128);
dht->addNode(addr, Port);
dht->addNode(*addr, Port);
ASSERT_EQ(1U, std::size(mediator.mock_dht_.pinged_));
EXPECT_EQ(addr, mediator.mock_dht_.pinged_.front().address);

View File

@@ -225,7 +225,7 @@ TEST_F(FileTest, getInfo)
// Can't get info of non-existent file/directory
tr_error* err = nullptr;
auto info = tr_sys_path_get_info(path1, 0, &err);
EXPECT_FALSE(info);
ASSERT_FALSE(info.has_value());
EXPECT_NE(nullptr, err);
tr_error_clear(&err);
@@ -234,7 +234,8 @@ TEST_F(FileTest, getInfo)
// Good file info
info = tr_sys_path_get_info(path1, 0, &err);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(nullptr, err) << *err;
EXPECT_EQ(TR_SYS_PATH_IS_FILE, info->type);
EXPECT_EQ(4U, info->size);
@@ -247,7 +248,8 @@ TEST_F(FileTest, getInfo)
t = time(nullptr);
tr_sys_dir_create(path1, 0, 0777);
info = tr_sys_path_get_info(path1, 0, &err);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(nullptr, err) << *err;
EXPECT_EQ(TR_SYS_PATH_IS_DIRECTORY, info->type);
EXPECT_NE(uint64_t(-1), info->size);
@@ -259,7 +261,7 @@ TEST_F(FileTest, getInfo)
{
// Can't get info of non-existent file/directory
info = tr_sys_path_get_info(path1, 0, &err);
EXPECT_FALSE(info);
ASSERT_FALSE(info.has_value());
EXPECT_NE(nullptr, err);
tr_error_clear(&err);
@@ -268,7 +270,8 @@ TEST_F(FileTest, getInfo)
// Good file info
info = tr_sys_path_get_info(path1, 0, &err);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(nullptr, err) << *err;
EXPECT_EQ(TR_SYS_PATH_IS_FILE, info->type);
EXPECT_EQ(4, info->size);
@@ -277,7 +280,8 @@ TEST_F(FileTest, getInfo)
// Symlink
info = tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW, &err);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(nullptr, err) << *err;
EXPECT_EQ(TR_SYS_PATH_IS_OTHER, info->type);
@@ -289,7 +293,8 @@ TEST_F(FileTest, getInfo)
tr_sys_dir_create(path2, 0, 0777);
EXPECT_TRUE(createSymlink(path1, path2, true)); /* Win32: directory and file symlinks differ :( */
info = tr_sys_path_get_info(path1, 0, &err);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(nullptr, err) << *err;
EXPECT_EQ(TR_SYS_PATH_IS_DIRECTORY, info->type);
EXPECT_NE(uint64_t(-1), info->size);
@@ -1134,7 +1139,8 @@ TEST_F(FileTest, fileOpen)
/* Pointer is at the end of file */
auto info = tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(4U, info->size);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_APPEND, 0600, &err);
EXPECT_NE(TR_BAD_SYS_FILE, fd);
@@ -1144,17 +1150,20 @@ TEST_F(FileTest, fileOpen)
/* File gets truncated */
info = tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(5U, info->size);
fd = tr_sys_file_open(path1, TR_SYS_FILE_WRITE | TR_SYS_FILE_TRUNCATE, 0600, &err);
EXPECT_NE(TR_BAD_SYS_FILE, fd);
EXPECT_EQ(nullptr, err) << *err;
info = tr_sys_path_get_info(path1);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(0U, info->size);
tr_sys_file_close(fd);
info = tr_sys_path_get_info(path1, TR_SYS_PATH_NO_FOLLOW);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(0U, info->size);
/* TODO: symlink and hardlink tests */
@@ -1173,19 +1182,22 @@ TEST_F(FileTest, fileTruncate)
EXPECT_TRUE(tr_sys_file_truncate(fd, 10, &err));
EXPECT_EQ(nullptr, err) << *err;
auto info = tr_sys_path_get_info(path);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(10U, info->size);
EXPECT_TRUE(tr_sys_file_truncate(fd, 20, &err));
EXPECT_EQ(nullptr, err) << *err;
info = tr_sys_path_get_info(path);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(20U, info->size);
EXPECT_TRUE(tr_sys_file_truncate(fd, 0, &err));
EXPECT_EQ(nullptr, err) << *err;
info = tr_sys_path_get_info(path);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(0U, info->size);
EXPECT_TRUE(tr_sys_file_truncate(fd, 50, &err));
@@ -1194,7 +1206,8 @@ TEST_F(FileTest, fileTruncate)
tr_sys_file_close(fd);
info = tr_sys_path_get_info(path);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(50U, info->size);
fd = tr_sys_file_open(path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0600);
@@ -1205,7 +1218,8 @@ TEST_F(FileTest, fileTruncate)
tr_sys_file_close(fd);
info = tr_sys_path_get_info(path);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(25U, info->size);
// try to truncate a closed file
@@ -1229,7 +1243,8 @@ TEST_F(FileTest, filePreallocate)
{
EXPECT_EQ(nullptr, err) << *err;
auto info = tr_sys_path_get_info(path1);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(prealloc_size, info->size);
}
else
@@ -1255,7 +1270,8 @@ TEST_F(FileTest, filePreallocate)
{
EXPECT_EQ(nullptr, err) << *err;
auto info = tr_sys_path_get_info(path1);
EXPECT_TRUE(info);
EXPECT_TRUE(info.has_value());
assert(info.has_value());
EXPECT_EQ(prealloc_size, info->size);
}
else

View File

@@ -238,7 +238,8 @@ TEST_F(HandshakeTest, incomingPlaintext)
auto const res = runHandshake(&mediator, io);
// check the results
EXPECT_TRUE(res);
EXPECT_TRUE(res.has_value());
assert(res.has_value());
EXPECT_TRUE(res->is_connected);
EXPECT_TRUE(res->read_anything_from_peer);
EXPECT_EQ(io, res->io);
@@ -265,7 +266,8 @@ TEST_F(HandshakeTest, incomingPlaintextUnknownInfoHash)
auto const res = runHandshake(&mediator, io);
// check the results
EXPECT_TRUE(res);
EXPECT_TRUE(res.has_value());
assert(res.has_value());
EXPECT_FALSE(res->is_connected);
EXPECT_TRUE(res->read_anything_from_peer);
EXPECT_EQ(io, res->io);
@@ -290,7 +292,8 @@ TEST_F(HandshakeTest, outgoingPlaintext)
auto const res = runHandshake(&mediator, io);
// check the results
EXPECT_TRUE(res);
EXPECT_TRUE(res.has_value());
assert(res.has_value());
EXPECT_TRUE(res->is_connected);
EXPECT_TRUE(res->read_anything_from_peer);
EXPECT_EQ(io, res->io);
@@ -328,7 +331,8 @@ TEST_F(HandshakeTest, incomingEncrypted)
auto const res = runHandshake(&mediator, io);
// check the results
EXPECT_TRUE(res);
EXPECT_TRUE(res.has_value());
assert(res.has_value());
EXPECT_TRUE(res->is_connected);
EXPECT_TRUE(res->read_anything_from_peer);
EXPECT_EQ(io, res->io);
@@ -365,7 +369,8 @@ TEST_F(HandshakeTest, incomingEncryptedUnknownInfoHash)
auto const res = runHandshake(&mediator, io);
// check the results
EXPECT_TRUE(res);
EXPECT_TRUE(res.has_value());
assert(res.has_value());
EXPECT_FALSE(res->is_connected);
EXPECT_TRUE(res->read_anything_from_peer);
EXPECT_EQ(tr_sha1_digest_t{}, io->torrent_hash());
@@ -404,7 +409,8 @@ TEST_F(HandshakeTest, outgoingEncrypted)
auto const res = runHandshake(&mediator, io, TR_ENCRYPTION_PREFERRED);
// check the results
EXPECT_TRUE(res);
EXPECT_TRUE(res.has_value());
assert(res.has_value());
EXPECT_TRUE(res->is_connected);
EXPECT_TRUE(res->read_anything_from_peer);
EXPECT_EQ(io, res->io);

View File

@@ -23,7 +23,8 @@ TEST_F(NetTest, conversionsIPv4)
auto constexpr AddrStr = "127.0.0.1"sv;
auto addr = tr_address::from_string(AddrStr);
EXPECT_TRUE(addr);
EXPECT_TRUE(addr.has_value());
assert(addr.has_value());
EXPECT_EQ(AddrStr, addr->display_name());
auto [ss, sslen] = addr->to_sockaddr(Port);
@@ -31,7 +32,8 @@ TEST_F(NetTest, conversionsIPv4)
EXPECT_EQ(Port.network(), reinterpret_cast<sockaddr_in const*>(&ss)->sin_port);
auto addrport = tr_address::from_sockaddr(reinterpret_cast<sockaddr const*>(&ss));
EXPECT_TRUE(addrport);
ASSERT_TRUE(addrport.has_value());
assert(addrport.has_value());
EXPECT_EQ(addr, addrport->first);
EXPECT_EQ(Port, addrport->second);
}
@@ -172,7 +174,8 @@ TEST_F(NetTest, isGlobalUnicastAddress)
for (auto const& [presentation, expected] : Tests)
{
auto const address = tr_address::from_string(presentation);
EXPECT_TRUE(address);
EXPECT_TRUE(address.has_value());
assert(address.has_value());
EXPECT_EQ(expected, address->is_global_unicast_address()) << presentation;
}
}

View File

@@ -35,7 +35,8 @@ TEST_F(OpenFilesTest, getOpensIfNotCached)
// confirm that we can cache the file
auto fd = session_->openFiles().get(0, 0, false, filename, TR_PREALLOCATE_FULL, std::size(Contents));
EXPECT_TRUE(fd);
EXPECT_TRUE(fd.has_value());
assert(fd.has_value());
EXPECT_NE(TR_BAD_SYS_FILE, *fd);
// test the file contents to confirm that fd points to the right file
@@ -66,8 +67,10 @@ TEST_F(OpenFilesTest, getCachedReturnsTheSameFd)
EXPECT_FALSE(session_->openFiles().get(0, 0, false));
auto const fd1 = session_->openFiles().get(0, 0, false, filename, TR_PREALLOCATE_FULL, std::size(Contents));
auto const fd2 = session_->openFiles().get(0, 0, false);
EXPECT_TRUE(fd1);
EXPECT_TRUE(fd2);
EXPECT_TRUE(fd1.has_value());
EXPECT_TRUE(fd2.has_value());
assert(fd1.has_value());
assert(fd2.has_value());
EXPECT_EQ(*fd1, *fd2);
}
@@ -95,6 +98,8 @@ TEST_F(OpenFilesTest, opensInReadOnlyUnlessWritableIsRequested)
// cache a file read-only mode
tr_error* error = nullptr;
auto fd = session_->openFiles().get(0, 0, false, filename, TR_PREALLOCATE_FULL, std::size(Contents));
EXPECT_TRUE(fd.has_value());
assert(fd.has_value());
// confirm that writing to it fails
EXPECT_FALSE(tr_sys_file_write(*fd, std::data(Contents), std::size(Contents), nullptr, &error));
@@ -113,7 +118,8 @@ TEST_F(OpenFilesTest, createsMissingFileIfWriteRequested)
EXPECT_FALSE(tr_sys_path_exists(filename));
fd = session_->openFiles().get(0, 0, true, filename, TR_PREALLOCATE_FULL, std::size(Contents));
EXPECT_TRUE(fd);
EXPECT_TRUE(fd.has_value());
assert(fd.has_value());
EXPECT_NE(TR_BAD_SYS_FILE, *fd);
EXPECT_TRUE(tr_sys_path_exists(filename));
}

View File

@@ -28,7 +28,8 @@ TEST_F(QuarkTest, allPredefinedKeysCanBeLookedUp)
{
auto const str = quarkGetString(i);
auto const q = tr_quark_lookup(str);
EXPECT_TRUE(q);
ASSERT_TRUE(q.has_value());
assert(q.has_value());
EXPECT_EQ(i, *q);
}
}

View File

@@ -83,13 +83,15 @@ TEST_F(TorrentFilesTest, find)
auto search_path = std::vector<std::string_view>{ search_path_1.sv(), search_path_2.sv() };
auto found = files.find(file_index, std::data(search_path), std::size(search_path));
EXPECT_TRUE(found);
EXPECT_TRUE(found.has_value());
assert(found.has_value());
EXPECT_EQ(filename, found->filename());
// same search, but with the search paths reversed
search_path = std::vector<std::string_view>{ search_path_2.sv(), search_path_1.sv() };
found = files.find(file_index, std::data(search_path), std::size(search_path));
EXPECT_TRUE(found);
EXPECT_TRUE(found.has_value());
assert(found.has_value());
EXPECT_EQ(filename, found->filename());
// now make it an incomplete file
@@ -97,13 +99,15 @@ TEST_F(TorrentFilesTest, find)
EXPECT_TRUE(tr_sys_path_rename(filename, partial_filename));
search_path = std::vector<std::string_view>{ search_path_1.sv(), search_path_2.sv() };
found = files.find(file_index, std::data(search_path), std::size(search_path));
EXPECT_TRUE(found);
EXPECT_TRUE(found.has_value());
assert(found.has_value());
EXPECT_EQ(partial_filename, found->filename());
// same search, but with the search paths reversed
search_path = std::vector<std::string_view>{ search_path_2.sv(), search_path_1.sv() };
found = files.find(file_index, std::data(search_path), std::size(search_path));
EXPECT_TRUE(found);
EXPECT_TRUE(found.has_value());
assert(found.has_value());
EXPECT_EQ(partial_filename, found->filename());
// what about if we look for a file that does not exist

View File

@@ -94,7 +94,8 @@ TEST_F(VariantTest, parseInt)
auto benc = Benc;
auto const value = transmission::benc::impl::ParseInt(&benc);
EXPECT_TRUE(value);
EXPECT_TRUE(value.has_value());
assert(value.has_value());
EXPECT_EQ(ExpectVal, *value);
EXPECT_EQ(std::data(Benc) + std::size(Benc), std::data(benc));
}
@@ -133,7 +134,8 @@ TEST_F(VariantTest, parseNegativeInt)
auto benc = Benc;
auto const value = transmission::benc::impl::ParseInt(&benc);
EXPECT_TRUE(value);
EXPECT_TRUE(value.has_value());
assert(value.has_value());
EXPECT_EQ(Expected, *value);
EXPECT_EQ(std::data(Benc) + std::size(Benc), std::data(benc));
}
@@ -154,7 +156,8 @@ TEST_F(VariantTest, parseIntZero)
auto benc = Benc;
auto const value = transmission::benc::impl::ParseInt(&benc);
EXPECT_TRUE(value);
EXPECT_TRUE(value.has_value());
assert(value.has_value());
EXPECT_EQ(Expected, *value);
EXPECT_EQ(std::data(Benc) + std::size(Benc), std::data(benc));
}
@@ -182,7 +185,8 @@ TEST_F(VariantTest, str)
// good string
inout = benc = "4:boat";
value = ParseString(&inout);
EXPECT_TRUE(value);
EXPECT_TRUE(value.has_value());
assert(value.has_value());
EXPECT_EQ("boat"sv, *value);
EXPECT_EQ(std::data(benc) + std::size(benc), std::data(inout));
@@ -195,14 +199,16 @@ TEST_F(VariantTest, str)
// empty string
inout = benc = "0:"sv;
value = ParseString(&inout);
EXPECT_TRUE(value);
EXPECT_TRUE(value.has_value());
assert(value.has_value());
EXPECT_EQ(""sv, *value);
EXPECT_EQ(std::data(benc) + std::size(benc), std::data(inout));
// short string
inout = benc = "3:boat";
value = ParseString(&inout);
EXPECT_TRUE(value);
EXPECT_TRUE(value.has_value());
assert(value.has_value());
EXPECT_EQ("boa"sv, *value);
EXPECT_EQ(std::data(benc) + benc.find('t'), std::data(inout));
}