utils_tests.c (2401B)
1 #include "utils.h" 2 3 #include <limits.h> 4 #include <string.h> 5 6 #include "utest.h" 7 8 UTEST(path_concat, CanConcatenatePaths) { 9 char out[PATH_MAX]; 10 const char* returned = path_concat(out, sizeof(out), "p1", "p2"); 11 EXPECT_STREQ("p1/p2", out); 12 EXPECT_STREQ("p1/p2", returned); 13 } 14 15 UTEST(path_concat, CanConcatenatePathsFirstEmpty) { 16 char out[PATH_MAX]; 17 const char* returned = path_concat(out, sizeof(out), "", "p2"); 18 EXPECT_STREQ("p2", out); 19 EXPECT_STREQ("p2", returned); 20 } 21 22 UTEST(path_concat, CanConcatenatePathsSecondEmpty) { 23 char out[PATH_MAX]; 24 const char* returned = path_concat(out, sizeof(out), "p1", ""); 25 EXPECT_STREQ("p1", out); 26 EXPECT_STREQ("p1", returned); 27 } 28 29 UTEST(is_safe_repo_path, AllowsValidRelativePaths) { 30 EXPECT_TRUE(is_safe_repo_path("foo")); 31 EXPECT_TRUE(is_safe_repo_path("foo/bar")); 32 EXPECT_TRUE(is_safe_repo_path("foo/bar.txt")); 33 EXPECT_TRUE(is_safe_repo_path(".foo")); 34 EXPECT_TRUE(is_safe_repo_path("foo.bar/baz")); 35 } 36 37 UTEST(is_safe_repo_path, RejectsInvalidPaths) { 38 EXPECT_FALSE(is_safe_repo_path("/foo")); 39 EXPECT_FALSE(is_safe_repo_path("/foo/bar")); 40 EXPECT_FALSE(is_safe_repo_path("..")); 41 EXPECT_FALSE(is_safe_repo_path("../foo")); 42 EXPECT_FALSE(is_safe_repo_path("foo/..")); 43 EXPECT_FALSE(is_safe_repo_path("foo/../bar")); 44 EXPECT_FALSE(is_safe_repo_path("foo/./../bar")); 45 } 46 47 UTEST(is_safe_url, AllowsValidSchemes) { 48 EXPECT_TRUE(is_safe_url("http://example.com")); 49 EXPECT_TRUE(is_safe_url("https://example.com")); 50 EXPECT_TRUE(is_safe_url("git://example.com")); 51 } 52 53 UTEST(is_safe_url, RejectsInvalidSchemes) { 54 EXPECT_FALSE(is_safe_url("ftp://example.com")); 55 EXPECT_FALSE(is_safe_url("javascript:alert(1)")); 56 EXPECT_FALSE(is_safe_url("example.com")); 57 EXPECT_FALSE(is_safe_url("http:/example.com")); 58 EXPECT_FALSE(is_safe_url("http//example.com")); 59 EXPECT_FALSE(is_safe_url("")); 60 } 61 62 UTEST(is_safe_mailto, AllowsValidEmails) { 63 EXPECT_TRUE(is_safe_mailto("test@example.com")); 64 EXPECT_TRUE(is_safe_mailto("test.user+alias@example.co.uk")); 65 } 66 67 UTEST(is_safe_mailto, RejectsInvalidCharacters) { 68 EXPECT_FALSE(is_safe_mailto("test<script>@example.com")); 69 EXPECT_FALSE(is_safe_mailto("test>@example.com")); 70 EXPECT_FALSE(is_safe_mailto("test\"@example.com")); 71 EXPECT_FALSE(is_safe_mailto("test'@example.com")); 72 EXPECT_FALSE(is_safe_mailto("test\\@example.com")); 73 EXPECT_FALSE(is_safe_mailto("test@example.com\n")); 74 }