Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1458 from advancedtelematic/feat/c-api-updates
Browse files Browse the repository at this point in the history
Feat/c api updates
  • Loading branch information
pattivacek authored Nov 25, 2019
2 parents 5b09647 + d323579 commit 79d5a4f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 138 deletions.
21 changes: 18 additions & 3 deletions src/libaktualizr-c/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ SET(SOURCES api-test.c)

SET(CMAKE_SKIP_RPATH TRUE)

SET(REPO_PATH ${PROJECT_BINARY_DIR}/uptane_repos/c-api-test-repo)

if(CMAKE_CROSSCOMPILING)
find_program(UPTANE_GENERATOR NAMES uptane-generator)
else()
set(UPTANE_GENERATOR $<TARGET_FILE:uptane-generator>)
endif()

add_custom_target(api-test-resources-generation
COMMAND ${PROJECT_SOURCE_DIR}/tests/uptane_repo_generation/generate_repo.sh
${UPTANE_GENERATOR}
${REPO_PATH}
--add_campaigns)
add_dependencies(api-test-resources-generation uptane-generator)

add_executable(${TARGET_NAME} EXCLUDE_FROM_ALL ${SOURCES})
add_dependencies(build_tests ${TARGET_NAME})
target_link_libraries(${TARGET_NAME} aktualizr-c api-test-utils)
add_dependencies(${TARGET_NAME} api-test-resources-generation)

aktualizr_source_file_checks(${SOURCES})

