diff options
author | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-03-24 07:02:28 +0000 |
---|---|---|
committer | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-03-24 07:02:28 +0000 |
commit | a34cf37404104ff5bb13fb5fecdf5e492401c6a3 (patch) | |
tree | e41a60d8a551b4ec7ef65be4ce084b7ae02be79e /lib/tests/test_opts.c | |
parent | dce143e354da414bdd5fa5fb0b9b488ac4221200 (diff) | |
download | monitoring-plugins-a34cf37404104ff5bb13fb5fecdf5e492401c6a3.tar.gz |
Add extra-opts argument parsing with tests
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1961 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/tests/test_opts.c')
-rw-r--r-- | lib/tests/test_opts.c | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c new file mode 100644 index 00000000..12726059 --- /dev/null +++ b/lib/tests/test_opts.c | |||
@@ -0,0 +1,208 @@ | |||
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 | * $Id: test_ini.c 1951 2008-03-16 18:10:47Z dermoth $ | ||
17 | * | ||
18 | *****************************************************************************/ | ||
19 | |||
20 | #include "common.h" | ||
21 | #include "extra_opts.h" | ||
22 | #include "utils_base.h" | ||
23 | |||
24 | #include "tap.h" | ||
25 | |||
26 | void my_free(int *argc, char **argv) { | ||
27 | int i; | ||
28 | printf (" Arg(%i): ", *argc); | ||
29 | for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]); | ||
30 | printf ("\n"); | ||
31 | free(argv); | ||
32 | *argc=0; | ||
33 | } | ||
34 | |||
35 | int array_diff(int i1, char **a1, int i2, char **a2) { | ||
36 | int i; | ||
37 | |||
38 | if (i1 != i2) { | ||
39 | printf(" Argument count doesn't match!\n"); | ||
40 | return 0; | ||
41 | } | ||
42 | for (i=0; i<=i1; i++) { | ||
43 | if (a1[i]==NULL && a2[i]==NULL) continue; | ||
44 | if (a1[i]==NULL || a2[i]==NULL) { | ||
45 | printf(" Argument # %i null in one array!\n", i); | ||
46 | return 0; | ||
47 | } | ||
48 | if (strcmp(a1[i], a2[i])) { | ||
49 | printf(" Argument # %i doesn't match!\n", i); | ||
50 | return 0; | ||
51 | } | ||
52 | } | ||
53 | return 1; | ||
54 | } | ||
55 | |||
56 | int | ||
57 | main (int argc, char **argv) | ||
58 | { | ||
59 | char **argv_test=NULL, **argv_known=NULL; | ||
60 | int i, argc_test, argc_new; | ||
61 | |||
62 | plan_tests(8); | ||
63 | |||
64 | argv_test=(char **)malloc(2*sizeof(char **)); | ||
65 | argv_test[0] = "prog_name"; | ||
66 | argv_test[1] = NULL; | ||
67 | argc_test=1; | ||
68 | argv_known=(char **)realloc(argv_known, 2*sizeof(char **)); | ||
69 | argv_known[0] = "prog_name"; | ||
70 | argv_known[1] = NULL; | ||
71 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
72 | ok(array_diff(argc_new, argv_test, 1, argv_known), "No opts, returns correct argv/argc"); | ||
73 | my_free(&argc_new, argv_test); | ||
74 | |||
75 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
76 | argv_test[0] = "prog_name"; | ||
77 | argv_test[1] = "arg1"; | ||
78 | argv_test[2] = "--arg2=val1"; | ||
79 | argv_test[3] = "--arg3"; | ||
80 | argv_test[4] = "val2"; | ||
81 | argv_test[5] = NULL; | ||
82 | argc_test=5; | ||
83 | argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); | ||
84 | argv_known[0] = "prog_name"; | ||
85 | argv_known[1] = "arg1"; | ||
86 | argv_known[2] = "--arg2=val1"; | ||
87 | argv_known[3] = "--arg3"; | ||
88 | argv_known[4] = "val2"; | ||
89 | argv_known[5] = NULL; | ||
90 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
91 | ok(array_diff(argc_new, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv"); | ||
92 | my_free(&argc_new,argv_test); | ||
93 | |||
94 | argv_test=(char **)malloc(3*sizeof(char **)); | ||
95 | argv_test[0] = "prog_name"; | ||
96 | argv_test[1] = "--extra-opts=@./config-opts.ini"; | ||
97 | argv_test[2] = NULL; | ||
98 | argc_test=2; | ||
99 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
100 | argv_known[0] = "prog_name"; | ||
101 | argv_known[1] = "--foo=Bar"; | ||
102 | argv_known[2] = "--this=Your Mother!"; | ||
103 | argv_known[3] = "--blank"; | ||
104 | argv_known[4] = NULL; | ||
105 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
106 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts using default section"); | ||
107 | my_free(&argc_new,argv_test); | ||
108 | |||
109 | argv_test=(char **)malloc(5*sizeof(char **)); | ||
110 | argv_test[0] = "prog_name"; | ||
111 | argv_test[1] = "--extra-opts=sect1@./config-opts.ini"; | ||
112 | argv_test[2] = "--extra-opts"; | ||
113 | argv_test[3] = "sect2@./config-opts.ini"; | ||
114 | argv_test[4] = NULL; | ||
115 | argc_test=4; | ||
116 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
117 | argv_known[0] = "prog_name"; | ||
118 | argv_known[1] = "--one=two"; | ||
119 | argv_known[2] = "--something else=oops"; | ||
120 | argv_known[3] = "--this=that"; | ||
121 | argv_known[4] = NULL; | ||
122 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
123 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Only extra opts specified twice"); | ||
124 | my_free(&argc_new,argv_test); | ||
125 | |||
126 | argv_test=(char **)malloc(7*sizeof(char **)); | ||
127 | argv_test[0] = "prog_name"; | ||
128 | argv_test[1] = "--arg1=val1"; | ||
129 | argv_test[2] = "--extra-opts=@./config-opts.ini"; | ||
130 | argv_test[3] = "--extra-opts"; | ||
131 | argv_test[4] = "sect1@./config-opts.ini"; | ||
132 | argv_test[5] = "--arg2"; | ||
133 | argv_test[6] = NULL; | ||
134 | argc_test=6; | ||
135 | argv_known=(char **)realloc(argv_known, 8*sizeof(char **)); | ||
136 | argv_known[0] = "prog_name"; | ||
137 | argv_known[1] = "--foo=Bar"; | ||
138 | argv_known[2] = "--this=Your Mother!"; | ||
139 | argv_known[3] = "--blank"; | ||
140 | argv_known[4] = "--one=two"; | ||
141 | argv_known[5] = "--arg1=val1"; | ||
142 | argv_known[6] = "--arg2"; | ||
143 | argv_known[7] = NULL; | ||
144 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
145 | ok(array_diff(argc_new, argv_test, 7, argv_known), "twice extra opts using two sections"); | ||
146 | my_free(&argc_new,argv_test); | ||
147 | |||
148 | /* Next three checks should die according to N::P - for now they're useful | ||
149 | * to test code is working properly (i.e. no srash or unexpected behavior) | ||
150 | */ | ||
151 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
152 | argv_test[0] = "prog_name"; | ||
153 | argv_test[1] = "arg1"; | ||
154 | argv_test[2] = "--extra-opts=missing@./config-opts.ini"; | ||
155 | argv_test[3] = "--arg3"; | ||
156 | argv_test[4] = "val2"; | ||
157 | argv_test[5] = NULL; | ||
158 | argc_test=5; | ||
159 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
160 | argv_known[0] = "prog_name"; | ||
161 | argv_known[1] = "arg1"; | ||
162 | argv_known[2] = "--arg3"; | ||
163 | argv_known[3] = "val2"; | ||
164 | argv_known[4] = NULL; | ||
165 | argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); | ||
166 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 1"); | ||
167 | my_free(&argc_new,argv_test); | ||
168 | |||
169 | argv_test=(char **)malloc(7*sizeof(char **)); | ||
170 | argv_test[0] = "prog_name"; | ||
171 | argv_test[1] = "arg1"; | ||
172 | argv_test[2] = "--extra-opts"; | ||
173 | argv_test[3] = "missing@./config-opts.ini"; | ||
174 | argv_test[4] = "--arg3"; | ||
175 | argv_test[5] = "val2"; | ||
176 | argv_test[6] = NULL; | ||
177 | argc_test=6; | ||
178 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
179 | argv_known[0] = "prog_name"; | ||
180 | argv_known[1] = "arg1"; | ||
181 | argv_known[2] = "--arg3"; | ||
182 | argv_known[3] = "val2"; | ||
183 | argv_known[4] = NULL; | ||
184 | argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); | ||
185 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 2"); | ||
186 | my_free(&argc_new,argv_test); | ||
187 | |||
188 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
189 | argv_test[0] = "prog_name"; | ||
190 | argv_test[1] = "arg1"; | ||
191 | argv_test[2] = "--extra-opts"; | ||
192 | argv_test[3] = "--arg3"; | ||
193 | argv_test[4] = "val2"; | ||
194 | argv_test[5] = NULL; | ||
195 | argc_test=5; | ||
196 | argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); | ||
197 | argv_known[0] = "prog_name"; | ||
198 | argv_known[1] = "arg1"; | ||
199 | argv_known[2] = "--arg3"; | ||
200 | argv_known[3] = "val2"; | ||
201 | argv_known[4] = NULL; | ||
202 | argv_test=np_extra_opts(argc_test, argv_test, "check_missing", &argc_new); | ||
203 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3"); | ||
204 | my_free(&argc_new,argv_test); | ||
205 | |||
206 | return exit_status(); | ||
207 | } | ||
208 | |||