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

clangfmt, add readme, fix off by one arg and fix ut build #687

Merged
merged 3 commits into from
Jan 23, 2025
Merged
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
18 changes: 18 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ root_dir=$script_dir/..
build_clean=false
build_documentation=false
build_packages=false
verbose_build=false
platform_layer="linux"
trace_target_deps=false
step_handlers="microsoft/apt,microsoft/script,microsoft/simulator,microsoft/swupdate_v2"
Expand Down Expand Up @@ -62,6 +63,7 @@ print_help() {
echo "--enable-e2e-testing Enables settings for the E2E test pipelines."
echo "--build-packages Builds and packages the client in various package formats e.g debian."
echo "-o, --out-dir <out_dir> Sets the build output directory. Default is out."
echo "-v, --verbose Enables verbose compilation and linking output."
echo "-s, --static-analysis <tools...> Runs static analysis as part of the build."
echo " Tools is a comma delimited list of static analysis tools to run at build time."
echo " Tools: clang-tidy cppcheck cpplint iwyu lwyu (or all)"
Expand Down Expand Up @@ -97,6 +99,8 @@ print_help() {
echo ""
echo "--patch-version Patch version of ADU"
echo ""
echo ""
echo ""
echo "-h, --help Show this help message."
}

Expand Down Expand Up @@ -249,6 +253,9 @@ while [[ $1 != "" ]]; do
fi
output_directory=$1

;;
-v | --verbose)
verbose_build=true
;;
-s | --static-analysis)
shift
Expand Down Expand Up @@ -499,6 +506,17 @@ for i in "${static_analysis_tools[@]}"; do
esac
done

if [[ $verbose_build == "true" ]]; then
# Verbose output (very verbose, but useful!):
CMAKE_OPTIONS+=('--trace-expand')

# See cmake dependencies (very verbose):
CMAKE_OPTIONS+=('--debug-output')

# See library search output:
CMAKE_OPTIONS+=('-DCMAKE_EXE_LINKER_FLAGS=--enable-extra-pep-debug')
fi

if [[ $build_clean == "true" ]]; then
rm -rf "$output_directory"
rm -rf "/tmp/adu/testdata"
Expand Down
2 changes: 0 additions & 2 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ add_subdirectory (workflow_utils)
if (ADUC_BUILD_UNIT_TESTS)
add_subdirectory (test_utils)
endif ()

add_subdirectory (download_file)
8 changes: 0 additions & 8 deletions src/utils/download_file/CMakeLists.txt

This file was deleted.

10 changes: 10 additions & 0 deletions tools/download_file/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.22)

set(target_name download_file)

add_executable (${target_name})
target_sources (${target_name} PRIVATE main.cpp)
target_include_directories (${target_name} PUBLIC ../../src/inc)

find_package (deliveryoptimization_sdk CONFIG REQUIRED)
target_link_libraries (${target_name} PRIVATE Microsoft::deliveryoptimization)
55 changes: 55 additions & 0 deletions tools/download_file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Usage

Usage: ./download_file (--rootkeypkg-download|--payload-download) [--simulate-bad-hash] <URL> <FILEPATH>

Example:

```sh
sudo -u adu ./download_file --rootkeypkg-download <MY URL> <
```

# Build

mkdir build; cd build
rm -f CMakeCache.txt
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=DEBUG && ninja

# Run

## Simulate DO 404 Not Found for rootkey package

```
sudo -u adu ./download_file --rootkeypkg-download http://catchpoint.b.nlu.dl.adu.microsoft.com/westeurope/rootkeypackages/rootkeypackage-1.json.DOESNOTEXIST /tmp/rootkeypackage-1.json
```

Output:

```
Parsed URL arg: 'http://catchpoint.b.nlu.dl.adu.microsoft.com/westeurope/rootkeypackages/rootkeypackage-1.json.DOESNOTEXIST'
Parsed FILE arg: '/tmp/rootkeypackage-1.json'
DownloadRootKeyPkg_DO - Downloading File 'http://catchpoint.b.nlu.dl.adu.microsoft.com/westeurope/rootkeypackages/rootkeypackage-1.json.DOESNOTEXIST' to '/tmp/rootkeypackage-1.json'
DO error, msg: unrecognized error, code: 0x80190194, timeout? 0
Download rc: 0, erc: 0xa0190194
ResultCode: 0(0x00000000)
ERC: -1608973932(0xa0190194)
```

### Example: /var/log/deliveryoptimization-agent/do-agent.20250110_194623.log

```
2025-01-15T00:49:30.3076606Z 47127 47130 {TraceDownloadStatus} id: db4dd540-29fd-b648-9898-43ba09f04ec0, 5, codes: [404, 0x80190194, 0x0], 0 / 0
2025-01-15T00:49:30.3080816Z 47127 47129 info {ParseAndProcess} Download state change: 5
2025-01-15T00:49:30.3081585Z 47127 47130 {_PerformStateChange} db4dd540-29fd-b648-9898-43ba09f04ec0, state change request 5 --> 4
2025-01-15T00:49:30.3082808Z 47127 47130 info {Delete} (hr:0) Delete file /tmp/rootkeypackage-1.json
2025-01-15T00:49:30.3082853Z 47127 47130 {TraceDownloadStatus} id: db4dd540-29fd-b648-9898-43ba09f04ec0, 4, codes: [404, 0x80190194, 0x0], 0 / 0
2025-01-15T00:49:30.3082886Z 47127 47130 info {TraceDownloadCanceled} (hr:80190194) id: db4dd540-29fd-b648-9898-43ba09f04ec0, extError: 0, cdnUrl: http://
catchpoint.b.nlu.dl.adu.microsoft.com/westeurope/rootkeypackages/rootkeypackage-1.json.DOESNOTEXIST, mccHost: , filePath: /tmp/rootkeypackage-1.json, bytes: [
total: 0, down: 0]
```

