Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tslibmeminfo_interface.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/meminfo.c
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "testutils.h"
24
25 #if defined (PROC_MEMINFO) && !defined (HAVE_LIBPROCPS)
26
27 # define NPL_TESTING
28 # include "../lib/meminfo.c"
29 # undef NPL_TESTING
30
31 proc_sysmem_t *sysmem = NULL;
32
33 static int
34 test_memory_init ()
35 {
36 int ret = 0;
37 const char *env_variable = "NPL_TEST_PATH_PROCMEMINFO";
38
39 ret = proc_sysmem_new (&sysmem);
40 if (ret < 0)
41 return EXIT_AM_HARDFAIL;
42
43 ret = setenv (env_variable, NPL_TEST_PATH_PROCMEMINFO, 1);
44 if (ret < 0)
45 return EXIT_AM_HARDFAIL;
46
47 proc_sysmem_read (sysmem);
48 unsetenv (env_variable);
49
50 return 0;
51 }
52
53 static void
54 test_memory_release ()
55 {
56 proc_sysmem_unref (sysmem);
57 }
58
59 # define test_memory_label(arg, value) \
60 static int test_memory_ ## arg (const void *tdata) \
61 { \
62 int err, ret = 0; \
63 unsigned long kb_value; \
64 const char *env_variable = "NPL_TEST_PATH_PROCMEMINFO"; \
65 \
66 /* read the data from (a static copy of) /proc/meminfo */ \
67 err = setenv (env_variable, NPL_TEST_PATH_PROCMEMINFO, 1); \
68 if (err < 0) \
69 return EXIT_AM_HARDFAIL; \
70 \
71 kb_value = proc_sysmem_get_ ## arg (sysmem); \
72 TEST_ASSERT_EQUAL_NUMERIC(kb_value, value); \
73 \
74 return ret; \
75 }
76
77 /* function used by check_memory */
78 test_memory_label (active, 3090692UL);
79 test_memory_label (anon_pages, 1008780UL);
80 test_memory_label (committed_as, 3678828UL);
81 test_memory_label (dirty, 92UL);
82 test_memory_label (inactive, 1044404UL);
83 test_memory_label (main_available, 14564424UL);
84 test_memory_label (main_buffers, 384876UL);
85 test_memory_label (main_free, 11918208UL);
86 test_memory_label (main_shared, 387476UL);
87 test_memory_label (main_total, 16384256UL);
88
89 /* function used by check_swap */
90 test_memory_label (swap_cached, 1024UL);
91 test_memory_label (swap_free, 8387580UL);
92 test_memory_label (swap_total, 8388604UL);
93
94 static int
95 mymain (void)
96 {
97 int err, ret = 0;
98
99 if ((err = test_memory_init ()) != 0)
100 return err;
101
102 /* test the public interface of the library */
103
104 # define DO_TEST(MSG, FUNC, DATA) \
105 do { if (test_run (MSG, FUNC, DATA) < 0) ret = -1; } while (0)
106
107 /* system memory */
108 DO_TEST ("check active memory", test_memory_active, NULL);
109 DO_TEST ("check anon_pages memory", test_memory_anon_pages, NULL);
110 DO_TEST ("check committed_as memory", test_memory_committed_as, NULL);
111 DO_TEST ("check dirty memory", test_memory_dirty, NULL);
112 DO_TEST ("check inactive memory", test_memory_inactive, NULL);
113 DO_TEST ("check main_available memory", test_memory_main_available, NULL);
114 DO_TEST ("check main_buffers memory", test_memory_main_buffers, NULL);
115 DO_TEST ("check main_free memory", test_memory_main_free, NULL);
116 DO_TEST ("check main_shared memory", test_memory_main_shared, NULL);
117 DO_TEST ("check main_total memory", test_memory_main_total, NULL);
118
119 /* system swap */
120 DO_TEST ("check swap_cached memory", test_memory_swap_cached, NULL);
121 DO_TEST ("check swap_free memory", test_memory_swap_free, NULL);
122 DO_TEST ("check swap_total memory", test_memory_swap_total, NULL);
123
124 test_memory_release ();
125
126 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
127 }
128
129 TEST_MAIN (mymain)
130
131 #else
132
133 int
134 main (void)
135 {
136 return EXIT_AM_SKIP;
137 }
138
139 #endif