github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/libsnap-confine-private/cleanup-funcs-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 "cleanup-funcs.h" 19 #include "cleanup-funcs.c" 20 21 #include <glib.h> 22 23 #include <sys/timerfd.h> 24 25 static int called = 0; 26 27 static void cleanup_fn(int *ptr) 28 { 29 called = 1; 30 } 31 32 // Test that cleanup functions are applied as expected 33 static void test_cleanup_sanity(void) 34 { 35 { 36 int test SC_CLEANUP(cleanup_fn); 37 test = 0; 38 test++; 39 } 40 g_assert_cmpint(called, ==, 1); 41 } 42 43 static void test_cleanup_string(void) 44 { 45 /* It is safe to use with a NULL pointer to a string. */ 46 sc_cleanup_string(NULL); 47 48 /* It is safe to use with a NULL string. */ 49 char *str = NULL; 50 sc_cleanup_string(&str); 51 52 /* It is safe to use with a non-NULL string. */ 53 str = malloc(1); 54 g_assert_nonnull(str); 55 sc_cleanup_string(&str); 56 g_assert_null(str); 57 } 58 59 static void test_cleanup_file(void) 60 { 61 /* It is safe to use with a NULL pointer to a FILE. */ 62 sc_cleanup_file(NULL); 63 64 /* It is safe to use with a NULL FILE. */ 65 FILE *f = NULL; 66 sc_cleanup_file(&f); 67 68 /* It is safe to use with a non-NULL FILE. */ 69 f = fmemopen(NULL, 10, "rt"); 70 g_assert_nonnull(f); 71 sc_cleanup_file(&f); 72 g_assert_null(f); 73 } 74 75 static void test_cleanup_endmntent(void) 76 { 77 /* It is safe to use with a NULL pointer to a FILE. */ 78 sc_cleanup_endmntent(NULL); 79 80 /* It is safe to use with a NULL FILE. */ 81 FILE *f = NULL; 82 sc_cleanup_endmntent(&f); 83 84 /* It is safe to use with a non-NULL FILE. */ 85 f = setmntent("/etc/fstab", "rt"); 86 g_assert_nonnull(f); 87 sc_cleanup_endmntent(&f); 88 g_assert_null(f); 89 } 90 91 static void test_cleanup_closedir(void) 92 { 93 /* It is safe to use with a NULL pointer to a DIR. */ 94 sc_cleanup_closedir(NULL); 95 96 /* It is safe to use with a NULL DIR. */ 97 DIR *d = NULL; 98 sc_cleanup_closedir(&d); 99 100 /* It is safe to use with a non-NULL DIR. */ 101 d = opendir("."); 102 g_assert_nonnull(d); 103 sc_cleanup_closedir(&d); 104 g_assert_null(d); 105 } 106 107 static void test_cleanup_close(void) 108 { 109 /* It is safe to use with a NULL pointer to an int. */ 110 sc_cleanup_close(NULL); 111 112 /* It is safe to use with a -1 file descriptor. */ 113 int fd = -1; 114 sc_cleanup_close(&fd); 115 116 /* It is safe to use with a non-invalid file descriptor. */ 117 /* Timerfd is a simple to use and widely available object that can be 118 * created and closed without interacting with the filesystem. */ 119 fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); 120 g_assert_cmpint(fd, !=, -1); 121 sc_cleanup_close(&fd); 122 g_assert_cmpint(fd, ==, -1); 123 } 124 125 static void __attribute__((constructor)) init(void) 126 { 127 g_test_add_func("/cleanup/sanity", test_cleanup_sanity); 128 g_test_add_func("/cleanup/string", test_cleanup_string); 129 g_test_add_func("/cleanup/file", test_cleanup_file); 130 g_test_add_func("/cleanup/endmntent", test_cleanup_endmntent); 131 g_test_add_func("/cleanup/closedir", test_cleanup_closedir); 132 g_test_add_func("/cleanup/close", test_cleanup_close); 133 }