utils_tests.c (2188B)
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_concat("p1", "p2"); 10 EXPECT_STREQ("p1/p2", out); 11 free(out); 12 } 13 14 UTEST(path_concat, CanConcatenatePathsFirstEmpty) { 15 char* out = path_concat("", "p2"); 16 EXPECT_STREQ("p2", out); 17 free(out); 18 } 19 20 UTEST(path_concat, CanConcatenatePathsSecondEmpty) { 21 char* out = path_concat("p1", ""); 22 EXPECT_STREQ("p1", out); 23 free(out); 24 } 25 26 UTEST(is_safe_repo_path, AllowsValidRelativePaths) { 27 EXPECT_TRUE(is_safe_repo_path("foo")); 28 EXPECT_TRUE(is_safe_repo_path("foo/bar")); 29 EXPECT_TRUE(is_safe_repo_path("foo/bar.txt")); 30 EXPECT_TRUE(is_safe_repo_path(".foo")); 31 EXPECT_TRUE(is_safe_repo_path("foo.bar/baz")); 32 } 33 34 UTEST(is_safe_repo_path, RejectsInvalidPaths) { 35 EXPECT_FALSE(is_safe_repo_path("/foo")); 36 EXPECT_FALSE(is_safe_repo_path("/foo/bar")); 37 EXPECT_FALSE(is_safe_repo_path("..")); 38 EXPECT_FALSE(is_safe_repo_path("../foo")); 39 EXPECT_FALSE(is_safe_repo_path("foo/..")); 40 EXPECT_FALSE(is_safe_repo_path("foo/../bar")); 41 EXPECT_FALSE(is_safe_repo_path("foo/./../bar")); 42 } 43 44 UTEST(is_safe_url, AllowsValidSchemes) { 45 EXPECT_TRUE(is_safe_url("http://example.com")); 46 EXPECT_TRUE(is_safe_url("https://example.com")); 47 EXPECT_TRUE(is_safe_url("git://example.com")); 48 } 49 50 UTEST(is_safe_url, RejectsInvalidSchemes) { 51 EXPECT_FALSE(is_safe_url("ftp://example.com")); 52 EXPECT_FALSE(is_safe_url("javascript:alert(1)")); 53 EXPECT_FALSE(is_safe_url("example.com")); 54 EXPECT_FALSE(is_safe_url("http:/example.com")); 55 EXPECT_FALSE(is_safe_url("http//example.com")); 56 EXPECT_FALSE(is_safe_url("")); 57 } 58 59 UTEST(is_safe_mailto, AllowsValidEmails) { 60 EXPECT_TRUE(is_safe_mailto("test@example.com")); 61 EXPECT_TRUE(is_safe_mailto("test.user+alias@example.co.uk")); 62 } 63 64 UTEST(is_safe_mailto, RejectsInvalidCharacters) { 65 EXPECT_FALSE(is_safe_mailto("test<script>@example.com")); 66 EXPECT_FALSE(is_safe_mailto("test>@example.com")); 67 EXPECT_FALSE(is_safe_mailto("test\"@example.com")); 68 EXPECT_FALSE(is_safe_mailto("test'@example.com")); 69 EXPECT_FALSE(is_safe_mailto("test\\@example.com")); 70 EXPECT_FALSE(is_safe_mailto("test@example.com\n")); 71 }