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 "TestAllFiles.h"
30
31
32/*
33 * XpmCreateXpmImageFromData - parse an XPM from data strings
34 *
35 * Todo:
36 *  - actually check the returned info/image
37 *  - check with data other than read from XPM files
38 */
39static int
40TestCreateXpmImageFromData(const gchar *filepath)
41{
42    char **data = NULL;
43    int status;
44
45    status = XpmReadFileToData(filepath, &data);
46
47    if (status == XpmSuccess) {
48        XpmImage image;
49        XpmInfo info;
50
51        g_assert_nonnull(data);
52
53        status = XpmCreateXpmImageFromData(data, &image, &info);
54        g_assert_cmpint(status, ==, XpmSuccess);
55
56        XpmFreeXpmImage(&image);
57        XpmFreeXpmInfo(&info);
58        XpmFree(data);
59    }
60
61    return status;
62}
63
64static void
65test_XpmCreateXpmImageFromData(void)
66{
67    TestAllNormalFiles("good", XpmSuccess, TestCreateXpmImageFromData);
68    TestAllNormalFiles("invalid", XpmFileInvalid, TestCreateXpmImageFromData);
69    TestAllNormalFiles("no-mem", XpmNoMemory, TestCreateXpmImageFromData);
70    /* XpmReadFileToData calls XpmReadFileToXpmImage so it
71       supports compressed files */
72    TestAllCompressedFiles("good", XpmSuccess, TestCreateXpmImageFromData);
73    TestAllCompressedFiles("invalid", XpmFileInvalid, TestCreateXpmImageFromData);
74    TestAllCompressedFiles("no-mem", XpmNoMemory, TestCreateXpmImageFromData);
75}
76
77
78/*
79 * XpmCreateXpmImageFromBuffer - parse an XPM from data strings
80 *
81 * Todo:
82 *  - actually check the returned info/image
83 *  - check with data other than read from XPM files
84 */
85static int
86TestCreateXpmImageFromBuffer(const gchar *filepath)
87{
88    char *buffer = NULL;
89    XpmImage image;
90    XpmInfo info;
91    int status;
92
93    status = XpmReadFileToBuffer(filepath, &buffer);
94    g_assert_cmpint(status, ==, XpmSuccess);
95
96    status = XpmCreateXpmImageFromBuffer(buffer, &image, &info);
97
98    if (status == XpmSuccess) {
99        XpmFreeXpmImage(&image);
100        XpmFreeXpmInfo(&info);
101    }
102
103    XpmFree(buffer);
104
105    return status;
106}
107
108static void
109test_XpmCreateXpmImageFromBuffer(void)
110{
111    TestAllNormalFiles("good", XpmSuccess, TestCreateXpmImageFromBuffer);
112    TestAllNormalFiles("invalid", XpmFileInvalid, TestCreateXpmImageFromBuffer);
113    TestAllNormalFiles("no-mem", XpmNoMemory, TestCreateXpmImageFromBuffer);
114    /* XpmReadFileToBuffer does not support compressed files */
115}
116
117int
118main(int argc, char** argv)
119{
120    g_test_init(&argc, &argv, NULL);
121    g_test_bug_base(PACKAGE_BUGREPORT);
122
123    g_test_add_func("/XpmCreate/XpmCreateXpmImageFromData",
124                    test_XpmCreateXpmImageFromData);
125    g_test_add_func("/XpmCreate/XpmCreateXpmImageFromBuffer",
126                    test_XpmCreateXpmImageFromBuffer);
127
128    return g_test_run();
129}
130