ab4255bb39690dc5a227a05fb5482740091f3189
[platform/upstream/libexif.git] / test / test-null.c
1 /** \file test-null.c
2  * \brief Pass NULL values into libexif APIs and ensure it doesn't crash.
3  *
4  * Copyright (C) 2019 Dan Fandrich <dan@coneharvesters.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA  02110-1301  USA.
20  *
21  */
22
23 #include "libexif/exif-data.h"
24 #include "libexif/exif-entry.h"
25 #include "libexif/exif-loader.h"
26 #include "libexif/exif-mnote-data.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 static void loader_null_test(void)
32 {
33         ExifLoader *l;
34         ExifData *d;
35         unsigned char ret;
36
37         l = exif_loader_new_mem(NULL);
38         if (l) {
39                 fprintf(stderr, "Unexpected success in %s\n", "exif_loader_new_mem");
40                 exit(13);
41         }
42
43         exif_loader_ref(NULL);
44
45         exif_loader_unref(NULL);
46
47         exif_loader_write_file(NULL, "test");
48
49         exif_loader_write_file(NULL, NULL);
50
51         ret = exif_loader_write(NULL, (unsigned char *)"x", 1);
52         if (ret) {
53                 fprintf(stderr, "Unexpected success in %s\n", "exif_loader_write");
54                 exit(13);
55         }
56
57         exif_loader_write(NULL, NULL, 123);
58
59         exif_loader_reset(NULL);
60
61         d = exif_loader_get_data(NULL);
62         if (d) {
63                 fprintf(stderr, "Unexpected success in %s\n", "exif_loader_get_data");
64                 exit(13);
65         }
66
67         exif_loader_get_buf(NULL, NULL, NULL);
68
69         exif_loader_log(NULL, NULL);
70 }
71
72 static void data_null_test(void)
73 {
74         /* exif_data_new_from_file() is untested since it doesn't check path */
75
76         ExifData *d;
77         ExifByteOrder bo;
78         ExifMnoteData *m;
79         ExifDataType dt;
80         unsigned char *buf;
81         unsigned int len;
82
83         d = exif_data_new_mem(NULL);
84         if (d) {
85                 fprintf(stderr, "Unexpected success in %s\n", "exif_data_new_mem");
86                 exit(13);
87         }
88
89         d = exif_data_new_from_data(NULL, 123);
90         if (d) {
91                 exif_data_unref(d);
92         }
93
94         bo = exif_data_get_byte_order(NULL);
95         (void) bo;
96
97         exif_data_set_byte_order(NULL, EXIF_BYTE_ORDER_MOTOROLA);
98
99         m = exif_data_get_mnote_data(NULL);
100         if (m) {
101                 fprintf(stderr, "Unexpected success in %s\n", "exif_data_get_mnote_data");
102                 exit(13);
103         }
104
105         exif_data_fix(NULL);
106
107         exif_data_foreach_content(NULL, NULL, NULL);
108
109         exif_data_set_option(NULL, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
110
111         exif_data_unset_option(NULL, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
112
113         exif_data_set_data_type(NULL, EXIF_DATA_TYPE_UNCOMPRESSED_CHUNKY);
114
115         dt = exif_data_get_data_type(NULL);
116         (void) dt;
117
118         exif_data_dump(NULL);
119
120         exif_data_ref(NULL);
121
122         exif_data_unref(NULL);
123
124         d = exif_data_new();
125         if (!d) {
126                 fprintf(stderr, "Out of memory\n");
127                 exit(13);
128         }
129
130         exif_data_load_data(NULL, NULL, 123);
131         exif_data_load_data(d, NULL, 123);
132
133         exif_data_save_data(NULL, NULL, NULL);
134         exif_data_save_data(d, NULL, &len);
135         exif_data_save_data(d, &buf, NULL);
136         exif_data_save_data(d, NULL, NULL);
137
138         exif_data_log(NULL, NULL);
139         exif_data_log(d, NULL);
140
141         exif_data_unref(d);
142 }
143
144 static void content_null_test(void)
145 {
146         ExifContent *c;
147         ExifIfd i;
148
149         c = exif_content_new_mem(NULL);
150         if (c) {
151                 fprintf(stderr, "Unexpected success in %s\n", "exif_content_new_mem");
152                 exit(13);
153         }
154
155         exif_content_ref(NULL);
156
157         exif_content_unref(NULL);
158
159         exif_content_free(NULL);
160
161         exif_content_get_entry(NULL, EXIF_TAG_COMPRESSION);
162
163         exif_content_fix(NULL);
164
165         exif_content_foreach_entry(NULL, NULL, NULL);
166
167         i = exif_content_get_ifd(NULL);
168         (void) i;
169
170         exif_content_dump(NULL, 0);
171
172         c = exif_content_new();
173         if (!c) {
174                 fprintf(stderr, "Out of memory\n");
175                 exit(13);
176         }
177
178         exif_content_add_entry(c, NULL);
179
180         exif_content_add_entry(NULL, NULL);
181
182         exif_content_remove_entry(c, NULL);
183         exif_content_remove_entry(NULL, NULL);
184
185         exif_content_log(c, NULL);
186
187         exif_content_log(NULL, NULL);
188
189         exif_content_unref(c);
190 }
191
192 static void entry_null_test(void)
193 {
194         ExifEntry *e;
195         const char *v = NULL;
196         char buf[] = {0};
197
198         e = exif_entry_new_mem(NULL);
199         if (e) {
200                 fprintf(stderr, "Unexpected success in %s\n", "exif_entry_new_mem");
201                 exit(13);
202         }
203
204         exif_entry_ref(NULL);
205
206         exif_entry_unref(NULL);
207
208         exif_entry_free(NULL);
209
210         exif_entry_initialize(NULL, EXIF_TAG_COMPRESSION);
211
212         exif_entry_fix(NULL);
213
214         v = exif_entry_get_value(NULL, NULL, 123);
215         if (v) {
216                 fprintf(stderr, "Unexpected success in %s\n", "exif_entry_get_value");
217                 exit(13);
218         }
219
220         v = exif_entry_get_value(NULL, buf, 1);
221         if (v != buf) {
222                 fprintf(stderr, "Unexpected value in %s\n", "exif_entry_get_value");
223                 exit(13);
224         }
225
226         exif_entry_dump(NULL, 0);
227 }
228
229 static void mnote_null_test(void)
230 {
231         /* Note that these APIs aren't tested with a real ExifMnoteData pointer
232          * because it's impossible to create one without real data.
233          */
234         exif_mnote_data_ref(NULL);
235
236         exif_mnote_data_unref(NULL);
237
238         exif_mnote_data_load(NULL, NULL, 123);
239
240         exif_mnote_data_load(NULL, (const unsigned char *)"x", 1);
241
242         exif_mnote_data_save(NULL, NULL, NULL);
243
244         exif_mnote_data_count(NULL);
245
246         exif_mnote_data_get_id(NULL, 0);
247
248         exif_mnote_data_get_name(NULL, 0);
249
250         exif_mnote_data_get_title(NULL, 0);
251
252         exif_mnote_data_get_description(NULL, 0);
253
254         exif_mnote_data_get_value(NULL, 0, NULL, 123);
255
256         exif_mnote_data_log(NULL, NULL);
257 }
258
259 static void log_null_test(void)
260 {
261         ExifLog *l;
262         static va_list va;
263
264         l = exif_log_new_mem(NULL);
265         if (l) {
266                 fprintf(stderr, "Unexpected success in %s\n", "exif_log_new_mem");
267                 exit(13);
268         }
269
270         exif_log_ref(NULL);
271
272         exif_log_unref(NULL);
273
274         exif_log_free(NULL);
275
276         exif_log_set_func(NULL, NULL, NULL);
277
278         exif_log(NULL, EXIF_LOG_CODE_CORRUPT_DATA, "XXX", "YYY");
279
280         exif_logv(NULL, EXIF_LOG_CODE_CORRUPT_DATA, "XXX", "YYY", va);
281 }
282
283 int main(void)
284 {
285         loader_null_test();
286         data_null_test();
287         content_null_test();
288         entry_null_test();
289         mnote_null_test();
290         log_null_test();
291
292         /* If it gets here, we didn't get a SIGSEGV, so success! */
293         return 0;
294 }