Skip to content
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

Add GTest example #17

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions .ci/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def is_appveyor():

def appveyor_image():
return os.getenv("APPVEYOR_BUILD_WORKER_IMAGE","")


@contextmanager
def chdir(dir_path):
Expand Down Expand Up @@ -100,8 +100,10 @@ def get_build_list():
build = [it for it in files if "build.py" in it]
if not build:
build = [it for it in files if os.path.basename(it) == script]

if build:
if "gtest" not in root:
continue
builds.append(os.path.join(root, build[0]))
dirs[:] = []
continue
Expand Down Expand Up @@ -189,18 +191,18 @@ def run_scripts(scripts):
with chdir(os.path.dirname(script)):
print_build(script)
build_script = [sys.executable, abspath] if abspath.endswith(".py") else abspath

# Need to initialize the cache with default files if they are not already there
try:
subprocess.call(['conan', 'install', 'foobar/foobar@conan/stable'], env=env,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except:
pass

with ensure_python_environment_preserved():
with ensure_cache_preserved():
result = subprocess.call(build_script, env=env)

results[script] = result
if result != 0 and FAIL_FAST:
break
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ libraries/poco/md5/build/*
.pydevproject
.settings/*
.ropeproject/*

.tox
.python-version
43 changes: 1 addition & 42 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,6 @@ jobs:
fast_finish: true
include:

- stage: Linux - Conan develop
name: Python 3.5
python: 3.5
env: TOXENV=py35-conandev
dist: xenial
- name: Python 3.8
python: 3.8
env: TOXENV=py38-conandev
dist: xenial

# All Linux first, check versions
- stage: Linux - Conan Current
name: Python 3.5
python: 3.5
env: TOXENV=py35-conancurrent
dist: xenial
- name: Python 3.8
python: 3.8
env: TOXENV=py38-conancurrent
dist: xenial

- stage: Linux - Conan 1.23
name: Python 3.5
python: 3.5
env: TOXENV=py35-conan123
dist: xenial
- name: Python 3.8
python: 3.8
env: TOXENV=py38-conan123
dist: xenial

- stage: Linux - Conan 1.22
name: Python 3.5
python: 3.5
env: TOXENV=py35-conan122
dist: xenial
- name: Python 3.8
python: 3.8
env: TOXENV=py38-conan122
dist: xenial

# Macos is slow, only if everything has passed
- stage: Macos - All Conan versions
name: Conan develop - Python 3.8
Expand All @@ -67,7 +26,7 @@ jobs:

before_install:
- ./.ci/travis/before_install.sh

install:
- |
if [[ "$(uname -s)" == 'Darwin' ]]; then
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ Demonstrate how to use Folly to validate an URI using Futures, FBString, Executo

Blog Post: https://blog.conan.io/2018/12/03/Using-Facebook-Folly-with-Conan.html

### [Testing package with GTest](libraries/gtest/encryption)

Demonstrate how to use GTest with your test package.

Docs: https://docs.conan.io/en/latest/integrations/ci/appveyor.html#building-and-testing-your-project

### [An introduction to Dear ImGui and how to use with Conan](libraries/dear-imgui/basic)

Demonstrate how to use Dear ImGui with Conan to add a GUI to an OpenGL3 application.
Expand Down
Empty file modified features/integrate_build_system/build.py
100644 → 100755
Empty file.
Empty file modified features/makefiles/build.sh
100644 → 100755
Empty file.
17 changes: 17 additions & 0 deletions libraries/gtest/encryption/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(encrypter CXX)

# Initialize Conan #############################################################
INCLUDE(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
CONAN_BASIC_SETUP()

# Create Encrypter library #####################################################
ADD_LIBRARY(${CMAKE_PROJECT_NAME} encrypter.cpp)
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${CONAN_LIBS})

# Install Encrypter library ####################################################
install(TARGETS ${CMAKE_PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
install(FILES encrypter.h DESTINATION include)
11 changes: 11 additions & 0 deletions libraries/gtest/encryption/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@ECHO ON

RMDIR /Q /S build source package

conan source . --source-folder source
conan install . --install-folder build
conan build . --source-folder source --build-folder build
conan package . --source-folder source --build-folder build --package-folder package
conan export-pkg . conan/testing --package-folder package

conan test test_package conan-gtest-example/0.1.0@conan/testing
24 changes: 24 additions & 0 deletions libraries/gtest/encryption/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e
set -x

rm -rf build source package

if [[ "$OSTYPE" == "darwin"* ]]; then
conan source . --source-folder source
conan install . --install-folder build -s build_type=Debug --build missing -s os.version=10.13
conan build . --source-folder source --build-folder build
conan package . --source-folder source --build-folder build --package-folder package
conan export-pkg . conan/testing --package-folder package --force

conan test test_package conan-gtest-example/0.1.0@conan/testing -s build_type=Debug --build missing -s os.version=10.13
else
conan source . --source-folder source
conan install . --install-folder build -s build_type=Debug --build missing
conan build . --source-folder source --build-folder build
conan package . --source-folder source --build-folder build --package-folder package
conan export-pkg . conan/testing --package-folder package --force

conan test test_package conan-gtest-example/0.1.0@conan/testing -s build_type=Debug --build missing
fi
34 changes: 34 additions & 0 deletions libraries/gtest/encryption/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from conans import ConanFile
from conans import CMake


class ConanGTestExample(ConanFile):
"""Build Conan GTest Example"""
name = "conan-gtest-example"
version = "0.1.0"
url = "https://github.com/conan-io/examples"
author = "lasote"
license = "MIT"
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"
exports = "*"
description = "Google Test example of use for conan.io"
requires = "openssl/1.1.1e"
options = {"shared": [True, False]}
default_options = {"shared": False}

def _configure_cmake(self):
cmake = CMake(self)
cmake.configure()
return cmake

def build(self):
cmake = self._configure_cmake()
cmake.build()

def package(self):
cmake = self._configure_cmake()
cmake.install()

def package_info(self):
self.cpp_info.libs = ["encrypter"]
85 changes: 85 additions & 0 deletions libraries/gtest/encryption/encrypter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SOURCE CODE FROM https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption
// applink.c => https://www.openssl.org/docs/faq.html
#include "encrypter.h"

int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext){
EVP_CIPHER_CTX *ctx;

int len;

int ciphertext_len;

/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors("(encrypt) EVP_CIPHER_CTX_new");

/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors("(encrypt) EVP_EncryptInit_ex");

/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors("(encrypt) EVP_EncryptUpdate");
ciphertext_len = len;

/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors("(encrypt) EVP_EncryptFinal_ex");
ciphertext_len += len;

/* Clean up */
EVP_CIPHER_CTX_free(ctx);

return ciphertext_len;
}


