aboutsummaryrefslogtreecommitdiff
path: root/lib/tests/test_opts1.c
blob: b73e1a1f62d6f1555bf08563545fd77eaeb8139a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*****************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*****************************************************************************/

#include "common.h"
#include "utils_base.h"
#include "extra_opts.h"

#include "tap.h"

void my_free(int *argc, char **argv) {
	int i;
	printf ("    Arg(%i): ", *argc);
	for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]);
	printf ("\n");
	free(argv);
	*argc=0;
}

int array_diff(int i1, char **a1, int i2, char **a2) {
	int i;

	if (i1 != i2) {
		printf("    Argument count doesn't match!\n");
		return 0;
	}
	for (i=0; i<=i1; i++) {
		if (a1[i]==NULL && a2[i]==NULL) continue;
		if (a1[i]==NULL || a2[i]==NULL) {
			printf("    Argument # %i null in one array!\n", i);
			return 0;
		}
		if (strcmp(a1[i], a2[i])) {
			printf("    Argument # %i doesn't match!\n", i);
			return 0;
		}
	}
	return 1;
}

int
main (int argc, char **argv)
{
	char **argv_test=NULL, **argv_known=NULL;
	int i, argc_test;

	plan_tests(5);

	argv_test=(char **)malloc(2*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = NULL;
	argc_test=1;
	argv_known=(char **)realloc(argv_known, 2*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
	ok(array_diff(argc_test, argv_test, 1, argv_known), "No opts, returns correct argv/argc");
	my_free(&argc_test, argv_test);

	argv_test=(char **)malloc(6*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = "arg1";
	argv_test[2] = "--arg2=val1";
	argv_test[3] = "--arg3";
	argv_test[4] = "val2";
	argv_test[5] = NULL;
	argc_test=5;
	argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = "arg1";
	argv_known[2] = "--arg2=val1";
	argv_known[3] = "--arg3";
	argv_known[4] = "val2";
	argv_known[5] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
	ok(array_diff(argc_test, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv");
	my_free(&argc_test,argv_test);

	argv_test=(char **)malloc(3*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = "--extra-opts=@./config-opts.ini";
	argv_test[2] = NULL;
	argc_test=2;
	argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = "--foo=Bar";
	argv_known[2] = "--this=Your Mother!";
	argv_known[3] = "--blank";
	argv_known[4] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
	ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts using default section");
	my_free(&argc_test,argv_test);

	argv_test=(char **)malloc(5*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = "--extra-opts=sect1@./config-opts.ini";
	argv_test[2] = "--extra-opts";
	argv_test[3] = "sect2@./config-opts.ini";
	argv_test[4] = NULL;
	argc_test=4;
	argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = "--one=two";
	argv_known[2] = "--something else=oops";
	argv_known[3] = "--this=that";
	argv_known[4] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
	ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts specified twice");
	my_free(&argc_test,argv_test);

	argv_test=(char **)malloc(7*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = "--arg1=val1";
	argv_test[2] = "--extra-opts=@./config-opts.ini";
	argv_test[3] = "--extra-opts";
	argv_test[4] = "sect1@./config-opts.ini";
	argv_test[5] = "--arg2";
	argv_test[6] = NULL;
	argc_test=6;
	argv_known=(char **)realloc(argv_known, 8*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = "--foo=Bar";
	argv_known[2] = "--this=Your Mother!";
	argv_known[3] = "--blank";
	argv_known[4] = "--one=two";
	argv_known[5] = "--arg1=val1";
	argv_known[6] = "--arg2";
	argv_known[7] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
	ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
	my_free(&argc_test,argv_test);

	/* Next three checks are expected to die. They are commented out as they
	 * could possibly go in a sepatare test checked for return value.
	 */
	/* argv_test=(char **)malloc(6*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = "arg1";
	argv_test[2] = "--extra-opts=missing@./config-opts.ini";
	argv_test[3] = "--arg3";
	argv_test[4] = "val2";
	argv_test[5] = NULL;
	argc_test=5;
	argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = "arg1";
	argv_known[2] = "--arg3";
	argv_known[3] = "val2";
	argv_known[4] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
	ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 1");
	my_free(&argc_test,argv_test); */

	/* argv_test=(char **)malloc(7*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = "arg1";
	argv_test[2] = "--extra-opts";
	argv_test[3] = "missing@./config-opts.ini";
	argv_test[4] = "--arg3";
	argv_test[5] = "val2";
	argv_test[6] = NULL;
	argc_test=6;
	argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = "arg1";
	argv_known[2] = "--arg3";
	argv_known[3] = "val2";
	argv_known[4] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
	ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 2");
	my_free(&argc_test,argv_test); */

	/* argv_test=(char **)malloc(6*sizeof(char **));
	argv_test[0] = "prog_name";
	argv_test[1] = "arg1";
	argv_test[2] = "--extra-opts";
	argv_test[3] = "--arg3";
	argv_test[4] = "val2";
	argv_test[5] = NULL;
	argc_test=5;
	argv_known=(char **)realloc(argv_known, 5*sizeof(char **));
	argv_known[0] = "prog_name";
	argv_known[1] = "arg1";
	argv_known[2] = "--arg3";
	argv_known[3] = "val2";
	argv_known[4] = NULL;
	argv_test=np_extra_opts(&argc_test, argv_test, "check_missing");
	ok(array_diff(argc_test, argv_test, 4, argv_known), "Missing section 3");
	my_free(&argc_test,argv_test); */

	return exit_status();
}