9 Commits

Author SHA1 Message Date
Mudit Vats
b6e761fffc feat: use mocked values for now 2021-03-31 11:26:27 -07:00
Mudit Vats
6eecea264e feat: add secure host based config (sbhc) helpers to create response messages, use 16993 instead of 16992 if secure host configuration set. 2021-02-24 14:42:48 -07:00
Mudit Vats
13975eaf2d fix: use message status instead, cleanup message fields. 2021-02-23 08:47:04 -07:00
Mudit Vats
d9e94bdcb6 feat: add command to start secure host based config 2021-02-18 14:20:26 -07:00
Mudit Vats
cef96ee36a feat: add pthi command for secure host based configuration 2021-02-17 16:58:40 -07:00
trleasher-intel
da9c26bc26 Merge pull request #30 from open-amt-cloud-toolkit/feature_test_framework
feat: add unit test framework
2021-02-16 08:37:47 -07:00
Mudit Vats
bb4e144684 feat: add unit test framework 2021-02-12 16:49:02 -07:00
trleasher-intel
86cf9657ad Merge pull request #29 from open-amt-cloud-toolkit/feature_hostname
feat: add hostname to activation info
2021-02-12 15:54:23 -07:00
Mudit Vats
025152f359 feat: add hostname to activation info 2021-02-12 14:42:43 -07:00
15 changed files with 517 additions and 13 deletions

View File

