Skip to content

Commit

Permalink
Unit test for path sanitation porting
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybear327 committed Nov 22, 2023
1 parent 28f31a1 commit 02238fa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "elf.h"
#include "state.h"
#include "utils.h"

/* enable program trace mode */
static bool opt_trace = false;
Expand Down Expand Up @@ -188,6 +189,10 @@ static void dump_test_signature(elf_t *elf)

int main(int argc, char **args)
{
#ifdef UNITTEST
sanitize_path_test();
#endif

if (argc == 1 || !parse_args(argc, args)) {
print_usage(args[0]);
return 1;
Expand Down
67 changes: 67 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* "LICENSE" for information on usage and redistribution of this file.
*/

#include <assert.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdint.h>
#include <stdio.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.6 rule Note

MISRA 21.6 rule
#include <stdlib.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/time.h>
Expand Down Expand Up @@ -164,3 +166,68 @@ char *sanitize_path(const char *orig_path)
}
return ret;
}

#ifdef UNITTEST
void compare(char *input, char *expected_output)

Check warning

Code scanning / Cppcheck (reported by Codacy)

Parameter 'input' can be declared as pointer to const Warning

Parameter 'input' can be declared as pointer to const

Check warning

Code scanning / Cppcheck (reported by Codacy)

Parameter 'expected_output' can be declared as pointer to const Warning

Parameter 'expected_output' can be declared as pointer to const
{
char *input_sanitized = sanitize_path(input);
// printf("\n\nInput =\t\t\t%s\nOutput =\t\t%s\nExpected output =\t%s\n",
// input, input_sanitized, expected_output);
assert(strcmp(input_sanitized, expected_output) == 0);
free(input_sanitized);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.3 rule Note

MISRA 21.3 rule
}

void sanitize_path_test()
{
printf("sanitize_path_test - start\n");

// Already clean
compare("", ".");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("abc", "abc");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("abc/def", "abc/def");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare(".", ".");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("..", "..");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("../..", "../..");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("../../abc", "../../abc");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("/abc", "/abc");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("/", "/");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

// Remove trailing slash
compare("abc/", "abc");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("abc/def/", "abc/def");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("a/b/c/", "a/b/c");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("./", ".");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("../", "..");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("../../", "../..");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("/abc/", "/abc");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

// Remove doubled slash
compare("abc//def//ghi", "abc/def/ghi");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("//abc", "/abc");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("///abc", "/abc");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("//abc//", "/abc");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("abc//", "abc");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

// Remove . elements
compare("abc/./def", "abc/def");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("/./abc/def", "/abc/def");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("abc/.", "abc");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

// Remove .. elements
compare("abc/def/ghi/../jkl", "abc/def/jkl");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("abc/def/../ghi/../jkl", "abc/jkl");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("abc/def/..", "abc");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("abc/def/../..", ".");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("/abc/def/../..", "/");
compare("abc/def/../../..", "..");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("/abc/def/../../..", "/");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("abc/def/../../../ghi/jkl/../../../mno", "../../mno");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

// Combinations
compare("abc/./../def", "def");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule
compare("abc//./../def", "def");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
compare("abc/../../././../def", "../../def");

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.8 rule Note

MISRA 11.8 rule

printf("sanitize_path_test - end\n");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
}
#endif
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ void rv_clock_gettime(struct timespec *tp);
* https://9p.io/sys/doc/lexnames.html
*/
char *sanitize_path(const char *orig_path);

void sanitize_path_test();

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 802 with no text in the supplied rule-texts-file Warning

misra violation 802 with no text in the supplied rule-texts-file

0 comments on commit 02238fa

Please sign in to comment.