[base64] minimal encode/decode of raw string
This commit is contained in:
parent
e0295f21b5
commit
603646c230
@ -10,7 +10,7 @@ namespace score {
|
||||
|
||||
struct ServerConfig {
|
||||
std::string ip;
|
||||
int port;
|
||||
int port{0};
|
||||
};
|
||||
|
||||
struct TestData {
|
||||
|
||||
27
include/cloud_point_rpc/rpc_coder.h
Normal file
27
include/cloud_point_rpc/rpc_coder.h
Normal 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;
|
||||
};
|
||||
|
||||
}
|
||||
@ -3,7 +3,8 @@ add_project_arguments('-DCRPC_SERVER_API_EXPORT -pthread', language: 'cpp')
|
||||
cloud_point_rpc_sources = files(
|
||||
'rpc_server.cpp',
|
||||
'service.cpp',
|
||||
'server_api.cpp'
|
||||
'server_api.cpp',
|
||||
'rpc_coder.cpp'
|
||||
)
|
||||
|
||||
libcloud_point_rpc = shared_library('cloud_point_rpc',
|
||||
|
||||
42
src/rpc_coder.cpp
Normal file
42
src/rpc_coder.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
@ -3,7 +3,8 @@ test_sources = files(
|
||||
'test_integration.cpp',
|
||||
'test_tcp.cpp',
|
||||
'test_cli.cpp',
|
||||
'test_c_api.cpp'
|
||||
'test_c_api.cpp',
|
||||
'test_base64.cpp'
|
||||
)
|
||||
|
||||
test_exe = executable('unit_tests',
|
||||
|
||||
31
tests/test_base64.cpp
Normal file
31
tests/test_base64.cpp
Normal 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";
|
||||
}
|
||||
@ -69,7 +69,7 @@ class IntegrationTest : public ::testing::Test {
|
||||
std::remove("config.yaml");
|
||||
}
|
||||
|
||||
Config config_;
|
||||
Config config_{};
|
||||
std::unique_ptr<Service> service_;
|
||||
std::unique_ptr<RpcServer> rpc_server_;
|
||||
std::unique_ptr<TcpServer> tcp_server_;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user