mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-12 15:36:17 +01:00
Allow UTF-8 in file:// parse names.
2008-01-22 Alexander Larsson <alexl@redhat.com> * glocalfile.c: Allow UTF-8 in file:// parse names. * tests/Makefile.am: * tests/data-input-stream.c: * tests/data-output-stream.c: * tests/g-file-info.c: * tests/g-file.c: Added a bunch of tests from Tomas Bzatek svn path=/trunk/; revision=6351
This commit is contained in:
parent
211c72e12b
commit
b784923e9c
@ -1,3 +1,15 @@
|
||||
2008-01-22 Alexander Larsson <alexl@redhat.com>
|
||||
|
||||
* glocalfile.c:
|
||||
Allow UTF-8 in file:// parse names.
|
||||
|
||||
* tests/Makefile.am:
|
||||
* tests/data-input-stream.c:
|
||||
* tests/data-output-stream.c:
|
||||
* tests/g-file-info.c:
|
||||
* tests/g-file.c:
|
||||
Added a bunch of tests from Tomas Bzatek
|
||||
|
||||
2008-01-21 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* === Released 2.15.3 ===
|
||||
|
@ -348,7 +348,7 @@ name_is_valid_for_display (const char *string,
|
||||
gboolean is_valid_utf8)
|
||||
{
|
||||
char c;
|
||||
|
||||
|
||||
if (!is_valid_utf8 &&
|
||||
!g_utf8_validate (string, -1, NULL))
|
||||
return FALSE;
|
||||
@ -372,7 +372,8 @@ g_local_file_get_parse_name (GFile *file)
|
||||
char *roundtripped_filename;
|
||||
gboolean free_utf8_filename;
|
||||
gboolean is_valid_utf8;
|
||||
|
||||
char *escaped_path;
|
||||
|
||||
filename = G_LOCAL_FILE (file)->filename;
|
||||
if (get_filename_charset (&charset))
|
||||
{
|
||||
@ -402,7 +403,6 @@ g_local_file_get_parse_name (GFile *file)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (utf8_filename != NULL &&
|
||||
name_is_valid_for_display (utf8_filename, is_valid_utf8))
|
||||
{
|
||||
@ -413,7 +413,16 @@ g_local_file_get_parse_name (GFile *file)
|
||||
}
|
||||
else
|
||||
{
|
||||
parse_name = g_filename_to_uri (filename, NULL, NULL);
|
||||
escaped_path = g_uri_escape_string (filename,
|
||||
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
|
||||
TRUE);
|
||||
parse_name = g_strconcat ("file://",
|
||||
(*escaped_path != '/') ? "/" : "",
|
||||
escaped_path,
|
||||
NULL);
|
||||
|
||||
g_free (escaped_path);
|
||||
|
||||
if (free_utf8_filename)
|
||||
g_free (utf8_filename);
|
||||
}
|
||||
|
@ -16,7 +16,21 @@ progs_ldadd = \
|
||||
$(top_builddir)/gio/libgio-2.0.la
|
||||
|
||||
|
||||
TEST_PROGS += memory-input-stream
|
||||
TEST_PROGS += memory-input-stream g-file g-file-info data-input-stream data-output-stream
|
||||
|
||||
memory_input_stream_SOURCES = memory-input-stream.c
|
||||
memory_input_stream_LDADD = $(progs_ldadd)
|
||||
|
||||
g_file_SOURCES = g-file.c
|
||||
g_file_LDADD = $(progs_ldadd)
|
||||
|
||||
g_file_info_SOURCES = g-file-info.c
|
||||
g_file_info_LDADD = $(progs_ldadd)
|
||||
|
||||
data_input_stream_SOURCES = data-input-stream.c
|
||||
data_input_stream_LDADD = $(progs_ldadd)
|
||||
|
||||
data_output_stream_SOURCES = data-output-stream.c
|
||||
data_output_stream_LDADD = $(progs_ldadd)
|
||||
|
||||
|
||||
|
330
gio/tests/data-input-stream.c
Normal file
330
gio/tests/data-input-stream.c
Normal file
@ -0,0 +1,330 @@
|
||||
/* GLib testing framework examples and tests
|
||||
* Copyright (C) 2008 Red Hat, Inc.
|
||||
* Authors: Tomas Bzatek <tbzatek@redhat.com>
|
||||
*
|
||||
* This work is provided "as is"; redistribution and modification
|
||||
* in whole or in part, in any medium, physical or electronic is
|
||||
* permitted without restriction.
|
||||
*
|
||||
* This work 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.
|
||||
*
|
||||
* In no event shall the authors or contributors be liable for any
|
||||
* direct, indirect, incidental, special, exemplary, or consequential
|
||||
* damages (including, but not limited to, procurement of substitute
|
||||
* goods or services; loss of use, data, or profits; or business
|
||||
* interruption) however caused and on any theory of liability, whether
|
||||
* in contract, strict liability, or tort (including negligence or
|
||||
* otherwise) arising in any way out of the use of this software, even
|
||||
* if advised of the possibility of such damage.
|
||||
*/
|
||||
#include <glib/gtestutils.h>
|
||||
#include <glib/glib.h>
|
||||
#include <gio/gio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_LINES 0xFFF
|
||||
#define MAX_BYTES 0x10000
|
||||
|
||||
static void
|
||||
test_seek_to_start (GInputStream *stream)
|
||||
{
|
||||
GError *error = NULL;
|
||||
gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
|
||||
g_assert_cmpint (res, ==, TRUE);
|
||||
g_assert (error == NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_lines (GDataStreamNewlineType newline_type)
|
||||
{
|
||||
GInputStream *stream;
|
||||
GInputStream *base_stream;
|
||||
GError *error = NULL;
|
||||
char *data;
|
||||
int line;
|
||||
const char* lines[MAX_LINES];
|
||||
const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
|
||||
|
||||
/* prepare data */
|
||||
int i;
|
||||
for (i = 0; i < MAX_LINES; i++)
|
||||
lines[i] = "some_text";
|
||||
|
||||
base_stream = g_memory_input_stream_new ();
|
||||
g_assert (base_stream != NULL);
|
||||
stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
|
||||
g_assert(stream != NULL);
|
||||
|
||||
/* Byte order testing */
|
||||
g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
|
||||
g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
|
||||
g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
|
||||
g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
|
||||
|
||||
/* Line ends testing */
|
||||
g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
|
||||
g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
|
||||
|
||||
|
||||
/* Add sample data */
|
||||
for (i = 0; i < MAX_LINES; i++)
|
||||
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
|
||||
g_strconcat (lines[i], endl[newline_type], NULL), -1, NULL);
|
||||
|
||||
/* Seek to the start */
|
||||
test_seek_to_start (base_stream);
|
||||
|
||||
/* Test read line */
|
||||
error = NULL;
|
||||
data = (char*)1;
|
||||
line = 0;
|
||||
while (data)
|
||||
{
|
||||
gsize length = -1;
|
||||
data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
|
||||
if (data)
|
||||
{
|
||||
g_assert_cmpstr (data, ==, lines[line]);
|
||||
g_assert (error == NULL);
|
||||
line++;
|
||||
}
|
||||
}
|
||||
g_assert_cmpint (line, ==, MAX_LINES);
|
||||
|
||||
|
||||
g_object_unref (base_stream);
|
||||
g_object_unref (stream);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_lines_LF (void)
|
||||
{
|
||||
test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_lines_CR (void)
|
||||
{
|
||||
test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_lines_CR_LF (void)
|
||||
{
|
||||
test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_read_until (void)
|
||||
{
|
||||
GInputStream *stream;
|
||||
GInputStream *base_stream;
|
||||
GError *error = NULL;
|
||||
char *data;
|
||||
int line;
|
||||
|
||||
#define REPEATS 10 // number of rounds
|
||||
#define DATA_STRING " part1 # part2 $ part3 % part4 ^"
|
||||
#define DATA_PART_LEN 7 // number of characters between separators
|
||||
#define DATA_SEP "#$%^"
|
||||
const int DATA_PARTS_NUM = strlen (DATA_SEP) * REPEATS;
|
||||
|
||||
base_stream = g_memory_input_stream_new ();
|
||||
stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
|
||||
|
||||
int i;
|
||||
for (i = 0; i < REPEATS; i++)
|
||||
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
|
||||
|
||||
/* Test stop characters */
|
||||
error = NULL;
|
||||
data = (char*)1;
|
||||
line = 0;
|
||||
while (data)
|
||||
{
|
||||
gsize length = -1;
|
||||
data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
|
||||
if (data)
|
||||
{
|
||||
g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
|
||||
g_assert (error == NULL);
|
||||
line++;
|
||||
}
|
||||
}
|
||||
g_assert (error == NULL);
|
||||
g_assert_cmpint (line, ==, DATA_PARTS_NUM);
|
||||
|
||||
|
||||
g_object_unref (base_stream);
|
||||
g_object_unref (stream);
|
||||
}
|
||||
|
||||
enum TestDataType {
|
||||
TEST_DATA_BYTE = 0,
|
||||
TEST_DATA_INT16,
|
||||
TEST_DATA_UINT16,
|
||||
TEST_DATA_INT32,
|
||||
TEST_DATA_UINT32,
|
||||
TEST_DATA_INT64,
|
||||
TEST_DATA_UINT64
|
||||
};
|
||||
|
||||
#define TEST_DATA_RETYPE_BUFF(a, v) \
|
||||
(a == TEST_DATA_BYTE ? *(guchar*)v : \
|
||||
(a == TEST_DATA_INT16 ? *(gint16*)v : \
|
||||
(a == TEST_DATA_UINT16 ? *(guint16*)v : \
|
||||
(a == TEST_DATA_INT32 ? *(gint32*)v : \
|
||||
(a == TEST_DATA_UINT32 ? *(guint32*)v : \
|
||||
(a == TEST_DATA_INT64 ? *(gint64*)v : \
|
||||
*(guint64*)v ))))))
|
||||
|
||||
|
||||
static void
|
||||
test_data_array (GInputStream *stream, GInputStream *base_stream,
|
||||
gpointer buffer, int len,
|
||||
enum TestDataType data_type, GDataStreamByteOrder byte_order)
|
||||
{
|
||||
GError *error = NULL;
|
||||
int pos = 0;
|
||||
int data_size = 1;
|
||||
gint64 data;
|
||||
GDataStreamByteOrder native;
|
||||
gboolean swap;
|
||||
|
||||
/* Seek to start */
|
||||
test_seek_to_start (base_stream);
|
||||
|
||||
/* Set correct data size */
|
||||
switch (data_type)
|
||||
{
|
||||
case TEST_DATA_BYTE:
|
||||
data_size = 1;
|
||||
break;
|
||||
case TEST_DATA_INT16:
|
||||
case TEST_DATA_UINT16:
|
||||
data_size = 2;
|
||||
break;
|
||||
case TEST_DATA_INT32:
|
||||
case TEST_DATA_UINT32:
|
||||
data_size = 4;
|
||||
break;
|
||||
case TEST_DATA_INT64:
|
||||
case TEST_DATA_UINT64:
|
||||
data_size = 8;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Set flag to swap bytes if needed */
|
||||
native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
|
||||
swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
|
||||
|
||||
data = 1;
|
||||
while (data != 0)
|
||||
{
|
||||
switch (data_type)
|
||||
{
|
||||
case TEST_DATA_BYTE:
|
||||
data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
|
||||
break;
|
||||
case TEST_DATA_INT16:
|
||||
data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
|
||||
if (swap)
|
||||
data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
|
||||
break;
|
||||
case TEST_DATA_UINT16:
|
||||
data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
|
||||
if (swap)
|
||||
data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
|
||||
break;
|
||||
case TEST_DATA_INT32:
|
||||
data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
|
||||
if (swap)
|
||||
data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
|
||||
break;
|
||||
case TEST_DATA_UINT32:
|
||||
data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
|
||||
if (swap)
|
||||
data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
|
||||
break;
|
||||
case TEST_DATA_INT64:
|
||||
data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
|
||||
if (swap)
|
||||
data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
|
||||
break;
|
||||
case TEST_DATA_UINT64:
|
||||
data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
|
||||
if (swap)
|
||||
data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
|
||||
break;
|
||||
}
|
||||
if ((data) && (! error))
|
||||
g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, (buffer + pos)));
|
||||
|
||||
pos += data_size;
|
||||
}
|
||||
if (pos < len + 1)
|
||||
g_assert (error == NULL);
|
||||
g_assert_cmpint (pos - data_size, ==, len);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_int (void)
|
||||
{
|
||||
GInputStream *stream;
|
||||
GInputStream *base_stream;
|
||||
GRand *rand;
|
||||
int i;
|
||||
gpointer buffer;
|
||||
|
||||
rand = g_rand_new ();
|
||||
buffer = g_malloc0 (MAX_BYTES);
|
||||
|
||||
/* Fill in some random data */
|
||||
for (i = 0; i < MAX_BYTES; i++)
|
||||
{
|
||||
guchar x = 0;
|
||||
while (! x)
|
||||
x = (guchar)g_rand_int (rand);
|
||||
*(guchar*)(buffer + sizeof(guchar) * i) = x;
|
||||
}
|
||||
|
||||
base_stream = g_memory_input_stream_new ();
|
||||
stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
|
||||
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
|
||||
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
int j;
|
||||
g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
|
||||
|
||||
for (j = 0; j <= TEST_DATA_UINT64; j++)
|
||||
test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
|
||||
}
|
||||
|
||||
g_object_unref (base_stream);
|
||||
g_object_unref (stream);
|
||||
g_rand_free (rand);
|
||||
g_free (buffer);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_type_init ();
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
|
||||
g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
|
||||
g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
|
||||
g_test_add_func ("/data-input-stream/read-until", test_read_until);
|
||||
g_test_add_func ("/data-input-stream/read-int", test_read_int);
|
||||
|
||||
return g_test_run();
|
||||
}
|
292
gio/tests/data-output-stream.c
Normal file
292
gio/tests/data-output-stream.c
Normal file
@ -0,0 +1,292 @@
|
||||
/* GLib testing framework examples and tests
|
||||
* Copyright (C) 2008 Red Hat, Inc.
|
||||
* Authors: Tomas Bzatek <tbzatek@redhat.com>
|
||||
*
|
||||
* This work is provided "as is"; redistribution and modification
|
||||
* in whole or in part, in any medium, physical or electronic is
|
||||
* permitted without restriction.
|
||||
*
|
||||
* This work 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.
|
||||
*
|
||||
* In no event shall the authors or contributors be liable for any
|
||||
* direct, indirect, incidental, special, exemplary, or consequential
|
||||
* damages (including, but not limited to, procurement of substitute
|
||||
* goods or services; loss of use, data, or profits; or business
|
||||
* interruption) however caused and on any theory of liability, whether
|
||||
* in contract, strict liability, or tort (including negligence or
|
||||
* otherwise) arising in any way out of the use of this software, even
|
||||
* if advised of the possibility of such damage.
|
||||
*/
|
||||
#include <glib/gtestutils.h>
|
||||
#include <glib/glib.h>
|
||||
#include <gio/gio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_LINES 0xFFF
|
||||
#define MAX_LINES_BUFF 0xFFFFFF
|
||||
#define MAX_BYTES_BINARY 0x100
|
||||
|
||||
static void
|
||||
test_read_lines (GDataStreamNewlineType newline_type)
|
||||
{
|
||||
GOutputStream *stream;
|
||||
GOutputStream *base_stream;
|
||||
GError *error = NULL;
|
||||
gpointer data;
|
||||
char *lines;
|
||||
int i;
|
||||
|
||||
#define TEST_STRING "some_text"
|
||||
|
||||
const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
|
||||
|
||||
|
||||
data = g_malloc0 (MAX_LINES_BUFF);
|
||||
lines = g_malloc0 ((strlen (TEST_STRING) + strlen (endl[newline_type])) * MAX_LINES + 1);
|
||||
|
||||
/* initialize objects */
|
||||
base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
|
||||
stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
|
||||
|
||||
|
||||
/* fill data */
|
||||
for (i = 0; i < MAX_LINES; i++)
|
||||
{
|
||||
gboolean res;
|
||||
char *s = g_strconcat (TEST_STRING, endl[newline_type], NULL);
|
||||
res = g_data_output_stream_put_string (G_DATA_OUTPUT_STREAM (stream), s, NULL, &error);
|
||||
g_stpcpy ((char*)(lines + i*strlen(s)), s);
|
||||
g_assert (error == NULL);
|
||||
g_assert (res == TRUE);
|
||||
}
|
||||
|
||||
/* Byte order testing */
|
||||
g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
|
||||
g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
|
||||
g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
|
||||
g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
|
||||
|
||||
/* compare data */
|
||||
int size = strlen(data);
|
||||
g_assert_cmpint (size, <, MAX_LINES_BUFF);
|
||||
g_assert_cmpstr ((char*)data, ==, lines);
|
||||
|
||||
g_object_unref (base_stream);
|
||||
g_object_unref (stream);
|
||||
g_free (data);
|
||||
g_free (lines);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_lines_LF (void)
|
||||
{
|
||||
test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_lines_CR (void)
|
||||
{
|
||||
test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_lines_CR_LF (void)
|
||||
{
|
||||
test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
|
||||
}
|
||||
|
||||
enum TestDataType {
|
||||
TEST_DATA_BYTE = 0,
|
||||
TEST_DATA_INT16,
|
||||
TEST_DATA_UINT16,
|
||||
TEST_DATA_INT32,
|
||||
TEST_DATA_UINT32,
|
||||
TEST_DATA_INT64,
|
||||
TEST_DATA_UINT64
|
||||
};
|
||||
|
||||
#define TEST_DATA_RETYPE(a, v) \
|
||||
(a == TEST_DATA_BYTE ? (guchar)v : \
|
||||
(a == TEST_DATA_INT16 ? (gint16)v : \
|
||||
(a == TEST_DATA_UINT16 ? (guint16)v : \
|
||||
(a == TEST_DATA_INT32 ? (gint32)v : \
|
||||
(a == TEST_DATA_UINT32 ? (guint32)v : \
|
||||
(a == TEST_DATA_INT64 ? (gint64)v : \
|
||||
(guint64)v ))))))
|
||||
|
||||
#define TEST_DATA_RETYPE_BUFF(a, v) \
|
||||
(a == TEST_DATA_BYTE ? *(guchar*)v : \
|
||||
(a == TEST_DATA_INT16 ? *(gint16*)v : \
|
||||
(a == TEST_DATA_UINT16 ? *(guint16*)v : \
|
||||
(a == TEST_DATA_INT32 ? *(gint32*)v : \
|
||||
(a == TEST_DATA_UINT32 ? *(guint32*)v : \
|
||||
(a == TEST_DATA_INT64 ? *(gint64*)v : \
|
||||
*(guint64*)v ))))))
|
||||
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_data_array (gpointer buffer, int len,
|
||||
enum TestDataType data_type, GDataStreamByteOrder byte_order)
|
||||
{
|
||||
GOutputStream *stream;
|
||||
GOutputStream *base_stream;
|
||||
gpointer stream_data;
|
||||
|
||||
GError *error = NULL;
|
||||
int pos;
|
||||
int data_size = 1;
|
||||
GDataStreamByteOrder native;
|
||||
gboolean swap;
|
||||
gboolean res;
|
||||
guint64 data;
|
||||
|
||||
/* create objects */
|
||||
stream_data = g_malloc0 (len);
|
||||
base_stream = g_memory_output_stream_new (stream_data, len, NULL, NULL);
|
||||
stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
|
||||
g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), byte_order);
|
||||
|
||||
|
||||
/* Set correct data size */
|
||||
switch (data_type)
|
||||
{
|
||||
case TEST_DATA_BYTE:
|
||||
data_size = 1;
|
||||
break;
|
||||
case TEST_DATA_INT16:
|
||||
case TEST_DATA_UINT16:
|
||||
data_size = 2;
|
||||
break;
|
||||
case TEST_DATA_INT32:
|
||||
case TEST_DATA_UINT32:
|
||||
data_size = 4;
|
||||
break;
|
||||
case TEST_DATA_INT64:
|
||||
case TEST_DATA_UINT64:
|
||||
data_size = 8;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Set flag to swap bytes if needed */
|
||||
native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
|
||||
swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
|
||||
|
||||
/* Write data to the file */
|
||||
pos = 0;
|
||||
while (pos < len)
|
||||
{
|
||||
switch (data_type)
|
||||
{
|
||||
case TEST_DATA_BYTE:
|
||||
res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
|
||||
break;
|
||||
case TEST_DATA_INT16:
|
||||
res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
|
||||
break;
|
||||
case TEST_DATA_UINT16:
|
||||
res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
|
||||
break;
|
||||
case TEST_DATA_INT32:
|
||||
res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
|
||||
break;
|
||||
case TEST_DATA_UINT32:
|
||||
res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
|
||||
break;
|
||||
case TEST_DATA_INT64:
|
||||
res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
|
||||
break;
|
||||
case TEST_DATA_UINT64:
|
||||
res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
|
||||
break;
|
||||
}
|
||||
g_assert (error == NULL);
|
||||
g_assert_cmpint (res, ==, TRUE);
|
||||
pos += data_size;
|
||||
}
|
||||
|
||||
|
||||
/* Compare data back */
|
||||
pos = 0;
|
||||
data = 0;
|
||||
while (pos < len)
|
||||
{
|
||||
data = TEST_DATA_RETYPE_BUFF(data_type, (stream_data + pos));
|
||||
if (swap)
|
||||
{
|
||||
switch (data_type)
|
||||
{
|
||||
case TEST_DATA_BYTE:
|
||||
break;
|
||||
case TEST_DATA_UINT16:
|
||||
case TEST_DATA_INT16:
|
||||
data = TEST_DATA_RETYPE(data_type, GUINT16_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
|
||||
break;
|
||||
case TEST_DATA_UINT32:
|
||||
case TEST_DATA_INT32:
|
||||
data = TEST_DATA_RETYPE(data_type, GUINT32_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
|
||||
break;
|
||||
case TEST_DATA_UINT64:
|
||||
case TEST_DATA_INT64:
|
||||
data = TEST_DATA_RETYPE(data_type, GUINT64_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, (buffer + pos)));
|
||||
break;
|
||||
|
||||
pos += data_size;
|
||||
}
|
||||
|
||||
g_object_unref (base_stream);
|
||||
g_object_unref (stream);
|
||||
g_free (stream_data);
|
||||
}
|
||||
|
||||
static void
|
||||
test_read_int (void)
|
||||
{
|
||||
GRand *rand;
|
||||
gpointer buffer;
|
||||
|
||||
rand = g_rand_new ();
|
||||
buffer = g_malloc0(MAX_BYTES_BINARY);
|
||||
|
||||
/* Fill in some random data */
|
||||
int i;
|
||||
for (i = 0; i < MAX_BYTES_BINARY; i++)
|
||||
{
|
||||
guchar x = 0;
|
||||
while (! x) x = (guchar)g_rand_int (rand);
|
||||
*(guchar*)(buffer + sizeof(guchar) * i) = x;
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
int j;
|
||||
for (j = 0; j <= TEST_DATA_UINT64; j++)
|
||||
test_data_array (buffer, MAX_BYTES_BINARY, j, i);
|
||||
}
|
||||
|
||||
g_rand_free (rand);
|
||||
g_free (buffer);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_type_init ();
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
|
||||
g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
|
||||
g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
|
||||
g_test_add_func ("/data-input-stream/read-int", test_read_int);
|
||||
|
||||
return g_test_run();
|
||||
}
|
125
gio/tests/g-file-info.c
Normal file
125
gio/tests/g-file-info.c
Normal file
@ -0,0 +1,125 @@
|
||||
/* GLib testing framework examples and tests
|
||||
* Copyright (C) 2008 Red Hat, Inc.
|
||||
* Authors: Tomas Bzatek <tbzatek@redhat.com>
|
||||
*
|
||||
* This work is provided "as is"; redistribution and modification
|
||||
* in whole or in part, in any medium, physical or electronic is
|
||||
* permitted without restriction.
|
||||
*
|
||||
* This work 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.
|
||||
*
|
||||
* In no event shall the authors or contributors be liable for any
|
||||
* direct, indirect, incidental, special, exemplary, or consequential
|
||||
* damages (including, but not limited to, procurement of substitute
|
||||
* goods or services; loss of use, data, or profits; or business
|
||||
* interruption) however caused and on any theory of liability, whether
|
||||
* in contract, strict liability, or tort (including negligence or
|
||||
* otherwise) arising in any way out of the use of this software, even
|
||||
* if advised of the possibility of such damage.
|
||||
*/
|
||||
#include <glib/gtestutils.h>
|
||||
#include <glib/glib.h>
|
||||
#include <gio/gio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TEST_NAME "Prilis zlutoucky kun"
|
||||
#define TEST_DISPLAY_NAME "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"
|
||||
#define TEST_SIZE 0xFFFFFFF0
|
||||
|
||||
static void
|
||||
test_assigned_values (GFileInfo *info)
|
||||
{
|
||||
const char *name, *display_name, *mistake;
|
||||
guint64 size;
|
||||
GFileType type;
|
||||
|
||||
/* Test for attributes presence */
|
||||
g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_NAME) == TRUE);
|
||||
g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME) == TRUE);
|
||||
g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE) == TRUE);
|
||||
g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME) == FALSE);
|
||||
|
||||
/* Retrieve data back and compare */
|
||||
|
||||
name = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME);
|
||||
display_name = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
|
||||
mistake = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
|
||||
size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
|
||||
type = g_file_info_get_file_type (info);
|
||||
|
||||
g_assert_cmpstr (name, ==, TEST_NAME);
|
||||
g_assert_cmpstr (display_name, ==, TEST_DISPLAY_NAME);
|
||||
g_assert (mistake == NULL);
|
||||
g_assert_cmpint (size, ==, TEST_SIZE);
|
||||
g_assert_cmpstr (name, ==, g_file_info_get_name (info));
|
||||
g_assert_cmpstr (display_name, ==, g_file_info_get_display_name (info));
|
||||
g_assert_cmpint (size, ==, g_file_info_get_size (info) );
|
||||
g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_g_file_info (void)
|
||||
{
|
||||
GFileInfo *info;
|
||||
GFileInfo *info_dup;
|
||||
GFileInfo *info_copy;
|
||||
char **attr_list;
|
||||
|
||||
info = g_file_info_new ();
|
||||
|
||||
/* Test for empty instance */
|
||||
attr_list = g_file_info_list_attributes (info, NULL);
|
||||
g_assert (attr_list != NULL);
|
||||
g_assert (*attr_list == NULL);
|
||||
|
||||
g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME, TEST_NAME);
|
||||
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, TEST_DISPLAY_NAME);
|
||||
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE, TEST_SIZE);
|
||||
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
|
||||
|
||||
/* The attr list should not be empty now */
|
||||
attr_list = g_file_info_list_attributes (info, NULL);
|
||||
g_assert (attr_list != NULL);
|
||||
g_assert (*attr_list != NULL);
|
||||
|
||||
test_assigned_values (info);
|
||||
|
||||
/* Test dups */
|
||||
info_dup = g_file_info_dup (info);
|
||||
g_assert (info_dup != NULL);
|
||||
test_assigned_values (info_dup);
|
||||
|
||||
info_copy = g_file_info_new ();
|
||||
g_file_info_copy_into (info_dup, info_copy);
|
||||
g_assert (info_copy != NULL);
|
||||
test_assigned_values (info_copy);
|
||||
|
||||
/* Test remove attribute */
|
||||
g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == FALSE);
|
||||
g_file_info_set_attribute_int32 (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER, 10);
|
||||
g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == TRUE);
|
||||
|
||||
g_file_info_remove_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
|
||||
g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == FALSE);
|
||||
|
||||
g_object_unref (info);
|
||||
g_object_unref (info_dup);
|
||||
g_object_unref (info_copy);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_type_init ();
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/g-file-info/test_g_file_info", test_g_file_info);
|
||||
|
||||
return g_test_run();
|
||||
}
|
531
gio/tests/g-file.c
Normal file
531
gio/tests/g-file.c
Normal file
@ -0,0 +1,531 @@
|
||||
/* GLib testing framework examples and tests
|
||||
* Copyright (C) 2008 Red Hat, Inc.
|
||||
* Authors: Tomas Bzatek <tbzatek@redhat.com>
|
||||
*
|
||||
* This work is provided "as is"; redistribution and modification
|
||||
* in whole or in part, in any medium, physical or electronic is
|
||||
* permitted without restriction.
|
||||
*
|
||||
* This work 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.
|
||||
*
|
||||
* In no event shall the authors or contributors be liable for any
|
||||
* direct, indirect, incidental, special, exemplary, or consequential
|
||||
* damages (including, but not limited to, procurement of substitute
|
||||
* goods or services; loss of use, data, or profits; or business
|
||||
* interruption) however caused and on any theory of liability, whether
|
||||
* in contract, strict liability, or tort (including negligence or
|
||||
* otherwise) arising in any way out of the use of this software, even
|
||||
* if advised of the possibility of such damage.
|
||||
*/
|
||||
#include <glib/gtestutils.h>
|
||||
#include <glib/glib.h>
|
||||
#include <gio/gio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct TestPathsWithOper {
|
||||
const char *path1;
|
||||
gboolean equal;
|
||||
gboolean use_uri;
|
||||
const char *path2;
|
||||
const char *path3;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* TODO:
|
||||
* - test on Windows
|
||||
*
|
||||
**/
|
||||
|
||||
static void
|
||||
test_g_file_new_null (void)
|
||||
{
|
||||
const char *paths[] = {"/",
|
||||
"/tmp///",
|
||||
"/non-existent-file",
|
||||
"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
|
||||
NULL
|
||||
};
|
||||
const char *uris[] = {"file:///",
|
||||
"file:///tmp///",
|
||||
"non-existent-uri:///some-dir/",
|
||||
"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88",
|
||||
NULL
|
||||
};
|
||||
|
||||
GFile *file = NULL;
|
||||
|
||||
int i = 0;
|
||||
while (paths[i])
|
||||
{
|
||||
file = g_file_new_for_path (paths[i++]);
|
||||
g_assert (file != NULL);
|
||||
g_object_unref (file);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
while (uris[i])
|
||||
{
|
||||
file = g_file_new_for_uri (paths[i++]);
|
||||
g_assert (file != NULL);
|
||||
g_object_unref(file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static gboolean
|
||||
compare_two_files (const gboolean use_uri, const char *path1, const char *path2)
|
||||
{
|
||||
GFile *file1 = NULL;
|
||||
GFile *file2 = NULL;
|
||||
gboolean equal;
|
||||
|
||||
if (use_uri)
|
||||
{
|
||||
file1 = g_file_new_for_uri (path1);
|
||||
file2 = g_file_new_for_uri (path2);
|
||||
}
|
||||
else
|
||||
{
|
||||
file1 = g_file_new_for_path (path1);
|
||||
file2 = g_file_new_for_path (path2);
|
||||
}
|
||||
|
||||
g_assert (file1 != NULL);
|
||||
g_assert (file2 != NULL);
|
||||
|
||||
equal = g_file_equal (file1, file2);
|
||||
|
||||
g_object_unref (file1);
|
||||
g_object_unref (file2);
|
||||
|
||||
return equal;
|
||||
}
|
||||
|
||||
static void
|
||||
test_g_file_new_for_path (void)
|
||||
{
|
||||
const struct TestPathsWithOper cmp_paths[] =
|
||||
{
|
||||
{"/", TRUE, 0, "/./"},
|
||||
{"//", TRUE, 0, "//"},
|
||||
{"//", TRUE, 0, "//./"},
|
||||
{"/", TRUE, 0, "/.//"},
|
||||
{"/", TRUE, 0, "/././"},
|
||||
{"/tmp", TRUE, 0, "/tmp/d/../"},
|
||||
{"/", TRUE, 0, "/somedir/../"},
|
||||
{"/", FALSE, 0, "/somedir/.../"},
|
||||
{"//tmp/dir1", TRUE, 0, "//tmp/dir1"},
|
||||
// {"/tmp/dir1", TRUE, 0, "///tmp/dir1"},
|
||||
{"/tmp/dir1", TRUE, 0, "/tmp/./dir1"},
|
||||
{"/tmp/dir1", TRUE, 0, "/tmp//dir1"},
|
||||
{"/tmp/dir1", TRUE, 0, "/tmp///dir1///"},
|
||||
{"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, 0, "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/"}
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++)
|
||||
{
|
||||
gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2);
|
||||
g_assert_cmpint (equal, ==, cmp_paths[i].equal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
test_g_file_new_for_uri (void)
|
||||
{
|
||||
const struct TestPathsWithOper cmp_uris[] = {
|
||||
{"file:///", TRUE, 0, "file:///./"},
|
||||
{"file:////", TRUE, 0, "file:////"},
|
||||
{"file:////", TRUE, 0, "file:////./"},
|
||||
{"file:///", TRUE, 0, "file:///.//"},
|
||||
{"file:///", TRUE, 0, "file:///././"},
|
||||
{"file:///tmp", TRUE, 0, "file:///tmp/d/../"},
|
||||
{"file:///", TRUE, 0, "file:///somedir/../"},
|
||||
{"file:///", FALSE, 0, "file:///somedir/.../"},
|
||||
{"file:////tmp/dir1", TRUE, 0, "file:////tmp/dir1"},
|
||||
{"file:///tmp/dir1", TRUE, 0, "file:///tmp/./dir1"},
|
||||
{"file:///tmp/dir1", TRUE, 0, "file:///tmp//dir1"},
|
||||
{"file:///tmp/dir1", TRUE, 0, "file:///tmp///dir1///"},
|
||||
{"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, 0, "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/"}
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++)
|
||||
{
|
||||
gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2);
|
||||
g_assert_cmpint (equal, ==, cmp_uris[i].equal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static gboolean
|
||||
dup_equals (const gboolean use_uri, const char *path)
|
||||
{
|
||||
GFile *file1 = NULL;
|
||||
GFile *file2 = NULL;
|
||||
gboolean equal;
|
||||
|
||||
if (use_uri)
|
||||
file1 = g_file_new_for_uri (path);
|
||||
else
|
||||
file1 = g_file_new_for_path (path);
|
||||
|
||||
g_assert (file1 != NULL);
|
||||
|
||||
file2 = g_file_dup (file1);
|
||||
|
||||
g_assert (file2 != NULL);
|
||||
|
||||
equal = g_file_equal (file1, file2);
|
||||
|
||||
g_object_unref (file1);
|
||||
g_object_unref (file2);
|
||||
|
||||
return equal;
|
||||
}
|
||||
|
||||
static void
|
||||
test_g_file_dup (void)
|
||||
{
|
||||
const struct TestPathsWithOper dup_paths[] =
|
||||
{
|
||||
{"/", 0, FALSE, ""},
|
||||
{"file:///", 0, TRUE, ""},
|
||||
{"totalnonsense", 0, FALSE, ""},
|
||||
{"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, ""},
|
||||
{"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", 0, TRUE, ""},
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < G_N_ELEMENTS (dup_paths); i++)
|
||||
{
|
||||
gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1);
|
||||
g_assert (equal == TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static gboolean
|
||||
parse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name)
|
||||
{
|
||||
GFile *file1 = NULL;
|
||||
GFile *file2 = NULL;
|
||||
char *parsed_name;
|
||||
gboolean is_utf8_valid;
|
||||
gboolean equal;
|
||||
|
||||
if (use_uri)
|
||||
file1 = g_file_new_for_uri (path);
|
||||
else
|
||||
file1 = g_file_new_for_path (path);
|
||||
|
||||
g_assert (file1 != NULL);
|
||||
|
||||
parsed_name = g_file_get_parse_name (file1);
|
||||
|
||||
g_assert (parsed_name != NULL);
|
||||
|
||||
/* UTF-8 validation */
|
||||
is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL);
|
||||
g_assert (is_utf8_valid == TRUE);
|
||||
|
||||
if (result_parse_name)
|
||||
g_assert_cmpstr (parsed_name, ==, result_parse_name);
|
||||
|
||||
file2 = g_file_parse_name (parsed_name);
|
||||
|
||||
g_assert (file2 != NULL);
|
||||
|
||||
equal = g_file_equal (file1, file2);
|
||||
|
||||
g_object_unref (file1);
|
||||
g_object_unref (file2);
|
||||
|
||||
g_free (parsed_name);
|
||||
|
||||
return equal;
|
||||
}
|
||||
|
||||
static void
|
||||
test_g_file_get_parse_name_utf8 (void)
|
||||
{
|
||||
const struct TestPathsWithOper strings[] =
|
||||
{
|
||||
{"/", 0, FALSE, "/"},
|
||||
{"file:///", 0, TRUE, "/"},
|
||||
{"totalnonsense", 0, FALSE, NULL},
|
||||
{"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, NULL /* Depends on local file encoding */},
|
||||
{"file:///invalid%08/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, TRUE, "file:///invalid%08/UTF-8%20p\xc5\x99\xc3\xadli\xc5\xa1%20\xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd%20k\xc5\xaf\xc5\x88"},
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < G_N_ELEMENTS (strings); i++)
|
||||
{
|
||||
gboolean equal = parse_check_utf8 (strings[i].use_uri, strings[i].path1, strings[i].path2);
|
||||
g_assert (equal == TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
static char *
|
||||
resolve_arg (const gboolean is_uri_only, const char *arg)
|
||||
{
|
||||
GFile *file1 = NULL;
|
||||
char *uri = NULL;
|
||||
char *path = NULL;
|
||||
char *s = NULL;
|
||||
|
||||
file1 = g_file_new_for_commandline_arg (arg);
|
||||
g_assert (file1 != NULL);
|
||||
|
||||
/* Test if we get URI string */
|
||||
uri = g_file_get_uri (file1);
|
||||
g_assert (uri != NULL);
|
||||
|
||||
/* Test if we get correct value of the local path */
|
||||
path = g_file_get_path (file1);
|
||||
if (is_uri_only)
|
||||
g_assert (path == NULL);
|
||||
else
|
||||
g_assert (g_path_is_absolute (path) == TRUE);
|
||||
|
||||
/* Get the URI scheme and compare it with expected one */
|
||||
s = g_file_get_uri_scheme (file1);
|
||||
|
||||
g_object_unref (file1);
|
||||
g_free (uri);
|
||||
g_free (path);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static void
|
||||
test_g_file_new_for_commandline_arg (void)
|
||||
{
|
||||
/* TestPathsWithOper.use_uri represents IsURIOnly here */
|
||||
const struct TestPathsWithOper arg_data[] =
|
||||
{
|
||||
{"./", 0, FALSE, "file"},
|
||||
{"../", 0, FALSE, "file"},
|
||||
{"/tmp", 0, FALSE, "file"},
|
||||
{"//UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, "file"},
|
||||
{"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, FALSE, "file"},
|
||||
{"http://www.gtk.org/", 0, TRUE, "http"},
|
||||
{"ftp://user:pass@ftp.gimp.org/", 0, TRUE, "ftp"},
|
||||
};
|
||||
GFile *file;
|
||||
char *resolved;
|
||||
char *cwd;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < G_N_ELEMENTS (arg_data); i++)
|
||||
{
|
||||
char *s = resolve_arg (arg_data[i].use_uri, arg_data[i].path1);
|
||||
g_assert_cmpstr (s, ==, arg_data[i].path2);
|
||||
g_free (s);
|
||||
}
|
||||
|
||||
/* Manual test for getting correct cwd */
|
||||
file = g_file_new_for_commandline_arg ("./");
|
||||
resolved = g_file_get_path (file);
|
||||
cwd = g_get_current_dir ();
|
||||
g_assert_cmpstr (resolved, ==, cwd);
|
||||
g_object_unref (file);
|
||||
g_free (resolved);
|
||||
g_free (cwd);
|
||||
}
|
||||
|
||||
static char*
|
||||
get_relative_path (const gboolean use_uri, const gboolean should_contain_file, const char *dir1, const char *dir2)
|
||||
{
|
||||
GFile *file1 = NULL;
|
||||
GFile *file2 = NULL;
|
||||
GFile *file3 = NULL;
|
||||
gboolean contains_file = FALSE;
|
||||
char *relative_path = NULL;
|
||||
|
||||
if (use_uri)
|
||||
{
|
||||
file1 = g_file_new_for_uri (dir1);
|
||||
file2 = g_file_new_for_uri (dir2);
|
||||
}
|
||||
else
|
||||
{
|
||||
file1 = g_file_new_for_path (dir1);
|
||||
file2 = g_file_new_for_path (dir2);
|
||||
}
|
||||
|
||||
g_assert (file1 != NULL);
|
||||
g_assert (file2 != NULL);
|
||||
|
||||
contains_file = g_file_contains_file (file1, file2);
|
||||
g_assert (contains_file == should_contain_file);
|
||||
|
||||
relative_path = g_file_get_relative_path (file1, file2);
|
||||
if (should_contain_file)
|
||||
{
|
||||
g_assert (relative_path != NULL);
|
||||
|
||||
file3 = g_file_resolve_relative_path (file1, relative_path);
|
||||
g_assert (g_file_equal (file2, file3) == TRUE);
|
||||
}
|
||||
|
||||
if (file1)
|
||||
g_object_unref (file1);
|
||||
if (file2)
|
||||
g_object_unref (file2);
|
||||
if (file3)
|
||||
g_object_unref (file3);
|
||||
|
||||
return relative_path;
|
||||
}
|
||||
|
||||
static void
|
||||
test_g_file_contains_file (void)
|
||||
{
|
||||
/* TestPathsWithOper.equal represents here if the dir belongs to the directory structure */
|
||||
const struct TestPathsWithOper dirs[] =
|
||||
{
|
||||
/* path1 equal uri path2 path3 */
|
||||
{"/dir1", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2/dir3"},
|
||||
{"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2/dir3"},
|
||||
{"/dir1", TRUE, FALSE, "/dir1/dir2/dir3", "dir2/dir3"},
|
||||
{"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3", "dir2/dir3"},
|
||||
{"/tmp/", FALSE, FALSE, "/something/", NULL},
|
||||
{"/dir1/dir2", FALSE, FALSE, "/dir1/", NULL},
|
||||
{"//dir1/new", TRUE, FALSE, "//dir1/new/dir2/dir3", "dir2/dir3"},
|
||||
{"/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, FALSE, "/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/dir2", "dir2"},
|
||||
{"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2/dir3"},
|
||||
{"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2/dir3"},
|
||||
{"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2/dir3"},
|
||||
{"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2/dir3"},
|
||||
{"file:///tmp/", FALSE, TRUE, "file:///something/", NULL},
|
||||
{"file:///dir1/dir2", FALSE, TRUE, "file:///dir1/", NULL},
|
||||
{"file:////dir1/new", TRUE, TRUE, "file:////dir1/new/dir2/dir3", "dir2/dir3"},
|
||||
{"file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, TRUE, "file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/dir2", "dir2"},
|
||||
{"dav://www.gtk.org/plan/", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "meetings/20071218.txt"},
|
||||
{"dav://www.gtk.org/plan/meetings", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "20071218.txt"},
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < G_N_ELEMENTS (dirs); i++)
|
||||
{
|
||||
char *s = get_relative_path (dirs[i].use_uri, dirs[i].equal, dirs[i].path1, dirs[i].path2);
|
||||
if (dirs[i].equal)
|
||||
g_assert_cmpstr (s, ==, dirs[i].path3);
|
||||
g_free (s);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
roundtrip_parent_child (const gboolean use_uri, const gboolean under_root_descending,
|
||||
const char *path, const char *dir_holder)
|
||||
{
|
||||
GFile *files[6] = {0};
|
||||
int i;
|
||||
|
||||
if (use_uri)
|
||||
{
|
||||
files[0] = g_file_new_for_uri (path);
|
||||
files[1] = g_file_new_for_uri (path);
|
||||
}
|
||||
else
|
||||
{
|
||||
files[0] = g_file_new_for_path (path);
|
||||
files[1] = g_file_new_for_path (path);
|
||||
}
|
||||
|
||||
g_assert (files[0] != NULL);
|
||||
g_assert (files[1] != NULL);
|
||||
|
||||
files[2] = g_file_get_child (files[1], dir_holder);
|
||||
g_assert (files[2] != NULL);
|
||||
|
||||
files[3] = g_file_get_parent (files[2]);
|
||||
g_assert (files[3] != NULL);
|
||||
g_assert (g_file_equal (files[3], files[0]) == TRUE);
|
||||
|
||||
files[4] = g_file_get_parent (files[3]);
|
||||
/* Don't go lower beyond the root */
|
||||
if (under_root_descending)
|
||||
g_assert (files[4] == NULL);
|
||||
else
|
||||
{
|
||||
g_assert (files[4] != NULL);
|
||||
|
||||
files[5] = g_file_get_child (files[4], dir_holder);
|
||||
g_assert (files[5] != NULL);
|
||||
g_assert (g_file_equal (files[5], files[0]) == TRUE);
|
||||
}
|
||||
|
||||
for (i = 0; i < G_N_ELEMENTS (files); i++)
|
||||
{
|
||||
if (files[i])
|
||||
g_object_unref (files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_g_file_get_parent_child (void)
|
||||
{
|
||||
const struct TestPathsWithOper paths[] =
|
||||
{
|
||||
/* path root_desc uri dir_holder */
|
||||
{"/dir1/dir", FALSE, FALSE, "dir"},
|
||||
{"/dir", FALSE, FALSE, "dir"},
|
||||
{"/", TRUE, FALSE, "dir"},
|
||||
{"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/", FALSE, FALSE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"},
|
||||
{"file:///dir1/dir", FALSE, TRUE, "dir"},
|
||||
{"file:///dir", FALSE, TRUE, "dir"},
|
||||
{"file:///", TRUE, TRUE, "dir"},
|
||||
{"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", FALSE, TRUE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"},
|
||||
{"dav://www.gtk.org/plan/meetings", FALSE, TRUE, "meetings"},
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < G_N_ELEMENTS (paths); i++)
|
||||
roundtrip_parent_child (paths[i].use_uri, paths[i].equal, paths[i].path1, paths[i].path2);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_type_init ();
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
|
||||
/* Testing whether g_file_new_for_path() or g_file_new_for_uri() always returns non-NULL result */
|
||||
g_test_add_func ("/g-file/test_g_file_new_null", test_g_file_new_null);
|
||||
|
||||
/* Testing whether the g_file_new_for_path() correctly canonicalizes strings and two files equals (g_file_equal()) */
|
||||
g_test_add_func ("/g-file/test_g_file_new_for_path", test_g_file_new_for_path);
|
||||
|
||||
/* Testing whether the g_file_new_for_uri() correctly canonicalizes strings and two files equals (g_file_equal()) */
|
||||
g_test_add_func ("/g-file/test_g_file_new_for_uri", test_g_file_new_for_uri);
|
||||
|
||||
/* Testing g_file_dup() equals original file via g_file_equal() */
|
||||
g_test_add_func ("/g-file/test_g_file_dup", test_g_file_dup);
|
||||
|
||||
/* Testing g_file_get_parse_name() to return correct UTF-8 string */
|
||||
g_test_add_func ("/g-file/test_g_file_get_parse_name_utf8", test_g_file_get_parse_name_utf8);
|
||||
|
||||
/* Testing g_file_new_for_commandline_arg() for correct relavive path resolution and correct path/URI guess */
|
||||
g_test_add_func ("/g-file/test_g_file_new_for_commandline_arg", test_g_file_new_for_commandline_arg);
|
||||
|
||||
/* Testing g_file_contains_file(), g_file_get_relative_path() and g_file_resolve_relative_path() to return and process correct relative paths */
|
||||
g_test_add_func ("/g-file/test_g_file_contains_file", test_g_file_contains_file);
|
||||
|
||||
/* Testing g_file_get_parent() and g_file_get_child() */
|
||||
g_test_add_func ("/g-file/test_g_file_get_parent_child", test_g_file_get_parent_child);
|
||||
|
||||
return g_test_run();
|
||||
}
|
Loading…
Reference in New Issue
Block a user