diff --git a/.ci/run.py b/.ci/run.py index bcb756e1..d440c5e4 100644 --- a/.ci/run.py +++ b/.ci/run.py @@ -29,7 +29,7 @@ def is_appveyor(): def appveyor_image(): return os.getenv("APPVEYOR_BUILD_WORKER_IMAGE","") - + @contextmanager def chdir(dir_path): @@ -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 @@ -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 diff --git a/.gitignore b/.gitignore index 13918373..8f881759 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,6 @@ libraries/poco/md5/build/* .pydevproject .settings/* .ropeproject/* + +.tox +.python-version diff --git a/.travis.yml b/.travis.yml index 55b61216..05e59454 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 @@ -67,7 +26,7 @@ jobs: before_install: - ./.ci/travis/before_install.sh - + install: - | if [[ "$(uname -s)" == 'Darwin' ]]; then diff --git a/README.md b/README.md index e772e374..9a1c9ca5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/features/integrate_build_system/build.py b/features/integrate_build_system/build.py old mode 100644 new mode 100755 diff --git a/features/makefiles/build.sh b/features/makefiles/build.sh old mode 100644 new mode 100755 diff --git a/libraries/gtest/encryption/CMakeLists.txt b/libraries/gtest/encryption/CMakeLists.txt new file mode 100644 index 00000000..6613bc4a --- /dev/null +++ b/libraries/gtest/encryption/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/libraries/gtest/encryption/build.bat b/libraries/gtest/encryption/build.bat new file mode 100755 index 00000000..051451fd --- /dev/null +++ b/libraries/gtest/encryption/build.bat @@ -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 diff --git a/libraries/gtest/encryption/build.sh b/libraries/gtest/encryption/build.sh new file mode 100755 index 00000000..f7099562 --- /dev/null +++ b/libraries/gtest/encryption/build.sh @@ -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 diff --git a/libraries/gtest/encryption/conanfile.py b/libraries/gtest/encryption/conanfile.py new file mode 100644 index 00000000..3ad0ba92 --- /dev/null +++ b/libraries/gtest/encryption/conanfile.py @@ -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"] diff --git a/libraries/gtest/encryption/encrypter.cpp b/libraries/gtest/encryption/encrypter.cpp new file mode 100644 index 00000000..0ee79ce7 --- /dev/null +++ b/libraries/gtest/encryption/encrypter.cpp @@ -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(); +} diff --git a/libraries/gtest/encryption/encrypter.h b/libraries/gtest/encryption/encrypter.h new file mode 100644 index 00000000..468e6775 --- /dev/null +++ b/libraries/gtest/encryption/encrypter.h @@ -0,0 +1,10 @@ +#include +#include +#include +#include + + +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 *); + diff --git a/libraries/gtest/encryption/test_package/CMakeLists.txt b/libraries/gtest/encryption/test_package/CMakeLists.txt new file mode 100644 index 00000000..52fe9bca --- /dev/null +++ b/libraries/gtest/encryption/test_package/CMakeLists.txt @@ -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}) diff --git a/libraries/gtest/encryption/test_package/conanfile.py b/libraries/gtest/encryption/test_package/conanfile.py new file mode 100644 index 00000000..678f9279 --- /dev/null +++ b/libraries/gtest/encryption/test_package/conanfile.py @@ -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) diff --git a/libraries/gtest/encryption/test_package/encryption_test.cpp b/libraries/gtest/encryption/test_package/encryption_test.cpp new file mode 100644 index 00000000..9214b49a --- /dev/null +++ b/libraries/gtest/encryption/test_package/encryption_test.cpp @@ -0,0 +1,51 @@ +// SOURCE CODE FROM https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption + +#include +#include "encrypter.h" + +TEST(TestingEncryption, cipher) { + + /* A 256 bit key */ + unsigned char *key = (unsigned char *)"01234567890123456789012345678901"; + + /* A 128 bit IV */ + unsigned char *iv = (unsigned char *)"01234567890123456"; + + /* Message to be encrypted */ + unsigned char *plaintext = (unsigned char *) "The quick brown fox jumps over the lazy dog"; + unsigned char ciphertext[128]; + + /* Buffer for the decrypted text */ + unsigned char decryptedtext[128]; + + int decryptedtext_len, ciphertext_len; + + /* Initialise the library */ + ERR_load_crypto_strings(); + OpenSSL_add_all_algorithms(); + OPENSSL_config(NULL); + + /* Encrypt the plaintext */ + ciphertext_len = encrypt (plaintext, strlen ((char *)plaintext), key, iv, ciphertext); + + /* Do something useful with the ciphertext here */ + printf("Ciphertext is:\n"); + BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len); + + /* Decrypt the ciphertext */ + decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext); + + decryptedtext[decryptedtext_len] = '\0'; + + /* Show the decrypted text */ + printf("Decrypted text is:\n"); + printf("%s\n", decryptedtext); + + // The decoded data is the same that we encode + ASSERT_STREQ((const char*) decryptedtext, (const char*) plaintext); + + /* Clean up */ + EVP_cleanup(); + ERR_free_strings(); +} +