diff --git a/api/fs/fat.hpp b/api/fs/fat.hpp index 08b89b8c01..3a5530fc0a 100644 --- a/api/fs/fat.hpp +++ b/api/fs/fat.hpp @@ -71,6 +71,8 @@ namespace fs return "Invalid fat type"; } + bool is_valid() const noexcept override { return initialized_; }; + uint64_t block_size() const noexcept override { return device.block_size(); } /// ----------------------------------------------------- /// @@ -111,6 +113,8 @@ namespace fs // initialize filesystem by providing base sector void init(const void* base_sector); + bool initialized_{false}; + // return a list of entries from directory entries at @sector typedef delegate on_internal_ls_func; void int_ls(uint32_t sector, Dirvec_ptr, on_internal_ls_func) const; diff --git a/api/fs/filesystem.hpp b/api/fs/filesystem.hpp index 24ea4ac46b..9d329354a0 100644 --- a/api/fs/filesystem.hpp +++ b/api/fs/filesystem.hpp @@ -71,6 +71,9 @@ namespace fs { /** Returns the name of this filesystem */ virtual std::string name() const = 0; + /** Tells us is the filesystem is ok */ + virtual bool is_valid() const noexcept { return false; }; + /** Returns the block size of this filesystem */ virtual uint64_t block_size() const = 0; diff --git a/api/hal/detail/machine.hpp b/api/hal/detail/machine.hpp index 719d870d0e..e1e14147c0 100644 --- a/api/hal/detail/machine.hpp +++ b/api/hal/detail/machine.hpp @@ -160,7 +160,7 @@ namespace os::detail { } template - void remove(int i) { + void remove(size_t i) { auto& vec = get_vector(); if(UNLIKELY(vec.size() < i)) throw Machine_access_error{"Requested machine part not found: " + std::to_string(i)}; diff --git a/api/net/addr.hpp b/api/net/addr.hpp index 9c5aa1cab5..47f4f43085 100644 --- a/api/net/addr.hpp +++ b/api/net/addr.hpp @@ -122,6 +122,19 @@ union Addr { bool operator<=(const Addr& other) const noexcept { return (*this < other or *this == other); } + + bool operator==(const net::ip4::Addr& other) const noexcept + { return ip4_.addr == other; } + + bool operator!=(const net::ip4::Addr& other) const noexcept + { return ip4_.addr != other; } + + bool operator==(const net::ip6::Addr& other) const noexcept + { return ip6_ == other; } + + bool operator!=(const net::ip6::Addr& other) const noexcept + { return ip6_ != other; } + private: struct { uint64_t big; @@ -135,4 +148,10 @@ union Addr { static_assert(sizeof(Addr) == sizeof(ip6::Addr)); +inline bool operator==(const ip4::Addr& lhs, const Addr& rhs) noexcept { return rhs == lhs; } +inline bool operator!=(const ip4::Addr& lhs, const Addr& rhs) noexcept { return !(lhs == rhs); } + +inline bool operator==(const ip6::Addr& lhs, const Addr& rhs) noexcept { return rhs == lhs; } +inline bool operator!=(const ip6::Addr& lhs, const Addr& rhs) noexcept { return !(lhs == rhs); } + } diff --git a/api/net/inet.hpp b/api/net/inet.hpp index 0123b6a8e9..a95ff5cfb0 100644 --- a/api/net/inet.hpp +++ b/api/net/inet.hpp @@ -535,4 +535,34 @@ namespace net { }; } +namespace net::ip4 { + inline bool operator==(const net::ip4::Addr& lhs, const net::Inet& rhs) noexcept { + return lhs == rhs.ip_addr(); + } + inline bool operator==(const net::Inet& lhs, const net::ip4::Addr& rhs) noexcept { + return lhs.ip_addr() == rhs; + } + inline bool operator!=(const net::ip4::Addr& lhs, const net::Inet& rhs) noexcept { + return !(lhs == rhs); + } + inline bool operator!=(const net::Inet& lhs, const net::ip4::Addr& rhs) noexcept { + return !(lhs == rhs); + } +} // namespace net::ip4 + +namespace net::ip6 { + inline bool operator==(const net::ip6::Addr& lhs, const net::Inet& rhs) noexcept { + return lhs == rhs.ip6_addr(); + } + inline bool operator==(const net::Inet& lhs, const net::ip6::Addr& rhs) noexcept { + return lhs.ip6_addr() == rhs; + } + inline bool operator!=(const net::ip6::Addr& lhs, const net::Inet& rhs) noexcept { + return !(lhs == rhs); + } + inline bool operator!=(const net::Inet& lhs, const net::ip6::Addr& rhs) noexcept { + return !(lhs == rhs); + } +} // namespace net::ip6 + #endif diff --git a/api/net/router.hpp b/api/net/router.hpp index 86d1a64ed4..34c20ffcfa 100644 --- a/api/net/router.hpp +++ b/api/net/router.hpp @@ -214,8 +214,9 @@ namespace net { bytes_fwd{Statman::get().get_or_create(Stat::UINT64, "router.bytes_fwd").get_uint64()} { INFO("Router", "Router created with %lu routes", tbl.size()); - for(auto& route : routing_table_) + for([[maybe_unused]] auto& route : routing_table_) { INFO2("%s", route.to_string().c_str()); + } } void set_routing_table(Routing_table tbl) { diff --git a/api/util/statman.hpp b/api/util/statman.hpp index 0ef29ea482..4e888ead76 100644 --- a/api/util/statman.hpp +++ b/api/util/statman.hpp @@ -72,7 +72,7 @@ class Stat { void make_counter() noexcept { m_bits &= ~GAUGE_BIT; } void make_gauge() noexcept { m_bits |= GAUGE_BIT; } - const char* name() const noexcept { return name_; } + const std::string name() const noexcept { return name_; } bool unused() const noexcept { return name_[0] == 0; } const float& get_float() const; @@ -116,7 +116,7 @@ class Statman { // retrieve stat based on address from stats counter: &stat.get_xxx() Stat& get(const Stat* addr); // if you know the name of a statistic already - Stat& get_by_name(const char* name); + Stat& get_by_name(std::string_view name); // retrieve stat or create if it doesnt exists Stat& get_or_create(const Stat::Stat_type type, const std::string& name); // free/delete stat based on address from stats counter diff --git a/deps/lest/default.nix b/deps/lest/default.nix index 37ad42d8dc..9b3bb23b48 100644 --- a/deps/lest/default.nix +++ b/deps/lest/default.nix @@ -4,7 +4,7 @@ }: stdenv.mkDerivation rec { pname = "lest"; - version = "1.36.0"; + version = "1.37.0"; meta = { description = "A tiny C++11 test framework – lest errors escape testing."; @@ -15,7 +15,7 @@ stdenv.mkDerivation rec { src = fetchGit { url = "https://github.com/martinmoene/lest.git"; ref = "refs/tags/v${version}"; - rev = "57197f32f2c7d3f3d3664a9010d3ff181a40f6ca"; + rev = "fe1996f438ab8d1611c0324a156f9130ed971e9f"; }; cmakeBuildType = "Debug"; diff --git a/src/fs/fat.cpp b/src/fs/fat.cpp index 8e3423b96f..afd1a43b4e 100644 --- a/src/fs/fat.cpp +++ b/src/fs/fat.cpp @@ -182,6 +182,7 @@ namespace fs this->lba_base, this->lba_size, this->lba_size * 512); // on_init callback + initialized_ = true; on_init(no_error, *this); }) ); diff --git a/src/util/statman.cpp b/src/util/statman.cpp index 41062f847f..5ae199bc64 100644 --- a/src/util/statman.cpp +++ b/src/util/statman.cpp @@ -111,7 +111,7 @@ Stat& Statman::get(const Stat* st) throw std::out_of_range("Not a valid stat in this statman instance"); } -Stat& Statman::get_by_name(const char* name) +Stat& Statman::get_by_name(const std::string_view name) { #ifdef INCLUDEOS_SMP_ENABLE std::lock_guard lock(this->stlock); @@ -119,7 +119,7 @@ Stat& Statman::get_by_name(const char* name) for (auto& stat : this->m_stats) { if (stat.unused() == false) { - if (strncmp(stat.name(), name, Stat::MAX_NAME_LEN) == 0) + if (stat.name() == name) return stat; } } @@ -129,7 +129,7 @@ Stat& Statman::get_by_name(const char* name) Stat& Statman::get_or_create(const Stat::Stat_type type, const std::string& name) { try { - auto& stat = get_by_name(name.c_str()); + auto& stat = get_by_name(name); if(type == stat.type()) return stat; } diff --git a/test/integration/kernel/exception/test.py b/test/integration/kernel/exception/test.py index 7da6adc905..4099f74007 100755 --- a/test/integration/kernel/exception/test.py +++ b/test/integration/kernel/exception/test.py @@ -17,7 +17,7 @@ def is_good(line): if (counter == expected): vm.exit(0, "All tests passed") -vm.on_output("\\x15\\x07\\t\*\*\*\* PANIC \*\*\*\*", is_good) +vm.on_output("\\x15\\x07\\t\\*\\*\\*\\* PANIC \\*\\*\\*\\*", is_good) vm.on_output("Divide-by-zero Error", is_good) vm.on_output("__cpu_exception", is_good) diff --git a/test/integration/kernel/smp/service.cpp b/test/integration/kernel/smp/service.cpp index 17c4cdd45f..239a57da4c 100644 --- a/test/integration/kernel/smp/service.cpp +++ b/test/integration/kernel/smp/service.cpp @@ -39,7 +39,7 @@ void Service::start() printf("CPU %i active \n", i); SMP::global_unlock(); - SMP::add_task([i]{ + SMP::add_task([]{ // NOTE: We can't call printf here as it's not SMP safe // Test regular malloc diff --git a/test/integration/memory/paging/service.cpp b/test/integration/memory/paging/service.cpp index 595ee71a69..e4a7c21724 100644 --- a/test/integration/memory/paging/service.cpp +++ b/test/integration/memory/paging/service.cpp @@ -414,6 +414,7 @@ int main() auto pml3 = __pml4->page_dir(__pml4->entry(magic_loc)); auto pml2 = pml3->page_dir(pml3->entry(magic_loc)); auto pml1 = pml2->page_dir(pml2->entry(magic_loc)); + (void) pml1; // Write-protect if (magic->reboots == 0) { @@ -421,6 +422,7 @@ int main() pml3 = __pml4->page_dir(__pml4->entry(mapped.lin)); pml2 = pml3->page_dir(pml3->entry(mapped.lin)); pml1 = pml2->page_dir(pml2->entry(mapped.lin)); + (void) pml1; protected_page[magic->i] = 'a'; mem::protect_range((uint64_t)protected_page, mem::Access::read); diff --git a/test/integration/net/dns/service.cpp b/test/integration/net/dns/service.cpp index fe9e1fefed..508c1913df 100644 --- a/test/integration/net/dns/service.cpp +++ b/test/integration/net/dns/service.cpp @@ -86,7 +86,7 @@ void Service::start() [] (net::Inet& inet) { const ip4::Addr level3{4, 2, 2, 1}; - const ip4::Addr google{8, 8, 8, 8}; + //const ip4::Addr google{8, 8, 8, 8}; static std::vector requests { // Having these here fails the test in some cases. A timing issue or a bug? diff --git a/test/misc/lest_util/nic_mock.hpp b/test/misc/lest_util/nic_mock.hpp index bbc9239801..554095cba3 100644 --- a/test/misc/lest_util/nic_mock.hpp +++ b/test/misc/lest_util/nic_mock.hpp @@ -130,7 +130,7 @@ class Nic_mock : public hw::Nic { transmit_to_physical_(std::move(pkt)); } - void transmit(net::Packet_ptr pkt) + void transmit(net::Packet_ptr pkt) // FIXME: this function is logging but not doing anything { NIC_INFO("transimtting packet"); //tx_queue_.emplace_back(std::move(ptr)); diff --git a/test/unit/fs/unit_fat.cpp b/test/unit/fs/unit_fat.cpp index 4a51b15d17..23329b5759 100644 --- a/test/unit/fs/unit_fat.cpp +++ b/test/unit/fs/unit_fat.cpp @@ -40,6 +40,11 @@ CASE("Initialize FAT fs") [&lest_env] (auto err, File_system& fs) { EXPECT(!err); + EXPECT(fs.is_valid() == true); + + // EXPECT(fs.name() == "FAT32"); // FIXME: test fails + Dirent dirent = fs.stat("/"); + EXPECT(dirent.is_valid() == true); }); } diff --git a/test/unit/fs/unit_fs.cpp b/test/unit/fs/unit_fs.cpp index 7f2c816941..5749f4853d 100644 --- a/test/unit/fs/unit_fs.cpp +++ b/test/unit/fs/unit_fs.cpp @@ -4,18 +4,19 @@ using namespace fs; -CASE("Initialize mock FS") +CASE("Initialize invalid FS") { fs::MemDisk memdisk {0, 0}; fs::Disk disk { memdisk }; - + EXPECT(disk.empty()); EXPECT(disk.device_id() >= 0); EXPECT(disk.name().size() > 1); // name0 disk.init_fs( [&] (fs::error_t error, fs::File_system& fs) { - EXPECT(error != fs::no_error); + (void) fs; + EXPECT(error != fs::no_error); // expecting failure }); - + } diff --git a/test/unit/fs/vfs_test.cpp b/test/unit/fs/vfs_test.cpp index 292aa1cd27..3663cafc35 100644 --- a/test/unit/fs/vfs_test.cpp +++ b/test/unit/fs/vfs_test.cpp @@ -27,6 +27,7 @@ class Person { public: explicit Person(std::string name, int age) : name_ {name}, age_ {age} {}; bool isBjarne() const { return (name_ == "Bjarne") ? true : false; } + bool isAdult() const { return (age_ >= 18) ? true : false; } private: std::string name_; int age_; @@ -75,7 +76,9 @@ CASE("VFS entries can contain arbitrary objects") EXPECT(info == "Person"); Person bjarne = e.obj(); EXPECT(bjarne.isBjarne() == true); + EXPECT(bjarne.isAdult() == true); EXPECT_THROWS(e.obj()); + // constness is checked const Person q {"Dennis", 70}; fs::VFS_entry f(q, "inspiration", "duh^2"); @@ -110,7 +113,7 @@ CASE("VFS can mount entries in a tree") EXPECT(fs::VFS::root().child_count() == 3); // get mounted objects of correct type - char our_char; + [[maybe_unused]] char our_char; EXPECT_THROWS(auto dir = fs::get("/mnt/chars/c")); EXPECT_NO_THROW(our_char = fs::get("/mnt/chars/c")); } diff --git a/test/unit/memory/alloc/buddy_alloc_test.cpp b/test/unit/memory/alloc/buddy_alloc_test.cpp index cdac74d4aa..e9788fd442 100644 --- a/test/unit/memory/alloc/buddy_alloc_test.cpp +++ b/test/unit/memory/alloc/buddy_alloc_test.cpp @@ -185,7 +185,7 @@ CASE("mem::buddy random ordered allocation then deallocation"){ int highest_i = 0; void* highest = nullptr; - for (int i = 0; i < addresses.size(); i++) { + for (size_t i = 0; i < addresses.size(); i++) { if (addresses.at(i) > highest) { highest = addresses.at(i); highest_i = i; @@ -198,7 +198,7 @@ CASE("mem::buddy random ordered allocation then deallocation"){ EXPECT(computed_use >= alloc.bytes_used()); // Deallocate - for (int i = 0; i < addresses.size(); i++) { + for (size_t i = 0; i < addresses.size(); i++) { auto addr = addresses.at(i); auto sz = sizes.at(i); if (addr) EXPECT(sz) ; diff --git a/test/unit/memory/alloc/pmr_alloc_test.cpp b/test/unit/memory/alloc/pmr_alloc_test.cpp index 97b43a0358..aea305c21c 100644 --- a/test/unit/memory/alloc/pmr_alloc_test.cpp +++ b/test/unit/memory/alloc/pmr_alloc_test.cpp @@ -61,8 +61,9 @@ CASE("pmr::Pmr_pool usage") { for (auto i : test::random) numbers.push_back(i); - for (auto i = 0; i < numbers.size(); i++) + for (size_t i = 0; i < numbers.size(); i++) { EXPECT(numbers.at(i) == test::random.at(i)); + } EXPECT(res->allocatable() <= sub_free - 1000); @@ -305,7 +306,7 @@ CASE("pmr::on_non_full event") { event_fired = false; EXPECT(not event_fired); - for (int i = 2; i < pool_cap / 2; i++) { + for (size_t i = 2; i < pool_cap / 2; i++) { numbers.push_back(i); } @@ -356,7 +357,7 @@ CASE("pmr::on_avail event") { event_fired = false; EXPECT(not event_fired); - for (int i = 2; i < 40_KiB; i++) { + for (size_t i = 2; i < 40_KiB; i++) { numbers.push_back(i); } diff --git a/test/unit/memory/generic/test_memory.cpp b/test/unit/memory/generic/test_memory.cpp index 3be60dccb8..e7750d37b2 100644 --- a/test/unit/memory/generic/test_memory.cpp +++ b/test/unit/memory/generic/test_memory.cpp @@ -285,16 +285,18 @@ CASE ("os::mem page table destructors") delete ptr; std::array ints {}; + EXPECT(ints.size() == 1000); + std::array tbls {}; + EXPECT(sizeof(tbls)== 8*sizeof(Pml4*)); Pml4* ars = new Pml4[8]; delete[] ars; EXPECT(sizeof(Pml4) <= 4096 * 2); - EXPECT(sizeof(ars) <= 8 * sizeof(Pml4)); + EXPECT(sizeof(ars) <= 8 * sizeof(Pml4)); // NOLINT(bugprone-sizeof-expression) std::array stack_tbls{{0}}; - int it = 0; for (auto& tbl : tbls) { tbl = new Pml4(0); @@ -302,8 +304,6 @@ CASE ("os::mem page table destructors") } for (auto& tbl : tbls) delete tbl; - - } @@ -384,6 +384,7 @@ CASE("os::mem::protect try to break stuff"){ EXPECT(__pml4 == nullptr); __pml4 = new x86::paging::Pml4(0); EXPECT(__pml4->is_empty()); + auto initial_use = __pml4->bytes_allocated(); MYINFO("Initial memory use: %zi \n", initial_use); @@ -392,6 +393,8 @@ CASE("os::mem::protect try to break stuff"){ auto phys = 1_MiB + r % 100_MiB; auto size = 4_KiB + (r % 2 ? r % 2_GiB : r % 4_MiB); + EXPECT(__pml4->flags_r(lin) == x86::paging::to_x86(init_access)); + mem::Map req; req.lin = util::bits::roundto<4_KiB>(lin); req.phys = util::bits::roundto<4_KiB>(phys); @@ -417,14 +420,14 @@ CASE("os::mem::protect try to break stuff"){ // Unmap mem::unmap(m.lin); EXPECT(__pml4->bytes_allocated() <= bytes_after_map); - auto bytes_after_unmap = __pml4->bytes_allocated(); + [[maybe_unused]] auto bytes_after_unmap = __pml4->bytes_allocated(); MYINFO("Allocated bytes after unmap: %zi == %zi tables\n", bytes_after_unmap, bytes_after_unmap / sizeof(decltype(*__pml4))); // Purge unused __pml4->purge_unused(); - auto bytes_after_purge = __pml4->bytes_allocated(); + [[maybe_unused]] auto bytes_after_purge = __pml4->bytes_allocated(); MYINFO("Allocated bytes after purge: %zi == %zi tables\n", bytes_after_purge, bytes_after_purge / sizeof(decltype(*__pml4))); @@ -437,7 +440,7 @@ CASE("os::mem::protect try to break stuff"){ CASE("os::mem::protect verify consistency"){ using namespace util::literals; - auto init_access = mem::Access::none; + auto init_access = mem::Access::none; // FIXME: should probably be before and after mapping if (__pml4 != nullptr) { printf("NOT NULL\n"); diff --git a/test/unit/memory/lstack/test_lstack.hpp b/test/unit/memory/lstack/test_lstack.hpp index 8d5dd2f6f4..6780ace9b7 100644 --- a/test/unit/memory/lstack/test_lstack.hpp +++ b/test/unit/memory/lstack/test_lstack.hpp @@ -37,6 +37,7 @@ template inline void print_summary(L& lstack) { #ifndef DEBUG_UNIT + (void) lstack; return; #else diff --git a/test/unit/memory/lstack/test_lstack_common.cpp b/test/unit/memory/lstack/test_lstack_common.cpp index 874003c18f..38ce79ddb4 100644 --- a/test/unit/memory/lstack/test_lstack_common.cpp +++ b/test/unit/memory/lstack/test_lstack_common.cpp @@ -527,7 +527,7 @@ CASE("lstack::" STR(LSTACK_OPT) " allocate_back") { EXPECT(heap.bytes_allocated() == 0); - // Deallocate all but last + // Deallocate all but last // FIXME: this doesn't seem to do what it says std::vector allocs; size_t allocated = 0; for (int i = 0; i < chunks - 1; i++) { @@ -598,11 +598,16 @@ CASE("lstack::" STR(LSTACK_OPT) " random allocs") { data = 'A'; for (auto a : allocs) { // Verify data consistency - char* c = (char*)a.ptr; + char* c = static_cast(a.ptr); + EXPECT(*c == data); + EXPECT(reinterpret_cast(c) >= heap.pool_begin()); + EXPECT(reinterpret_cast(c) <= heap.pool_end()); + std::string A (a.size, data); std::string B {(const char*)a.ptr, a.size}; EXPECT(A == B); EXPECT(A.size() > 0); + data++; // Deallocate and verify size diff --git a/test/unit/memory/lstack/test_lstack_nodes.cpp b/test/unit/memory/lstack/test_lstack_nodes.cpp index 6f24f91d67..d1028e3723 100644 --- a/test/unit/memory/lstack/test_lstack_nodes.cpp +++ b/test/unit/memory/lstack/test_lstack_nodes.cpp @@ -112,6 +112,7 @@ CASE("lstack::nodes: testing lstack node traversal") print_summary(heap); EXPECT(heap.find_prior(pop1) == nullptr); auto pr2 = heap.find_prior(pop2); + EXPECT(pr2 == heap.find_prior(pop2)); EXPECT(heap.find_prior(pop2) == pop1); } @@ -195,6 +196,7 @@ CASE("lstack::nodes: testing lstack node traversal") print_summary(heap); EXPECT(heap.find_prior(pop1) == nullptr); auto pr2 = heap.find_prior(pop2); + EXPECT(pr2 == heap.find_prior(pop2)); EXPECT(heap.find_prior(pop2) == pop1); print_summary(heap); diff --git a/test/unit/memory/paging/x86_paging.cpp b/test/unit/memory/paging/x86_paging.cpp index 214e376bb6..8a10a8e9ff 100644 --- a/test/unit/memory/paging/x86_paging.cpp +++ b/test/unit/memory/paging/x86_paging.cpp @@ -205,7 +205,7 @@ CASE("x86::paging 4-level x86_64 paging") { EXPECT(__pml4->size() == 512); uintptr_t* entries = static_cast(__pml4->data()); - for (int i = 0; i < __pml4->size(); i++) + for (size_t i = 0; i < __pml4->size(); i++) { EXPECT(entries[i] == 0); EXPECT(__pml4->at(i) == entries[i]); @@ -354,8 +354,6 @@ CASE ("x86::paging Verify execute protection") EXPECT(os::mem::active_page_size(0LU) == 4_KiB); EXPECT(os::mem::flags(0) == Access::none); - auto flags = os::mem::flags(__exec_begin); - // .text segment has execute + read access up to next 4kb page EXPECT(os::mem::flags(__exec_begin) == (Access::execute | Access::read)); EXPECT(os::mem::flags(__exec_end - 1) == (Access::execute | Access::read)); @@ -533,6 +531,9 @@ CASE ("x86::paging Verify default paging setup") // Map 4k-aligned sizes auto addr = (rand() & ~(4_KiB -1)); auto map = __pml4->map_r({addr, addr, Flags::present, increment}); + EXPECT(map); + EXPECT(has_flag(map.flags, x86::paging::Flags::present) == true); + EXPECT(map.size >= increment); auto summary_pre = __pml4->summary(); auto* pml3_ent2 = __pml4->entry(513_GiB); @@ -617,6 +618,7 @@ CASE ("x86::paging Verify default paging setup") auto diff_4k = summary_post.pages_4k - summary_pre.pages_4k; EXPECT(kb_pages_found == summary_post.pages_4k); + EXPECT(page_dirs_found == summary_post.dirs_512g + summary_post.dirs_2m + summary_post.dirs_1g); EXPECT(diff_4k == sum_pml3.pages_4k); EXPECT(sum_pml3.pages_1g == 0); EXPECT(sum_pml3.pages_2m == 0); diff --git a/test/unit/net/dhcp.cpp b/test/unit/net/dhcp.cpp index aec0bbbc7d..8d4cb749a2 100644 --- a/test/unit/net/dhcp.cpp +++ b/test/unit/net/dhcp.cpp @@ -57,7 +57,9 @@ CASE("Create DHCP request") static bool done = false; inet.on_config( [] (net::Inet& inet) { - //assert(inet.ip_addr() == net::ip4::Addr{10,0,0,1}); + //assert((inet.ip_addr() == net::ip4::Addr{10,0,0,1})); + assert((inet.ip_addr() != net::ip4::Addr{0,0,0,0})); + assert((inet.ip_addr() != net::ip4::Addr{255,255,255,255})); printf("Configured!\n"); done = true; }); diff --git a/test/unit/net/dhcp_message_test.cpp b/test/unit/net/dhcp_message_test.cpp index 75896b5128..f52ad9e029 100644 --- a/test/unit/net/dhcp_message_test.cpp +++ b/test/unit/net/dhcp_message_test.cpp @@ -158,13 +158,41 @@ CASE("Reading Message and parsing options with Message_view") EXPECT(param_req_list_opt->code == option::DHCP_PARAMETER_REQUEST_LIST); EXPECT(anon_opt == param_req_list_opt); - int i = 0; - auto y = view.parse_options([&i] (const option::base* opt) + int options_handled = 0; + std::array seen{false, false, false}; + auto options_found = view.parse_options([&] (const option::base* opt) { - ++i; + EXPECT(opt->code != net::dhcp::option::PAD); + EXPECT(opt->code != net::dhcp::option::END); + EXPECT(opt->length != 0); + + switch (opt->code) { + case option::DHCP_MESSAGE_TYPE: + EXPECT(opt->length == 1); + EXPECT(opt->val[0] == uint8_t(message_type::DISCOVER)); + seen[0] = true; + break; + case option::DHCP_CLIENT_IDENTIFIER: + EXPECT(opt->length == 7); + EXPECT(opt->val[0] == uint8_t(htype::ETHER)); + EXPECT(std::memcmp(&opt->val[1], &link_addr, ETH_ALEN) == 0); + seen[1] = true; + break; + case option::DHCP_PARAMETER_REQUEST_LIST: + EXPECT(opt->length == 3); + EXPECT(opt->val[0] == option::ROUTERS); + EXPECT(opt->val[1] == option::SUBNET_MASK); + EXPECT(opt->val[2] == option::DOMAIN_NAME_SERVERS); + seen[2] = true; + break; + default: + EXPECT(false); + break; + } + ++options_handled; }); - EXPECT(i == 3); - EXPECT(y == i); + EXPECT(options_handled == 3); + EXPECT(options_found == options_handled); } CASE("Creating Message and adding options with Message_view") diff --git a/test/unit/net/http_header_test.cpp b/test/unit/net/http_header_test.cpp index c00d668ba5..c28ec1e67f 100644 --- a/test/unit/net/http_header_test.cpp +++ b/test/unit/net/http_header_test.cpp @@ -109,6 +109,7 @@ CASE("Headers can be streamed") { http::Header header; bool res = header.set_field("Connection", "close"); + EXPECT(res == true); std::stringstream ss; ss << header; EXPECT(ss.str().size() > 15); diff --git a/test/unit/net/ip4_packet_test.cpp b/test/unit/net/ip4_packet_test.cpp index e715eba5ac..c689efc7ad 100644 --- a/test/unit/net/ip4_packet_test.cpp +++ b/test/unit/net/ip4_packet_test.cpp @@ -48,7 +48,7 @@ CASE("IP4 Packet TTL - multiple packets") {10,10,10,10}, {1,1,1,1}, {0,0,0,0}, {111,222,111,222}, {123,123,0,123} }; - for(int i = 0; i < addrs.size()-1; i++) + for(size_t i = 0; i < addrs.size()-1; i++) { auto ip4 = create_ip4_packet_init(addrs[i], addrs[i+1]); diff --git a/test/unit/net/ip6_addr.cpp b/test/unit/net/ip6_addr.cpp index 57ddd3f48c..867b4ffba1 100644 --- a/test/unit/net/ip6_addr.cpp +++ b/test/unit/net/ip6_addr.cpp @@ -43,7 +43,7 @@ CASE("Create IP6 addresses from strings") { Addr addr1 { 0xfe80, 0, 0, 0, 0xe823, 0xfcff, 0xfef4, 0x85bd }; Addr addr2 { 0xfe80, 0, 0, 0, 0xe823, 0xfcff, 0xfef4, 0x84aa }; - Addr addr3 { }; + //lowercase test std::string valid_addr_string1{"fe80:0000:0000:0000:e823:fcff:fef4:85bd"}; std::string valid_addr_short_string1{"fe80::e823:fcff:fef4:85bd"}; @@ -57,7 +57,7 @@ CASE("Create IP6 addresses from strings") EXPECT (valid_addr1 == addr1); EXPECT (valid_addr2 == addr2); - EXPECT (Addr{valid_addr_short_string1}== addr1); + EXPECT (Addr{valid_addr_short_string1} == addr1); EXPECT (Addr{valid_addr_short_string2} == addr2); std::string invalid_addr_string1 {"CAFEBABE::e823:fcff:fef4::85bd"}; diff --git a/test/unit/net/ip6_packet_test.cpp b/test/unit/net/ip6_packet_test.cpp index 786b735594..05a787fc36 100644 --- a/test/unit/net/ip6_packet_test.cpp +++ b/test/unit/net/ip6_packet_test.cpp @@ -48,7 +48,7 @@ CASE("IP6 Packet HOPLIMIT - multiple packets") {0xfe80, 0, 0, 0, 0x0202, 0xb3ff, 0xff1e, 0x8329}, }; - for(int i = 0; i < addrs.size()-1; i++) + for(size_t i = 0; i < addrs.size()-1; i++) { auto ip6 = create_ip6_packet_init(addrs[i], addrs[i+1]); diff --git a/test/unit/net/router_test.cpp b/test/unit/net/router_test.cpp index ed19b24693..88f59b5b6e 100644 --- a/test/unit/net/router_test.cpp +++ b/test/unit/net/router_test.cpp @@ -184,7 +184,6 @@ CASE("net::router: Actual routing verifying TTL") const Socket src{ip4::Addr{10,0,1,10}, 32222}; const Socket dst{ip4::Addr{10,0,2,10}, 80}; - const uint8_t DEFAULT_TTL = PacketIP4::DEFAULT_TTL; // Here we gonna receive the ICMP TTL Exceeded ONCE static int time_exceeded_count = 0; @@ -192,6 +191,8 @@ CASE("net::router: Actual routing verifying TTL") auto packet = static_unique_ptr_cast(std::move(pckt)); EXPECT(packet->ip_protocol() == Protocol::ICMPv4); EXPECT(packet->ip_ttl() == PacketIP4::DEFAULT_TTL); + EXPECT(ip == src.address()); + EXPECT(ip.is_loopback() == false); auto icmp = icmp4::Packet(std::move(packet)); ICMP_error err{icmp.type(), icmp.code()}; @@ -209,6 +210,8 @@ CASE("net::router: Actual routing verifying TTL") EXPECT(packet->destination() == dst); EXPECT(packet->ip_ttl() == (PacketIP4::DEFAULT_TTL-1)); + EXPECT(ip == dst.address()); + EXPECT(ip.is_loopback() == false); tcp_packet_recv++; }); diff --git a/test/unit/net/tcp_read_buffer_test.cpp b/test/unit/net/tcp_read_buffer_test.cpp index 5cf799de42..55de4c6d77 100644 --- a/test/unit/net/tcp_read_buffer_test.cpp +++ b/test/unit/net/tcp_read_buffer_test.cpp @@ -85,7 +85,6 @@ CASE("Filling the buffer") using namespace net::tcp; const seq_t SEQ_START = 322; seq_t SEQ = SEQ_START; - const size_t BUFSZ = MIN_BUFSZ; Read_buffer buf{SEQ, MIN_BUFSZ, MAX_BUFSZ}; @@ -133,8 +132,10 @@ CASE("Reseting the buffer") size_t written = 0; - SEQ += buf.insert(SEQ, (uint8_t*)str1.data(), str1.size(), true); + written += buf.insert(SEQ, (uint8_t*)str1.data(), str1.size(), true); + SEQ += written; + EXPECT(buf.size() == written); EXPECT(buf.size() == str1.size()); EXPECT(buf.is_ready()); diff --git a/test/unit/net/tcp_read_request_test.cpp b/test/unit/net/tcp_read_request_test.cpp index a157c024ad..995df2a65e 100644 --- a/test/unit/net/tcp_read_request_test.cpp +++ b/test/unit/net/tcp_read_request_test.cpp @@ -30,7 +30,7 @@ CASE("Operating with out of order data") int no_reads; size_t fits, insert; - Read_request::ReadCallback read_cb = [&](auto buf) mutable { + Read_request::ReadCallback read_cb = [&]([[maybe_unused]] auto buf) mutable { no_reads++; }; diff --git a/test/unit/util/delegate.cpp b/test/unit/util/delegate.cpp index 1b309ad7e6..82669aa6bd 100644 --- a/test/unit/util/delegate.cpp +++ b/test/unit/util/delegate.cpp @@ -288,14 +288,26 @@ CASE("The delegate operator() uses correct argument type forwarding") int val = 3; test_arg_fwd(del_t{ [](count_ctor arg) { return arg; } }); - test_arg_fwd(del_t{ [val](count_ctor arg) { return arg; } }); - test_arg_fwd(del_t{ [&val](count_ctor arg) { return arg; } }); + test_arg_fwd(del_t{ [val](count_ctor arg) { (void) val; return arg; } }); + test_arg_fwd(del_t{ [&val](count_ctor arg) { (void) val; return arg; } }); count_ctor_wrap ccw{}; test_arg_fwd(del_t{ ccw, &count_ctor_wrap::foo }); test_arg_fwd(del_t{ &ccw, &count_ctor_wrap::foo }); } +CASE("The delegate capture semantics understand value vs reference semantics") { + using del_t = delegate; + int val = 3; + + auto by_val = del_t{ [val] { return val; } }; + auto by_ref = del_t{ [&val] { return val; } }; + + val = 42; + EXPECT(by_val() == 3); + EXPECT(by_ref() == 42); +} + CASE("A delegate can be constructed with a mutable lambda") { using del_t = delegate; @@ -420,8 +432,9 @@ CASE("A delegate constructor can be called multiple times with the same type") using del_t = delegate; std::vector vec; - for (int i = start; i <= end; ++i) + for (size_t i = start; i <= end; ++i) { vec.emplace_back([i]() { return i; }); + } int first = vec.front()(); EXPECT(first == start); diff --git a/test/unit/util/fixed_queue.cpp b/test/unit/util/fixed_queue.cpp index d0bd45574d..206ef9fd43 100644 --- a/test/unit/util/fixed_queue.cpp +++ b/test/unit/util/fixed_queue.cpp @@ -33,7 +33,7 @@ SETUP("fixed_queue test_basic") SECTION("fold on uninitialized") { size_t count_folds{}; - fq.fold([&count_folds](const auto& val) -> void { ++count_folds; }); + fq.fold([&count_folds](const auto& val) -> void { (void) val; ++count_folds; }); EXPECT(count_folds == 0); } diff --git a/test/unit/util/statman.cpp b/test/unit/util/statman.cpp index b792e48b18..ab58044412 100644 --- a/test/unit/util/statman.cpp +++ b/test/unit/util/statman.cpp @@ -63,7 +63,14 @@ CASE( "Creating and running through three Stats using Statman iterators begin an // create more stats Stat& stat2 = statman_.create(Stat::UINT64, "net.tcp.bytes_transmitted"); + EXPECT(std::string(stat2.name()) == "net.tcp.bytes_transmitted"); + EXPECT(stat2.type() == Stat::UINT64); + EXPECT(stat2.get_uint64() == 0); + Stat& stat3 = statman_.create(Stat::FLOAT, "net.tcp.average"); + EXPECT(std::string(stat3.name()) == "net.tcp.average"); + EXPECT(stat3.type() == Stat::FLOAT); + EXPECT(stat3.get_float() == 0); ++stat3; stat3.get_float()++; @@ -209,13 +216,18 @@ CASE("A Stat is accessible through index operator") CASE("stats names can only be MAX_NAME_LEN characters long") { Statman statman_; + // ok std::string statname1 {"a.stat"}; Stat& stat1 = statman_.create(Stat::UINT32, statname1); + EXPECT(stat1.name() == "a.stat"); + // also ok std::string statname2(Stat::MAX_NAME_LEN, 'x'); Stat& stat2 = statman_.create(Stat::UINT32, statname2); int size_before = statman_.size(); + EXPECT(stat2.name() == std::string(Stat::MAX_NAME_LEN, 'x')); + // not ok std::string statname3(Stat::MAX_NAME_LEN + 1, 'y'); EXPECT_THROWS(statman_.create(Stat::FLOAT, statname3)); @@ -252,7 +264,7 @@ CASE("Various stat to_string()") ++stat1; ++stat2; ++stat3; - + EXPECT(stat1.to_string() == std::to_string(1u)); EXPECT(stat2.to_string() == std::to_string(1ul)); EXPECT(stat3.to_string() == std::to_string(1.0f));