From 22b24a81d96994d712fbe6aeca4932e55f723ee3 Mon Sep 17 00:00:00 2001 From: Ludvig Strigeus Date: Sun, 7 Oct 2018 19:42:51 +0200 Subject: [PATCH] Add a test for ip_to_peer_map --- Tests/Tests.cpp | 126 +++++++++++++++++++++++++ Tests/Tests.vcxproj | 178 ++++++++++++++++++++++++++++++++++++ Tests/Tests.vcxproj.filters | 36 ++++++++ Tests/stdafx.cpp | 5 + Tests/stdafx.h | 14 +++ TunSafe.sln | 10 ++ 6 files changed, 369 insertions(+) create mode 100644 Tests/Tests.cpp create mode 100644 Tests/Tests.vcxproj create mode 100644 Tests/Tests.vcxproj.filters create mode 100644 Tests/stdafx.cpp create mode 100644 Tests/stdafx.h diff --git a/Tests/Tests.cpp b/Tests/Tests.cpp new file mode 100644 index 0000000..f8f70a7 --- /dev/null +++ b/Tests/Tests.cpp @@ -0,0 +1,126 @@ +// Tests.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include "stdafx.h" +#include "../ip_to_peer_map.h" +#include "../tunsafe_endian.h" +#include "../bit_ops.h" +#include +class RoutingTrie32Ref { + typedef void *Value; + typedef uint32 NodePtr; + enum { + BITS = 32 + }; + + struct Node { + Value value; + NodePtr nodes[2]; + uint8 shift, cidr; + uint32 key; + }; + + std::vector nodes_; + NodePtr root_; + +#define NEXT_NODE(n, key) n->nodes[(key >> n->shift) & 1] + + static int CommonBits(const uint32 a, const uint32 b) { + return 32 - FindHighestSetBit32(a ^ b); + } + + NodePtr NewNode(uint32 key, int cidr, Value value) { + nodes_.emplace_back(); + Node *n = &nodes_.back(); + if (n) { + n->nodes[0] = n->nodes[1] = 0; + n->cidr = cidr; + n->shift = (~cidr & 31); + n->value = value; + n->key = key; + } + return (uint32)nodes_.size(); + } + +public: + RoutingTrie32Ref() : root_() {} + ~RoutingTrie32Ref() { + + } + + __declspec(noinline) Value Lookup(const uint8 *key_in) { + NodePtr ni = root_; + Node *n; + Value value = NULL; + uint32 key = ReadBE32(key_in); + while (ni != NULL && (n = &nodes_[ni - 1], CommonBits(key, n->key) >= n->cidr)) { + value = n->value ? n->value : value; + ni = n->nodes[(key >> n->shift) & 1]; + } + return value; + } + + Node *deref(NodePtr x) { return &nodes_[x - 1]; } + + bool Insert(const uint8 *key_in, int cidr, Value value) { + NodePtr p = 0, no = root_; + uint32 key = ReadBE32(key_in); + while (no != NULL && (deref(no)->cidr <= cidr && CommonBits(key, deref(no)->key) >= deref(no)->cidr)) { + if (deref(no)->cidr == cidr) { + deref(no)->value = value; + return true; + } + p = no;// &NEXT_NODE(deref(no), key); + no = NEXT_NODE(deref(p), key); + } + NodePtr n = NewNode(key, cidr, value); + if (!n) + return false; + + if (no != NULL) { + int cbits = CommonBits(deref(no)->key, key); + if (cbits < cidr) { + NodePtr nn = NewNode(key, cbits, NULL); + if (!nn) { + // delete n; + return false; + } + NEXT_NODE(deref(nn), key) = n; + n = nn; + } + NEXT_NODE(deref(n), deref(no)->key) = no; + } + if (p == NULL) + root_ = n; + else + NEXT_NODE(deref(p), key) = n; + return true; + } + +}; + + + +int main() { + RoutingTrie32Ref ref; + RoutingTrie32 test; + + for (int i = 0; i < 256; i++) { + uint8 ip[4]; + uint32 ipv = 0x10200000 + i * 256; + WriteBE32(ip, ipv); + ref.Insert(ip, 24, (void*)(i * 2)); + void*x = (void*)(i * 2); + test.Insert(ipv, 24, &x); + } + + uint32 iters = 0x800000; + for (uint32 i = 0; i < iters; i++) { + uint32 j = i * 944676833;// (i & 0xF) + (i & ~0xF) * 16 + 0xF0; + uint8 xx[4] = { (uint8)(j >> 24),(uint8)(j >> 16),(uint8)(j >> 8),(uint8)j }; + void *ans1 = ref.Lookup(xx); + void *ans2 = test.Lookup(j); + assert(ans1 == ans2); + } + +} diff --git a/Tests/Tests.vcxproj b/Tests/Tests.vcxproj new file mode 100644 index 0000000..9305590 --- /dev/null +++ b/Tests/Tests.vcxproj @@ -0,0 +1,178 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB} + Win32Proj + Tests + 10.0.17134.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)build\$(Platform)_$(Configuration)\ + $(SolutionDir)build\$(Platform)_$(Configuration)\obj\$(ProjectName)\ + + + true + $(SolutionDir)build\$(Platform)_$(Configuration)\ + $(SolutionDir)build\$(Platform)_$(Configuration)\obj\$(ProjectName)\ + + + false + $(SolutionDir)build\$(Platform)_$(Configuration)\ + $(SolutionDir)build\$(Platform)_$(Configuration)\obj\$(ProjectName)\ + + + false + $(SolutionDir)build\$(Platform)_$(Configuration)\ + $(SolutionDir)build\$(Platform)_$(Configuration)\obj\$(ProjectName)\ + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdafx.h + + + Console + true + + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdafx.h + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdafx.h + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdafx.h + + + Console + true + true + true + + + + + + + + + + Create + Create + Create + Create + + + + + + + \ No newline at end of file diff --git a/Tests/Tests.vcxproj.filters b/Tests/Tests.vcxproj.filters new file mode 100644 index 0000000..cef526e --- /dev/null +++ b/Tests/Tests.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/Tests/stdafx.cpp b/Tests/stdafx.cpp new file mode 100644 index 0000000..79af02b --- /dev/null +++ b/Tests/stdafx.cpp @@ -0,0 +1,5 @@ +// pch.cpp: source file corresponding to pre-compiled header; necessary for compilation to succeed + +#include "stdafx.h" + +// In general, ignore this file, but keep it around if you are using pre-compiled headers. diff --git a/Tests/stdafx.h b/Tests/stdafx.h new file mode 100644 index 0000000..b04e71e --- /dev/null +++ b/Tests/stdafx.h @@ -0,0 +1,14 @@ +// Tips for Getting Started: +// 1. Use the Solution Explorer window to add/manage files +// 2. Use the Team Explorer window to connect to source control +// 3. Use the Output window to see build output and other messages +// 4. Use the Error List window to view errors +// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project +// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file + +#ifndef PCH_H +#define PCH_H + +// TODO: add headers that you want to pre-compile here + +#endif //PCH_H diff --git a/TunSafe.sln b/TunSafe.sln index ff47cf6..f059b46 100644 --- a/TunSafe.sln +++ b/TunSafe.sln @@ -7,6 +7,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TunSafe", "TunSafe.vcxproj" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ts", "ts.vcxproj", "{443E105E-8D7C-401F-BD41-D3F56C76104B}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tests", "Tests\Tests.vcxproj", "{F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -31,6 +33,14 @@ Global {443E105E-8D7C-401F-BD41-D3F56C76104B}.Release|Win32.Build.0 = Release|Win32 {443E105E-8D7C-401F-BD41-D3F56C76104B}.Release|x64.ActiveCfg = Release|x64 {443E105E-8D7C-401F-BD41-D3F56C76104B}.Release|x64.Build.0 = Release|x64 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Debug|Win32.ActiveCfg = Debug|Win32 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Debug|Win32.Build.0 = Debug|Win32 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Debug|x64.ActiveCfg = Debug|x64 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Debug|x64.Build.0 = Debug|x64 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Release|Win32.ActiveCfg = Release|Win32 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Release|Win32.Build.0 = Release|Win32 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Release|x64.ActiveCfg = Release|x64 + {F9E2DF08-4B9A-4205-82B4-C54718EAF2AB}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE