Skip to content

Commit

Permalink
Merge pull request #486 from atsign-foundation/memcheck
Browse files Browse the repository at this point in the history
feat: memcheck with tests
  • Loading branch information
XavierChanth authored Jan 3, 2025
2 parents fe14743 + d7f35df commit 0192eb7
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 37 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Configurable options
set(TARGET_ESPIDF FALSE CACHE BOOL "Build for the espidf platform")

option(ATSDK_MEMCHECK "Enable memcheck configuration" OFF)
option(ATSDK_BUILD_TESTS "Build tests for atsdk ON | \"unit\" | \"func\" " OFF)

# to avoid caching issues
Expand All @@ -22,6 +23,9 @@ else()
set(ATSDK_BUILD_FUNCTIONAL_TESTS OFF)
endif()

# Disable building mbedtls programs
set(ENABLE_PROGRAMS OFF)

# Basic project setup
cmake_minimum_required(VERSION 3.24)
set(CMAKE_C_STANDARD 99)
Expand Down Expand Up @@ -111,5 +115,10 @@ if(ATSDK_BUILD_FUNCTIONAL_TESTS)
endif()

if(ATSDK_BUILD_UNIT_TESTS OR ATSDK_BUILD_FUNCTIONAL_TESTS)
if(ATSDK_MEMCHECK)
include(CTest)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
endif()
enable_testing()
endif()
60 changes: 47 additions & 13 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ alias build := build-debug
alias test := test-all
alias unit := test-unit
alias func := test-func
alias memd := memcheck-docker

set dotenv-filename := "just.env"
set dotenv-load

# SETUP COMMANDS

setup: configure-debug configure-test-func
ln -s $PWD/build/debug/compile_commands.json $PWD
ln -s $PWD/build/test-func/compile_commands.json $PWD/tests

setup-memcheck-docker:
docker build --platform linux/amd64 -t atc-memcheck-docker:latest -f $PWD/valgrind.Dockerfile $PWD

clean:
rm -rf $PWD/build
rm $PWD/compile_commands.json
rm $PWD/tests/compile_commands.json

# INSTALL COMMANDS

install: build-debug
cmake: --build $PWD/build/debug --target install

# BUILD COMMANDS

build-debug: configure-debug
cmake --build $PWD/build/debug

Expand All @@ -33,6 +43,11 @@ build-test-func: configure-test-func
build-test-all: configure-test-all
cmake --build $PWD/build/test-all

build-test-memcheck: configure-test-memcheck
cmake --build $PWD/build/test-memcheck

# TEST COMMANDS

test-unit: build-test-unit
ctest --test-dir $PWD/build/test-unit

Expand All @@ -42,6 +57,15 @@ test-func: build-test-func
test-all: build-test-all
ctest --test-dir $PWD/build/test-all

memcheck +ARGS='': build-test-memcheck
ctest -T memcheck --test-dir $PWD/build/test-memcheck {{ARGS}}

memcheck-docker +ARGS='':
docker run --rm --platform linux/amd64 --mount type=bind,src=$PWD,dst=/mnt/at_c atc-memcheck-docker:latest \
just memcheck {{ARGS}}

# CONFIGURE COMMANDS

configure-debug:
cmake -B $PWD/build/debug -S $PWD \
-DCMAKE_INSTALL_PREFIX="$HOME/.local/" \
Expand All @@ -50,47 +74,57 @@ configure-debug:
-DCMAKE_C_COMPILER=$C_COMPILER \
-DCMAKE_C_FLAGS="-std=c99 -Wno-error" \
-DATSDK_BUILD_TESTS=OFF \
-DATSDK_BUILD_UNIT_TESTS=OFF \
-DATSDK_BUILD_FUNCTIONAL_TESTS=OFF
-DATSDK_MEMCHECK=OFF

configure-release:
cmake -B $PWD/build/release -S $PWD \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER=$C_COMPILER \
-DCMAKE_C_FLAGS="-std=c99 -Wno-error" \
-DATSDK_BUILD_TESTS=OFF \
-DATSDK_BUILD_UNIT_TESTS=OFF \
-DATSDK_BUILD_FUNCTIONAL_TESTS=OFF
-DATSDK_MEMCHECK=OFF

configure-test-unit:
cmake -B $PWD/build/test-unit -S $PWD \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_COMPILER=$C_COMPILER \
-DCMAKE_C_FLAGS="-std=c99 -Wno-error" \
-DATSDK_BUILD_TESTS=OFF \
-DATSDK_BUILD_UNIT_TESTS=ON \
-DATSDK_BUILD_FUNCTIONAL_TESTS=OFF
-DCMAKE_C_FLAGS="-std=c99 -Wno-error " \
-DATSDK_BUILD_TESTS="unit" \
-DATSDK_MEMCHECK=OFF

configure-test-func:
cmake -B $PWD/build/test-func -S $PWD \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_COMPILER=$C_COMPILER \
-DCMAKE_C_FLAGS="-std=c99 -Wno-error" \
-DATSDK_BUILD_TESTS=OFF \
-DATSDK_BUILD_UNIT_TESTS=OFF \
-DATSDK_BUILD_FUNCTIONAL_TESTS=ON \
-DCMAKE_C_FLAGS="-std=c99 -Wno-error " \
-DATSDK_BUILD_TESTS="func" \
-DATSDK_MEMCHECK=OFF \
-DFIRST_ATSIGN="\"$FIRST_ATSIGN\"" \
-DSECOND_ATSIGN="\"$SECOND_ATSIGN\""

configure-test-all:
cmake -B $PWD/build/test-all -S $PWD \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_COMPILER=$C_COMPILER \
-DCMAKE_C_FLAGS="-std=c99 -Wno-error" \
-DCMAKE_C_FLAGS="-std=c99 -Wno-error " \
-DATSDK_BUILD_TESTS=ON \
-DATSDK_MEMCHECK=OFF \
-DFIRST_ATSIGN="\"$FIRST_ATSIGN\"" \
-DSECOND_ATSIGN="\"$SECOND_ATSIGN\""

configure-test-memcheck:
cmake -B $PWD/build/test-memcheck -S $PWD \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_COMPILER=$C_COMPILER \
-DCMAKE_C_FLAGS="-std=gnu99 -Wno-error" \
-DATSDK_BUILD_TESTS=ON \
-DBUILD_SHARED_LIBS=ON \
-DATSDK_MEMCHECK=ON \
-DFIRST_ATSIGN="\"$FIRST_ATSIGN\"" \
-DSECOND_ATSIGN="\"$SECOND_ATSIGN\""

# DIAGNOSTIC COMMANDS

show-env:
echo "$FIRST_ATSIGN"
echo "$SECOND_ATSIGN"
Expand Down
11 changes: 6 additions & 5 deletions packages/atauth/src/atactivate_arg_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cr
char **app_name, char **device_name, char **namespaces, char **root_host, int *root_port) {
int ret = 0, opt = 0;
char *root_fqdn = NULL;
const char *usage = "Usage: \n\tActivate: \t./atactivate -a atsign -c cram-secret [-k path_to_store_keysfile] [-r root-domain]"
"\n\n\tNew enrollment: ./at_auth_cli -a atsign -s otp/spp -p app_name -d device_name -n "
"namespaces(\"wavi:rw,buzz:r\") [-k path_to_store_keysfile] [-r root-domain]\n";
const char *usage =
"Usage: \n\tActivate: \t./atactivate -a atsign -c cram-secret [-k path_to_store_keysfile] [-r root-domain]"
"\n\n\tNew enrollment: ./at_auth_cli -a atsign -s otp/spp -p app_name -d device_name -n "
"namespaces(\"wavi:rw,buzz:r\") [-k path_to_store_keysfile] [-r root-domain]\n";

// Parse command-line arguments
while ((opt = getopt(argc, argv, "a:c:k:s:p:d:n:r:vh")) != -1) {
Expand Down Expand Up @@ -140,12 +141,12 @@ int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cr
}

int parse_root_domain(const char *root_domain_string, char **root_host, int *root_port) {
if(root_domain_string == NULL) {
if (root_domain_string == NULL) {
return 1;
}
*root_host = strdup(strtok((char *)root_domain_string, ":"));
*root_port = atoi(strtok(NULL, ":"));
if(*root_host == NULL || root_port == NULL) {
if (*root_host == NULL || root_port == NULL) {
return 1;
}
return 0;
Expand Down
5 changes: 3 additions & 2 deletions packages/atchops/src/rsa_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,13 @@ int atchops_rsa_key_generate_base64(unsigned char **public_key_base64_output,

// 7b. Second, we need to add headers to the PKCS#1 formatted private key to make it PKCS#8 formatted
const size_t private_key_pkcs8_size = private_key_non_base64_len + 22;
private_key_pkcs8 = (unsigned char *)malloc(private_key_pkcs8_size);
const size_t private_key_alloc_size = private_key_pkcs8_size + 4;
private_key_pkcs8 = (unsigned char *)malloc(private_key_alloc_size);
if (private_key_pkcs8 == NULL) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for private_key_pkcs8\n");
goto exit;
}
memset(private_key_pkcs8, 0, sizeof(unsigned char) * private_key_pkcs8_size);
memset(private_key_pkcs8, 0, sizeof(unsigned char) * private_key_alloc_size);
// https://lapo.it/asn1js/ use this to debug
// PrivateKeyInfo SEQUENCE (3 elements)
private_key_pkcs8[0] = 0x30; // constructed sequence tag
Expand Down
16 changes: 9 additions & 7 deletions packages/atchops/tests/test_rsaencrypt.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

#include "atchops/rsa.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

// #define PUBLICKEYBASE64 \
// "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg3P7mefqZg2GNQPiEHYinmTYUcbbW2Ar9Wi5LCD/" \
Expand All @@ -12,7 +12,12 @@
// "FutJTF8M6LKQY8E+h2cQjTEn3RRJlcMp4rwq/0GNmm3mNY5EhUcamKiSWILG9a8nYzeIUafXmESCZk+J1yVu9QcmXP8Dokv+4KLv76/" \
// "Y1RsqQIDAQAB"

#define PUBLICKEYBASE64 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0puvhTIiwSaqM9j6zPbvgEaj0Byg4AU/mtL5d6j6hnjJt6BDeztfqOxmLz9s+5NYHnS5ZExbtOohlFZXqISI39EWBsKgcqTOgfjCCWj3Cf1BbbNFiz/D322BvT6TLYeMtErSaQYPlQk4i3GZkp2SfwQcJ9CM7Gp+uCHTZ3NxEuU3Ut3roYOhKEVI72XesMPDlf4y8nSAsrpWs9KbU3isflj0yQXqMH3NXbDn2ie7h02Ul+u1fBuRs05TEFfaYt7R5ia+4USvcnkzGtA7mm8Xyo7AF2ZWnWYfx+368936buo7Vu9twt4Xynd/rvMxv2Fc/H/CFN9SI/n6zRN97uf85wIDAQAB"
#define PUBLICKEYBASE64 \
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0puvhTIiwSaqM9j6zPbvgEaj0Byg4AU/" \
"mtL5d6j6hnjJt6BDeztfqOxmLz9s+5NYHnS5ZExbtOohlFZXqISI39EWBsKgcqTOgfjCCWj3Cf1BbbNFiz/" \
"D322BvT6TLYeMtErSaQYPlQk4i3GZkp2SfwQcJ9CM7Gp+" \
"uCHTZ3NxEuU3Ut3roYOhKEVI72XesMPDlf4y8nSAsrpWs9KbU3isflj0yQXqMH3NXbDn2ie7h02Ul+u1fBuRs05TEFfaYt7R5ia+" \
"4USvcnkzGtA7mm8Xyo7AF2ZWnWYfx+368936buo7Vu9twt4Xynd/rvMxv2Fc/H/CFN9SI/n6zRN97uf85wIDAQAB"

#define PLAINTEXT "banana"

Expand All @@ -28,7 +33,6 @@ int main() {
const size_t ciphertextsize = 256;
unsigned char ciphertext[ciphertextsize];
memset(ciphertext, 0, sizeof(unsigned char) * ciphertextsize);
size_t ciphertextlen = 0;

atchops_rsa_key_public_key publickey;
atchops_rsa_key_public_key_init(&publickey);
Expand All @@ -46,11 +50,9 @@ int main() {
goto ret;
}
printf("atchops_rsa_encrypt (success): %d\n", ret);
printf("ciphertext (base64 encoded): \"%s\"\n", ciphertext);
printf("ciphertext (base64 encoded): \"%.*s\"\n", (int)ciphertextsize, ciphertext);

goto ret;

ret: {
return ret;
}
ret: { return ret; }
}
2 changes: 1 addition & 1 deletion packages/atchops/tests/test_rsasign.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main() {
goto exit;
}
printf("atchops_rsa_sign (success): %d\n", ret);
printf("signature: \"%s\"\n", signature);
printf("signature: \"%.*s\"\n", SIGNATURE_SIZE, signature);

ret = atchops_base64_encode(signature, SIGNATURE_SIZE, (unsigned char *)signaturebase64, SIGNATURE_BASE64_SIZE,
&signaturebase64len);
Expand Down
1 change: 0 additions & 1 deletion packages/atclient/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ foreach(file ${files})
target_link_libraries(${filename} PRIVATE atclient)
add_test(NAME ${filename} COMMAND $<TARGET_FILE:${filename}>)
endforeach()

16 changes: 8 additions & 8 deletions packages/atcommons/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
include_directories(${PROJECT_SOURCE_DIR}/src)
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})
foreach(file ${files})
# ${filename} - without `.c`
get_filename_component(filename ${file} NAME)
string(REPLACE ".c" "" filename ${filename})

add_executable(${filename} ${file})
target_link_libraries(${filename} PRIVATE atcommons)
add_test(NAME ${filename} COMMAND $<TARGET_FILE:${filename}>)
endforeach ()
add_executable(${filename} ${file})
target_link_libraries(${filename} PRIVATE atcommons)
add_test(NAME ${filename} COMMAND $<TARGET_FILE:${filename}>)
endforeach()
16 changes: 16 additions & 0 deletions valgrind.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM ubuntu:24.04

RUN apt-get update; apt-get upgrade -y
RUN apt-get install -y git-core \
build-essential \
sudo \
curl \
just \
valgrind \
gcc \
cmake

WORKDIR /mnt/at_c

CMD [ "tail", "-f", "/dev/null" ]

0 comments on commit 0192eb7

Please # to comment.