summaryrefslogtreecommitdiffstats
path: root/lib/tests/test_opts1.c
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@aei.ca>2009-01-21 04:55:34 (GMT)
committerThomas Guyot-Sionnest <dermoth@aei.ca>2009-01-21 04:58:02 (GMT)
commit3b69f134671e60eb5de0ca895177064294b6eb78 (patch)
treee4a006bf794efb29f539b3002f979f179a655b5e /lib/tests/test_opts1.c
parent26bf63a5bba0fae77033693030c75eace202f988 (diff)
downloadmonitoring-plugins-3b69f134671e60eb5de0ca895177064294b6eb78.tar.gz
Some platform are missing setenv
Instead of adding the gnulib module for solving tests only I split the test in two, setting environment from Perl for the later.
Diffstat (limited to 'lib/tests/test_opts1.c')
-rw-r--r--lib/tests/test_opts1.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/lib/tests/test_opts1.c b/lib/tests/test_opts1.c
new file mode 100644
index 0000000..ea98403
--- /dev/null
+++ b/lib/tests/test_opts1.c
@@ -0,0 +1,206 @@
1/*****************************************************************************
2*
3* This program is free software: you can redistribute it and/or modify
4* it under the terms of the GNU General Public License as published by
5* the Free Software Foundation, either version 3 of the License, or
6* (at your option) any later version.
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 "common.h"
19#include "utils_base.h"
20#include "extra_opts.h"
21
22#include "tap.h"
23
24void my_free(int *argc, char **argv) {
25 int i;
26 printf (" Arg(%i): ", *argc);
27 for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]);
28 printf ("\n");
29 free(argv);
30 *argc=0;
31}
32
33int array_diff(int i1, char **a1, int i2, char **a2) {
34 int i;
35
36 if (i1 != i2) {
37 printf(" Argument count doesn't match!\n");
38 return 0;
39 }
40 for (i=0; i<=i1; i++) {
41 if (a1[i]==NULL && a2[i]==NULL) continue;
42 if (a1[i]==NULL || a2[i]==NULL) {
43 printf(" Argument # %i null in one array!\n", i);
44 return 0;
45 }
46 if (strcmp(a1[i], a2[i])) {
47 printf(" Argument # %i doesn't match!\n", i);
48 return 0;
49 }
50 }
51 return 1;
52}
53
54int
55main (int argc, char **argv)
56{
57 char **argv_test=NULL, **argv_known=NULL;
58 int i, argc_test;
59
60 plan_tests(5);
61
62 argv_test=(char **)malloc(2*sizeof(char **));
63 argv_test[0] = "prog_name";
64 argv_test[1] = NULL;
65 argc_test=1;
66 argv_known=(char **)realloc(argv_known, 2*sizeof(char **));
67 argv_known[0] = "prog_name";
68 argv_known[1] = NULL;
69 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
70 ok(array_diff(argc_test, argv_test, 1, argv_known), "No opts, returns correct argv/argc");
71 my_free(&argc_test, argv_test);
72
73 argv_test=(char **)malloc(6*sizeof(char **));
74 argv_test[0] = "prog_name";
75 argv_test[1] = "arg1";
76 argv_test[2] = "--arg2=val1";
77 argv_test[3] = "--arg3";
78 argv_test[4] = "val2";
79 argv_test[5] = NULL;
80 argc_test=5;
81 argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
82 argv_known[0] = "prog_name";
83 argv_known[1] = "arg1";
84 argv_known[2] = "--arg2=val1";
85 argv_known[3] = "--arg3";
86 argv_known[4] = "val2";
87 argv_known[5] = NULL;
88 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
89 ok(array_diff(argc_test, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv");
90 my_free(&argc_test,argv_test);
91
92 argv_test=(char **)malloc(3*sizeof(char **));
93 argv_test[0] = "prog_name";
94 argv_test[1] = "--extra-opts=@./config-opts.ini";
95 argv_test[2] = NULL;
96 argc_test=2;
97 argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
98 argv_known[0] = "prog_name";
99 argv_known[1] = "--foo=Bar";
100 argv_known[2] = "--this=Your Mother!";
101 argv_known[3] = "--blank";
102 argv_known[4] = NULL;
103 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
104 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts using default section");
105 my_free(&argc_test,argv_test);
106
107 argv_test=(char **)malloc(5*sizeof(char **));
108 argv_test[0] = "prog_name";
109 argv_test[1] = "--extra-opts=sect1@./config-opts.ini";
110 argv_test[2] = "--extra-opts";
111 argv_test[3] = "sect2@./config-opts.ini";
112 argv_test[4] = NULL;
113 argc_test=4;
114 argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
115 argv_known[0] = "prog_name";
116 argv_known[1] = "--one=two";
117 argv_known[2] = "--something else=oops";
118 argv_known[3] = "--this=that";
119 argv_known[4] = NULL;
120 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
121 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts specified twice");
122 my_free(&argc_test,argv_test);
123
124 argv_test=(char **)malloc(7*sizeof(char **));
125 argv_test[0] = "prog_name";
126 argv_test[1] = "--arg1=val1";
127 argv_test[2] = "--extra-opts=@./config-opts.ini";
128 argv_test[3] = "--extra-opts";
129 argv_test[4] = "sect1@./config-opts.ini";
130 argv_test[5] = "--arg2";
131 argv_test[6] = NULL;
132 argc_test=6;
133 argv_known=(char **)realloc(argv_known, 8*sizeof(char **));
134 argv_known[0] = "prog_name";
135 argv_known[1] = "--foo=Bar";
136 argv_known[2] = "--this=Your Mother!";
137 argv_known[3] = "--blank";
138 argv_known[4] = "--one=two";
139 argv_known[5] = "--arg1=val1";
140 argv_known[6] = "--arg2";
141 argv_known[7] = NULL;
142 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
143 ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
144 my_free(&argc_test,argv_test);
145
146 /* Next three checks are expected to die. They are commented out as they
147 * could possibly go in a sepatare test checked for return value.
148 */
149 /* argv_test=(char **)malloc(6*sizeof(char **));
150 argv_test[0] = "prog_name";
151 argv_test[1] = "arg1";
152 argv_test[2] = "--extra-opts=missing@./config-opts.ini";
153 argv_test[3] = "--arg3";
154 argv_test[4] = "val2";
155 argv_test[5] = NULL;
156 argc_test=5;
157 argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
158 argv_known[0] = "prog_name";
159 argv_known[1] = "arg1";
160 argv_known[2] = "--arg3";
161 argv_known[3] = "val2";
162 argv_known[4] = NULL;
163 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
164 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 1");
165 my_free(&argc_test,argv_test); */
166
167 /* argv_test=(char **)malloc(7*sizeof(char **));
168 argv_test[0] = "prog_name";
169 argv_test[1] = "arg1";
170 argv_test[2] = "--extra-opts";
171 argv_test[3] = "missing@./config-opts.ini";
172 argv_test[4] = "--arg3";
173 argv_test[5] = "val2";
174 argv_test[6] = NULL;
175 argc_test=6;
176 argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
177 argv_known[0] = "prog_name";
178 argv_known[1] = "arg1";
179 argv_known[2] = "--arg3";
180 argv_known[3] = "val2";
181 argv_known[4] = NULL;
182 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
183 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 2");
184 my_free(&argc_test,argv_test); */
185
186 /* argv_test=(char **)malloc(6*sizeof(char **));
187 argv_test[0] = "prog_name";
188 argv_test[1] = "arg1";
189 argv_test[2] = "--extra-opts";
190 argv_test[3] = "--arg3";
191 argv_test[4] = "val2";
192 argv_test[5] = NULL;
193 argc_test=5;
194 argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
195 argv_known[0] = "prog_name";
196 argv_known[1] = "arg1";
197 argv_known[2] = "--arg3";
198 argv_known[3] = "val2";
199 argv_known[4] = NULL;
200 argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
201 ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 3");
202 my_free(&argc_test,argv_test); */
203
204 return exit_status();
205}
206