| 1 |
/* |
| 2 |
* Copyright (c) 2023, Oracle and/or its affiliates. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#include <X11/xpm.h> |
| 27 |
#include <glib.h> |
| 28 |
|
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
#include "TestAllFiles.h" |
| 35 |
|
| 36 |
#ifndef O_CLOEXEC |
| 37 |
# define O_CLOEXEC 0 |
| 38 |
#endif |
| 39 |
|
| 40 |
#ifndef g_assert_no_errno /* defined in glib 2.66 & later */ |
| 41 |
#define g_assert_no_errno(n) g_assert_cmpint(n, >=, 0) |
| 42 |
#endif |
| 43 |
|
| 44 |
/* |
| 45 |
* XpmReadFileToXpmImage - Read XPM files without requiring an X Display |
| 46 |
* |
| 47 |
* Todo: actually check the returned image/info. |
| 48 |
*/ |
| 49 |
static int |
| 50 |
TestReadFileToXpmImage(const gchar *filepath) |
| 51 |
{ |
| 52 |
XpmImage image; |
| 53 |
XpmInfo info; |
| 54 |
int status; |
| 55 |
|
| 56 |
status = XpmReadFileToXpmImage(filepath, &image, &info); |
| 57 |
|
| 58 |
if (status == XpmSuccess) { |
| 59 |
XpmFreeXpmImage(&image); |
| 60 |
XpmFreeXpmInfo(&info); |
| 61 |
} |
| 62 |
|
| 63 |
return status; |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
test_XpmReadFileToXpmImage(void) |
| 68 |
{ |
| 69 |
int status; |
| 70 |
|
| 71 |
status = TestReadFileToXpmImage("no-such-file.xpm"); |
| 72 |
g_assert_cmpint(status, ==, XpmOpenFailed); |
| 73 |
|
| 74 |
TestAllNormalFiles("good", XpmSuccess, TestReadFileToXpmImage); |
| 75 |
TestAllNormalFiles("invalid", XpmFileInvalid, TestReadFileToXpmImage); |
| 76 |
TestAllNormalFiles("no-mem", XpmNoMemory, TestReadFileToXpmImage); |
| 77 |
/* XpmReadFileToXpmImage supports compressed files */ |
| 78 |
TestAllCompressedFiles("good", XpmSuccess, TestReadFileToXpmImage); |
| 79 |
TestAllCompressedFiles("invalid", XpmFileInvalid, TestReadFileToXpmImage); |
| 80 |
TestAllCompressedFiles("no-mem", XpmNoMemory, TestReadFileToXpmImage); |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
* XpmReadFileToData - wrapper around XpmReadFileToXpmImage that |
| 85 |
* converts the image into a list of strings. |
| 86 |
* |
| 87 |
* Todo: actually check the returned data. |
| 88 |
*/ |
| 89 |
static int |
| 90 |
TestReadFileToData(const gchar *filepath) |
| 91 |
{ |
| 92 |
char **data = NULL; |
| 93 |
int status; |
| 94 |
|
| 95 |
status = XpmReadFileToData(filepath, &data); |
| 96 |
|
| 97 |
if (status == XpmSuccess) { |
| 98 |
XpmImage image; |
| 99 |
XpmInfo info; |
| 100 |
|
| 101 |
g_assert_nonnull(data); |
| 102 |
|
| 103 |
status = XpmCreateXpmImageFromData(data, &image, &info); |
| 104 |
g_assert_cmpint(status, ==, XpmSuccess); |
| 105 |
|
| 106 |
XpmFreeXpmImage(&image); |
| 107 |
XpmFreeXpmInfo(&info); |
| 108 |
XpmFree(data); |
| 109 |
} |
| 110 |
|
| 111 |
return status; |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
test_XpmReadFileToData(void) |
| 116 |
{ |
| 117 |
int status; |
| 118 |
|
| 119 |
status = TestReadFileToData("no-such-file.xpm"); |
| 120 |
g_assert_cmpint(status, ==, XpmOpenFailed); |
| 121 |
|
| 122 |
TestAllNormalFiles("good", XpmSuccess, TestReadFileToData); |
| 123 |
TestAllNormalFiles("invalid", XpmFileInvalid, TestReadFileToData); |
| 124 |
TestAllNormalFiles("no-mem", XpmNoMemory, TestReadFileToData); |
| 125 |
/* XpmReadFileToData calls XpmReadFileToXpmImage so it |
| 126 |
supports compressed files */ |
| 127 |
TestAllCompressedFiles("good", XpmSuccess, TestReadFileToData); |
| 128 |
TestAllCompressedFiles("invalid", XpmFileInvalid, TestReadFileToData); |
| 129 |
TestAllCompressedFiles("no-mem", XpmNoMemory, TestReadFileToData); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/* |
| 134 |
* XpmReadFileToBuffer - helper function that just reads the file |
| 135 |
* into memory and doesn't try to parse it. |
| 136 |
*/ |
| 137 |
static int |
| 138 |
TestReadFileToBuffer(const gchar *filepath) |
| 139 |
{ |
| 140 |
char *buffer = NULL; |
| 141 |
int status; |
| 142 |
|
| 143 |
status = XpmReadFileToBuffer(filepath, &buffer); |
| 144 |
|
| 145 |
if (status == XpmSuccess) { |
| 146 |
char readbuf[8192]; |
| 147 |
char *b = buffer; |
| 148 |
int fd; |
| 149 |
ssize_t rd; |
| 150 |
|
| 151 |
g_assert_nonnull(buffer); |
| 152 |
|
| 153 |
/* Read file ourselves and verify the data matches */ |
| 154 |
g_assert_no_errno(fd = open(filepath, O_RDONLY | O_CLOEXEC)); |
| 155 |
while ((rd = read(fd, readbuf, sizeof(readbuf))) > 0) { |
| 156 |
g_assert_cmpmem(b, rd, readbuf, rd); |
| 157 |
b += rd; |
| 158 |
} |
| 159 |
/* Verify a nil terminator was added to the end */ |
| 160 |
g_assert_cmpint(b[0], ==, '\0'); |
| 161 |
g_assert_no_errno(close(fd)); |
| 162 |
|
| 163 |
XpmFree(buffer); |
| 164 |
} |
| 165 |
|
| 166 |
return status; |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
test_XpmReadFileToBuffer(void) |
| 171 |
{ |
| 172 |
int status; |
| 173 |
|
| 174 |
status = TestReadFileToBuffer("no-such-file.xpm"); |
| 175 |
g_assert_cmpint(status, ==, XpmOpenFailed); |
| 176 |
|
| 177 |
TestAllNormalFiles("good", XpmSuccess, TestReadFileToBuffer); |
| 178 |
/* Since this test just reads the file from disk without parsing, |
| 179 |
we expect Success even from files with bad fields in. */ |
| 180 |
TestAllNormalFiles("invalid", XpmSuccess, TestReadFileToBuffer); |
| 181 |
TestAllNormalFiles("no-mem", XpmSuccess, TestReadFileToBuffer); |
| 182 |
/* XpmReadFileToBuffer does not support compressed files */ |
| 183 |
} |
| 184 |
|
| 185 |
int |
| 186 |
main(int argc, char** argv) |
| 187 |
{ |
| 188 |
g_test_init(&argc, &argv, NULL); |
| 189 |
g_test_bug_base(PACKAGE_BUGREPORT); |
| 190 |
|
| 191 |
g_test_add_func("/XpmRead/XpmReadFileToXpmImage", |
| 192 |
test_XpmReadFileToXpmImage); |
| 193 |
g_test_add_func("/XpmRead/XpmReadFileToData", |
| 194 |
test_XpmReadFileToData); |
| 195 |
g_test_add_func("/XpmRead/XpmReadFileToBuffer", |
| 196 |
test_XpmReadFileToBuffer); |
| 197 |
|
| 198 |
return g_test_run(); |
| 199 |
} |