Skip to content

Commit

Permalink
more changes to improve testability
Browse files Browse the repository at this point in the history
  • Loading branch information
wwiv committed Jan 2, 2022
1 parent 1613b2a commit 3d4d35a
Show file tree
Hide file tree
Showing 27 changed files with 135 additions and 121 deletions.
8 changes: 0 additions & 8 deletions bbs/bbs_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ class BbsHelper : public CommonHelper {
[[nodiscard]] wwiv::sdk::User* user() override;
[[nodiscard]] const wwiv::sdk::User* user() const;
[[nodiscard]] wwiv::common::Context& context() override;
/*
[[nodiscard]] wwiv::sdk::User* user() const { return user_; }
[[nodiscard]] TestIO* io() const { return io_.get(); }

// Accessors for various directories
FileHelper& files() { return files_; }
[[nodiscard]] const std::string& data() const { return dir_data_; }
[[nodiscard]] const std::string& gfiles() const { return dir_gfiles_; }
*/
std::unique_ptr<Application> app_;

};
Expand Down
2 changes: 1 addition & 1 deletion binkp/binkp_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const binkp_session_config_t* BinkConfig::binkp_session_config_for(const std::st
} else if (callout_network().type == network_type_t::ftn) {
try {
FidoAddress address(node);
FidoCallout fc(config_, callout_network());
FidoCallout fc(config_.root_directory(), config_.max_backups(), callout_network());
if (!fc.IsInitialized())
return nullptr;
auto fido_node = fc.fido_node_config_for(address);
Expand Down
2 changes: 1 addition & 1 deletion binkp/file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void FileManager::rename_ftn_pending_files(const Remote& remote) {
continue;
}
LOG(INFO) << "Tic file " << ticpath.string() << " is valid.";
FidoCallout callout(config_, remote.network());
FidoCallout callout(config_.root_directory(), config_.max_backups(), remote.network());
std::string expected_tic_pw;
try {
FidoAddress address(remote.ftn_address());
Expand Down
4 changes: 2 additions & 2 deletions binkp/file_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ TEST_F(FileManagerTest, WithPassword) {

fm->ReceiveFile("foo.zip");
fm->ReceiveFile("foo.tic");
wwiv::sdk::fido::FidoCallout callout(*config_, net);
wwiv::sdk::fido::FidoCallout callout(config_->root_directory(), config_->max_backups(), net);
const wwiv::sdk::fido::FidoAddress remote_addr("1:1/1");
fido_node_config_t node_config{};
node_config.packet_config.tic_password = "rush";
Expand All @@ -166,7 +166,7 @@ TEST_F(FileManagerTest, WithPassword_WrongPassword) {

fm->ReceiveFile("foo.zip");
fm->ReceiveFile("foo.tic");
wwiv::sdk::fido::FidoCallout callout(*config_, net);
wwiv::sdk::fido::FidoCallout callout(config_->root_directory(), config_->max_backups(), net);
const wwiv::sdk::fido::FidoAddress remote_addr("1:1/1");
fido_node_config_t node_config{};
node_config.packet_config.tic_password = "rush";
Expand Down
6 changes: 4 additions & 2 deletions network3/network3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,13 @@ static int network3_fido(const NetworkCommandLine& net_cmdline) {
if (!File::Exists(FilePath(dirs.net_dir(), FIDO_CALLOUT_JSON))) {
text << " ** fido_callout.json file DOES NOT EXIST.\r\n\n";
}
FidoCallout callout(net_cmdline.config(), net);
FidoCallout callout(net_cmdline.config().root_directory(), net_cmdline.config().max_backups(),
net);
if (!callout.IsInitialized()) {
text << " ** Unable to read fido_callout.json\r\n\n";
} else {
check_fido_host_networks(net_cmdline.config(), net_cmdline.networks(), net, net_cmdline.network_number(), text);
check_fido_host_networks(net_cmdline.config(), net_cmdline.networks(), net,
net_cmdline.network_number(), text);
}

text << "Using nodelist base: " << net.fido.nodelist_base << "\r\n";
Expand Down
3 changes: 2 additions & 1 deletion networkb/networkb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ static int Main(const NetworkCommandLine& net_cmdline) {
LOG(ERROR) << "Domain is empty for FTN address. Please set it for: "
<< n.fido.fido_address;
}
auto c = std::make_unique<FidoCallout>(net_cmdline.config(), n);
auto c = std::make_unique<FidoCallout>(net_cmdline.config().root_directory(),
net_cmdline.config().max_backups(), n);
for (const auto& kv : c->node_configs_map()) {
bink_config.address_pw_map.try_emplace(kv.first, kv.second.binkp_config.password);
}
Expand Down
38 changes: 17 additions & 21 deletions networkf/networkf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,13 @@ static std::string get_echomail_areaname(const std::string& text) {
return "";
}

NetworkF::NetworkF(const wwiv::net::NetworkCommandLine& net_cmdline,
const wwiv::sdk::BbsListNet& bbslist, wwiv::core::Clock& clock)
: net_cmdline_(net_cmdline), bbslist_(bbslist), clock_(clock), net_(net_cmdline_.network()),
fido_callout_(net_cmdline_.config(), net_),
netdat_(net_cmdline_.config().gfilesdir(), net_cmdline_.config().logdir(), net_,
net_cmdline_.net_cmd(), clock_),
dirs_(net_cmdline_.config().root_directory(), net_) {
const IniFile ini(FilePath(net_cmdline_.config().root_directory(), WWIV_INI), {"WWIV"});
NetworkF::NetworkF(const sdk::BbsDirectories& bbsdirs, const networkf_options_t& opts,
const sdk::net::Network net, const sdk::BbsListNet& bbslist, core::Clock& clock)
: bbslist_(bbslist), clock_(clock), net_(net),
fido_callout_(bbsdirs.root_directory(), opts.max_backups, net_),
netdat_(bbsdirs.gfilesdir(), bbsdirs.logdir(), net_, opts.net_cmd, clock_),
dirs_(bbsdirs.root_directory(), net_), opts_(opts), datadir_(bbsdirs.datadir()) {
const IniFile ini(FilePath(bbsdirs.root_directory(), WWIV_INI), {"WWIV"});
if (ini.IsOpen()) {
// pull out new user colors
for (auto i = 0; i < 10; i++) {
Expand Down Expand Up @@ -247,7 +246,7 @@ bool NetworkF::import_packets(const std::string& dir, const std::string& mask) {
for (const auto& f : files) {
if (import_packet_file(FilePath(dir, f.name))) {
LOG(INFO) << "Successfully imported packet: " << FilePath(dir, f.name);
if (net_cmdline_.skip_delete()) {
if (opts_.skip_delete) {
backup_file(FilePath(net_.dir, f.name));
}
File::Remove(FilePath(dir, f.name));
Expand All @@ -273,7 +272,7 @@ bool NetworkF::import_bundle_file(const std::filesystem::path& path) {
File::set_current_directory(dirs_.temp_inbound_dir());

// were in the temp dir now.
const auto arcs = files::read_arcs(net_cmdline_.config().datadir());
const auto arcs = files::read_arcs(datadir_);
if (arcs.empty()) {
LOG(ERROR) << "No archivers defined!";
return false;
Expand Down Expand Up @@ -322,15 +321,15 @@ int NetworkF::import_bundles(const std::string& dir, const std::string& mask) {
if (import_packet_file(path)) {
LOG(INFO) << "Successfully imported packet: " << path;
++num_bundles_processed;
if (net_cmdline_.skip_delete()) {
if (opts_.skip_delete) {
backup_file(path);
}
File::Remove(path);
}
} else if (import_bundle_file(path)) {
LOG(INFO) << "Successfully imported bundle: " << path;
++num_bundles_processed;
if (net_cmdline_.skip_delete()) {
if (opts_.skip_delete) {
backup_file(path);
}
File::Remove(path);
Expand Down Expand Up @@ -359,7 +358,7 @@ static std::string rename_fido_packet(const std::string& dir, const std::string&
std::optional<std::string> NetworkF::create_ftn_bundle(const FidoAddress& route_to,
const std::string& fido_packet_name) {
// were in the temp dir now.
auto arcs = files::read_arcs(net_cmdline_.config().datadir());
auto arcs = files::read_arcs(datadir_);
if (arcs.empty()) {
LOG(ERROR) << "No archivers defined!";
return std::nullopt;
Expand Down Expand Up @@ -577,7 +576,7 @@ std::optional<std::string> NetworkF::create_ftn_packet(const FidoAddress& dest,
vh.to_user_name = "All";
}

FtnMessageDupe dupe(net_cmdline_.config());
FtnMessageDupe dupe(datadir_, true);
auto msgid = FtnMessageDupe::GetMessageIDFromWWIVText(raw_text);
auto needs_msgid = false;
if (msgid.empty()) {
Expand Down Expand Up @@ -634,7 +633,7 @@ std::optional<std::string> NetworkF::create_ftn_packet(const FidoAddress& dest,
auto origin_line = net_.fido.origin_line;
if (origin_line.empty()) {
// default origin line to system name if it doesn't exist.
origin_line = net_cmdline_.config().system_name();
origin_line = opts_.system_name;
}

if (from_address.point() == 0) {
Expand Down Expand Up @@ -919,23 +918,21 @@ bool NetworkF::export_main_type_email_name(std::set<std::string>& bundles, Packe

sdk::FtnMessageDupe& NetworkF::dupe() {
if (!dupe_) {
dupe_ = std::make_unique<wwiv::sdk::FtnMessageDupe>(net_cmdline_.config());
dupe_ = std::make_unique<wwiv::sdk::FtnMessageDupe>(datadir_, true);
}
return *dupe_;
}

bool NetworkF::Run() {
bool NetworkF::Run(std::vector<std::string> cmds) {
if (!fido_callout_.IsInitialized()) {
LOG(ERROR) << "Unable to initialize fido_callout.";
return false;
}

auto num_packets_processed = 0;

auto cmds = net_cmdline_.cmdline().remaining();
if (cmds.empty()) {
LOG(ERROR) << "No command specified. Exiting.";
ShowNetworkfHelp(net_cmdline_);
return false;
}

Expand Down Expand Up @@ -979,7 +976,7 @@ bool NetworkF::Run() {
if (response == ReadPacketResponse::END_OF_FILE) {
// Delete the packet.
f.Close();
if (net_cmdline_.skip_delete()) {
if (opts_.skip_delete) {
backup_file(f.full_pathname());
}
File::Remove(f.path());
Expand Down Expand Up @@ -1011,7 +1008,6 @@ bool NetworkF::Run() {

} else {
LOG(ERROR) << "Unknown command: " << cmd;
ShowNetworkfHelp(net_cmdline_);
return false;
}
return num_packets_processed > 0;
Expand Down
14 changes: 12 additions & 2 deletions networkf/networkf.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@

namespace wwiv::net::networkf {

struct networkf_options_t {
int max_backups{0};
bool skip_delete{false};
char net_cmd{'f'};
std::string system_name;
};

class NetworkF final {
public:
NetworkF(const NetworkCommandLine& cmdline, const sdk::BbsListNet& bbslist,
NetworkF(const sdk::BbsDirectories& bbsdirs,
const networkf_options_t& opts, const sdk::net::Network, const sdk::BbsListNet& bbslist,
core::Clock& clock);
~NetworkF();

bool Run();
bool Run(std::vector<std::string> cmds);

private:
bool import_packet_file(const std::filesystem::path& path);
Expand Down Expand Up @@ -104,6 +112,8 @@ class NetworkF final {
sdk::fido::FidoCallout fido_callout_;
NetDat netdat_;
sdk::fido::FtnDirectories dirs_;
const networkf_options_t opts_;
const std::string datadir_;


std::unique_ptr<sdk::FtnMessageDupe> dupe_;
Expand Down
28 changes: 16 additions & 12 deletions networkf/networkf_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,42 +80,46 @@ int main(int argc, char** argv) {
Logger::Init(argc, argv, config);

CommandLine cmdline(argc, argv, "net");
const NetworkCommandLine net_cmdline_(cmdline, 'f');
const NetworkCommandLine net_cmdline(cmdline, 'f');
try {
ScopeExit at_exit(Logger::ExitLogger);
if (!net_cmdline_.IsInitialized() || net_cmdline_.cmdline().help_requested()) {
ShowNetworkfHelp(net_cmdline_);
if (!net_cmdline.IsInitialized() || net_cmdline.cmdline().help_requested()) {
ShowNetworkfHelp(net_cmdline);
return 1;
}
const auto& net = net_cmdline_.network();
const auto& net = net_cmdline.network();
if (net.type != network_type_t::ftn) {
LOG(ERROR) << "NETWORKF is only for use on FTN type networks.";
ShowNetworkfHelp(net_cmdline_);
ShowNetworkfHelp(net_cmdline);
return 1;
}

VLOG(3) << "Reading bbsdata.net_..";
auto b = BbsListNet::ReadBbsDataNet(net.dir);
if (b.empty()) {
auto bbslist = BbsListNet::ReadBbsDataNet(net.dir);
if (bbslist.empty()) {
LOG(ERROR) << "ERROR: Unable to read bbsdata.net_.";
LOG(ERROR) << " Do you need to run network3?";
return 3;
}

const auto fake_ftn_node = b.node_config_for(FTN_FAKE_OUTBOUND_NODE);
const auto fake_ftn_node = bbslist.node_config_for(FTN_FAKE_OUTBOUND_NODE);
if (!fake_ftn_node) {
LOG(ERROR) << "Can not find node for outbound FTN address.";
LOG(ERROR) << " Do you need to run network3?";
return 2;
}

auto semaphore =
SemaphoreFile::try_acquire(net_cmdline_.semaphore_path(), net_cmdline_.semaphore_timeout());
SemaphoreFile::try_acquire(net_cmdline.semaphore_path(), net_cmdline.semaphore_timeout());
SystemClock clock{};
NetworkF nf(net_cmdline_, b, clock);
return nf.Run() ? 0 : 2;

networkf_options_t opts{net_cmdline.config().max_backups(), net_cmdline.skip_delete()};
opts.system_name = net_cmdline.config().system_name();
const auto& net = net_cmdline.network();
NetworkF nf(net_cmdline.config(), opts, net, bbslist, clock);
return nf.Run(net_cmdline.cmdline().remaining()) ? 0 : 2;
} catch (const semaphore_not_acquired& e) {
LOG(ERROR) << "ERROR: [network" << net_cmdline_.net_cmd()
LOG(ERROR) << "ERROR: [network" << net_cmdline.net_cmd()
<< "]: Unable to Acquire Network Semaphore: " << e.what();
} catch (const std::exception& e) {
LOG(ERROR) << "ERROR: [networkf]: " << e.what();
Expand Down
21 changes: 11 additions & 10 deletions sdk/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef INCLUDED_SDK_CONFIG_H
#define INCLUDED_SDK_CONFIG_H

#include "sdk/bbs_directories.h"
#include "sdk/vardec.h"
#include <filesystem>
#include <map>
Expand Down Expand Up @@ -173,7 +174,7 @@ struct config_t {
system_toggles_t toggles;
};

class Config final {
class Config final : public BbsDirectories {
public:
Config(std::filesystem::path root_directory, config_t config);
explicit Config(const Config& c);
Expand Down Expand Up @@ -209,20 +210,20 @@ class Config final {
[[nodiscard]] int config_revision_number() const { return config_.header.config_revision_number; }
void config_revision_number(int v) { config_.header.config_revision_number = v; }

[[nodiscard]] std::string root_directory() const { return root_directory_.string(); }
[[nodiscard]] std::string datadir() const { return datadir_; }
[[nodiscard]] std::string root_directory() const override { return root_directory_.string(); }
[[nodiscard]] std::string datadir() const override { return datadir_; }
void datadir(const std::string& d) { config_.datadir = d; }
[[nodiscard]] std::string msgsdir() const { return msgsdir_; }
[[nodiscard]] std::string msgsdir() const override { return msgsdir_; }
void msgsdir(const std::string& d) { config_.msgsdir = d; }
[[nodiscard]] std::string gfilesdir() const { return gfilesdir_; }
[[nodiscard]] std::string gfilesdir() const override { return gfilesdir_; }
void gfilesdir(const std::string& d) { config_.gfilesdir = d; }
[[nodiscard]] std::string menudir() const { return menudir_; }
[[nodiscard]] std::string menudir() const override { return menudir_; }
void menudir(const std::string& d) { config_.menudir = d; }
[[nodiscard]] std::string dloadsdir() const { return dloadsdir_; }
[[nodiscard]] std::string dloadsdir() const override { return dloadsdir_; }
void dloadsdir(const std::string& d) { config_.dloadsdir = d; }
[[nodiscard]] std::string scriptdir() const { return script_dir_; }
[[nodiscard]] std::string scriptdir() const override { return script_dir_; }
void scriptdir(const std::string& d) { config_.scriptdir = d; }
[[nodiscard]] std::string logdir() const { return log_dir_; }
[[nodiscard]] std::string logdir() const override { return log_dir_; }
void logdir(const std::string& d) { config_.logdir = d; }

// moved from wwiv.ini
Expand All @@ -236,7 +237,7 @@ class Config final {
/**
* Returns the scrarch directory for a given node.
*/
[[nodiscard]] std::filesystem::path scratch_dir(int node) const;
[[nodiscard]] std::filesystem::path scratch_dir(int node) const override;

[[nodiscard]] int num_instances() const { return config_.num_instances; }
void num_instances(int n) { config_.num_instances = n; }
Expand Down
Loading

0 comments on commit 3d4d35a

Please sign in to comment.