From 6b1a513131dbae95caaa1b4feb3170acd8b6f5ea Mon Sep 17 00:00:00 2001 From: XavierChanth Date: Fri, 6 Dec 2024 15:04:40 -0500 Subject: [PATCH 1/5] chore: allow tests to be overridden at build time with different atSigns --- CMakeLists.txt | 5 ++ packages/atchops/tests/CMakeLists.txt | 22 ++++++--- packages/atchops/tests/test_aes_generate.c | 19 ++++---- packages/atchops/tests/test_aesctr.c | 18 ++++--- packages/atchops/tests/test_aesctr_decrypt.c | 22 ++++----- packages/atchops/tests/test_iv.c | 6 +-- tests/functional_tests/CMakeLists.txt | 47 ++++++++++++------- tests/functional_tests/config.json | 13 +++-- tests/functional_tests/lib/CMakeLists.txt | 30 +++++------- .../lib/include/functional_tests/config.h | 5 ++ .../tests/test_atclient_get_atkeys.c | 17 ++++--- ...est_atclient_utils_find_atserver_address.c | 11 +++-- 12 files changed, 121 insertions(+), 94 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b51fe144..79769726 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,11 @@ message(STATUS "Building atauth") set(ATAUTH_BUILD_TESTS ${ATSDK_BUILD_TESTS}) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/packages/atauth) +if(${ATSDK_BUILD_TESTS}) + message(STATUS "Building functional tests") + add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tests/functional_tests) +endif() + if(NOT ESP_PLATFORM) # install dependencies set( diff --git a/packages/atchops/tests/CMakeLists.txt b/packages/atchops/tests/CMakeLists.txt index 336e4368..e5883398 100644 --- a/packages/atchops/tests/CMakeLists.txt +++ b/packages/atchops/tests/CMakeLists.txt @@ -1,12 +1,22 @@ +option(FIRST_ATSIGN "First atSign used for all functional tests") +if(${FIRST_ATSIGN}) + add_compile_definitions(FIRST_ATSIGN) +endif() + +option(SECOND_ATSIGN "Second atSign for two-way atSign functional tests") +if(${SECOND_ATSIGN}) + add_compile_definitions(SECOND_ATSIGN) +endif() + # loop through every .c file in this directory file(GLOB_RECURSE files ${CMAKE_CURRENT_LIST_DIR}/test_*.c) foreach(file ${files}) - # ${filename} - without `.c` - get_filename_component(filename ${file} NAME) - string(REPLACE ".c" "" filename ${filename}) + # ${filename} - without `.c` + get_filename_component(filename ${file} NAME) + string(REPLACE ".c" "" filename ${filename}) - add_executable(${filename} ${file}) - target_link_libraries(${filename} PRIVATE atchops atlogger) - add_test(NAME ${filename} COMMAND $) + add_executable(${filename} ${file}) + target_link_libraries(${filename} PRIVATE atchops atlogger) + add_test(NAME ${filename} COMMAND $) endforeach() diff --git a/packages/atchops/tests/test_aes_generate.c b/packages/atchops/tests/test_aes_generate.c index e064f5cb..40551469 100644 --- a/packages/atchops/tests/test_aes_generate.c +++ b/packages/atchops/tests/test_aes_generate.c @@ -1,9 +1,9 @@ #include "atchops/aes.h" #include "atchops/aes_ctr.h" #include "atchops/iv.h" +#include #include #include -#include #define PLAINTEXT "Hello World!\n" @@ -33,16 +33,15 @@ int main() { goto exit; } // log the key - printf("key (%d): ", keysize); + printf("key (%zu): ", keysize); for (size_t i = 0; i < keysize; i++) { printf("%02x ", key[i]); } printf("\n"); - memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - ret = atchops_aes_ctr_encrypt(key, ATCHOPS_AES_256, iv, (const unsigned char *)PLAINTEXT, - strlen(PLAINTEXT), ciphertext, ciphertextsize, &ciphertextlen); + ret = atchops_aes_ctr_encrypt(key, ATCHOPS_AES_256, iv, (const unsigned char *)PLAINTEXT, strlen(PLAINTEXT), + ciphertext, ciphertextsize, &ciphertextlen); if (ret != 0) { printf("Error encrypting\n"); goto exit; @@ -55,28 +54,28 @@ int main() { } // log ciphertext bytes - printf("ciphertext (%d): ", ciphertextlen); + printf("ciphertext (%zu): ", ciphertextlen); for (size_t i = 0; i < ciphertextlen; i++) { printf("%02x ", ciphertext[i]); } printf("\n"); memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, - plaintext2, plaintext2size, &plaintext2len); + ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext2, plaintext2size, + &plaintext2len); if (ret != 0) { printf("Error decrypting\n"); goto exit; } // log plaintext2 bytes - printf("plaintext2 (%d): ", plaintext2len); + printf("plaintext2 (%zu): ", plaintext2len); for (size_t i = 0; i < plaintext2len; i++) { printf("%02x ", plaintext2[i]); } printf("\n"); - if(strcmp(PLAINTEXT, (char *)plaintext2) != 0) { + if (strcmp(PLAINTEXT, (char *)plaintext2) != 0) { printf("plaintext2 is \"%.*s\" when it should be \"%s\"\n", (int)plaintext2len, plaintext2, PLAINTEXT); ret = 1; goto exit; diff --git a/packages/atchops/tests/test_aesctr.c b/packages/atchops/tests/test_aesctr.c index 92d60144..902b122f 100644 --- a/packages/atchops/tests/test_aesctr.c +++ b/packages/atchops/tests/test_aesctr.c @@ -1,10 +1,10 @@ #include "atchops/aes_ctr.h" #include "atchops/base64.h" #include "atchops/iv.h" +#include #include #include #include -#include #define PLAINTEXT "I like to eat pizza 123" #define AESKEY_BASE64 "1DPU9OP3CYvamnVBMwGgL7fm8yB1klAap0Uc5Z9R79g=" @@ -32,22 +32,22 @@ int main() { unsigned char iv[ATCHOPS_IV_BUFFER_SIZE]; memset(iv, 0, sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - ret = atchops_base64_decode(AESKEY_BASE64, strlen(AESKEY_BASE64), key, keysize, &keylen); - if(ret != 0) { + ret = atchops_base64_decode((unsigned char *)AESKEY_BASE64, strlen(AESKEY_BASE64), key, keysize, &keylen); + if (ret != 0) { printf("atchops_base64_decode (failed): %d\n", ret); goto exit; } - ret = atchops_aes_ctr_encrypt(key, ATCHOPS_AES_256, iv, (unsigned char *) PLAINTEXT, - strlen(PLAINTEXT), ciphertext, ciphertextsize, &ciphertextlen); + ret = atchops_aes_ctr_encrypt((unsigned char *)key, ATCHOPS_AES_256, iv, (unsigned char *)PLAINTEXT, + strlen(PLAINTEXT), ciphertext, ciphertextsize, &ciphertextlen); if (ret != 0) { printf("atchops_aes_ctr_encrypt (failed): %d\n", ret); goto exit; } memset(iv, 0, sizeof(unsigned char) * 16); - ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext2, - plaintext2size, &plaintext2len); + ret = atchops_aes_ctr_decrypt((unsigned char *)key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext2, + plaintext2size, &plaintext2len); if (ret != 0) { printf("atchops_aes_ctr_decrypt (failed): %d\n", ret); goto exit; @@ -55,7 +55,5 @@ int main() { goto exit; -exit: { - return ret; -} +exit: { return ret; } } diff --git a/packages/atchops/tests/test_aesctr_decrypt.c b/packages/atchops/tests/test_aesctr_decrypt.c index 6f16b3aa..5b50d990 100644 --- a/packages/atchops/tests/test_aesctr_decrypt.c +++ b/packages/atchops/tests/test_aesctr_decrypt.c @@ -1,10 +1,10 @@ #include "atchops/aes_ctr.h" -#include "atchops/iv.h" #include "atchops/base64.h" +#include "atchops/iv.h" +#include #include #include #include -#include #define CIPHERTEXTBASE64 \ "yY9vOA8bbzyADNGOxFPdT6bGxySKmNd6usP5/" \ @@ -56,27 +56,27 @@ int main() { size_t keylen = 0; unsigned char *iv = malloc(sizeof(unsigned char) * ATCHOPS_IV_BUFFER_SIZE); - if(iv == NULL) { + if (iv == NULL) { printf("malloc (failed): %d\n", ret); goto exit; } memset(iv, 0, ATCHOPS_IV_BUFFER_SIZE); // keys in the atKeys file are encrypted with AES with IV {0} * 16 - ret = atchops_base64_decode(CIPHERTEXTBASE64, strlen(CIPHERTEXTBASE64), ciphertext, ciphertextsize, &ciphertextlen); - if(ret != 0) { + ret = atchops_base64_decode((unsigned char *)CIPHERTEXTBASE64, strlen(CIPHERTEXTBASE64), ciphertext, ciphertextsize, + &ciphertextlen); + if (ret != 0) { printf("atchops_base64_decode (failed): %d\n", ret); goto exit; } - ret = atchops_base64_decode(AESKEYBASE64, strlen(AESKEYBASE64), key, 32, &keylen); - if(ret != 0) { + ret = atchops_base64_decode((unsigned char *)AESKEYBASE64, strlen(AESKEYBASE64), key, 32, &keylen); + if (ret != 0) { printf("atchops_base64_decode (failed): %d\n", ret); goto exit; } - ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, - ciphertext, ciphertextlen, - plaintext, plaintextsize, &plaintextlen); + ret = atchops_aes_ctr_decrypt(key, ATCHOPS_AES_256, iv, ciphertext, ciphertextlen, plaintext, plaintextsize, + &plaintextlen); if (ret != 0) { printf("atchops_aes_ctr_decrypt (failed): %d\n", ret); goto exit; @@ -90,7 +90,7 @@ int main() { printf("decrypted text: %.*s\n", (int)plaintextlen, plaintext); printf("decrypted bytes:\n"); - for (int i = 0; i < plaintextlen && i < plaintextsize; i++) { + for (size_t i = 0; i < plaintextlen && i < plaintextsize; i++) { printf("%02x ", *(plaintext + i)); } printf("\n"); diff --git a/packages/atchops/tests/test_iv.c b/packages/atchops/tests/test_iv.c index 29b03310..8fd660c5 100644 --- a/packages/atchops/tests/test_iv.c +++ b/packages/atchops/tests/test_iv.c @@ -1,9 +1,9 @@ #include "atchops/iv.h" +#include #include #include #include #include -#include #define TAG "test_iv" @@ -18,9 +18,9 @@ int main() { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to generate IV\n"); goto exit; } - + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "IV (%d bytes): ", ivsize); - for (int i = 0; i < ivsize; i++) { + for (size_t i = 0; i < ivsize; i++) { printf("%02x ", iv[i]); } printf("\n"); diff --git a/tests/functional_tests/CMakeLists.txt b/tests/functional_tests/CMakeLists.txt index 6a9f8bd2..2e594fe8 100644 --- a/tests/functional_tests/CMakeLists.txt +++ b/tests/functional_tests/CMakeLists.txt @@ -1,12 +1,24 @@ +option(FIRST_ATSIGN "First atSign used for all functional tests" OFF) +if(NOT "${FIRST_ATSIGN}" STREQUAL OFF) + message("Functional tests override first atsign: ${FIRST_ATSIGN}") + add_compile_definitions(FIRST_ATSIGN=${FIRST_ATSIGN}) +endif() + +option(SECOND_ATSIGN "Second atSign for two-way atSign functional tests" OFF) +if(NOT "${SECOND_ATSIGN}" STREQUAL OFF) + message("Functional tests override second atsign: ${SECOND_ATSIGN}") + add_compile_definitions(SECOND_ATSIGN=${SECOND_ATSIGN}) +endif() + cmake_minimum_required(VERSION 3.19) set(CMAKE_C_STANDARD 99) project( - functional_tests - VERSION 0.0.1 - DESCRIPTION "Functional tests for atsdk" - HOMEPAGE_URL https://atsign.com - LANGUAGES C + functional_tests + VERSION 0.0.1 + DESCRIPTION "Functional tests for atsdk" + HOMEPAGE_URL https://atsign.com + LANGUAGES C ) find_package(atsdk CONFIG REQUIRED) @@ -18,15 +30,18 @@ enable_testing() file(GLOB_RECURSE files ${CMAKE_CURRENT_LIST_DIR}/tests/*test_*.c) foreach(file ${files}) - # ${filename} - without `.c` - get_filename_component(filename ${file} NAME) - string(REPLACE ".c" "" filename ${filename}) - - add_executable(${filename} ${file}) - target_include_directories(${filename} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/) - target_link_libraries(${filename} PRIVATE atsdk::atclient atsdk::functional_tests_lib) - add_test( - NAME ${filename} - COMMAND ${filename} - ) + # ${filename} - without `.c` + get_filename_component(filename ${file} NAME) + string(REPLACE ".c" "" filename ${filename}) + + add_executable(${filename} ${file}) + target_include_directories( + ${filename} + PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/ + ) + target_link_libraries( + ${filename} + PRIVATE atsdk::atclient atsdk::functional_tests_lib + ) + add_test(NAME ${filename} COMMAND ${filename}) endforeach() diff --git a/tests/functional_tests/config.json b/tests/functional_tests/config.json index 01682cb5..44b69f35 100644 --- a/tests/functional_tests/config.json +++ b/tests/functional_tests/config.json @@ -1,8 +1,7 @@ - - { - "rootHost": "root.atsign.org", - "rootPort": 64, - "firstAtSign": "@12alpaca", - "secondAtSign": "@12snowboating" -} \ No newline at end of file + "rootHost": "root.atsign.org", + "rootPort": 64, + "firstAtSign": "@12alpaca", + "secondAtSign": "@12snowboating" +} + diff --git a/tests/functional_tests/lib/CMakeLists.txt b/tests/functional_tests/lib/CMakeLists.txt index dc09b639..0704fab2 100644 --- a/tests/functional_tests/lib/CMakeLists.txt +++ b/tests/functional_tests/lib/CMakeLists.txt @@ -1,31 +1,25 @@ # check if subdirectory if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) - set(FUNCTIONAL_TESTS_LIB_SUBDIRECTORY FALSE) + set(FUNCTIONAL_TESTS_LIB_SUBDIRECTORY FALSE) else() - set(FUNCTIONAL_TESTS_LIB_SUBDIRECTORY TRUE) + set(FUNCTIONAL_TESTS_LIB_SUBDIRECTORY TRUE) endif() -set(FUNCTIONAL_TESTS_LIB_SOURCES - ${CMAKE_CURRENT_LIST_DIR}/src/config.c - ${CMAKE_CURRENT_LIST_DIR}/src/helpers.c +set( + FUNCTIONAL_TESTS_LIB_SOURCES + ${CMAKE_CURRENT_LIST_DIR}/src/config.c + ${CMAKE_CURRENT_LIST_DIR}/src/helpers.c ) -set(FUNCTIONAL_TESTS_LIB_INCLUDE_DIRS - ${CMAKE_CURRENT_LIST_DIR}/include -) +set(FUNCTIONAL_TESTS_LIB_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include) -add_library(functional_tests_lib STATIC - ${FUNCTIONAL_TESTS_LIB_SOURCES} -) +add_library(functional_tests_lib STATIC ${FUNCTIONAL_TESTS_LIB_SOURCES}) -target_include_directories(functional_tests_lib - PUBLIC - ${FUNCTIONAL_TESTS_LIB_INCLUDE_DIRS} +target_include_directories( + functional_tests_lib + PUBLIC ${FUNCTIONAL_TESTS_LIB_INCLUDE_DIRS} ) -target_link_libraries(functional_tests_lib - PUBLIC - atsdk::atclient -) +target_link_libraries(functional_tests_lib PUBLIC atsdk::atclient) add_library(atsdk::functional_tests_lib ALIAS functional_tests_lib) diff --git a/tests/functional_tests/lib/include/functional_tests/config.h b/tests/functional_tests/lib/include/functional_tests/config.h index 82292520..d27d5292 100644 --- a/tests/functional_tests/lib/include/functional_tests/config.h +++ b/tests/functional_tests/lib/include/functional_tests/config.h @@ -9,8 +9,13 @@ extern "C" { #define ROOT_HOST "root.atsign.org" #define ROOT_PORT 64 +#ifndef FIRST_ATSIGN #define FIRST_ATSIGN "@12alpaca" +#endif + +#ifndef SECOND_ATSIGN #define SECOND_ATSIGN "@12snowboating" +#endif /** * @brief Get the atkeys file path for the given atSign. diff --git a/tests/functional_tests/tests/test_atclient_get_atkeys.c b/tests/functional_tests/tests/test_atclient_get_atkeys.c index 9f1d435e..6f6c1213 100644 --- a/tests/functional_tests/tests/test_atclient_get_atkeys.c +++ b/tests/functional_tests/tests/test_atclient_get_atkeys.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -9,7 +10,7 @@ #define SCAN_REGEX ".*" -#define ATSIGN1 "@12alpaca" +#define ATSIGN1 FIRST_ATSIGN static int test_1_atclient_get_atkeys(atclient *ctx, const char *scan_regex, const bool showhidden); static int test_2_atclient_get_atkeys_null(atclient *ctx, const char *scan_regex, const bool showhidden); @@ -47,12 +48,12 @@ int main() { goto exit; } - if((ret = test_3_atclient_get_atkeys_null_ctx(SCAN_REGEX, false)) != 0) { + if ((ret = test_3_atclient_get_atkeys_null_ctx(SCAN_REGEX, false)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_3_atclient_get_atkeys_null_ctx failed: %d", ret); goto exit; } - if((ret = test_4_atclient_get_atkeys_null_regex(&atclient1, SCAN_REGEX, false)) != 0) { + if ((ret = test_4_atclient_get_atkeys_null_regex(&atclient1, SCAN_REGEX, false)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_4_atclient_get_atkeys_null_regex failed: %d", ret); goto exit; } @@ -79,7 +80,7 @@ static int test_1_atclient_get_atkeys(atclient *ctx, const char *scan_regex, con goto exit; } - for(size_t i = 0; i < atkey_array_len; i++) { + for (size_t i = 0; i < atkey_array_len; i++) { char *atkeystr = NULL; atclient_atkey_to_string(&atkey_array[i], &atkeystr); atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atkey_array[%zu]: %s\n", i, atkeystr); @@ -119,8 +120,7 @@ exit: { } } -static int test_3_atclient_get_atkeys_null_ctx(const char *scan_regex, const bool showhidden) -{ +static int test_3_atclient_get_atkeys_null_ctx(const char *scan_regex, const bool showhidden) { int ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test_3_atclient_get_atkeys_null_ctx\n"); @@ -144,8 +144,7 @@ exit: { } } -static int test_4_atclient_get_atkeys_null_regex(atclient *ctx, const char *scan_regex, const bool showhidden) -{ +static int test_4_atclient_get_atkeys_null_regex(atclient *ctx, const char *scan_regex, const bool showhidden) { int ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test_4_atclient_get_atkeys_null_regex\n"); @@ -166,4 +165,4 @@ exit: { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "test_4_atclient_get_atkeys_null_regex: %d\n", ret); return ret; } -} \ No newline at end of file +} diff --git a/tests/functional_tests/tests/test_atclient_utils_find_atserver_address.c b/tests/functional_tests/tests/test_atclient_utils_find_atserver_address.c index c4573691..d60acb0b 100644 --- a/tests/functional_tests/tests/test_atclient_utils_find_atserver_address.c +++ b/tests/functional_tests/tests/test_atclient_utils_find_atserver_address.c @@ -1,15 +1,17 @@ #include #include +#include #include #include #define ATDIRECTORY_HOST "root.atsign.org" #define ATDIRECTORY_PORT 64 -#define ATSIGN "@12alpaca" - #define TAG "test_atclient_find_atserver_address" +// This ATSIGN should be pinned, we don't need the keys for it +// we want to test for a successful lookup in the atDirectory +#define ATSIGN "@12alpaca" #define EXPECTED_HOST "228aafb0-94d3-5aa2-a3b3-e36af115480d.swarm0002.atsign.zone" #define EXPECTED_PORT 6943 @@ -85,7 +87,8 @@ static int test_2_find_atserver_address_should_fail() { if ((ret = atclient_utils_find_atserver_address(ATDIRECTORY_HOST, ATDIRECTORY_PORT, "asjdflasjdf", &atserver_host, &atserver_port)) == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_find_atserver_address passed with exit code 0, when it was expected to fail... %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "atclient_find_atserver_address passed with exit code 0, when it was expected to fail... %d\n", ret); goto exit; } @@ -96,4 +99,4 @@ exit: { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "test_2_find_atserver_address_should_fail End: %d\n", ret); return ret; } -} \ No newline at end of file +} From 9250ed7768a0bac6b0362826536e22f206ae1b64 Mon Sep 17 00:00:00 2001 From: XavierChanth Date: Fri, 6 Dec 2024 14:48:22 -0500 Subject: [PATCH 2/5] chore: better way to organize arduino library --- generators/arduino/atsdk/.clangd | 2 + .../arduino/atsdk/atsdk_atsdk.cpptemplate | 4 -- generators/arduino/atsdk/generate.sh | 10 ++--- .../atsdk/{atsdk.htemplate => lib/atsdk.h} | 4 +- generators/arduino/atsdk/lib/atsdk_atsdk.cpp | 43 +++++++++++++++++++ .../atsdk_cjson.c} | 0 .../atsdk_cjson.h} | 0 7 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 generators/arduino/atsdk/.clangd delete mode 100644 generators/arduino/atsdk/atsdk_atsdk.cpptemplate rename generators/arduino/atsdk/{atsdk.htemplate => lib/atsdk.h} (73%) create mode 100644 generators/arduino/atsdk/lib/atsdk_atsdk.cpp rename generators/arduino/atsdk/{atsdk_cjson.ctemplate => lib/atsdk_cjson.c} (100%) rename generators/arduino/atsdk/{atsdk_cjson.htemplate => lib/atsdk_cjson.h} (100%) diff --git a/generators/arduino/atsdk/.clangd b/generators/arduino/atsdk/.clangd new file mode 100644 index 00000000..71a0dcb1 --- /dev/null +++ b/generators/arduino/atsdk/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-xc++, -std=c++11] diff --git a/generators/arduino/atsdk/atsdk_atsdk.cpptemplate b/generators/arduino/atsdk/atsdk_atsdk.cpptemplate deleted file mode 100644 index 1d822fdd..00000000 --- a/generators/arduino/atsdk/atsdk_atsdk.cpptemplate +++ /dev/null @@ -1,4 +0,0 @@ -#include "atsdk.h" -#include - -void atsdk_arduino_setup() {} diff --git a/generators/arduino/atsdk/generate.sh b/generators/arduino/atsdk/generate.sh index 31de4c43..78fbb1ae 100755 --- a/generators/arduino/atsdk/generate.sh +++ b/generators/arduino/atsdk/generate.sh @@ -180,20 +180,20 @@ fix_rel_headers() { } public_headers() { - cat "$script_dir/atsdk.htemplate" >>$src_base/atsdk.h + cp $script_dir/lib/* $src_base/ + # cat "$script_dir/atsdk.htemplate" >>$src_base/atsdk.h for d in ${packages[@]}; do local includes=$( cd "$src_base/$d" && ls *.h ) for f in $includes; do - echo "#include \"$d/$f\" // IWYU pragma: export" >>$src_base/atsdk.h done done - cp "$script_dir/atsdk_cjson.htemplate" $src_base/atsdk_cjson.h - cp "$script_dir/atsdk_cjson.ctemplate" $src_base/atsdk_cjson.c - cp "$script_dir/atsdk_atsdk.cpptemplate" $src_base/atsdk_atsdk.cpp + # cp "$script_dir/atsdk_cjson.htemplate" $src_base/atsdk_cjson.h + # cp "$script_dir/atsdk_cjson.ctemplate" $src_base/atsdk_cjson.c + # cp "$script_dir/atsdk_atsdk.cpptemplate" $src_base/atsdk_atsdk.cpp } echo "Cleaning generated files and folders" diff --git a/generators/arduino/atsdk/atsdk.htemplate b/generators/arduino/atsdk/lib/atsdk.h similarity index 73% rename from generators/arduino/atsdk/atsdk.htemplate rename to generators/arduino/atsdk/lib/atsdk.h index 2574cb55..f1cd3722 100644 --- a/generators/arduino/atsdk/atsdk.htemplate +++ b/generators/arduino/atsdk/lib/atsdk.h @@ -1,3 +1,4 @@ + #ifndef ATSDK_ARDUINO_H #define ATSDK_ARDUINO_H @@ -5,8 +6,7 @@ extern "C" { #endif -#include -#define ATLOGGER_DISABLE_TIMESTAMPS +#include // IWYU pragma: export #ifdef __cplusplus } diff --git a/generators/arduino/atsdk/lib/atsdk_atsdk.cpp b/generators/arduino/atsdk/lib/atsdk_atsdk.cpp new file mode 100644 index 00000000..09fbc0a8 --- /dev/null +++ b/generators/arduino/atsdk/lib/atsdk_atsdk.cpp @@ -0,0 +1,43 @@ +// For both development and compile time +#include "atsdk.h" +#include +#include +#include + +#ifdef __clang__ // For development to supress irrelevant errors +#include "../../../../packages/atlogger/include/atlogger/atlogger.h" +class DummySerial { +public: + void print(const char *); + void println(const char *); +}; +extern DummySerial Serial; + +#else // Actual compile time includes +#include +#endif + +void atsdk_arduino_setup() {} + +extern "C" { +void atlogger_log(const char *tag, const enum atlogger_logging_level level, const char *format, ...) { + atlogger_logging_level allowed_level = atlogger_get_logging_level(); + if (level > allowed_level) { + return; + } + + va_list args; + va_start(args, format); + if (tag != nullptr) { + Serial.print(tag); + Serial.print(" | "); + } + int len = vsnprintf(NULL, 0, format, args); + char *buf = new char[len + 1]; + vsnprintf(buf, len, format, args); + Serial.println(buf); + delete[] buf; + + va_end(args); +} +} diff --git a/generators/arduino/atsdk/atsdk_cjson.ctemplate b/generators/arduino/atsdk/lib/atsdk_cjson.c similarity index 100% rename from generators/arduino/atsdk/atsdk_cjson.ctemplate rename to generators/arduino/atsdk/lib/atsdk_cjson.c diff --git a/generators/arduino/atsdk/atsdk_cjson.htemplate b/generators/arduino/atsdk/lib/atsdk_cjson.h similarity index 100% rename from generators/arduino/atsdk/atsdk_cjson.htemplate rename to generators/arduino/atsdk/lib/atsdk_cjson.h From e60dfc121983138b3c0c7872ece5fff749e9bc9f Mon Sep 17 00:00:00 2001 From: XavierChanth Date: Fri, 6 Dec 2024 14:48:22 -0500 Subject: [PATCH 3/5] feat: Allow custom atlogger implementation --- generators/arduino/atsdk/generate.sh | 24 +++++++++++++++---- packages/atchops/include/atchops/platform.h | 4 ---- .../atchops/tests/test_rsaprivatepopulate.c | 2 +- packages/atclient/src/notify.c | 5 ---- packages/atcommons/include/atcommons/json.h | 10 -------- packages/atlogger/src/atlogger.c | 10 ++++---- 6 files changed, 26 insertions(+), 29 deletions(-) diff --git a/generators/arduino/atsdk/generate.sh b/generators/arduino/atsdk/generate.sh index 78fbb1ae..fdb0b9f4 100755 --- a/generators/arduino/atsdk/generate.sh +++ b/generators/arduino/atsdk/generate.sh @@ -181,7 +181,6 @@ fix_rel_headers() { public_headers() { cp $script_dir/lib/* $src_base/ - # cat "$script_dir/atsdk.htemplate" >>$src_base/atsdk.h for d in ${packages[@]}; do local includes=$( cd "$src_base/$d" && @@ -191,9 +190,22 @@ public_headers() { echo "#include \"$d/$f\" // IWYU pragma: export" >>$src_base/atsdk.h done done - # cp "$script_dir/atsdk_cjson.htemplate" $src_base/atsdk_cjson.h - # cp "$script_dir/atsdk_cjson.ctemplate" $src_base/atsdk_cjson.c - # cp "$script_dir/atsdk_atsdk.cpptemplate" $src_base/atsdk_atsdk.cpp +} + +overrides() { + # logging + echo "#define ATLOGGER_OVERRIDE_LOG_FUNCTION" >>$src_base/atlogger/atlogger.h + # json + { + echo "#define ATCOMMONS_JSON_PROVIDER_CJSON" + echo "#include " + } >>$src_base/atcommons/json.h + # platform + { + echo '#define PRIu64 "llu"' + echo "#define ATCHOPS_TARGET_ARDUINO" + echo "#define ATCHOPS_MBEDTLS_VERSION_2" + } >>$src_base/atchops/platform.h } echo "Cleaning generated files and folders" @@ -228,6 +240,9 @@ public_headers echo "Fixing all includes to be relative" fix_rel_headers +echo "Applying Arduino specific overrides" +overrides + echo "Done generating Arduino library" # functions @@ -236,6 +251,7 @@ unset clean unset gen_src unset public_headers unset fix_rel_headers +unset overrides # global variables unset script_dir diff --git a/packages/atchops/include/atchops/platform.h b/packages/atchops/include/atchops/platform.h index 056416a4..b07faf10 100644 --- a/packages/atchops/include/atchops/platform.h +++ b/packages/atchops/include/atchops/platform.h @@ -18,9 +18,5 @@ #elif defined(CONFIG_IDF_TARGET_ESP32) #define ATCHOPS_TARGET_ESPIDF -#elif defined(TARGET_PORTENTA_H7) -#define ATCHOPS_TARGET_ARDUINO -#define ATCHOPS_MBEDTLS_VERSION_2 - #endif #endif diff --git a/packages/atchops/tests/test_rsaprivatepopulate.c b/packages/atchops/tests/test_rsaprivatepopulate.c index 0adcd5a1..81e5e92d 100644 --- a/packages/atchops/tests/test_rsaprivatepopulate.c +++ b/packages/atchops/tests/test_rsaprivatepopulate.c @@ -1,7 +1,7 @@ #include "atchops/rsa_key.h" +#include #include #include -#include #define PRIVATE_KEY_BASE64 \ "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCy64Pzy9ZDdm6e96z3DmjektD7sKUo40Ax+" \ diff --git a/packages/atclient/src/notify.c b/packages/atclient/src/notify.c index c8e3e9b3..5cd91ee3 100644 --- a/packages/atclient/src/notify.c +++ b/packages/atclient/src/notify.c @@ -335,12 +335,7 @@ static int generate_cmd(const atclient_notify_params *params, const char *cmdval } if (atclient_notify_params_is_notification_expiry_initialized(params) && params->notification_expiry > 0) { -#ifdef TARGET_PORTENTA_H7 - // PRIu64 not defined on portenta but we know it's 32 bit so we can hard code it to llu - snprintf(cmd + off, cmdsize - off, ":ttln:%llu", params->notification_expiry); -#else snprintf(cmd + off, cmdsize - off, ":ttln:%" PRIu64, params->notification_expiry); -#endif off += strlen(":ttln:") + atclient_string_utils_long_strlen(params->notification_expiry); } diff --git a/packages/atcommons/include/atcommons/json.h b/packages/atcommons/include/atcommons/json.h index 7c6e983c..584070bd 100644 --- a/packages/atcommons/include/atcommons/json.h +++ b/packages/atcommons/include/atcommons/json.h @@ -17,16 +17,6 @@ extern "C" { #define ATCOMMONS_JSON_PROVIDER_CJSON #include // IWYU pragma: export -#elif defined(TARGET_PORTENTA_H7) -#define ATCOMMONS_JSON_PROVIDER_CJSON -#define ATCOMMONS_JSON_PROVIDER_ARDUINO - -// This file will be injected into the Arduino build by the generator -// search for atsdk_cjson.h.template in the generators directory -#include - -#else -#error "unable to resolve json provider for platform" #endif #ifdef __cplusplus diff --git a/packages/atlogger/src/atlogger.c b/packages/atlogger/src/atlogger.c index 11cee62a..cf32b685 100644 --- a/packages/atlogger/src/atlogger.c +++ b/packages/atlogger/src/atlogger.c @@ -6,11 +6,7 @@ #include #include -#if defined(TARGET_PORTENTA_H7) -#define ATLOGGER_DISABLE_TIMESTAMPS -#endif - -#ifndef ATLOGGER_DISABLE_TIMESTAMPS +#if !defined(ATLOGGER_DISABLE_TIMESTAMPS) && !defined(ATLOGGER_OVERRIDE_LOG_FUNCTION) #include #endif @@ -35,6 +31,7 @@ static atlogger_ctx *atlogger_get_instance() { return &ctx; } +#ifndef ATLOGGER_OVERRIDE_LOG_FUNCTION static void atlogger_get_prefix(enum atlogger_logging_level logging_level, char *prefix, size_t prefixlen) { memset(prefix, 0, prefixlen); int off = 0; @@ -88,6 +85,7 @@ static void atlogger_get_prefix(enum atlogger_logging_level logging_level, char off += 2; prefix[off] = '\0'; } +#endif enum atlogger_logging_level atlogger_get_logging_level() { atlogger_ctx *ctx = atlogger_get_instance(); @@ -104,6 +102,7 @@ void atlogger_set_opts(int opts) { ctx->opts = opts; } +#ifndef ATLOGGER_OVERRIDE_LOG_FUNCTION void atlogger_log(const char *tag, const enum atlogger_logging_level level, const char *format, ...) { atlogger_ctx *ctx = atlogger_get_instance(); @@ -125,6 +124,7 @@ void atlogger_log(const char *tag, const enum atlogger_logging_level level, cons vprintf(format, args); va_end(args); } +#endif void atlogger_fix_stdout_buffer(char *str, const size_t strlen) { // if str == 'Jeremy\r\n', i want it to be 'Jeremy' From 6e4a48eb75ec7ea23de85fd3dd0ca0ffbbc1e624 Mon Sep 17 00:00:00 2001 From: XavierChanth Date: Fri, 6 Dec 2024 19:51:35 -0500 Subject: [PATCH 4/5] chore: Remove template file settings --- .gitignore | 2 ++ .lazy.lua | 11 ----------- .vscode/.gitignore | 2 +- .vscode/settings.json | 11 ----------- CMakeLists.txt | 1 + generators/arduino/atsdk/lib/atsdk_atsdk.cpp | 2 +- 6 files changed, 5 insertions(+), 24 deletions(-) delete mode 100644 .lazy.lua delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 199ae4a6..8a9aa662 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,8 @@ lib/**/lib*.a sdkconfig +Testing/ + __pycache__/ .pio/ diff --git a/.lazy.lua b/.lazy.lua deleted file mode 100644 index d51167fb..00000000 --- a/.lazy.lua +++ /dev/null @@ -1,11 +0,0 @@ --- Anybody using the lazy package manager in nvim will have `.[c|cpp|h]template` --- files act like their normal respective filetype in this project -vim.filetype.add({ - extension = { - ctemplate = "c", - cpptemplate = "cpp", - htemplate = "cpp", - }, -}) - -return {} diff --git a/.vscode/.gitignore b/.vscode/.gitignore index 3e1cbdd4..23448927 100644 --- a/.vscode/.gitignore +++ b/.vscode/.gitignore @@ -1,4 +1,4 @@ * !.gitignore !c_cpp_properties.json -!settings.json + diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index cdf71f3d..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -// If you want to have custom local settings, vscode should have something like -// settings.local.json, but they don't the correct way to handle this is: -// https://code.visualstudio.com/docs/editor/workspaces -{ - "files.associations": { - "*.ctemplate": "c", - "*.cpptemplate": "cpp", - "*.htemplate": "cpp" - } -} - diff --git a/CMakeLists.txt b/CMakeLists.txt index 79769726..92f59d9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/packages/atauth) if(${ATSDK_BUILD_TESTS}) message(STATUS "Building functional tests") add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tests/functional_tests) + enable_testing() endif() if(NOT ESP_PLATFORM) diff --git a/generators/arduino/atsdk/lib/atsdk_atsdk.cpp b/generators/arduino/atsdk/lib/atsdk_atsdk.cpp index 09fbc0a8..7d5944e4 100644 --- a/generators/arduino/atsdk/lib/atsdk_atsdk.cpp +++ b/generators/arduino/atsdk/lib/atsdk_atsdk.cpp @@ -35,7 +35,7 @@ void atlogger_log(const char *tag, const enum atlogger_logging_level level, cons int len = vsnprintf(NULL, 0, format, args); char *buf = new char[len + 1]; vsnprintf(buf, len, format, args); - Serial.println(buf); + Serial.print(buf); delete[] buf; va_end(args); From b08407ba2991d6fb85d1b4ae64d06190c55332c1 Mon Sep 17 00:00:00 2001 From: XavierChanth Date: Fri, 6 Dec 2024 20:49:50 -0500 Subject: [PATCH 5/5] fix: tests --- .github/workflows/tests.yaml | 33 ++++++++++---------- CMakeLists.txt | 43 ++++++++++++++++++++------- tests/functional_tests/CMakeLists.txt | 13 +++++++- 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index ef497cb1..373e4b02 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -7,7 +7,7 @@ on: pull_request: branches: [trunk] -permissions: # added using https://github.com/step-security/secure-repo +permissions: # added using https://github.com/step-security/secure-repo contents: read jobs: @@ -15,13 +15,18 @@ jobs: runs-on: "ubuntu-latest" steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - - name: atsdk Unit CTest + - name: build atSDK run: | - cmake -S . -B build -DATSDK_BUILD_TESTS=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + cmake -S . -B build -DATSDK_BUILD_TESTS="unit" cmake --build build --target all - ctest --test-dir build/packages/atchops/tests --output-on-failure --timeout 2 - ctest --test-dir build/packages/atclient/tests --output-on-failure --timeout 2 + + - name: atchops Unit CTest + working-directory: build/packages/atchops/tests + run: ctest --test-dir . --output-on-failure --timeout 5 + + - name: atclient Unit CTest + working-directory: build/packages/atclient/tests + run: ctest --test-dir . --output-on-failure --timeout 5 functional-tests: runs-on: "ubuntu-latest" @@ -37,15 +42,13 @@ jobs: - name: Install atSDK run: | - cmake -S . -B build + cmake -S . -B build -DATSDK_BUILD_TESTS="func" sudo cmake --build build --target install - name: Build and Run Functional Tests - working-directory: tests/functional_tests + working-directory: build/tests/functional_tests run: | - cmake -S . -B build - cmake --build build - ctest --test-dir build -VV --timeout 60 + ctest --test-dir . -VV --timeout 90 build-examples: runs-on: "ubuntu-latest" @@ -73,19 +76,19 @@ jobs: cmake -S . -B build -DTARGET_SRC=put_publickey.c && cmake --build build cmake -S . -B build -DTARGET_SRC=put_selfkey.c && cmake --build build cmake -S . -B build -DTARGET_SRC=put_sharedkey.c && cmake --build build - + - name: Build events working-directory: examples/desktop/events run: | cmake -S . -B build cmake --build build - + - name: Build pkam_authenticate working-directory: examples/desktop/pkam_authenticate run: | cmake -S . -B build cmake --build build - + - name: Build REPL working-directory: examples/desktop/repl run: | @@ -102,4 +105,4 @@ jobs: working-directory: examples/desktop/sample_cmake_project run: | cmake -S . -B build -Datsdk="/usr/local/bin/cmake/atsdk" - cmake --build build \ No newline at end of file + cmake --build build diff --git a/CMakeLists.txt b/CMakeLists.txt index 92f59d9e..08f7819a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,26 @@ # Configurable options set(TARGET_ESPIDF FALSE CACHE BOOL "Build for the espidf platform") -option(ATSDK_BUILD_TESTS "Build tests for atsdk" OFF) + +option(ATSDK_BUILD_TESTS "Build tests for atsdk ON | \"unit\" | \"func\" " OFF) + +# to avoid caching issues +if(ATSDK_BUILD_TESTS STREQUAL "func") + message("FUNCTIONAL TESTING ENABLED") + set(ATSDK_BUILD_UNIT_TESTS OFF) + set(ATSDK_BUILD_FUNCTIONAL_TESTS ON) +elseif(ATSDK_BUILD_TESTS STREQUAL "unit") + message("UNIT TESTING ENABLED") + set(ATSDK_BUILD_UNIT_TESTS ON) + set(ATSDK_BUILD_FUNCTIONAL_TESTS OFF) +elseif(ATSDK_BUILD_TESTS) + message("ALL TESTING ENABLED") + set(ATSDK_BUILD_FUNCTIONAL_TESTS ON) + set(ATSDK_BUILD_UNIT_TESTS ON) +else() + message("TESTING DISABLED") + set(ATSDK_BUILD_UNIT_TESTS OFF) + set(ATSDK_BUILD_FUNCTIONAL_TESTS OFF) +endif() # Basic project setup cmake_minimum_required(VERSION 3.24) @@ -30,23 +50,17 @@ message(STATUS "Building atlogger") add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/packages/atlogger) message(STATUS "Building atchops") -set(ATCHOPS_BUILD_TESTS ${ATSDK_BUILD_TESTS}) +set(ATCHOPS_BUILD_TESTS ${ATSDK_BUILD_UNIT_TESTS}) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/packages/atchops) message(STATUS "Building atclient") -set(ATCLIENT_BUILD_TESTS ${ATSDK_BUILD_TESTS}) +set(ATCLIENT_BUILD_TESTS ${ATSDK_BUILD_UNIT_TESTS}) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/packages/atclient) message(STATUS "Building atauth") -set(ATAUTH_BUILD_TESTS ${ATSDK_BUILD_TESTS}) +set(ATAUTH_BUILD_TESTS ${ATSDK_BUILD_UNIT_TESTS}) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/packages/atauth) -if(${ATSDK_BUILD_TESTS}) - message(STATUS "Building functional tests") - add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tests/functional_tests) - enable_testing() -endif() - if(NOT ESP_PLATFORM) # install dependencies set( @@ -90,3 +104,12 @@ if(NOT ESP_PLATFORM) ) endif() endif() + +if(ATSDK_BUILD_FUNCTIONAL_TESTS) + message(STATUS "Building functional tests") + add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tests/functional_tests) +endif() + +if(ATSDK_BUILD_UNIT_TESTS OR ATSDK_BUILD_FUNCTIONAL_TESTS) + enable_testing() +endif() diff --git a/tests/functional_tests/CMakeLists.txt b/tests/functional_tests/CMakeLists.txt index 2e594fe8..66c069c6 100644 --- a/tests/functional_tests/CMakeLists.txt +++ b/tests/functional_tests/CMakeLists.txt @@ -21,7 +21,18 @@ project( LANGUAGES C ) -find_package(atsdk CONFIG REQUIRED) +if(NOT DEFINED FUNCTIONAL_TESTS_AS_SUBPROJECT) + set(FUNCTIONAL_TESTS_AS_SUBPROJECT ON) + if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(FUNCTIONAL_TESTS_AS_SUBPROJECT OFF) + endif() +endif() + +if(NOT FUNCTIONAL_TESTS_AS_SUBPROJECT) + find_package(atsdk CONFIG REQUIRED) +else() + add_library(atsdk::atclient ALIAS atclient) +endif() add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lib)