add_test(NAME t_c_api_test COMMAND ${TARGET_NAME}
${PROJECT_SOURCE_DIR}/tests/fake_http_server/fake_test_server.py
${PROJECT_BINARY_DIR}/c-api-test-repo
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
set_tests_properties(t_c_api_test PROPERTIES FIXTURES_REQUIRED c_api_test)
${REPO_PATH}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
set_tests_properties(t_c_api_test PROPERTIES
ENVIRONMENT LD_PRELOAD=${PROJECT_BINARY_DIR}/src/libaktualizr-c/test/api-test-utils/libapi-test-utils.so:${PROJECT_BINARY_DIR}/src/libaktualizr-c/libaktualizr-c.so)

Expand Down
23 changes: 0 additions & 23 deletions src/libaktualizr-c/test/api-test-utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,3 @@ target_link_libraries(${TARGET_NAME} testutilities)
add_dependencies(build_tests ${TARGET_NAME})

aktualizr_source_file_checks(${SOURCES} ${HEADERS})



add_executable(uptane_generator_utils uptane-generator-utils.cc)
target_include_directories(uptane_generator_utils PUBLIC ${PROJECT_SOURCE_DIR}/tests)
target_link_libraries(uptane_generator_utils
aktualizr_static_lib
testutilities
uptane_generator_lib
${TEST_LIBS})
add_dependencies(build_tests uptane_generator_utils)

aktualizr_source_file_checks(uptane-generator-utils.cc)

add_test(NAME t_uptane_generator_setup COMMAND uptane_generator_utils
${PROJECT_BINARY_DIR}/src/uptane_generator/uptane-generator
${PROJECT_BINARY_DIR}/c-api-test-repo
${PROJECT_SOURCE_DIR}/tests/test_data/firmware.txt)
set_tests_properties(t_uptane_generator_setup PROPERTIES FIXTURES_SETUP c_api_test)

add_test(NAME t_uptane_generator_cleanup COMMAND uptane_generator_utils
${PROJECT_BINARY_DIR}/c-api-test-repo)
set_tests_properties(t_uptane_generator_cleanup PROPERTIES FIXTURES_CLEANUP c_api_test)
20 changes: 9 additions & 11 deletions src/libaktualizr-c/test/api-test-utils/api-test-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
#include <boost/process.hpp>
#include "config/config.h"
#include "test_utils.h"
#include "utilities/utils.h"

std::string serverAddress;
std::unique_ptr<boost::process::child> server;
std::unique_ptr<TemporaryDirectory> temp_dir;

FakeHttpServer *Run_fake_http_server(const char *serverPath, const char *metaPath) {
void Run_fake_http_server(const char *serverPath, const char *metaPath) {
std::string port = TestUtils::getFreePort();
serverAddress = "http://127.0.0.1:" + port;

auto *server_handle = new boost::process::child(serverPath, port, "-f", "-m", metaPath);
server = std_::make_unique<boost::process::child>(serverPath, port, "-f", "-m", metaPath);
TestUtils::waitForServer(serverAddress + "/");

return server_handle;
}

void Stop_fake_http_server(FakeHttpServer *server) {
if (server != nullptr && server->valid()) {
delete server;
}
}
void Stop_fake_http_server() { server.reset(); }

Config *Get_test_config(const char *storagePath) {
Config *Get_test_config() {
auto *config = new Config();

config->tls.server = serverAddress;
Expand All @@ -32,7 +29,8 @@ Config *Get_test_config(const char *storagePath) {
config->provision.server = serverAddress;
config->provision.provision_path = "tests/test_data/cred.zip";

config->storage.path = storagePath;
temp_dir = std_::make_unique<TemporaryDirectory>();
config->storage.path = temp_dir->Path();
config->pacman.type = PackageManager::kNone;

config->postUpdateValues();
Expand Down
6 changes: 3 additions & 3 deletions src/libaktualizr-c/test/api-test-utils/api-test-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ typedef struct Config Config;
typedef struct FakeHttpServer FakeHttpServer;
#endif

FakeHttpServer* Run_fake_http_server(const char* serverPath, const char* metaPath);
void Stop_fake_http_server(FakeHttpServer* server);
void Run_fake_http_server(const char* serverPath, const char* metaPath);
void Stop_fake_http_server();

Config* Get_test_config(const char* storagePath);
Config* Get_test_config();
void Remove_test_config(Config* config);

#ifdef __cplusplus
Expand Down
55 changes: 0 additions & 55 deletions src/libaktualizr-c/test/api-test-utils/uptane-generator-utils.cc

This file was deleted.

8 changes: 3 additions & 5 deletions src/libaktualizr-c/test/api-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "libaktualizr-c.h"

#define CLEANUP_AND_RETURN_FAILED \
Stop_fake_http_server(server); \
Stop_fake_http_server(); \
return EXIT_FAILURE;

struct EventCounts {
Expand Down Expand Up @@ -40,7 +40,6 @@ int main(int argc, char **argv) {
Updates *u;
Target *t;
Config *cfg;
FakeHttpServer *server;
int err;

if (argc != 3) {
Expand All @@ -50,9 +49,9 @@ int main(int argc, char **argv) {

const char *serverPath = argv[1];
const char *metaPath = argv[2];
server = Run_fake_http_server(serverPath, metaPath);
Run_fake_http_server(serverPath, metaPath);

cfg = Get_test_config(/* storagePath = */ metaPath);
cfg = Get_test_config();
a = Aktualizr_create_from_cfg(cfg);
if (!a) {
printf("Aktualizr_create_from_cfg failed\n");
Expand Down Expand Up @@ -205,7 +204,6 @@ int main(int argc, char **argv) {

Aktualizr_destroy(a);

Stop_fake_http_server(server);
Remove_test_config(cfg);

if (counts.AllInstallsCompleteCount == 0 || counts.CampaignCheckCompleteCount == 0 ||
Expand Down
4 changes: 2 additions & 2 deletions tests/uptane_repo_generation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_custom_target(uptane_repo_full_no_correlation_id
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/full_no_correlation_id.sh
${UPTANE_GENERATOR} ${PROJECT_BINARY_DIR}/uptane_repos/full_no_correlation_id)
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/generate_repo.sh
${UPTANE_GENERATOR} ${PROJECT_BINARY_DIR}/uptane_repos/full_no_correlation_id --add_default_secondary)
add_dependencies(uptane_repo_full_no_correlation_id uptane-generator)
36 changes: 0 additions & 36 deletions tests/uptane_repo_generation/full_no_correlation_id.sh

This file was deleted.

62 changes: 62 additions & 0 deletions tests/uptane_repo_generation/generate_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#! /bin/bash
set -eEuo pipefail

if [ "$#" -eq 0 ]; then
echo "Usage: $0 <uptane-generator> <output directory> [options]"
echo "Options:"
echo " -s|--add_default_secondary"
echo " -c|--add_campaigns"
exit 1
fi

ADD_DEFAULT_SECONDARY=false
ADD_CAMPAIGNS=false

for arg in "$@"
do
case $arg in
-s|--add_default_secondary)
ADD_DEFAULT_SECONDARY=true
;;
-c|--add_campaigns)
ADD_CAMPAIGNS=true
;;
esac
done

UPTANE_GENERATOR="$1"
DEST_DIR="$2"

uptane_gen() {
"$UPTANE_GENERATOR" --path "$DEST_DIR" "$@"
}

if [ -d "$DEST_DIR" ]; then
# already here, bailing
exit 0
fi

mkdir -p "$DEST_DIR"
trap 'rm -rf "$DEST_DIR"' ERR

IMAGES=$(mktemp -d)
trap 'rm -rf "$IMAGES"' exit
PRIMARY_FIRMWARE="$IMAGES/primary.txt"
echo "primary" > "$PRIMARY_FIRMWARE"

uptane_gen --command generate --expires 2021-07-04T16:33:27Z
uptane_gen --command image --filename "$PRIMARY_FIRMWARE" --targetname primary.txt --hwid primary_hw
uptane_gen --command addtarget --hwid primary_hw --serial CA:FE:A6:D2:84:9D --targetname primary.txt

if [ "$ADD_DEFAULT_SECONDARY" = true ]; then
SECONDARY_FIRMWARE="$IMAGES/secondary.txt"
echo "secondary" > "$SECONDARY_FIRMWARE"
uptane_gen --command image --filename "$SECONDARY_FIRMWARE" --targetname secondary.txt --hwid secondary_hw
uptane_gen --command addtarget --hwid secondary_hw --serial secondary_ecu_serial --targetname secondary.txt
fi

uptane_gen --command signtargets

if [ "$ADD_CAMPAIGNS" = true ]; then
uptane_gen --command addcampaigns
fi

0 comments on commit 79d5a4f

Please sign in to comment.