github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap-confine/mount-support-test.c (about) 1 /* 2 * Copyright (C) 2015 Canonical Ltd 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 3 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * 16 */ 17 18 #include "mount-support.h" 19 #include "mount-support.c" 20 #include "mount-support-nvidia.h" 21 #include "mount-support-nvidia.c" 22 23 #include <glib.h> 24 25 static void replace_slashes_with_NUL(char *path, size_t len) 26 { 27 for (size_t i = 0; i < len; i++) { 28 if (path[i] == '/') 29 path[i] = '\0'; 30 } 31 } 32 33 static void test_get_nextpath__typical(void) 34 { 35 char path[] = "/some/path"; 36 size_t offset = 0; 37 size_t fulllen = strlen(path); 38 39 // Prepare path for usage with get_nextpath() by replacing 40 // all path separators with the NUL byte. 41 replace_slashes_with_NUL(path, fulllen); 42 43 // Run get_nextpath a few times to see what happens. 44 char *result; 45 result = get_nextpath(path, &offset, fulllen); 46 g_assert_cmpstr(result, ==, "some"); 47 result = get_nextpath(path, &offset, fulllen); 48 g_assert_cmpstr(result, ==, "path"); 49 result = get_nextpath(path, &offset, fulllen); 50 g_assert_cmpstr(result, ==, NULL); 51 } 52 53 static void test_get_nextpath__weird(void) 54 { 55 char path[] = "..///path"; 56 size_t offset = 0; 57 size_t fulllen = strlen(path); 58 59 // Prepare path for usage with get_nextpath() by replacing 60 // all path separators with the NUL byte. 61 replace_slashes_with_NUL(path, fulllen); 62 63 // Run get_nextpath a few times to see what happens. 64 char *result; 65 result = get_nextpath(path, &offset, fulllen); 66 g_assert_cmpstr(result, ==, "path"); 67 result = get_nextpath(path, &offset, fulllen); 68 g_assert_cmpstr(result, ==, NULL); 69 } 70 71 static void test_is_subdir(void) 72 { 73 // Sensible exaples are sensible 74 g_assert_true(is_subdir("/dir/subdir", "/dir/")); 75 g_assert_true(is_subdir("/dir/subdir", "/dir")); 76 g_assert_true(is_subdir("/dir/", "/dir")); 77 g_assert_true(is_subdir("/dir", "/dir")); 78 // Also without leading slash 79 g_assert_true(is_subdir("dir/subdir", "dir/")); 80 g_assert_true(is_subdir("dir/subdir", "dir")); 81 g_assert_true(is_subdir("dir/", "dir")); 82 g_assert_true(is_subdir("dir", "dir")); 83 // Some more ideas 84 g_assert_true(is_subdir("//", "/")); 85 g_assert_true(is_subdir("/", "/")); 86 g_assert_true(is_subdir("", "")); 87 // but this is not true 88 g_assert_false(is_subdir("/", "/dir")); 89 g_assert_false(is_subdir("/rid", "/dir")); 90 g_assert_false(is_subdir("/different/dir", "/dir")); 91 g_assert_false(is_subdir("/", "")); 92 } 93 94 static void __attribute__((constructor)) init(void) 95 { 96 g_test_add_func("/mount/get_nextpath/typical", 97 test_get_nextpath__typical); 98 g_test_add_func("/mount/get_nextpath/weird", test_get_nextpath__weird); 99 g_test_add_func("/mount/is_subdir", test_is_subdir); 100 }