2019-05-09 01:04:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Benjamin Otte
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../../gtk/css/gtkcssdataurlprivate.h"
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
|
|
typedef struct _Test Test;
|
|
|
|
|
|
|
|
struct _Test
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
const char *url;
|
|
|
|
const char *mimetype;
|
|
|
|
const char *contents;
|
|
|
|
gsize contents_len;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CONTENTS(data) (data), sizeof(data) - 1
|
|
|
|
Test tests[] = {
|
|
|
|
{ "simple",
|
|
|
|
"data:,HelloWorld",
|
|
|
|
NULL, CONTENTS("HelloWorld") },
|
|
|
|
{ "nodata",
|
|
|
|
"data:,",
|
|
|
|
NULL, CONTENTS("") },
|
|
|
|
{ "case_sensitive",
|
|
|
|
"dATa:,HelloWorld",
|
|
|
|
NULL, CONTENTS("HelloWorld") },
|
|
|
|
{ "semicolon_after_comma",
|
|
|
|
"data:,;base64",
|
|
|
|
NULL, CONTENTS(";base64") },
|
|
|
|
{ "mimetype",
|
|
|
|
"data:image/png,nopng",
|
|
|
|
"image/png", CONTENTS("nopng") },
|
|
|
|
{ "charset",
|
|
|
|
"data:text/plain;charset=ISO-8859-1,Timm B\344der",
|
|
|
|
"text/plain", CONTENTS("Timm Bäder") },
|
2020-07-10 00:13:50 +00:00
|
|
|
{ "charset_escaped",
|
|
|
|
"data:text/plain;charset=ISO-8859-1,Timm%20B%E4der",
|
|
|
|
"text/plain", CONTENTS("Timm Bäder") },
|
2019-05-09 01:04:21 +00:00
|
|
|
{ "charset_base64",
|
|
|
|
"data:text/plain;charset=ISO-8859-5;base64,wOPh29DdILjW0ePb0OLe0g==",
|
|
|
|
"text/plain", CONTENTS("Руслан Ижбулатов") },
|
2020-10-30 23:26:56 +00:00
|
|
|
{ "wrong_scheme",
|
|
|
|
"duda:,Hello",
|
|
|
|
NULL, NULL, 0 },
|
|
|
|
{ "missing_comma",
|
|
|
|
"data:text/plain;charset=ISO-8859-1:bla",
|
|
|
|
NULL, NULL, 0 },
|
|
|
|
{ "bad_escape",
|
|
|
|
"data:,abc%00",
|
|
|
|
NULL, NULL, -1 },
|
2019-05-09 01:04:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_parse (gconstpointer data)
|
|
|
|
{
|
|
|
|
const Test *test = data;
|
|
|
|
GError *error = NULL;
|
|
|
|
char *mimetype = NULL;
|
|
|
|
GBytes *bytes;
|
|
|
|
|
|
|
|
bytes = gtk_css_data_url_parse (test->url, &mimetype, &error);
|
|
|
|
|
2020-10-30 23:26:56 +00:00
|
|
|
if (test->contents)
|
|
|
|
{
|
|
|
|
g_assert_nonnull (bytes);
|
|
|
|
g_assert_no_error (error);
|
|
|
|
if (test->mimetype == NULL)
|
2021-04-12 12:33:41 +00:00
|
|
|
g_assert_null (mimetype);
|
2020-10-30 23:26:56 +00:00
|
|
|
else
|
|
|
|
g_assert_cmpstr (mimetype, ==, test->mimetype);
|
2019-05-09 01:04:21 +00:00
|
|
|
|
2020-10-30 23:26:56 +00:00
|
|
|
g_assert_cmpmem (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes),
|
|
|
|
test->contents, test->contents_len);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_assert_null (bytes);
|
|
|
|
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
2019-05-09 01:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2021-04-13 01:22:04 +00:00
|
|
|
(g_test_init) (&argc, &argv, NULL);
|
2019-05-09 01:04:21 +00:00
|
|
|
setlocale (LC_ALL, "C");
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (tests); i++)
|
|
|
|
{
|
|
|
|
char *name = g_strdup_printf ("/css/data/load/%s", tests[i].name);
|
|
|
|
g_test_add_data_func (name, &tests[i], test_parse);
|
|
|
|
g_free (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|
|
|
|
|