rublon-ssh/PAM/ssh/tests/utils_tests.cpp
rublon-bwi 6b9d2f938c
Bwi/v2.1.0 (#15)
* Add phone call authentication method

* Remove dynamic mem allocation from error handler

* Add more error handling code

* Move error handling to different file

* Remove Socket IO dependency

* cleanup in websocket code

* Add rapidjson as cmake dependency

* Added Dockerfiles as primary build system for packages

* Changed policy in CMakeList to work with lower version of CMake

* Fix opensuse builds

* Link filesystem library in gcc 8.5 or older
2024-11-18 12:57:20 +01:00

113 lines
3.2 KiB
C++

#include "gmock/gmock.h"
#include <gtest/gtest.h>
#include <memory_resource>
#include <string>
#include <rublon/utils.hpp>
using namespace rublon;
using namespace testing;
TEST(Utils, toBoolReturnsTrueWhenStringIsPassed) {
EXPECT_TRUE(conv::to_bool("TrUe"));
}
TEST(Utils, toBoolReturnsFalseWhenStringIsPassed) {
EXPECT_FALSE(conv::to_bool("False"));
}
std::string response = R"http(HTTP/2 200
date: Thu, 22 Jun 2023 13:24:58 GMT
content-type: application/json
server: nginx
cache-control: no-cache, private
x-rublon-signature: 1a01558bedaa2dd92ff659fb8ee3c65a89163d63e312fcb9b6f60463cce864d7
x-ratelimit-limit: 300
x-ratelimit-remaining: 299
{"status":"OK","result":{"tid":"2039132542F6465691BF8C41D7CC38C5","status":"pending","companyName":"rublon","applicationName":"Bartoszek_SSH","methods":["email","totp","qrcode","push"]}})http";
class LoggingAllocator : public std::pmr::memory_resource {
public:
int count{};
int bytes{};
void clear() {
count = 0;
bytes = 0;
}
// memory_resource interface
private:
void * do_allocate(std::size_t __bytes, std::size_t __alignment) override {
bytes += __bytes;
count++;
return std::pmr::get_default_resource()->allocate(__bytes, __alignment);
}
void do_deallocate(void * __p, std::size_t __bytes, std::size_t __alignment) override {
bytes -= __bytes;
count--;
std::pmr::get_default_resource()->deallocate(__p, __bytes, __alignment);
}
bool do_is_equal(const memory_resource & __other) const noexcept override {
return std::pmr::get_default_resource()->is_equal(__other);
}
};
TEST(Utils, responseParser) {
memory::MonotonicStackResource< 2048 > resource;
std::map< std::string, std::string > sut{};
rublon::details::headers(response, sut);
EXPECT_THAT(sut,
AllOf(Contains(Pair("date", "Thu, 22 Jun 2023 13:24:58 GMT")),
Contains(Pair("x-rublon-signature", "1a01558bedaa2dd92ff659fb8ee3c65a89163d63e312fcb9b6f60463cce864d7")),
Contains(Pair("x-ratelimit-remaining", "299"))));
}
class Memory : public testing::Test {
public:
// Test interface
protected:
void SetUp() override {
memory::set_default_resource(&alloc);
}
void TearDown() override {
memory::set_default_resource(std::pmr::get_default_resource());
}
LoggingAllocator alloc;
};
class X {};
TEST_F(Memory, setSetsProperResource) {
LoggingAllocator s;
memory::set_default_resource(&s);
EXPECT_EQ(&s, memory::default_resource());
}
TEST_F(Memory, stackResourceDoesNotUseHeapAtAll) {
memory::MonotonicStackResource< 1024 > mr;
std::pmr::string{"to large to fit in small buffer optimization", &mr};
EXPECT_EQ(alloc.count, 0);
}
TEST_F(Memory, heapResourceWillAllocate1kWhenAsk) {
memory::set_default_resource(&alloc);
memory::StrictMonotonic_1k_HeapResource mr;
std::pmr::string{"to large to fit in small buffer optimization", &mr};
EXPECT_EQ(alloc.count, 1);
EXPECT_EQ(alloc.bytes, 1024);
}
TEST_F(Memory, heapResourceWillAllocate8kWhenAsk) {
memory::StrictMonotonic_8k_HeapResource mr;
std::pmr::string{"to large to fit in small buffer optimization", &mr};
EXPECT_EQ(alloc.count, 1);
EXPECT_EQ(alloc.bytes, 8 * 1024);
}