Imported Upstream version 0.19.1
[platform/upstream/p11-kit.git] / tools / tests / test-tools.c
1 /*
2  * Copyright (c) 2013, Red Hat Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *     * Redistributions of source code must retain the above
9  *       copyright notice, this list of conditions and the
10  *       following disclaimer.
11  *     * Redistributions in binary form must reproduce the
12  *       above copyright notice, this list of conditions and
13  *       the following disclaimer in the documentation and/or
14  *       other materials provided with the distribution.
15  *     * The names of contributors to this software may not be
16  *       used to endorse or promote products derived from this
17  *       software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  * DAMAGE.
31  *
32  * Author: Stef Walter <stefw@collabora.co.uk>
33  */
34
35 #include "config.h"
36 #include "test.h"
37
38 #include "debug.h"
39 #include "test-tools.h"
40
41 #include <sys/stat.h>
42
43 #include <assert.h>
44 #include <dirent.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 static char *
54 read_file (const char *file,
55            int line,
56            const char *function,
57            const char *filename,
58            long *len)
59 {
60         struct stat sb;
61         FILE *f = NULL;
62         char *data;
63
64         f = fopen (filename, "rb");
65         if (f == NULL)
66                 p11_test_fail (file, line, function, "Couldn't open file: %s", filename);
67
68         /* Figure out size */
69         if (stat (filename, &sb) < 0)
70                 p11_test_fail (file, line, function, "Couldn't stat file: %s", filename);
71
72         *len = sb.st_size;
73         data = malloc (*len ? *len : 1);
74         assert (data != NULL);
75
76         /* And read in one block */
77         if (fread (data, 1, *len, f) != *len)
78                 p11_test_fail (file, line, function, "Couldn't read file: %s", filename);
79
80         fclose (f);
81
82         return data;
83 }
84
85 void
86 test_check_file_msg (const char *file,
87                      int line,
88                      const char *function,
89                      const char *directory,
90                      const char *name,
91                      const char *reference)
92 {
93         char *refdata;
94         long reflen;
95
96         refdata = read_file (file, line, function, reference, &reflen);
97         test_check_data_msg (file, line, function, directory, name, refdata, reflen);
98         free (refdata);
99 }
100
101 void
102 test_check_data_msg (const char *file,
103                      int line,
104                      const char *function,
105                      const char *directory,
106                      const char *name,
107                      const void *refdata,
108                      long reflen)
109 {
110         char *filedata;
111         char *filename;
112         long filelen;
113
114         if (asprintf (&filename, "%s/%s", directory, name) < 0)
115                 assert_not_reached ();
116
117         filedata = read_file (file, line, function, filename, &filelen);
118
119         if (filelen != reflen || memcmp (filedata, refdata, reflen) != 0)
120                 p11_test_fail (file, line, function, "File contents not as expected: %s", filename);
121
122         if (unlink (filename) < 0)
123                 p11_test_fail (file, line, function, "Couldn't remove file: %s", filename);
124         free (filename);
125         free (filedata);
126 }
127
128 #ifdef OS_UNIX
129
130 void
131 test_check_symlink_msg (const char *file,
132                         int line,
133                         const char *function,
134                         const char *directory,
135                         const char *name,
136                         const char *destination)
137 {
138         char buf[1024] = { 0, };
139         char *filename;
140
141         if (asprintf (&filename, "%s/%s", directory, name) < 0)
142                 assert_not_reached ();
143
144         if (readlink (filename, buf, sizeof (buf)) < 0)
145                 p11_test_fail (file, line, function, "Couldn't read symlink: %s", filename);
146
147         if (strcmp (destination, buf) != 0)
148                 p11_test_fail (file, line, function, "Symlink contents wrong: %s != %s", destination, buf);
149
150         if (unlink (filename) < 0)
151                 p11_test_fail (file, line, function, "Couldn't remove symlink: %s", filename);
152         free (filename);
153 }
154
155 #endif /* OS_UNIX */
156
157 p11_dict *
158 test_check_directory_files (const char *file,
159                             ...)
160 {
161         p11_dict *files;
162         va_list va;
163
164         files = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL);
165
166         va_start (va, file);
167
168         while (file != NULL) {
169                 if (!p11_dict_set (files, (void *)file, (void *)file))
170                         return_val_if_reached (NULL);
171                 file = va_arg (va, const char *);
172         }
173
174         va_end (va);
175
176         return files;
177 }
178
179 void
180 test_check_directory_msg (const char *file,
181                           int line,
182                           const char *function,
183                           const char *directory,
184                           p11_dict *files)
185 {
186         p11_dictiter iter;
187         struct dirent *dp;
188         const char *name;
189         DIR *dir;
190
191         dir = opendir (directory);
192         if (dir == NULL)
193                 p11_test_fail (file ,line, function, "Couldn't open directory: %s", directory);
194
195         while ((dp = readdir (dir)) != NULL) {
196                 if (strcmp (dp->d_name, ".") == 0 ||
197                     strcmp (dp->d_name, "..") == 0)
198                         continue;
199
200                 if (!p11_dict_remove (files, dp->d_name))
201                         p11_test_fail  (file, line, function, "Unexpected file in directory: %s", dp->d_name);
202         }
203
204         closedir (dir);
205
206 #ifdef OS_UNIX
207         if (chmod (directory, S_IRWXU) < 0)
208                 p11_test_fail (file, line, function, "couldn't chown directory: %s: %s", directory, strerror (errno));
209 #endif
210
211         p11_dict_iterate (files, &iter);
212         while (p11_dict_next (&iter, (void **)&name, NULL))
213                 p11_test_fail (file, line, function, "Couldn't find file in directory: %s", name);
214
215         p11_dict_free (files);
216 }