Skip to content

Adding real C/C++ tests and circleci integration #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2.0

jobs:
build:
docker:
- image: circleci/rust:stretch
steps:
- checkout
- run:
name: Setup
command: |
sudo sh -c 'echo "deb http://deb.debian.org/debian stretch-backports main" > /etc/apt/sources.list.d/backports.list'
sudo apt-get update
sudo apt-get -t stretch-backports install cmake
- run:
name: Build everything
command: |
./build.sh
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

# cmake stuff
cmake-build-*
/build

# Rust specific ignores
# Please follow https://help.github.com/en/articles/ignoring-files to create a global
# .gitignore file locally for IDE/Emacs/Vim generated files.
**/target
**/*.rs.bk

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.7)

project(libra-client-dev)

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# libra-client-dev
Core rust client library for Libra

# How do I even
./build.sh
18 changes: 18 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -euo pipefail

# Build libra-dev first
cd libra-dev
cargo build
cd ..

# Then build everything else
rm -rf build
mkdir build
cd build
cmake ..
make VERBOSE=1

# Test!
./c/c-client
./cpp/cpp-client
18 changes: 14 additions & 4 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_C_STANDARD 11)

project(libra-client-c-dev)
add_executable(c-client main.c)

set(CMAKE_C_STANDARD 11)
target_include_directories(c-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/include")
target_link_directories(c-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/target/debug")
target_link_libraries(c-client PRIVATE "libra_dev")

IF(APPLE)
target_link_libraries(c-client PRIVATE "-framework Security")
elseif(WIN32)

add_executable(c-client main.c)
else()
target_link_libraries(c-client PRIVATE "pthread")
target_link_libraries(c-client PRIVATE "dl")
target_link_libraries(c-client PRIVATE "m")
endif()
80 changes: 78 additions & 2 deletions c/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,81 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

int main() {
fprintf(stdout, "it works!");
#include "data.h"

int hexchr2bin(const char hex, char *out)
{
if (out == NULL)
return 0;

if (hex >= '0' && hex <= '9') {
*out = hex - '0';
} else if (hex >= 'A' && hex <= 'F') {
*out = hex - 'A' + 10;
} else if (hex >= 'a' && hex <= 'f') {
*out = hex - 'a' + 10;
} else {
return 0;
}

return 1;
}

size_t hexs2bin(const char *hex, unsigned char **out)
{
size_t len;
char b1;
char b2;
size_t i;

if (hex == NULL || *hex == '\0' || out == NULL)
return 0;

len = strlen(hex);
if (len % 2 != 0)
return 0;
len /= 2;

*out = malloc(len);
memset(*out, 0 , len);
for (i=0; i<len; i++) {
if (!hexchr2bin(hex[i*2], &b1) || !hexchr2bin(hex[i*2+1], &b2)) {
return 0;
}
(*out)[i] = (b1 << 4) | b2;
}
return len;
}

int main(int argc, const char **argv)
{
const char* blob = "020000002100000001674deac5e7fca75f00ca92b1ba3697f5f01ef585011beea7b361150f4504638f0800000002000000000000002100000001a208df134fefed8442b1f01fab59071898f5a1af5164e12c594de55a7004a91c8e0000002000000036ccb9ba8b4f0cd1f3e2d99338806893dff7478c69acee9b8e1247c053783a4800e876481700000000000200000000000000200000000b14ed4f5af8f8f077c7ec4313c6d395b9a7eb5f41eab9ec15367215ca9e420a01000000000000002000000032f56f77b09773aa64c78ee39943da7ec73f91cd757e325098e11b3edc4eccb10100000000000000";

uint8_t * result;
int len = hexs2bin(blob, &result);

struct CDevAccountResource account_resource = account_resource_from_lcs((const uint8_t *) result, len);
printf("balance: %llu \n", account_resource.balance);
printf("sequence: %llu \n", account_resource.sequence);
printf("delegated_key_rotation_capability: %d \n", account_resource.delegated_key_rotation_capability);

struct CEventHandle sent_events = account_resource.sent_events;
printf("sent events count: %llu \n", sent_events.count);

printf("sent events key: ");
for(int i = 0; i < 32; i++) {
printf("%d ", sent_events.key[i]);
}

struct CEventHandle received_events = account_resource.received_events;
printf("received events count: %llu \n", received_events.count);

printf("received events key: ");
for(int i = 0; i < 32; i++) {
printf("%d ", received_events.key[i]);
}

return 0;
}
18 changes: 14 additions & 4 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)

project(libra-client-cpp-dev)
add_executable(cpp-client main.cc)

set(CMAKE_CXX_STANDARD 11)
target_include_directories(cpp-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/include")
target_link_directories(cpp-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/target/debug")
target_link_libraries(cpp-client PRIVATE "libra_dev")

IF(APPLE)
target_link_libraries(cpp-client PRIVATE "-framework Security")
elseif(WIN32)

add_executable(cpp-client main.cc)
else()
target_link_libraries(cpp-client PRIVATE "pthread")
target_link_libraries(cpp-client PRIVATE "dl")
target_link_libraries(cpp-client PRIVATE "m")
endif()
62 changes: 61 additions & 1 deletion cpp/main.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
#include <string>

#include "data.h"

#include <iostream>

typedef std::basic_string<unsigned char> byte_string;

// C++98 guarantees that '0', '1', ... '9' are consecutive.
// It only guarantees that 'a' ... 'f' and 'A' ... 'F' are
// in increasing order, but the only two alternative encodings
// of the basic source character set that are still used by
// anyone today (ASCII and EBCDIC) make them consecutive.
unsigned char hexval(unsigned char c) {
if ('0' <= c && c <= '9')
return c - '0';
else if ('a' <= c && c <= 'f')
return c - 'a' + 10;
else if ('A' <= c && c <= 'F')
return c - 'A' + 10;
else abort();
}

byte_string hex2bin(const std::string &in) {
byte_string result;
result.reserve(in.length() / 2);
for (auto p = in.begin(); p != in.end(); p++) {
unsigned char c = hexval(*p);
p++;
if (p == in.end()) break; // incomplete last digit - should report error
c = (c << 4) + hexval(*p); // + takes precedence over <<
result.push_back(c);
}
return result;
}

int main() {
std::cout << "it works!";
std::basic_string<unsigned char> blob = hex2bin(
"020000002100000001674deac5e7fca75f00ca92b1ba3697f5f01ef585011beea7b361150f4504638f0800000002000000000000002100000001a208df134fefed8442b1f01fab59071898f5a1af5164e12c594de55a7004a91c8e0000002000000036ccb9ba8b4f0cd1f3e2d99338806893dff7478c69acee9b8e1247c053783a4800e876481700000000000200000000000000200000000b14ed4f5af8f8f077c7ec4313c6d395b9a7eb5f41eab9ec15367215ca9e420a01000000000000002000000032f56f77b09773aa64c78ee39943da7ec73f91cd757e325098e11b3edc4eccb10100000000000000"
);

struct CDevAccountResource account_resource = account_resource_from_lcs(blob.c_str(), blob.length());
std::cout << "balance: " << account_resource.balance
<< std::endl
<< "sequence: " << account_resource.sequence
<< std::endl
<< "delegated_key_rotation_capability: " << account_resource.delegated_key_rotation_capability
<< std::endl;

struct CEventHandle sent_events = account_resource.sent_events;
std::cout << "sent events count: " << sent_events.count << std::endl;
std::cout << std::endl;

std::cout << "sent events key:" << sent_events.key;
std::cout << std::endl;

struct CEventHandle received_events = account_resource.received_events;
std::cout << "received events count: " << received_events.count;
std::cout << std::endl;

std::cout << "sent events key:" << received_events.key;
std::cout << std::endl;


}
5 changes: 0 additions & 5 deletions libra-dev/Makefile

This file was deleted.

6 changes: 0 additions & 6 deletions libra-dev/account_resource.h

This file was deleted.

17 changes: 17 additions & 0 deletions libra-dev/data.h → libra-dev/include/data.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#ifndef LIBRA_DEV_H
#define LIBRA_DEV_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>

Expand All @@ -15,3 +22,13 @@ struct CDevAccountResource {
struct CEventHandle sent_events;
struct CEventHandle received_events;
};

struct CDevAccountResource account_resource_from_lcs(const uint8_t *buf, size_t len);
void account_resource_free(struct CDevAccountResource *point);


#ifdef __cplusplus
};
#endif

#endif // LIBRA_DEV_H
81 changes: 0 additions & 81 deletions libra-dev/main.c

This file was deleted.