Merge branch 'staging' of ssh://github.com/loki-project/loki-network into staging

pull/236/head
Jeff Becker 6 years ago
commit 1e1b40fbab
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

3
.gitignore vendored

@ -47,4 +47,5 @@ rapidjson/
.idea .idea
.vscode .vscode
build64/ build64/
build2/ build2/
default.profraw

@ -16,6 +16,7 @@ option(JEMALLOC "use jemalloc. Not required on BSD" )
option(DEBIAN "build for debian" ) option(DEBIAN "build for debian" )
option(TESTNET "testnet build" ) option(TESTNET "testnet build" )
option(WITH_SHARED "build shared library") option(WITH_SHARED "build shared library")
option(WITH_COVERAGE "generate coverage data")
if(WIN32) if(WIN32)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
@ -44,6 +45,16 @@ add_compile_options(-Wvla)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fpermissive>) add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fpermissive>)
add_compile_options(-Wno-unused-function -Wno-deprecated-declarations -Wno-unknown-pragmas) add_compile_options(-Wno-unused-function -Wno-deprecated-declarations -Wno-unknown-pragmas)
if (WITH_COVERAGE)
if (USING_CLANG)
add_compile_options( -fprofile-instr-generate -fcoverage-mapping )
link_libraries( -fprofile-instr-generate )
else()
add_compile_options( --coverage -g0 )
link_libraries( --coverage )
endif()
endif()
# these vars are set by the cmake toolchain spec # these vars are set by the cmake toolchain spec
if (WOW64_CROSS_COMPILE OR WIN64_CROSS_COMPILE) if (WOW64_CROSS_COMPILE OR WIN64_CROSS_COMPILE)
# dynamic linking does this all the time # dynamic linking does this all the time
@ -619,6 +630,7 @@ set(TEST_SRC
test/test_llarp_dns.cpp test/test_llarp_dns.cpp
test/test_llarp_dnsd.cpp test/test_llarp_dnsd.cpp
test/test_llarp_encrypted_frame.cpp test/test_llarp_encrypted_frame.cpp
test/test_llarp_router.cpp
test/test_llarp_router_contact.cpp test/test_llarp_router_contact.cpp
test/util/test_llarp_util_aligned.cpp test/util/test_llarp_util_aligned.cpp
test/util/test_llarp_util_bencode.cpp test/util/test_llarp_util_bencode.cpp

@ -60,6 +60,9 @@ CLANG ?= OFF
CROSS ?= OFF CROSS ?= OFF
# build liblokinet-shared.so # build liblokinet-shared.so
SHARED_LIB ?= ON SHARED_LIB ?= ON
# enable generating coverage
COVERAGE ?= OFF
COVERAGE_OUTDIR ?= "$(TMPDIR)/lokinet-coverage"
# cmake generator type # cmake generator type
CMAKE_GEN ?= Unix Makefiles CMAKE_GEN ?= Unix Makefiles
@ -71,6 +74,8 @@ CONFIG_CMD = $(shell /bin/echo -n "cd '$(BUILD_ROOT)' && " ; /bin/echo -n "cmake
ANALYZE_CONFIG_CMD = $(shell /bin/echo -n "cd '$(BUILD_ROOT)' && " ; /bin/echo -n "$(SCAN_BUILD) cmake -G'$(CMAKE_GEN)' -DCMAKE_CROSSCOMPILING=$(CROSS) -DUSING_CLANG=$(CLANG) -DSTATIC_LINK=$(STATIC_LINK) -DUSE_NETNS=$(NETNS) -DUSE_AVX2=$(AVX2) -DUSE_LIBABYSS=$(JSONRPC) -DNON_PC_TARGET=$(NON_PC_TARGET) -DWITH_SHARED=$(SHARED_LIB) '$(REPO)'") ANALYZE_CONFIG_CMD = $(shell /bin/echo -n "cd '$(BUILD_ROOT)' && " ; /bin/echo -n "$(SCAN_BUILD) cmake -G'$(CMAKE_GEN)' -DCMAKE_CROSSCOMPILING=$(CROSS) -DUSING_CLANG=$(CLANG) -DSTATIC_LINK=$(STATIC_LINK) -DUSE_NETNS=$(NETNS) -DUSE_AVX2=$(AVX2) -DUSE_LIBABYSS=$(JSONRPC) -DNON_PC_TARGET=$(NON_PC_TARGET) -DWITH_SHARED=$(SHARED_LIB) '$(REPO)'")
COVERAGE_CONFIG_CMD = $(shell /bin/echo -n "cd '$(BUILD_ROOT)' && " ; /bin/echo -n "cmake -G'$(CMAKE_GEN)' -DCMAKE_CROSSCOMPILING=$(CROSS) -DUSING_CLANG=$(CLANG) -DSTATIC_LINK=$(STATIC_LINK) -DUSE_NETNS=$(NETNS) -DUSE_AVX2=$(AVX2) -DUSE_LIBABYSS=$(JSONRPC) -DNON_PC_TARGET=$(NON_PC_TARGET) -DWITH_SHARED=$(SHARED_LIB) -DWITH_COVERAGE=yes '$(REPO)'")
TARGETS = $(REPO)/lokinet TARGETS = $(REPO)/lokinet
SIGS = $(TARGETS:=.sig) SIGS = $(TARGETS:=.sig)
EXE = $(BUILD_ROOT)/lokinet EXE = $(BUILD_ROOT)/lokinet
@ -192,6 +197,21 @@ analyze-config: clean
analyze: analyze-config analyze: analyze-config
cd '$(BUILD_ROOT)' && $(SCAN_BUILD) $(MAKE) cd '$(BUILD_ROOT)' && $(SCAN_BUILD) $(MAKE)
coverage-config: clean
mkdir -p '$(BUILD_ROOT)'
$(COVERAGE_CONFIG_CMD)
coverage: coverage-config
$(MAKE) -C $(BUILD_ROOT) -j 12
$(TEST_EXE) || true # continue even if tests fail
mkdir -p "$(COVERAGE_OUTDIR)"
ifeq ($(CLANG),OFF)
gcovr -r . --branches --html --html-details -o "$(COVERAGE_OUTDIR)/lokinet.html"
else
llvm-profdata merge default.profraw -output $(BUILD_ROOT)/profdata
llvm-cov show -format=html -output-dir="$(COVERAGE_OUTDIR)" -instr-profile "$(BUILD_ROOT)/profdata" "$(BUILD_ROOT)/testAll" $(shell find ./llarp -type f)
endif
lint: $(LINT_CHECK) lint: $(LINT_CHECK)
%.cpp-check: %.cpp %.cpp-check: %.cpp
@ -209,13 +229,13 @@ debian-configure:
debian: debian-configure debian: debian-configure
$(MAKE) -C '$(BUILD_ROOT)' $(MAKE) -C '$(BUILD_ROOT)'
cp $(EXE) lokinet cp $(EXE) lokinet
cp $(BUILD_ROOT)/rcutil lokinet-rcutil cp $(BUILD_ROOT)/rcutil lokinet-rcutil
debian-test: debian-test:
$(TEST_EXE) $(TEST_EXE)
install: install:
$(MAKE) -C '$(BUILD_ROOT)' install $(MAKE) -C '$(BUILD_ROOT)' install
fuzz-configure: clean fuzz-configure: clean

@ -26,7 +26,9 @@ namespace llarp
std::ifstream f; std::ifstream f;
f.open(fname, std::ios::binary); f.open(fname, std::ios::binary);
if(!f.is_open()) if(!f.is_open())
{
return false; return false;
}
size_t sz = 0; size_t sz = 0;
f.seekg(0, std::ios::end); f.seekg(0, std::ios::end);
sz = f.tellg(); sz = f.tellg();
@ -41,7 +43,9 @@ namespace llarp
byte_t tmp[128]; byte_t tmp[128];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(sz > sizeof(tmp)) if(sz > sizeof(tmp))
{
return false; return false;
}
f.read((char*)tmp, sz); f.read((char*)tmp, sz);
return BDecode(&buf); return BDecode(&buf);
} }
@ -52,7 +56,9 @@ namespace llarp
byte_t tmp[128]; byte_t tmp[128];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!BEncode(&buf)) if(!BEncode(&buf))
{
return false; return false;
}
std::ofstream f; std::ofstream f;
f.open(fname, std::ios::binary); f.open(fname, std::ios::binary);

