1 /*
2  * Tests for the lib_xlsx_writer library.
3  *
4  * Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
5  *
6  */
7 
8 #include "../ctest.h"
9 #include "../helper.h"
10 
11 #include "../../../include/xlsxwriter/worksheet.h"
12 
13 
14 /* 1. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection01)15 CTEST(worksheet, write_write_sheet_protection01) {
16     char* got;
17     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\"/>";
18     FILE* testfile = lxw_tmpfile(NULL);
19 
20     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
21     worksheet->file = testfile;
22 
23     worksheet_protect(worksheet, NULL, NULL);
24     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
25 
26     RUN_XLSX_STREQ(exp, got);
27 
28     lxw_worksheet_free(worksheet);
29 }
30 
31 
32 /* 2. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection02)33 CTEST(worksheet, write_write_sheet_protection02) {
34     char* got;
35     char exp[] = "<sheetProtection password=\"83AF\" sheet=\"1\" objects=\"1\" scenarios=\"1\"/>";
36     FILE* testfile = lxw_tmpfile(NULL);
37 
38     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
39     worksheet->file = testfile;
40 
41     worksheet_protect(worksheet, "password", NULL);
42     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
43 
44     RUN_XLSX_STREQ(exp, got);
45 
46     lxw_worksheet_free(worksheet);
47 }
48 
49 /* 3. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection03)50 CTEST(worksheet, write_write_sheet_protection03) {
51     char* got;
52     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" selectLockedCells=\"1\"/>";
53     FILE* testfile = lxw_tmpfile(NULL);
54 
55     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
56     worksheet->file = testfile;
57 
58     lxw_protection options = {.no_select_locked_cells = 1};
59 
60     worksheet_protect(worksheet, NULL, &options);
61     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
62 
63     RUN_XLSX_STREQ(exp, got);
64 
65     lxw_worksheet_free(worksheet);
66 }
67 
68 
69 /* 4. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection04)70 CTEST(worksheet, write_write_sheet_protection04) {
71     char* got;
72     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" formatCells=\"0\"/>";
73     FILE* testfile = lxw_tmpfile(NULL);
74 
75     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
76     worksheet->file = testfile;
77 
78     lxw_protection options = {.format_cells = 1};
79 
80     worksheet_protect(worksheet, NULL, &options);
81     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
82 
83     RUN_XLSX_STREQ(exp, got);
84 
85     lxw_worksheet_free(worksheet);
86 }
87 
88 
89 /* 5. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection05)90 CTEST(worksheet, write_write_sheet_protection05) {
91     char* got;
92     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" formatColumns=\"0\"/>";
93     FILE* testfile = lxw_tmpfile(NULL);
94 
95     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
96     worksheet->file = testfile;
97 
98     lxw_protection options = {.format_columns = 1};
99 
100     worksheet_protect(worksheet, NULL, &options);
101     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
102 
103     RUN_XLSX_STREQ(exp, got);
104 
105     lxw_worksheet_free(worksheet);
106 }
107 
108 
109 /* 6. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection06)110 CTEST(worksheet, write_write_sheet_protection06) {
111     char* got;
112     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" formatRows=\"0\"/>";
113     FILE* testfile = lxw_tmpfile(NULL);
114 
115     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
116     worksheet->file = testfile;
117 
118     lxw_protection options = {.format_rows = 1};
119 
120     worksheet_protect(worksheet, NULL, &options);
121     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
122 
123     RUN_XLSX_STREQ(exp, got);
124 
125     lxw_worksheet_free(worksheet);
126 }
127 
128 
129 /* 7. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection07)130 CTEST(worksheet, write_write_sheet_protection07) {
131     char* got;
132     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" insertColumns=\"0\"/>";
133     FILE* testfile = lxw_tmpfile(NULL);
134 
135     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
136     worksheet->file = testfile;
137 
138     lxw_protection options = {.insert_columns = 1};
139 
140     worksheet_protect(worksheet, NULL, &options);
141     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
142 
143     RUN_XLSX_STREQ(exp, got);
144 
145     lxw_worksheet_free(worksheet);
146 }
147 
148 
149 /* 8. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection08)150 CTEST(worksheet, write_write_sheet_protection08) {
151     char* got;
152     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" insertRows=\"0\"/>";
153     FILE* testfile = lxw_tmpfile(NULL);
154 
155     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
156     worksheet->file = testfile;
157 
158     lxw_protection options = {.insert_rows = 1};
159 
160     worksheet_protect(worksheet, NULL, &options);
161     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
162 
163     RUN_XLSX_STREQ(exp, got);
164 
165     lxw_worksheet_free(worksheet);
166 }
167 
168 
169 /* 9. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection09)170 CTEST(worksheet, write_write_sheet_protection09) {
171     char* got;
172     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" insertHyperlinks=\"0\"/>";
173     FILE* testfile = lxw_tmpfile(NULL);
174 
175     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
176     worksheet->file = testfile;
177 
178     lxw_protection options = {.insert_hyperlinks = 1};
179 
180     worksheet_protect(worksheet, NULL, &options);
181     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
182 
183     RUN_XLSX_STREQ(exp, got);
184 
185     lxw_worksheet_free(worksheet);
186 }
187 
188 
189 /* 10. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection10)190 CTEST(worksheet, write_write_sheet_protection10) {
191     char* got;
192     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" deleteColumns=\"0\"/>";
193     FILE* testfile = lxw_tmpfile(NULL);
194 
195     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
196     worksheet->file = testfile;
197 
198     lxw_protection options = {.delete_columns = 1};
199 
200     worksheet_protect(worksheet, NULL, &options);
201     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
202 
203     RUN_XLSX_STREQ(exp, got);
204 
205     lxw_worksheet_free(worksheet);
206 }
207 
208 
209 /* 11. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection11)210 CTEST(worksheet, write_write_sheet_protection11) {
211     char* got;
212     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" deleteRows=\"0\"/>";
213     FILE* testfile = lxw_tmpfile(NULL);
214 
215     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
216     worksheet->file = testfile;
217 
218     lxw_protection options = {.delete_rows = 1};
219 
220     worksheet_protect(worksheet, NULL, &options);
221     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
222 
223     RUN_XLSX_STREQ(exp, got);
224 
225     lxw_worksheet_free(worksheet);
226 }
227 
228 
229 /* 12. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection12)230 CTEST(worksheet, write_write_sheet_protection12) {
231     char* got;
232     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" sort=\"0\"/>";
233     FILE* testfile = lxw_tmpfile(NULL);
234 
235     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
236     worksheet->file = testfile;
237 
238     lxw_protection options = {.sort = 1};
239 
240     worksheet_protect(worksheet, NULL, &options);
241     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
242 
243     RUN_XLSX_STREQ(exp, got);
244 
245     lxw_worksheet_free(worksheet);
246 }
247 
248 
249 /* 13. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection13)250 CTEST(worksheet, write_write_sheet_protection13) {
251     char* got;
252     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" autoFilter=\"0\"/>";
253     FILE* testfile = lxw_tmpfile(NULL);
254 
255     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
256     worksheet->file = testfile;
257 
258     lxw_protection options = {.autofilter = 1};
259 
260     worksheet_protect(worksheet, NULL, &options);
261     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
262 
263     RUN_XLSX_STREQ(exp, got);
264 
265     lxw_worksheet_free(worksheet);
266 }
267 
268 
269 /* 14. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection14)270 CTEST(worksheet, write_write_sheet_protection14) {
271     char* got;
272     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" pivotTables=\"0\"/>";
273     FILE* testfile = lxw_tmpfile(NULL);
274 
275     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
276     worksheet->file = testfile;
277 
278     lxw_protection options = {.pivot_tables = 1};
279 
280     worksheet_protect(worksheet, NULL, &options);
281     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
282 
283     RUN_XLSX_STREQ(exp, got);
284 
285     lxw_worksheet_free(worksheet);
286 }
287 
288 
289 /* 15. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection15)290 CTEST(worksheet, write_write_sheet_protection15) {
291     char* got;
292     char exp[] = "<sheetProtection sheet=\"1\" scenarios=\"1\"/>";
293     FILE* testfile = lxw_tmpfile(NULL);
294 
295     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
296     worksheet->file = testfile;
297 
298     lxw_protection options = {.objects = 1};
299 
300     worksheet_protect(worksheet, NULL, &options);
301     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
302 
303     RUN_XLSX_STREQ(exp, got);
304 
305     lxw_worksheet_free(worksheet);
306 }
307 
308 
309 /* 16. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection16)310 CTEST(worksheet, write_write_sheet_protection16) {
311     char* got;
312     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\"/>";
313     FILE* testfile = lxw_tmpfile(NULL);
314 
315     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
316     worksheet->file = testfile;
317 
318     lxw_protection options = {.scenarios = 1};
319 
320     worksheet_protect(worksheet, NULL, &options);
321     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
322 
323     RUN_XLSX_STREQ(exp, got);
324 
325     lxw_worksheet_free(worksheet);
326 }
327 
328 
329 /* 17. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection17)330 CTEST(worksheet, write_write_sheet_protection17) {
331     char* got;
332     char exp[] = "<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\" formatCells=\"0\" selectLockedCells=\"1\" selectUnlockedCells=\"1\"/>";
333     FILE* testfile = lxw_tmpfile(NULL);
334 
335     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
336     worksheet->file = testfile;
337 
338     lxw_protection options = {
339         .format_cells             = 1,
340         .no_select_locked_cells   = 1,
341         .no_select_unlocked_cells = 1,
342     };
343 
344     worksheet_protect(worksheet, NULL, &options);
345     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
346 
347     RUN_XLSX_STREQ(exp, got);
348 
349     lxw_worksheet_free(worksheet);
350 }
351 
352 
353 /* 18. Test the _write_sheet_protection() method. */
CTEST(worksheet,write_write_sheet_protection18)354 CTEST(worksheet, write_write_sheet_protection18) {
355     char* got;
356     char exp[] = "<sheetProtection password=\"996B\" sheet=\"1\" formatCells=\"0\" formatColumns=\"0\" formatRows=\"0\" insertColumns=\"0\" insertRows=\"0\" insertHyperlinks=\"0\" deleteColumns=\"0\" deleteRows=\"0\" selectLockedCells=\"1\" sort=\"0\" autoFilter=\"0\" pivotTables=\"0\" selectUnlockedCells=\"1\"/>";
357     FILE* testfile = lxw_tmpfile(NULL);
358 
359     lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
360     worksheet->file = testfile;
361 
362     lxw_protection options = {
363         .objects                  = 1,
364         .scenarios                = 1,
365         .format_cells             = 1,
366         .format_columns           = 1,
367         .format_rows              = 1,
368         .insert_columns           = 1,
369         .insert_rows              = 1,
370         .insert_hyperlinks        = 1,
371         .delete_columns           = 1,
372         .delete_rows              = 1,
373         .no_select_locked_cells   = 1,
374         .sort                     = 1,
375         .autofilter               = 1,
376         .pivot_tables             = 1,
377         .no_select_unlocked_cells = 1,
378     };
379 
380     worksheet_protect(worksheet, "drowssap", &options);
381     _worksheet_write_sheet_protection(worksheet, &worksheet->protection);
382 
383     RUN_XLSX_STREQ(exp, got);
384 
385     lxw_worksheet_free(worksheet);
386 }
387