## Simulate DO 200 OK

```sh
rm -f /tmp/rootkeypackage*.json
sudo -u adu ./download_file --rootkeypkg-download http://catchpoint.b.nlu.dl.adu.microsoft.com/westeurope/rootkeypackages/rootkeypackage-1.json /tmp/rootkeypackage-1.json
```
81 changes: 58 additions & 23 deletions src/utils/download_file/main.cpp → tools/download_file/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <do_config.h>
#include <do_download.h>

#include <stdio.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -13,8 +13,9 @@ ADUC_Result DownloadRootKeyPkg_DO(const char* url, const char* targetFilePath);

//decls
typedef int32_t ADUC_Result_t;
typedef enum {
SimMode_NONE=0,
typedef enum
{
SimMode_NONE = 0,
SimMode_DOWNLOAD_ROOTKEY_PKG,
SimMode_DOWNLOAD_UPDATE_PAYLOAD,
} SimMode;
Expand All @@ -24,37 +25,54 @@ SimMode g_SimMode = SimMode_NONE;
char* g_arg_url = NULL;
char* g_arg_out_filepath = NULL;

bool parse_args(int argc, char**argv) {
bool parse_args(int argc, char** argv)
{
bool parsed = false;

if (argc != 4 && argc != 5)
{
return false;
}

// argv[0] (--rootkeypkg-download|--payload-download) [--simulate-bad-hash] <URL> <FILEPATH>
int non_switch_cnt = 0;
for(int i=1; i<argc-1; ++i) {
if(0 == strcmp(argv[i], "--rootkeypkg-download")) {
for (int i = 1; i < argc; ++i)
{
if (0 == strcmp(argv[i], "--rootkeypkg-download"))
{
g_SimMode = SimMode_DOWNLOAD_ROOTKEY_PKG;
} else if(0 == strcmp(argv[i], "--rootkeypkg-download")) {
}
else if (0 == strcmp(argv[i], "--rootkeypkg-download"))
{
g_SimMode = SimMode_DOWNLOAD_UPDATE_PAYLOAD;
} else {
}
else
{
size_t len = strlen(argv[i]);
++non_switch_cnt;
if(non_switch_cnt > 2) {
if (non_switch_cnt > 2)
{
fprintf(stderr, "Too many non-switch args!\n");
goto done;
}
switch(non_switch_cnt) {
switch (non_switch_cnt)
{
case 1:
g_arg_url = (char*)calloc(1 + len, 1);
if(NULL == g_arg_url) {
if (NULL == g_arg_url)
{
goto done;
}
strncpy(g_arg_url/*dest*/, argv[i]/*src*/, len);
strncpy(g_arg_url /*dest*/, argv[i] /*src*/, len);
printf("Parsed URL arg: '%s'\n", g_arg_url);
break;
case 2:
g_arg_out_filepath = (char*)calloc(1 + len, 1);
if(NULL == g_arg_out_filepath) {
if (NULL == g_arg_out_filepath)
{
goto done;
}
strncpy(g_arg_out_filepath/*dest*/, argv[i]/*src*/, len);
strncpy(g_arg_out_filepath /*dest*/, argv[i] /*src*/, len);
printf("Parsed FILE arg: '%s'\n", g_arg_out_filepath);
break;
default:
Expand All @@ -67,31 +85,47 @@ bool parse_args(int argc, char**argv) {
return parsed;
}

void usage(const char* argv0) {
fprintf(stderr, "\nUsage: %s (--rootkeypkg-download|--payload-download) [--simulate-bad-hash] <URL> <FILEPATH>\n\n", argv0);
void usage(const char* argv0)
{
fprintf(
stderr,
"\nUsage: %s (--rootkeypkg-download|--payload-download) [--simulate-bad-hash] <URL> <FILEPATH>\n\n",
argv0);
}

int main(int argc, char** argv)
{
ADUC_Result result = {};

if (!parse_args(argc,argv)) {
if (!parse_args(argc, argv))
{
usage(argv[0]);
exit(1);
}

if(g_SimMode == SimMode_DOWNLOAD_ROOTKEY_PKG) {
if (g_SimMode == SimMode_DOWNLOAD_ROOTKEY_PKG)
{
result = DownloadRootKeyPkg_DO(g_arg_url, g_arg_out_filepath);
} else if (g_SimMode == SimMode_DOWNLOAD_UPDATE_PAYLOAD) {
}
else if (g_SimMode == SimMode_DOWNLOAD_UPDATE_PAYLOAD)
{
// TODO
fprintf(stderr, "\nDownload Update Payload NOT SUPPORTED YET\n");
}

printf("ResultCode: %d(0x%08x)\nERC: %d(0x%08x)\n", result.ResultCode, result.ResultCode, result.ExtendedResultCode, result.ExtendedResultCode);
printf(
"ResultCode: %d(0x%08x)\nERC: %d(0x%08x)\n",
result.ResultCode,
result.ResultCode,
result.ExtendedResultCode,
result.ExtendedResultCode);

if(IsAducResultCodeFailure(result.ResultCode)) {
if (IsAducResultCodeFailure(result.ResultCode))
{
return 1;
} else {
}
else
{
printf("Download Succeeded\n");
return 0;
}
Expand All @@ -115,7 +149,8 @@ ADUC_Result DownloadRootKeyPkg_DO(const char* url, const char* targetFilePath)
}
else
{
fprintf(stderr,
fprintf(
stderr,
"DO error, msg: %s, code: %#08x, timeout? %d\n",
ret.message().c_str(),
ret.value(),
Expand Down