@ -21,7 +21,7 @@
namespace llarp namespace llarp
{ {
int inline int
tcp_conn::read(byte_t* buf, size_t sz) tcp_conn::read(byte_t* buf, size_t sz)
{ {
if(sz == 0) if(sz == 0)
@ -53,14 +53,14 @@ namespace llarp
return 0; return 0;
} }
void inline void
tcp_conn::flush_write() tcp_conn::flush_write()
{ {
connected(); connected();
ev_io::flush_write(); ev_io::flush_write();
} }
ssize_t inline ssize_t
tcp_conn::do_write(void* buf, size_t sz) tcp_conn::do_write(void* buf, size_t sz)
{ {
if(_shouldClose) if(_shouldClose)
@ -74,7 +74,7 @@ namespace llarp
#endif #endif
} }
void inline void
tcp_conn::connect() tcp_conn::connect()
{ {
socklen_t slen = sizeof(sockaddr_in); socklen_t slen = sizeof(sockaddr_in);
@ -100,7 +100,7 @@ namespace llarp
} }
} }
int inline int
tcp_serv::read(byte_t*, size_t) tcp_serv::read(byte_t*, size_t)
{ {
int new_fd = ::accept(fd, nullptr, nullptr); int new_fd = ::accept(fd, nullptr, nullptr);

@ -16,6 +16,7 @@ Build requirements:
* CMake * CMake
* C++ 17 capable C++ compiler * C++ 17 capable C++ compiler
* rapidjson (if enabling jsonrpc server) * rapidjson (if enabling jsonrpc server)
* gcovr (if generating test coverage with gcc)
### Linux ### Linux
@ -27,22 +28,22 @@ build:
$ make -j8 $ make -j8
install: install:
$ sudo make install $ sudo make install
### FreeBSD ### FreeBSD
build: build:
$ pkg install wget cmake git $ pkg install wget cmake git
$ git clone https://github.com/loki-project/loki-network $ git clone https://github.com/loki-project/loki-network
$ cd loki-network $ cd loki-network
$ gmake -j8 $ gmake -j8
install (root): install (root):
# gmake install # gmake install
## Windows ## Windows
@ -55,10 +56,10 @@ build (where `$ARCH` is your platform - `i686` or `x86_64`):
$ cmake .. -DCMAKE_BUILD_TYPE=[Debug|Release] -DSTATIC_LINK=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DDNS_PORT=53 -G 'Unix Makefiles' $ cmake .. -DCMAKE_BUILD_TYPE=[Debug|Release] -DSTATIC_LINK=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DDNS_PORT=53 -G 'Unix Makefiles'
install (elevated) to `$PROGRAMFILES/lokinet` or `$ProgramFiles(x86)/lokinet`: install (elevated) to `$PROGRAMFILES/lokinet` or `$ProgramFiles(x86)/lokinet`:
$ make install $ make install
if cross-compiling, install mingw-w64 from your distro's package manager, or [build from source](https://sourceforge.net/p/mingw-w64/wiki2/Cross%20Win32%20and%20Win64%20compiler/), then: if cross-compiling, install mingw-w64 from your distro's package manager, or [build from source](https://sourceforge.net/p/mingw-w64/wiki2/Cross%20Win32%20and%20Win64%20compiler/), then:
$ mkdir -p build; cd build $ mkdir -p build; cd build
$ export COMPILER=clang # if using clang for windows $ export COMPILER=clang # if using clang for windows

@ -0,0 +1,123 @@
#include <router.hpp>
#include <crypto.hpp>
#include <functional>
#include <random>
#include <gtest/gtest.h>
std::string
randFilename()
{
static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
std::random_device rd;
std::uniform_int_distribution< size_t > dist{0, sizeof(alphabet) - 2};
std::string filename;
for(size_t i = 0; i < 5; ++i)
{
filename.push_back(alphabet[dist(rd)]);
}
filename.push_back('.');
for(size_t i = 0; i < 5; ++i)
{
filename.push_back(alphabet[dist(rd)]);
}
return filename;
}
struct FileGuard
{
const fs::path &p;
explicit FileGuard(const fs::path &_p) : p(_p)
{
}
~FileGuard()
{
if(fs::exists(fs::status(p)))
{
fs::remove(p);
}
}
};
using FindOrCreateFunc = std::function< bool(llarp::Crypto *, const fs::path &,
llarp::SecretKey &) >;
struct FindOrCreate : public ::testing::TestWithParam< FindOrCreateFunc >
{
FindOrCreate() : crypto(llarp::Crypto::sodium{})
{
}
llarp::Crypto crypto;
};
// Concerns
// - file missing
// - file empty
// - happy path
TEST_P(FindOrCreate, find_file_missing)
{
// File missing. Should create a new file
llarp::SecretKey key;
fs::path p = randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
FileGuard guard(p);
ASSERT_TRUE(GetParam()(&crypto, p, key));
ASSERT_TRUE(fs::exists(fs::status(p)));
ASSERT_FALSE(key.IsZero());
}
TEST_P(FindOrCreate, find_file_empty)
{
// File empty.
llarp::SecretKey key;
fs::path p = randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
std::fstream f;
f.open(p.string(), std::ios::out);
f.close();
FileGuard guard(p);
ASSERT_FALSE(GetParam()(&crypto, p, key));
// Verify we didn't delete an invalid file
ASSERT_TRUE(fs::exists(fs::status(p)));
}
TEST_P(FindOrCreate, happy_path)
{
// happy path.
llarp::SecretKey key;
fs::path p = randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
std::ofstream f;
f.open(p.string(), std::ios::out);
std::fill_n(std::ostream_iterator< byte_t >(f), key.size(), 0x20);
f.close();
FileGuard guard(p);
ASSERT_TRUE(GetParam()(&crypto, p, key));
// Verify we didn't delete the file
ASSERT_TRUE(fs::exists(fs::status(p)));
}
FindOrCreateFunc findOrCreateFunc[] = {llarp_findOrCreateEncryption,
llarp_findOrCreateIdentity};
INSTANTIATE_TEST_CASE_P(TestRouter, FindOrCreate,
::testing::ValuesIn(findOrCreateFunc));
Loading…
Cancel
Save