ddnet/src/test/serverbrowser.cpp
heinrich5991 dcd76fd3e1 Add support for Rust code in DDNet
The glue is done using the [cxx crate](https://cxx.rs/) on the Rust
side.

As a proof-of-concept, only a small console command (`rust_version`)
printing the currently used Rust version was added.

You can generate and open the Rust documentation using
`DDNET_TEST_NO_LINK=1 cargo doc --open`.

You can run the Rust tests using `cmake --build <build dir> --target
run_rust_tests`, they're automatically included in the `run_tests`
target as well.

Rust tests don't work on Windows in debug mode on Windows because Rust
cannot currently link with the debug version of the C stdlib on Windows:
https://github.com/rust-lang/rust/issues/39016.

---

The stuff in `src/rust-bridge` is generated using
```
cxxbridge src/engine/shared/rust_version.rs --output src/rust-bridge/engine/shared/rust_version.cpp --output src/rust-bridge/engine/shared/rust_version.h
cxxbridge src/engine/console.rs --output src/rust-bridge/cpp/console.cpp --output src/rust-bridge/cpp/console.h
```
2022-10-19 23:46:06 +02:00

100 lines
3.9 KiB
C++

#include <gtest/gtest.h>
#include <memory>
#include <engine/client/serverbrowser_ping_cache.h>
#include <engine/console.h>
#include <engine/engine.h>
#include <engine/shared/config.h>
#include <engine/storage.h>
#include <test/test.h>
TEST(ServerBrowser, PingCache)
{
CTestInfo Info;
Info.m_DeleteTestStorageFilesOnSuccess = true;
auto pConsole = CreateConsole(CFGFLAG_CLIENT);
auto pStorage = std::unique_ptr<IStorage>(Info.CreateTestStorage());
auto pPingCache = std::unique_ptr<IServerBrowserPingCache>(CreateServerBrowserPingCache(pConsole.get(), pStorage.get()));
NETADDR Localhost4, Localhost6, OtherLocalhost4, OtherLocalhost6;
ASSERT_FALSE(net_addr_from_str(&Localhost4, "127.0.0.1:8303"));
ASSERT_FALSE(net_addr_from_str(&Localhost6, "[::1]:8304"));
ASSERT_FALSE(net_addr_from_str(&OtherLocalhost4, "127.0.0.1:8305"));
ASSERT_FALSE(net_addr_from_str(&OtherLocalhost6, "[::1]:8306"));
EXPECT_LT(net_addr_comp(&Localhost4, &Localhost6), 0);
NETADDR aLocalhostBoth[2] = {Localhost4, Localhost6};
EXPECT_EQ(pPingCache->NumEntries(), 0);
EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), -1);
EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), -1);
EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), -1);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), -1);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), -1);
pPingCache->Load();
EXPECT_EQ(pPingCache->NumEntries(), 0);
EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), -1);
EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), -1);
EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), -1);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), -1);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), -1);
// Newer pings overwrite older.
pPingCache->CachePing(Localhost4, 123);
pPingCache->CachePing(Localhost4, 234);
pPingCache->CachePing(Localhost4, 345);
pPingCache->CachePing(Localhost4, 456);
pPingCache->CachePing(Localhost4, 567);
pPingCache->CachePing(Localhost4, 678);
pPingCache->CachePing(Localhost4, 789);
pPingCache->CachePing(Localhost4, 890);
pPingCache->CachePing(Localhost4, 901);
pPingCache->CachePing(Localhost4, 135);
pPingCache->CachePing(Localhost4, 246);
pPingCache->CachePing(Localhost4, 357);
pPingCache->CachePing(Localhost4, 468);
pPingCache->CachePing(Localhost4, 579);
pPingCache->CachePing(Localhost4, 680);
pPingCache->CachePing(Localhost4, 791);
pPingCache->CachePing(Localhost4, 802);
pPingCache->CachePing(Localhost4, 913);
EXPECT_EQ(pPingCache->NumEntries(), 1);
EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 913);
EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), -1);
EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 913);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 913);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), -1);
pPingCache->CachePing(Localhost4, 234);
pPingCache->CachePing(Localhost6, 345);
EXPECT_EQ(pPingCache->NumEntries(), 2);
EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 234);
EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), 345);
EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 234);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 234);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), 345);
// Port doesn't matter for overwriting.
pPingCache->CachePing(Localhost4, 1337);
EXPECT_EQ(pPingCache->NumEntries(), 2);
EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 1337);
EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), 345);
EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 345);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 1337);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), 345);
pPingCache.reset(CreateServerBrowserPingCache(pConsole.get(), pStorage.get()));
// Persistence.
pPingCache->Load();
EXPECT_EQ(pPingCache->NumEntries(), 2);
EXPECT_EQ(pPingCache->GetPing(&Localhost4, 1), 1337);
EXPECT_EQ(pPingCache->GetPing(&Localhost6, 1), 345);
EXPECT_EQ(pPingCache->GetPing(aLocalhostBoth, 2), 345);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost4, 1), 1337);
EXPECT_EQ(pPingCache->GetPing(&OtherLocalhost6, 1), 345);
}