2 * Copyright (c) 2013, Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
32 * Author: Stef Walter <stefw@collabora.co.uk>
39 #include "test-tools.h"
54 read_file (const char *file,
64 f = fopen (filename, "rb");
66 p11_test_fail (file, line, function, "Couldn't open file: %s", filename);
69 if (stat (filename, &sb) < 0)
70 p11_test_fail (file, line, function, "Couldn't stat file: %s", filename);
73 data = malloc (*len ? *len : 1);
74 assert (data != NULL);
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);
86 test_check_file_msg (const char *file,
89 const char *directory,
91 const char *reference)
96 refdata = read_file (file, line, function, reference, &reflen);
97 test_check_data_msg (file, line, function, directory, name, refdata, reflen);
102 test_check_data_msg (const char *file,
104 const char *function,
105 const char *directory,
114 if (asprintf (&filename, "%s/%s", directory, name) < 0)
115 assert_not_reached ();
117 filedata = read_file (file, line, function, filename, &filelen);
119 if (filelen != reflen || memcmp (filedata, refdata, reflen) != 0)
120 p11_test_fail (file, line, function, "File contents not as expected: %s", filename);
122 if (unlink (filename) < 0)
123 p11_test_fail (file, line, function, "Couldn't remove file: %s", filename);
131 test_check_symlink_msg (const char *file,
133 const char *function,
134 const char *directory,
136 const char *destination)
138 char buf[1024] = { 0, };
141 if (asprintf (&filename, "%s/%s", directory, name) < 0)
142 assert_not_reached ();
144 if (readlink (filename, buf, sizeof (buf)) < 0)
145 p11_test_fail (file, line, function, "Couldn't read symlink: %s", filename);
147 if (strcmp (destination, buf) != 0)
148 p11_test_fail (file, line, function, "Symlink contents wrong: %s != %s", destination, buf);
150 if (unlink (filename) < 0)
151 p11_test_fail (file, line, function, "Couldn't remove symlink: %s", filename);
158 test_check_directory_files (const char *file,
164 files = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL);
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 *);
180 test_check_directory_msg (const char *file,
182 const char *function,
183 const char *directory,
191 dir = opendir (directory);
193 p11_test_fail (file ,line, function, "Couldn't open directory: %s", directory);
195 while ((dp = readdir (dir)) != NULL) {
196 if (strcmp (dp->d_name, ".") == 0 ||
197 strcmp (dp->d_name, "..") == 0)
200 if (!p11_dict_remove (files, dp->d_name))
201 p11_test_fail (file, line, function, "Unexpected file in directory: %s", dp->d_name);
207 if (chmod (directory, S_IRWXU) < 0)
208 p11_test_fail (file, line, function, "couldn't chown directory: %s: %s", directory, strerror (errno));
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);
215 p11_dict_free (files);