int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext){
EVP_CIPHER_CTX *ctx;

int len;

int plaintext_len;

/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors("(decrypt) EVP_CIPHER_CTX_new");

/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors("(decrypt) EVP_DecryptInitEx");

/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors("(decrypt) EVP_DecryptUpdate");
plaintext_len = len;

/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors("(decrypt) EVP_DecryptFinal_ex");
plaintext_len += len;

/* Clean up */
EVP_CIPHER_CTX_free(ctx);

return plaintext_len;
}

void handleErrors(const char * message){
fprintf(stderr, "%s\n", message);
ERR_print_errors_fp(stderr);
fflush(stderr);
abort();
}
10 changes: 10 additions & 0 deletions libraries/gtest/encryption/encrypter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>


int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext);
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext);
void handleErrors(const char *);

18 changes: 18 additions & 0 deletions libraries/gtest/encryption/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
PROJECT(encryption-test CXX)

# Initialize Conan #############################################################
INCLUDE(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
CONAN_BASIC_SETUP()

# Create application test ######################################################
ADD_EXECUTABLE(${PROJECT_NAME} encryption_test.cpp)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${CONAN_LIBS})
SET_PROPERTY(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
SET_PROPERTY(TARGET ${PROJECT_NAME} PROPERTY ENVIRONMENT "DYLD_LIBRARY_PATH=${CONAN_LIB_DIRS_GTEST}")

# Include Encryptor test #######################################################
ENABLE_TESTING()
ADD_TEST(NAME encryption
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
COMMAND ${PROJECT_NAME})
25 changes: 25 additions & 0 deletions libraries/gtest/encryption/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from conans import ConanFile
from conans import CMake, RunEnvironment, tools
import os

class TestConanGTestExample(ConanFile):
settings = "os", "compiler", "arch", "build_type"
generators = "cmake"
requires = "gtest/1.10.0"
default_options = {"gtest:shared": True}

def _configure_cmake(self):
cmake = CMake(self)
cmake.configure()
return cmake

def build(self):
cmake = self._configure_cmake()
cmake.build()

def test(self):
os.environ["CTEST_OUTPUT_ON_FAILURE"] = "1"
env_build = RunEnvironment(self)
with tools.environment_append(env_build.vars):
cmake = self._configure_cmake()
cmake.test(output_on_failure=True)
Loading