[base64] minimal encode/decode of raw string

This commit is contained in:
Artur Mukhamadiev 2026-03-12 00:09:26 +03:00
parent e0295f21b5
commit 603646c230
7 changed files with 106 additions and 4 deletions

View File

@ -10,7 +10,7 @@ namespace score {
struct ServerConfig { struct ServerConfig {
std::string ip; std::string ip;
int port; int port{0};
}; };
struct TestData { struct TestData {

View File

@ -0,0 +1,27 @@
//
// Created by vptyp on 11.03.2026.
//
#pragma once
#include <string>
#include <vector>
namespace score {
class IRPCCoder {
public:
virtual ~IRPCCoder() = default;
virtual std::vector<char> decode(const std::string& encoded) = 0;
virtual std::string encode(const std::vector<char>& data) = 0;
};
class Base64RPCCoder final : public IRPCCoder {
public:
Base64RPCCoder();
~Base64RPCCoder() override;
std::vector<char> decode(const std::string& encoded) override;
std::string encode(const std::vector<char>& data) override;
};
}

View File

@ -3,7 +3,8 @@ add_project_arguments('-DCRPC_SERVER_API_EXPORT -pthread', language: 'cpp')
cloud_point_rpc_sources = files( cloud_point_rpc_sources = files(
'rpc_server.cpp', 'rpc_server.cpp',
'service.cpp', 'service.cpp',
'server_api.cpp' 'server_api.cpp',
'rpc_coder.cpp'
) )
libcloud_point_rpc = shared_library('cloud_point_rpc', libcloud_point_rpc = shared_library('cloud_point_rpc',

42
src/rpc_coder.cpp Normal file
View File

@ -0,0 +1,42 @@
//
// Created by vptyp on 11.03.2026.
//
#include "cloud_point_rpc/rpc_coder.h"
#include "libbase64.h"
#include <glog/logging.h>
namespace score {
Base64RPCCoder::Base64RPCCoder() = default;
Base64RPCCoder::~Base64RPCCoder() = default;
/**
* Tries to decode ASCII complained string to the
* @param encoded ASCII complained base64 encoded string
* @return vector of raw bytes << allocated on encoded.size() / 4 * 3 + 1 size
*/
std::vector<char> Base64RPCCoder::decode(const std::string& encoded) {
DLOG(INFO) << "Base64RPCCoder::decode";
std::vector<char> result((encoded.length() >> 2) * 3 + 1);
size_t result_len = 0;
base64_decode(encoded.data(), encoded.size(),
result.data(), &result_len, 0);
DLOG(INFO) << "result_len: " << result_len;
return result;
}
/**
*
* @param data raw byte stream
* @return encoded base64 string
*/
std::string Base64RPCCoder::encode(const std::vector<char>& data) {
DLOG(INFO) << "Base64RPCCoder::encode";
size_t result_len = 0;
std::string result(data.size() / 3 * 4 + 1, 0);
base64_encode(data.data(), data.size(),
result.data(), &result_len, 0
);
DLOG(INFO) << "result_len: " << result_len;
return result;
}
}

View File

@ -3,7 +3,8 @@ test_sources = files(
'test_integration.cpp', 'test_integration.cpp',
'test_tcp.cpp', 'test_tcp.cpp',
'test_cli.cpp', 'test_cli.cpp',
'test_c_api.cpp' 'test_c_api.cpp',
'test_base64.cpp'
) )
test_exe = executable('unit_tests', test_exe = executable('unit_tests',

31
tests/test_base64.cpp Normal file
View File

@ -0,0 +1,31 @@
//
// Created by vptyp on 11.03.2026.
//
#include <chrono>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include "cloud_point_rpc/config.hpp"
#include "cloud_point_rpc/rpc_coder.h"
class Base64Test : public ::testing::Test {
protected:
void SetUp() override {
}
void TearDown() override {
}
};
TEST_F(Base64Test, EncodeDecode) {
std::vector raw{'H', 'e', 'l', 'l', 'o', 'w', '\0'};
score::Base64RPCCoder coder;
auto encoded = coder.encode(raw);
LOG(INFO) << "encoded: " << encoded;
auto decoded = coder.decode(encoded);
EXPECT_EQ(std::ranges::equal(decoded, raw), true);
LOG(INFO) << "done";
}

View File

@ -69,7 +69,7 @@ class IntegrationTest : public ::testing::Test {
std::remove("config.yaml"); std::remove("config.yaml");
} }
Config config_; Config config_{};
std::unique_ptr<Service> service_; std::unique_ptr<Service> service_;
std::unique_ptr<RpcServer> rpc_server_; std::unique_ptr<RpcServer> rpc_server_;
std::unique_ptr<TcpServer> tcp_server_; std::unique_ptr<TcpServer> tcp_server_;