Skip to content

Commit 3c9fb5e

Browse files
author
Yucong Sun
authored
Adding real C/C++ tests and circleci integration (#4)
* Adding circleci integration * Adding real c/cpp testcases
1 parent c84476a commit 3c9fb5e

File tree

13 files changed

+226
-104
lines changed

13 files changed

+226
-104
lines changed

.circleci/config.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: 2.0
2+
3+
jobs:
4+
build:
5+
docker:
6+
- image: circleci/rust:stretch
7+
steps:
8+
- checkout
9+
- run:
10+
name: Setup
11+
command: |
12+
sudo sh -c 'echo "deb http://deb.debian.org/debian stretch-backports main" > /etc/apt/sources.list.d/backports.list'
13+
sudo apt-get update
14+
sudo apt-get -t stretch-backports install cmake
15+
- run:
16+
name: Build everything
17+
command: |
18+
./build.sh

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
# cmake stuff
55
cmake-build-*
6+
/build
67

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

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
cmake_minimum_required(VERSION 3.7)
22

33
project(libra-client-dev)
44

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# libra-client-dev
22
Core rust client library for Libra
3+
4+
# How do I even
5+
./build.sh

build.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Build libra-dev first
5+
cd libra-dev
6+
cargo build
7+
cd ..
8+
9+
# Then build everything else
10+
rm -rf build
11+
mkdir build
12+
cd build
13+
cmake ..
14+
make VERBOSE=1
15+
16+
# Test!
17+
./c/c-client
18+
./cpp/cpp-client

c/CMakeLists.txt

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
set(CMAKE_C_STANDARD 11)
22

3-
project(libra-client-c-dev)
3+
add_executable(c-client main.c)
44

5-
set(CMAKE_C_STANDARD 11)
5+
target_include_directories(c-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/include")
6+
target_link_directories(c-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/target/debug")
7+
target_link_libraries(c-client PRIVATE "libra_dev")
8+
9+
IF(APPLE)
10+
target_link_libraries(c-client PRIVATE "-framework Security")
11+
elseif(WIN32)
612

7-
add_executable(c-client main.c)
13+
else()
14+
target_link_libraries(c-client PRIVATE "pthread")
15+
target_link_libraries(c-client PRIVATE "dl")
16+
target_link_libraries(c-client PRIVATE "m")
17+
endif()

c/main.c

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
11
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdlib.h>
25

3-
int main() {
4-
fprintf(stdout, "it works!");
6+
#include "data.h"
7+
8+
int hexchr2bin(const char hex, char *out)
9+
{
10+
if (out == NULL)
11+
return 0;
12+
13+
if (hex >= '0' && hex <= '9') {
14+
*out = hex - '0';
15+
} else if (hex >= 'A' && hex <= 'F') {
16+
*out = hex - 'A' + 10;
17+
} else if (hex >= 'a' && hex <= 'f') {
18+
*out = hex - 'a' + 10;
19+
} else {
20+
return 0;
21+
}
22+
23+
return 1;
24+
}
25+
26+
size_t hexs2bin(const char *hex, unsigned char **out)
27+
{
28+
size_t len;
29+
char b1;
30+
char b2;
31+
size_t i;
32+
33+
if (hex == NULL || *hex == '\0' || out == NULL)
34+
return 0;
35+
36+
len = strlen(hex);
37+
if (len % 2 != 0)
38+
return 0;
39+
len /= 2;
40+
41+
*out = malloc(len);
42+
memset(*out, 0 , len);
43+
for (i=0; i<len; i++) {
44+
if (!hexchr2bin(hex[i*2], &b1) || !hexchr2bin(hex[i*2+1], &b2)) {
45+
return 0;
46+
}
47+
(*out)[i] = (b1 << 4) | b2;
48+
}
49+
return len;
50+
}
51+
52+
int main(int argc, const char **argv)
53+
{
54+
const char* blob = "020000002100000001674deac5e7fca75f00ca92b1ba3697f5f01ef585011beea7b361150f4504638f0800000002000000000000002100000001a208df134fefed8442b1f01fab59071898f5a1af5164e12c594de55a7004a91c8e0000002000000036ccb9ba8b4f0cd1f3e2d99338806893dff7478c69acee9b8e1247c053783a4800e876481700000000000200000000000000200000000b14ed4f5af8f8f077c7ec4313c6d395b9a7eb5f41eab9ec15367215ca9e420a01000000000000002000000032f56f77b09773aa64c78ee39943da7ec73f91cd757e325098e11b3edc4eccb10100000000000000";
55+
56+
uint8_t * result;
57+
int len = hexs2bin(blob, &result);
58+
59+
struct CDevAccountResource account_resource = account_resource_from_lcs((const uint8_t *) result, len);
60+
printf("balance: %llu \n", account_resource.balance);
61+
printf("sequence: %llu \n", account_resource.sequence);
62+
printf("delegated_key_rotation_capability: %d \n", account_resource.delegated_key_rotation_capability);
63+
64+
struct CEventHandle sent_events = account_resource.sent_events;
65+
printf("sent events count: %llu \n", sent_events.count);
66+
67+
printf("sent events key: ");
68+
for(int i = 0; i < 32; i++) {
69+
printf("%d ", sent_events.key[i]);
70+
}
71+
72+
struct CEventHandle received_events = account_resource.received_events;
73+
printf("received events count: %llu \n", received_events.count);
74+
75+
printf("received events key: ");
76+
for(int i = 0; i < 32; i++) {
77+
printf("%d ", received_events.key[i]);
78+
}
79+
80+
return 0;
581
}

cpp/CMakeLists.txt

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
set(CMAKE_CXX_STANDARD 11)
22

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

5-
set(CMAKE_CXX_STANDARD 11)
5+
target_include_directories(cpp-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/include")
6+
target_link_directories(cpp-client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/target/debug")
7+
target_link_libraries(cpp-client PRIVATE "libra_dev")
8+
9+
IF(APPLE)
10+
target_link_libraries(cpp-client PRIVATE "-framework Security")
11+
elseif(WIN32)
612

7-
add_executable(cpp-client main.cc)
13+
else()
14+
target_link_libraries(cpp-client PRIVATE "pthread")
15+
target_link_libraries(cpp-client PRIVATE "dl")
16+
target_link_libraries(cpp-client PRIVATE "m")
17+
endif()

cpp/main.cc

+61-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
1+
#include <string>
2+
3+
#include "data.h"
4+
15
#include <iostream>
26

7+
typedef std::basic_string<unsigned char> byte_string;
8+
9+
// C++98 guarantees that '0', '1', ... '9' are consecutive.
10+
// It only guarantees that 'a' ... 'f' and 'A' ... 'F' are
11+
// in increasing order, but the only two alternative encodings
12+
// of the basic source character set that are still used by
13+
// anyone today (ASCII and EBCDIC) make them consecutive.
14+
unsigned char hexval(unsigned char c) {
15+
if ('0' <= c && c <= '9')
16+
return c - '0';
17+
else if ('a' <= c && c <= 'f')
18+
return c - 'a' + 10;
19+
else if ('A' <= c && c <= 'F')
20+
return c - 'A' + 10;
21+
else abort();
22+
}
23+
24+
byte_string hex2bin(const std::string &in) {
25+
byte_string result;
26+
result.reserve(in.length() / 2);
27+
for (auto p = in.begin(); p != in.end(); p++) {
28+
unsigned char c = hexval(*p);
29+
p++;
30+
if (p == in.end()) break; // incomplete last digit - should report error
31+
c = (c << 4) + hexval(*p); // + takes precedence over <<
32+
result.push_back(c);
33+
}
34+
return result;
35+
}
36+
337
int main() {
4-
std::cout << "it works!";
38+
std::basic_string<unsigned char> blob = hex2bin(
39+
"020000002100000001674deac5e7fca75f00ca92b1ba3697f5f01ef585011beea7b361150f4504638f0800000002000000000000002100000001a208df134fefed8442b1f01fab59071898f5a1af5164e12c594de55a7004a91c8e0000002000000036ccb9ba8b4f0cd1f3e2d99338806893dff7478c69acee9b8e1247c053783a4800e876481700000000000200000000000000200000000b14ed4f5af8f8f077c7ec4313c6d395b9a7eb5f41eab9ec15367215ca9e420a01000000000000002000000032f56f77b09773aa64c78ee39943da7ec73f91cd757e325098e11b3edc4eccb10100000000000000"
40+
);
41+
42+
struct CDevAccountResource account_resource = account_resource_from_lcs(blob.c_str(), blob.length());
43+
std::cout << "balance: " << account_resource.balance
44+
<< std::endl
45+
<< "sequence: " << account_resource.sequence
46+
<< std::endl
47+
<< "delegated_key_rotation_capability: " << account_resource.delegated_key_rotation_capability
48+
<< std::endl;
49+
50+
struct CEventHandle sent_events = account_resource.sent_events;
51+
std::cout << "sent events count: " << sent_events.count << std::endl;
52+
std::cout << std::endl;
53+
54+
std::cout << "sent events key:" << sent_events.key;
55+
std::cout << std::endl;
56+
57+
struct CEventHandle received_events = account_resource.received_events;
58+
std::cout << "received events count: " << received_events.count;
59+
std::cout << std::endl;
60+
61+
std::cout << "sent events key:" << received_events.key;
62+
std::cout << std::endl;
63+
64+
565
}

libra-dev/Makefile

-5
This file was deleted.

libra-dev/account_resource.h

-6
This file was deleted.

libra-dev/data.h renamed to libra-dev/include/data.h

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#ifndef LIBRA_DEV_H
2+
#define LIBRA_DEV_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
18
#include <stdint.h>
29
#include <stdbool.h>
310

@@ -15,3 +22,13 @@ struct CDevAccountResource {
1522
struct CEventHandle sent_events;
1623
struct CEventHandle received_events;
1724
};
25+
26+
struct CDevAccountResource account_resource_from_lcs(const uint8_t *buf, size_t len);
27+
void account_resource_free(struct CDevAccountResource *point);
28+
29+
30+
#ifdef __cplusplus
31+
};
32+
#endif
33+
34+
#endif // LIBRA_DEV_H

libra-dev/main.c

-81
This file was deleted.

0 commit comments

Comments
 (0)