@@ -41,6 +41,46 @@ add_subdirectory(MicroLMS)
# CppRestSDK
find_package(cpprestsdk CONFIG REQUIRED)
# GoogleTest
# Download and unpack googletest at configure time
configure_file(googletest.cmake.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# add the test executable
add_executable(rpctest
test.cpp
utils.cpp
)
target_link_libraries(rpctest
gtest_main
cpprestsdk::cpprest
cpprestsdk::cpprestsdk_zlib_internal
cpprestsdk::cpprestsdk_boost_internal
cpprestsdk::cpprestsdk_openssl_internal
)
# ccu-poc
add_executable (rpc
info.h
@@ -58,6 +98,8 @@ add_executable (rpc
commands.cpp
activation.h
activation.cpp
shbc.h
shbc.cpp
lms.h
lms.cpp
main.cpp

View File

@@ -171,6 +171,10 @@ const PTHI_MESSAGE_HEADER GET_UUID_REQUEST_HEADER = {
{AMT_MAJOR_VERSION, AMT_MINOR_VERSION}, 0, {{GET_UUID_REQUEST}}, 0
};
const PTHI_MESSAGE_HEADER GET_START_CONFIG_HBASED_REQUEST_HEADER = {
{AMT_MAJOR_VERSION, AMT_MINOR_VERSION}, 0, {{GET_START_CONFIG_HBASED_REQUEST}}, 396
};
AMT_STATUS _call(const unsigned char *command, UINT32 command_size, UINT8 **readBuffer, UINT32 rcmd, unsigned int expSize)
{
UINT32 inBuffSize;
@@ -1502,4 +1506,43 @@ AMT_STATUS pthi_GetUUID(AMT_UUID *uuid)
return status;
}
/*
* Start Configuration Host Based
* Arguments:
* request - host based configuration input parameters provided by the caller
* response - host based configuration output parameters returned to the caller
* Return values: (A status code returned in a response message that indicates whether the operation specified in the corresponding request message succeeded or failed. If the operation failed, this code indicates the specific reason for failure. Possible values described below.)
* AMT_STATUS_SUCCESS - Request succeeded.
* AMT_STATUS_INTERNAL_ERROR - An internal error to the AMT device has occurred. This may indicate an interface error, or a AMT application error.
* AMT_STATUS_INVALID_MESSAGE_LENGTH - Length field of header is invalid.
*
*/
AMT_STATUS pthi_StartConfigHBased(CFG_START_CONFIG_HBASED_REQUEST_INFO *request, CFG_START_CONFIG_HBASED_RESPONSE_INFO *response)
{
UINT8* readBuffer = NULL;
UINT32 command_size = sizeof(CFG_START_CONFIG_HBASED_REQUEST);
unsigned char command[sizeof(CFG_START_CONFIG_HBASED_REQUEST)];
AMT_STATUS status;
CFG_START_CONFIG_HBASED_RESPONSE* tmp_response;
memset(command, 0, sizeof(CFG_START_CONFIG_HBASED_REQUEST));
memcpy_s(command, sizeof(command), (char*)&(GET_START_CONFIG_HBASED_REQUEST_HEADER), sizeof(GET_START_CONFIG_HBASED_REQUEST_HEADER));
((CFG_START_CONFIG_HBASED_REQUEST*)command)->Info.ServerHashAlgorithm = request->ServerHashAlgorithm;
((CFG_START_CONFIG_HBASED_REQUEST*)command)->Info.HostVPNEnable = request->HostVPNEnable;
((CFG_START_CONFIG_HBASED_REQUEST*)command)->Info.SuffixListLen = request->SuffixListLen;
memcpy_s(((CFG_START_CONFIG_HBASED_REQUEST*)command)->Info.ServerCertHash, 64, request->ServerCertHash, 64);
memcpy_s(((CFG_START_CONFIG_HBASED_REQUEST*)command)->Info.NetworkDnsSuffixList, 320, request->NetworkDnsSuffixList, 320);
status = _call(command, command_size, &readBuffer, GET_START_CONFIG_HBASED_RESPONSE, sizeof(CFG_START_CONFIG_HBASED_RESPONSE));
if (status == AMT_STATUS_SUCCESS)
{
tmp_response = (CFG_START_CONFIG_HBASED_RESPONSE*)readBuffer;
memcpy_s(response, sizeof(CFG_START_CONFIG_HBASED_RESPONSE_INFO), &(tmp_response->Info), sizeof(CFG_START_CONFIG_HBASED_RESPONSE_INFO));
}
if (readBuffer != NULL) free(readBuffer);
return status;
}
#endif

View File

@@ -664,6 +664,38 @@ typedef struct _CFG_GET_UUID_RESPONSE
AMT_UUID UUID;
} CFG_GET_UUID_RESPONSE;
typedef struct _CFG_START_CONFIG_HBASED_REQUEST_INFO
{
CERT_HASH_ALGORITHM ServerHashAlgorithm;
UINT8 ServerCertHash[CERT_HASH_MAX_LENGTH];
AMT_BOOLEAN HostVPNEnable;
UINT32 SuffixListLen;
CHAR NetworkDnsSuffixList[320];
} CFG_START_CONFIG_HBASED_REQUEST_INFO;
typedef struct _CFG_START_CONFIG_HBASED_REQUEST
{
PTHI_MESSAGE_HEADER Header;
CFG_START_CONFIG_HBASED_REQUEST_INFO Info;
} CFG_START_CONFIG_HBASED_REQUEST;
typedef struct _CFG_START_CONFIG_HBASED_RESPONSE_INFO
{
PTHI_RESPONSE_MESSAGE_HEADER Header;
AMT_STATUS Status;
CERT_HASH_ALGORITHM HashAlgorithm;
UINT8 AMTCertHash[CERT_HASH_MAX_LENGTH];
} CFG_START_CONFIG_HBASED_RESPONSE_INFO;
typedef struct _CFG_START_CONFIG_HBASED_RESPONSE
{
PTHI_RESPONSE_MESSAGE_HEADER Header;
CFG_START_CONFIG_HBASED_RESPONSE_INFO Info;
} CFG_START_CONFIG_HBASED_RESPONSE;
#pragma pack()
@@ -701,6 +733,7 @@ AMT_STATUS pthi_Unprovision(CFG_PROVISIONING_MODE provisionMode);
AMT_STATUS pthi_GetStateEHBC(AMT_EHBC_STATE *state);
AMT_STATUS pthi_GetControlMode(int *state);
AMT_STATUS pthi_GetUUID(AMT_UUID *uuid);
AMT_STATUS pthi_StartConfigHBased(CFG_START_CONFIG_HBASED_REQUEST_INFO* request, CFG_START_CONFIG_HBASED_RESPONSE_INFO* response);
#define PROVISIONING_MODE_REQUEST 0x04000008
#define PROVISIONING_MODE_RESPONSE 0x04800008
@@ -831,6 +864,9 @@ const PTHI_MESSAGE_HEADER GET_CONTROL_MODE_HEADER;
#define GET_UUID_REQUEST 0x400005c
#define GET_UUID_RESPONSE 0x480005c
#define GET_START_CONFIG_HBASED_REQUEST 0x400008b
#define GET_START_CONFIG_HBASED_RESPONSE 0x480008b
#endif
#endif

View File

@@ -77,6 +77,16 @@ web::json::value get_dns()
return web::json::value::string(tmp);
}
web::json::value get_hostname()
{
utility::string_t tmp;
std::string hostName = net_get_hostname();
tmp = utility::conversions::convertstring(hostName);
return web::json::value::string(tmp);
}
bool getVersion(web::json::value& value)
{
std::string version;
@@ -202,6 +212,9 @@ bool get_activation_payload(web::json::value& payload)
// get DNS Info
activationParams[U("fqdn")] = get_dns();
// get hostname
activationParams[U("hostname")] = get_hostname();
// get client string
if (!get_client_string(value)) return false;
activationParams[U("client")] = value;
@@ -223,7 +236,7 @@ bool act_create_request(std::string commands, std::string dns_suffix, std::strin
utility::string_t tmp = utility::conversions::convertstring(commands);
msg[U("method")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("key");
tmp = utility::conversions::convertstring("");
msg[U("apiKey")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring(PROJECT_VER);
@@ -232,10 +245,10 @@ bool act_create_request(std::string commands, std::string dns_suffix, std::strin
tmp = utility::conversions::convertstring(PROTOCOL_VERSION);
msg[U("protocolVersion")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("ok");
tmp = utility::conversions::convertstring("");
msg[U("status")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("ok");
tmp = utility::conversions::convertstring("");
msg[U("message")] = web::json::value::string(tmp);
// get the activation payload
@@ -268,7 +281,7 @@ bool act_create_response(std::string payload, std::string& response)
utility::string_t tmp = utility::conversions::convertstring("response");
msg[U("method")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("key");
tmp = utility::conversions::convertstring("");
msg[U("apiKey")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring(PROJECT_VER);
@@ -277,10 +290,10 @@ bool act_create_response(std::string payload, std::string& response)
tmp = utility::conversions::convertstring(PROTOCOL_VERSION);
msg[U("protocolVersion")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("ok");
tmp = utility::conversions::convertstring("");
msg[U("status")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("ok");
tmp = utility::conversions::convertstring("");
msg[U("message")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring(util_encode_base64(payload));
@@ -290,3 +303,4 @@ bool act_create_response(std::string payload, std::string& response)
return true;
}

View File

@@ -32,6 +32,9 @@ bool cmd_is_admin()
bool cmd_get_version(std::string& version)
{
version = "14.0.0"; // FIXME: REMOVE WHEN HW AVAILABLE
return true; // FIXME: REMOVE WHEN HW AVAILABLE
version.clear();
// initialize HECI interface
@@ -410,3 +413,76 @@ bool cmd_get_lan_interface_settings(lan_interface_settings& lan_interface_settin
return false;
}
bool cmd_start_config_host_based(config_host_based_settings& server_cert, config_host_based_settings& amt_cert)
{
amt_cert.algorithm = "MD5"; // FIXME: REMOVE WHEN HW AVAILABLE
amt_cert.hash = "000102030405060708090a0b0c0d0e0f"; // FIXME: REMOVE WHEN HW AVAILABLE
return true; // FIXME: REMOVE WHEN HW AVAILABLE
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
CFG_START_CONFIG_HBASED_REQUEST_INFO request;
CFG_START_CONFIG_HBASED_RESPONSE_INFO response;
memset(&request, 0, sizeof(CFG_START_CONFIG_HBASED_REQUEST_INFO));
memset(&response, 0, sizeof(CFG_START_CONFIG_HBASED_RESPONSE_INFO));
if (server_cert.algorithm == "MD5")
{
request.ServerHashAlgorithm = CERT_HASH_ALGORITHM_MD5;
}
else if (server_cert.algorithm == "SHA1")
{
request.ServerHashAlgorithm = CERT_HASH_ALGORITHM_SHA1;
}
else if (server_cert.algorithm == "SHA256")
{
request.ServerHashAlgorithm = CERT_HASH_ALGORITHM_SHA256;
}
else if (server_cert.algorithm == "SHA512")
{
request.ServerHashAlgorithm = CERT_HASH_ALGORITHM_SHA512;
}
else
{
return false;
}
std::vector<char> cert_bytes;
util_hex_string_to_bytes(server_cert.hash, cert_bytes);
std::copy(std::begin(cert_bytes), std::end(cert_bytes), request.ServerCertHash);
// start secure host based configuration
AMT_STATUS amt_status = pthi_StartConfigHBased(&request, &response);
if (amt_status == 0)
{
switch (response.HashAlgorithm)
{
case CERT_HASH_ALGORITHM_MD5:
amt_cert.algorithm = "MD5";
break;
case CERT_HASH_ALGORITHM_SHA1:
amt_cert.algorithm = "SHA1";
break;
case CERT_HASH_ALGORITHM_SHA256:
amt_cert.algorithm = "SHA256";
break;
case CERT_HASH_ALGORITHM_SHA512:
amt_cert.algorithm = "SHA512";
break;
default:
break;
}
std::vector<char> hash;
std::copy(std::begin(response.AMTCertHash), std::end(response.AMTCertHash), std::begin(hash));
util_bytes_to_hex_string(hash, amt_cert.hash);
return true;
}
return false;
}

View File

@@ -37,6 +37,12 @@ struct fqdn_settings
std::string fqdn;
};
struct config_host_based_settings
{
std::string hash;
std::string algorithm;
};
bool cmd_is_admin();
bool cmd_get_version(std::string& version);
bool cmd_get_build_number(std::string& version);
@@ -50,5 +56,6 @@ bool cmd_get_wired_mac_address(std::vector<unsigned char>& address);
bool cmd_get_certificate_hashes(std::vector<cert_hash_entry>& hash_entries);
bool cmd_get_remote_access_connection_status(int& network_status, int& remote_status, int& remote_trigger, std::string& mps_hostname);
bool cmd_get_lan_interface_settings(lan_interface_settings& lan_interface_settings);
bool cmd_start_config_host_based(config_host_based_settings& server_cert, config_host_based_settings& amt_cert);
#endif

15
googletest.cmake.in Normal file
View File

@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 2.8.12)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)

13
lms.cpp
View File

@@ -15,13 +15,22 @@
#include <netdb.h>
#endif
SOCKET lms_connect()
SOCKET lms_connect(bool securePort)
{
std::string lmsAddress = "localhost";
std::string lmsPort = "16992";
std::string lmsPort;
SOCKET s = INVALID_SOCKET;
struct addrinfo *addr, hints;
if (securePort)
{
lmsPort = "16993";
}
else
{
lmsPort = "16992";
}
#ifdef _WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)

2
lms.h
View File

@@ -28,6 +28,6 @@ static inline int closesocket(int fd)
#define SD_BOTH SHUT_RDWR
#endif
SOCKET lms_connect();
SOCKET lms_connect(bool securePort = false);
#endif

View File

@@ -11,6 +11,7 @@
#include "lms.h"
#include "commands.h"
#include "activation.h"
#include "shbc.h"
#include "utils.h"
#include "usage.h"
#include "args.h"
@@ -61,6 +62,7 @@ int main(int argc, char* argv[])
std::string arg_info;
bool arg_verbose = false;
bool arg_nocertcheck = false;
bool secureHostBasedConfig = false;
if (argc == 1)
{
@@ -184,7 +186,7 @@ int main(int argc, char* argv[])
memset(&lms_socket, 0, sizeof(SOCKET));
// set receive handler
client.set_message_handler([&client, &mx, &cv, &lms_socket, arg_verbose](web::websockets::client::websocket_incoming_message ret_msg)
client.set_message_handler([&client, &mx, &cv, &lms_socket, arg_verbose, &secureHostBasedConfig](web::websockets::client::websocket_incoming_message ret_msg)
{
// kick the timer
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
@@ -246,10 +248,61 @@ int main(int argc, char* argv[])
return;
}
if (msgMethod.compare("secure_config_request") == 0)
{
std::string certAlgo;
std::string certHash;
// get server configuration
try
{
tmp = parsed[U("payload")].as_string();
web::json::value parsed_cert_info = web::json::value::parse(tmp);
out = parsed_cert_info[U("algorithm")].as_string();
certAlgo = utility::conversions::to_utf8string(out);
out = parsed_cert_info[U("hash")].as_string();
certHash = utility::conversions::to_utf8string(out);
}
catch (...)
{
std::cerr << std::endl << "JSON format error. Unable to parse message." << std::endl;
return;
}
// send secure config request
config_host_based_settings server_cert;
config_host_based_settings amt_cert;
server_cert.algorithm = certAlgo;
server_cert.hash = certHash;
if (cmd_start_config_host_based(server_cert, amt_cert))
{
// create the response
std::string response;
if (!shbc_create_response(amt_cert.algorithm, amt_cert.hash, response)) return;
// send it
web::websockets::client::websocket_outgoing_message send_websocket_msg;
std::string send_websocket_buffer(response);
send_websocket_msg.set_utf8_message(send_websocket_buffer);
client.send(send_websocket_msg).wait();
// use secure host post for LMS going forward
secureHostBasedConfig = true;
return;
}
return;
}
// process any messages we can
// - if success, done
// - if error, get out
if (msgMethod.compare("success")==0)
if (msgStatus.compare("success")==0)
{
// cleanup
g_timeout_val = 0;
@@ -258,7 +311,7 @@ int main(int argc, char* argv[])
std::cout << std::endl << msgMessage << std::endl;
return;
}
else if (msgMethod.compare("error")==0)
else if (msgStatus.compare("failed")==0)
{
// cleanup
g_timeout_val = 0;
@@ -296,7 +349,7 @@ int main(int argc, char* argv[])
try
{
// conntect to lms
lms_socket = lms_connect();
lms_socket = lms_connect(secureHostBasedConfig);
}
catch (...)
{

72
shbc.cpp Normal file
View File

@@ -0,0 +1,72 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#include "activation.h"
#include <cpprest/ws_client.h>
#include <cpprest/json.h>
#include <cpprest/streams.h>
#include <iostream>
#include <string>
#include "version.h"
#include "commands.h"
#include "network.h"
#include "utils.h"
bool get_response_payload(std::string cert_algo, std::string cert_hash, web::json::value& payload)
{
web::json::value value;
utility::string_t tmp;
web::json::value configParams;
// get client string
tmp = utility::conversions::convertstring(cert_algo);
configParams[U("algorithm")] = web::json::value::string(tmp);
// get certificate hashes
tmp = utility::conversions::convertstring(cert_hash);
configParams[U("hash")] = web::json::value::string(tmp);
payload = configParams;
return true;
}
bool shbc_create_response(std::string cert_algo, std::string cert_hash, std::string& response)
{
web::json::value msg;
utility::string_t tmp = utility::conversions::convertstring("secure_config_response");
msg[U("method")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("");
msg[U("apiKey")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring(PROJECT_VER);
msg[U("appVersion")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring(PROTOCOL_VERSION);
msg[U("protocolVersion")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("");
msg[U("status")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("");
msg[U("message")] = web::json::value::string(tmp);
// get the activation payload
web::json::value responsePayload;
if (!get_response_payload(cert_algo, cert_hash, responsePayload)) return false;
// serialize payload
std::string serializedPayload = utility::conversions::to_utf8string(responsePayload.serialize());
std::string encodedPayload = util_encode_base64(serializedPayload);
utility::string_t payload = utility::conversions::to_string_t(encodedPayload);
msg[U("payload")] = web::json::value::string(payload);
// serialize the entire message
response = utility::conversions::to_utf8string(msg.serialize());
return true;
}

19
shbc.h Normal file
View File

@@ -0,0 +1,19 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#ifndef __SHBC_H__
#define __SHBC_H__
#include <string>
#ifdef _WIN32
#define convertstring to_utf16string
#else
#define convertstring to_utf8string
#endif
bool shbc_create_response(std::string cert_algo, std::string cert_hash, std::string& response);
#endif

89
test.cpp Normal file
View File

@@ -0,0 +1,89 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#include "gtest/gtest.h"
#include <string>
#include <thread>
#include <cpprest/ws_client.h>
#include <cpprest/json.h>
#include "port.h"
#include "utils.h"
const std::string plainText = "Ut aliquet ex id enim accumsan bibendum. Nullam nibh ligula, rhoncus vitae nisl eu, fermentum luctus tellus. Sed non semper augue, vitae congue nibh. Suspendisse sed placerat metus. Nunc a sapien vel nisl semper fringilla. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam accumsan, nisi vitae efficitur ultricies, velit neque mattis velit, sed sodales tellus est at orci. Duis sed ipsum lorem. In eu enim eu odio fringilla lacinia id id lorem. Vestibulum velit augue, ultricies et neque eget, molestie vehicula urna. Etiam venenatis nibh vel nunc fringilla, vitae feugiat ipsum posuere. Pellentesque ac libero quis nulla pretium iaculis sed at felis. Integer malesuada turpis eget lectus interdum, a commodo nisl tristique. Proin rutrum nisl ut metus aliquam, vel lacinia tortor ullamcorper. Nulla rhoncus ullamcorper accumsan. Fusce eget augue vulputate, porta quam at, laoreet justo.";
const std::string encodedText = "VXQgYWxpcXVldCBleCBpZCBlbmltIGFjY3Vtc2FuIGJpYmVuZHVtLiBOdWxsYW0gbmliaCBsaWd1bGEsIHJob25jdXMgdml0YWUgbmlzbCBldSwgZmVybWVudHVtIGx1Y3R1cyB0ZWxsdXMuIFNlZCBub24gc2VtcGVyIGF1Z3VlLCB2aXRhZSBjb25ndWUgbmliaC4gU3VzcGVuZGlzc2Ugc2VkIHBsYWNlcmF0IG1ldHVzLiBOdW5jIGEgc2FwaWVuIHZlbCBuaXNsIHNlbXBlciBmcmluZ2lsbGEuIFBlbGxlbnRlc3F1ZSBoYWJpdGFudCBtb3JiaSB0cmlzdGlxdWUgc2VuZWN0dXMgZXQgbmV0dXMgZXQgbWFsZXN1YWRhIGZhbWVzIGFjIHR1cnBpcyBlZ2VzdGFzLiBBbGlxdWFtIGFjY3Vtc2FuLCBuaXNpIHZpdGFlIGVmZmljaXR1ciB1bHRyaWNpZXMsIHZlbGl0IG5lcXVlIG1hdHRpcyB2ZWxpdCwgc2VkIHNvZGFsZXMgdGVsbHVzIGVzdCBhdCBvcmNpLiBEdWlzIHNlZCBpcHN1bSBsb3JlbS4gSW4gZXUgZW5pbSBldSBvZGlvIGZyaW5naWxsYSBsYWNpbmlhIGlkIGlkIGxvcmVtLiBWZXN0aWJ1bHVtIHZlbGl0IGF1Z3VlLCB1bHRyaWNpZXMgZXQgbmVxdWUgZWdldCwgbW9sZXN0aWUgdmVoaWN1bGEgdXJuYS4gRXRpYW0gdmVuZW5hdGlzIG5pYmggdmVsIG51bmMgZnJpbmdpbGxhLCB2aXRhZSBmZXVnaWF0IGlwc3VtIHBvc3VlcmUuIFBlbGxlbnRlc3F1ZSBhYyBsaWJlcm8gcXVpcyBudWxsYSBwcmV0aXVtIGlhY3VsaXMgc2VkIGF0IGZlbGlzLiBJbnRlZ2VyIG1hbGVzdWFkYSB0dXJwaXMgZWdldCBsZWN0dXMgaW50ZXJkdW0sIGEgY29tbW9kbyBuaXNsIHRyaXN0aXF1ZS4gUHJvaW4gcnV0cnVtIG5pc2wgdXQgbWV0dXMgYWxpcXVhbSwgdmVsIGxhY2luaWEgdG9ydG9yIHVsbGFtY29ycGVyLiBOdWxsYSByaG9uY3VzIHVsbGFtY29ycGVyIGFjY3Vtc2FuLiBGdXNjZSBlZ2V0IGF1Z3VlIHZ1bHB1dGF0ZSwgcG9ydGEgcXVhbSBhdCwgbGFvcmVldCBqdXN0by4=";
// Test if characters are printable
TEST(testUtils, isPrintableTestValid)
{
std::string s = "The quick brown fox jumps over the lazy dog.";
EXPECT_EQ(true, util_is_printable(s));
}
// Test if characters are printable
TEST(testUtils, isPrintableTestInvalid)
{
std::string s = "The quick brown fox jumps over the lazy dog.";
s[0] = 10; // non-printable character
EXPECT_EQ(false, util_is_printable(s));
}
// Test encode of base64 string
TEST(testUtils, encodebase64)
{
EXPECT_EQ(encodedText, util_encode_base64(plainText));
}
// Test decode of base64 sstring
TEST(testUtils, decodebase64)
{
EXPECT_EQ(plainText, util_decode_base64(encodedText));
}
// Test return value of util_format_uuid
TEST(testUtils, formatUUIDSuccess)
{
std::vector<unsigned char> uuid_bytes;
for (int i=0; i<16; i++)
{
uuid_bytes.push_back(i);
}
std::string uuid_string;
util_format_uuid(uuid_bytes, uuid_string);
EXPECT_EQ(true, util_format_uuid(uuid_bytes, uuid_string));
}
// Test return value of util_format_uuid
TEST(testUtils, formatUUIDFail)
{
std::vector<unsigned char> uuid_bytes;
for (int i=0; i<5; i++) // invalid length
{
uuid_bytes.push_back(i);
}
std::string uuid_string;
util_format_uuid(uuid_bytes, uuid_string);
EXPECT_EQ(false, util_format_uuid(uuid_bytes, uuid_string));
}
// Test value of the uuid format to ensure format is correct
TEST(testUtils, formatUUIDValue)
{
std::string uuid_string;
std::vector<unsigned char> uuid_bytes;
for (int i=0; i<16; i++)
{
uuid_bytes.push_back(i);
}
util_format_uuid(uuid_bytes, uuid_string);
EXPECT_EQ("03020100-0504-0706-0809-0a0b0c0d0e0f", uuid_string);
}

View File

@@ -57,3 +57,30 @@ bool util_format_uuid(std::vector<unsigned char> uuid_bytes, std::string& uuid_s
return true;
}
bool util_hex_string_to_bytes(std::string hex_string, std::vector<char>& hex_bytes)
{
hex_bytes.clear();
for (int i = 0; i < hex_string.length(); i += 2)
{
std::string byte_string = hex_string.substr(i, 2);
char value = (char)strtol(byte_string.c_str(), NULL, 16);
hex_bytes.push_back(value);
}
return true;
}
bool util_bytes_to_hex_string(std::vector<char> hex_bytes, std::string& hex_string)
{
hex_string.clear();
for (char hex_char : hex_bytes)
{
char hex[10];
snprintf(hex, 10, "%02x", hex_char);
hex_string += hex;
}
return true;
}

View File

@@ -13,5 +13,7 @@ std::string util_encode_base64(std::string str);
std::string util_decode_base64(std::string str);
bool util_is_printable(std::string str);
bool util_format_uuid(std::vector<unsigned char> uuid_bytes, std::string& uuid_string);
bool util_hex_string_to_bytes(std::string hex_string, std::vector<char>& hex_bytes);
bool util_bytes_to_hex_string(std::vector<char> hex_bytes, std::string& hex_string);
#endif