1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-07-14 22:26:46 +02:00
Files
.gitlab-ci
docs
fuzzing
gio
completion
fam
gdbus-2.0
gvdb
inotify
kqueue
tests
cert-tests
de
desktop-files
gdbus-object-manager-example
modules
schema-tests
services
static-link
thumbnails
x-content
.gitignore
111_digit_test.gresource.xml
actions.c
appinfo-test-actions.desktop
appinfo-test-gnome.desktop.in
appinfo-test-notgnome.desktop.in
appinfo-test-static.desktop
appinfo-test.c
appinfo-test.desktop.in
appinfo-test2.desktop.in
appinfo.c
appmonitor.c
apps.c
async-close-output-stream.c
async-splice-output-stream.c
autoptr.c
basic-application.c
buffered-input-stream.c
buffered-output-stream.c
cancellable.c
codegen.py
contenttype.c
contexts.c
converter-stream.c
credentials.c
data-input-stream.c
data-output-stream.c
dbus-appinfo.c
dbus-launch.c
de.po
defaultvalue.c
desktop-app-info.c
echo-server.c
empty.txt
enums.xml.template
fake-document-portal.c
file.c
fileattributematcher.c
filter-cat.c
filter-streams.c
g-file-info-filesystem-readonly.c
g-file-info.c
g-file.c
g-icon.c
gapplication-example-actions.c
gapplication-example-cmdline.c
gapplication-example-cmdline2.c
gapplication-example-cmdline3.c
gapplication-example-cmdline4.c
gapplication-example-dbushooks.c
gapplication-example-open.c
gapplication.c
gdbus-address-get-session.c
gdbus-addresses.c
gdbus-auth.c
gdbus-bz627724.c
gdbus-close-pending.c
gdbus-connection-flush-helper.c
gdbus-connection-flush.c
gdbus-connection-loss.c
gdbus-connection-slow.c
gdbus-connection.c
gdbus-daemon.c
gdbus-error.c
gdbus-example-export.c
gdbus-example-objectmanager-client.c
gdbus-example-objectmanager-server.c
gdbus-example-own-name.c
gdbus-example-peer.c
gdbus-example-proxy-subclass.c
gdbus-example-server.c
gdbus-example-subtree.c
gdbus-example-unix-fd-client.c
gdbus-example-watch-name.c
gdbus-example-watch-proxy.c
gdbus-exit-on-close.c
gdbus-export.c
gdbus-introspection.c
gdbus-message.c
gdbus-names.c
gdbus-non-socket.c
gdbus-overflow.c
gdbus-peer-object-manager.c
gdbus-peer.c
gdbus-proxy-threads.c
gdbus-proxy-unique-name.c
gdbus-proxy-well-known-name.c
gdbus-proxy.c
gdbus-serialization.c
gdbus-server-auth.c
gdbus-sessionbus.c
gdbus-sessionbus.h
gdbus-test-codegen.c
gdbus-test-fixture.c
gdbus-tests.c
gdbus-tests.h
gdbus-testserver.c
gdbus-threading.c
gen-big-test-resource.py
gengiotypefuncs.py
gio-du.c
giomodule.c
glistmodel.c
gmenumodel.c
gnotification-server.c
gnotification-server.h
gnotification.c
gschema-compile.c
gsettings.c
gsocketclient-slow.c
gsubprocess-testprog.c
gsubprocess.c
gtesttlsbackend.c
gtesttlsbackend.h
gtlsconsoleinteraction.c
gtlsconsoleinteraction.h
httpd.c
inet-address.c
io-stream.c
live-g-file.c
live-g-file.txt
memory-input-stream.c
memory-monitor-dbus.py.in
memory-monitor-portal.py.in
memory-monitor.c
memory-output-stream.c
meson.build
mimeapps.c
mock-resolver.c
mock-resolver.h
mount-operation.c
network-address.c
network-monitor-race.c
network-monitor.c
org.gtk.schemasourcecheck.gschema.xml
org.gtk.test.dbusappinfo.desktop
org.gtk.test.dbusappinfo.flatpak.desktop
org.gtk.test.gschema.override.orig
org.gtk.test.gschema.xml.orig
permission.c
pollable.c
proxy-test.c
proxy.c
readwrite.c
resolver.c
resourceplugin.c
resources.c
send-data.c
simple-async-result.c
simple-proxy.c
sleepy-stream.c
slow-connect-preload.c
socket-address.c
socket-client.c
socket-common.c
socket-listener.c
socket-server.c
socket-service.c
socket.c
srvtarget.c
static-link.py
stream-rw_all.c
taptestrunner.py
task.c
test-codegen.xml
test-io-stream.c
test-io-stream.h
test-pipe-unix.c
test-pipe-unix.h
test.gresource.xml
test1.overlay
test1.txt
test2.gresource.xml
test2.txt
test3.gresource.xml
test3.txt
test4.gresource.xml
test5.gresource.xml
testenum.h
testfilemonitor.c
thumbnail-verification.c
tls-bindings.c
tls-certificate.c
tls-database.c
tls-interaction.c
trash.c
unix-fd.c
unix-mounts.c
unix-streams.c
vfs.c
volumemonitor.c
win32-appinfo.c
win32-streams.c
win32
xdgmime
data-to-c.py
dbus-daemon.xml
gaction.c
gaction.h
gactiongroup.c
gactiongroup.h
gactiongroupexporter.c
gactiongroupexporter.h
gactionmap.c
gactionmap.h
gappinfo.c
gappinfo.h
gappinfoprivate.h
gapplication-tool.c
gapplication.c
gapplication.h
gapplicationcommandline.c
gapplicationcommandline.h
gapplicationimpl-dbus.c
gapplicationimpl.h
gasynchelper.c
gasynchelper.h
gasyncinitable.c
gasyncinitable.h
gasyncresult.c
gasyncresult.h
gbufferedinputstream.c
gbufferedinputstream.h
gbufferedoutputstream.c
gbufferedoutputstream.h
gbytesicon.c
gbytesicon.h
gcancellable.c
gcancellable.h
gcharsetconverter.c
gcharsetconverter.h
gcocoanotificationbackend.m
gcontenttype-win32.c
gcontenttype.c
gcontenttype.h
gcontenttypeprivate.h
gcontextspecificgroup.c
gcontextspecificgroup.h
gconverter.c
gconverter.h
gconverterinputstream.c
gconverterinputstream.h
gconverteroutputstream.c
gconverteroutputstream.h
gcredentials.c
gcredentials.h
gcredentialsprivate.h
gdatagrambased.c
gdatagrambased.h
gdatainputstream.c
gdatainputstream.h
gdataoutputstream.c
gdataoutputstream.h
gdbus-tool.c
gdbusactiongroup-private.h
gdbusactiongroup.c
gdbusactiongroup.h
gdbusaddress.c
gdbusaddress.h
gdbusauth.c
gdbusauth.h
gdbusauthmechanism.c
gdbusauthmechanism.h
gdbusauthmechanismanon.c
gdbusauthmechanismanon.h
gdbusauthmechanismexternal.c
gdbusauthmechanismexternal.h
gdbusauthmechanismsha1.c
gdbusauthmechanismsha1.h
gdbusauthobserver.c
gdbusauthobserver.h
gdbusconnection.c
gdbusconnection.h
gdbusdaemon.c
gdbusdaemon.h
gdbuserror.c
gdbuserror.h
gdbusinterface.c
gdbusinterface.h
gdbusinterfaceskeleton.c
gdbusinterfaceskeleton.h
gdbusintrospection.c
gdbusintrospection.h
gdbusmenumodel.c
gdbusmenumodel.h
gdbusmessage.c
gdbusmessage.h
gdbusmethodinvocation.c
gdbusmethodinvocation.h
gdbusnameowning.c
gdbusnameowning.h
gdbusnamewatching.c
gdbusnamewatching.h
gdbusobject.c
gdbusobject.h
gdbusobjectmanager.c
gdbusobjectmanager.h
gdbusobjectmanagerclient.c
gdbusobjectmanagerclient.h
gdbusobjectmanagerserver.c
gdbusobjectmanagerserver.h
gdbusobjectproxy.c
gdbusobjectproxy.h
gdbusobjectskeleton.c
gdbusobjectskeleton.h
gdbusprivate.c
gdbusprivate.h
gdbusproxy.c
gdbusproxy.h
gdbusserver.c
gdbusserver.h
gdbusutils.c
gdbusutils.h
gdelayedsettingsbackend.c
gdelayedsettingsbackend.h
gdesktopappinfo.c
gdesktopappinfo.h
gdocumentportal.c
gdocumentportal.h
gdrive.c
gdrive.h
gdtlsclientconnection.c
gdtlsclientconnection.h
gdtlsconnection.c
gdtlsconnection.h
gdtlsserverconnection.c
gdtlsserverconnection.h
gdummyfile.c
gdummyfile.h
gdummyproxyresolver.c
gdummyproxyresolver.h
gdummytlsbackend.c
gdummytlsbackend.h
gemblem.c
gemblem.h
gemblemedicon.c
gemblemedicon.h
gfdonotificationbackend.c
gfile.c
gfile.h
gfileattribute-priv.h
gfileattribute.c
gfileattribute.h
gfiledescriptorbased.c
gfiledescriptorbased.h
gfileenumerator.c
gfileenumerator.h
gfileicon.c
gfileicon.h
gfileinfo-priv.h
gfileinfo.c
gfileinfo.h
gfileinputstream.c
gfileinputstream.h
gfileiostream.c
gfileiostream.h
gfilemonitor.c
gfilemonitor.h
gfilenamecompleter.c
gfilenamecompleter.h
gfileoutputstream.c
gfileoutputstream.h
gfilterinputstream.c
gfilterinputstream.h
gfilteroutputstream.c
gfilteroutputstream.h
ggtknotificationbackend.c
ghttpproxy.c
ghttpproxy.h
gicon.c
gicon.h
ginetaddress.c
ginetaddress.h
ginetaddressmask.c
ginetaddressmask.h
ginetsocketaddress.c
ginetsocketaddress.h
ginitable.c
ginitable.h
ginputstream.c
ginputstream.h
gio-autocleanups.h
gio-querymodules-wrapper.py
gio-querymodules.c
gio-tool-cat.c
gio-tool-copy.c
gio-tool-info.c
gio-tool-launch.c
gio-tool-list.c
gio-tool-mime.c
gio-tool-mkdir.c
gio-tool-monitor.c
gio-tool-mount.c
gio-tool-move.c
gio-tool-open.c
gio-tool-remove.c
gio-tool-rename.c
gio-tool-save.c
gio-tool-set.c
gio-tool-trash.c
gio-tool-tree.c
gio-tool.c
gio-tool.h
gio.h
gio.rc.in
gio.stp.in
gio_probes.d
gio_trace.h
gioenums.h
gioenumtypes.c.template
gioenumtypes.h.template
gioerror.c
gioerror.h
giomodule-priv.c
giomodule-priv.h
giomodule.c
giomodule.h
gioprivate.h
gioscheduler.c
gioscheduler.h
giostream.c
giostream.h
giotypes.h
giowin32-priv.h
giowin32-private.c
gkeyfilesettingsbackend.c
glib-compile-resources.c
glib-compile-schemas.c
glistmodel.c
glistmodel.h
gliststore.c
gliststore.h
gloadableicon.c
gloadableicon.h
glocalfile.c
glocalfile.h
glocalfileenumerator.c
glocalfileenumerator.h
glocalfileinfo.c
glocalfileinfo.h
glocalfileinputstream.c
glocalfileinputstream.h
glocalfileiostream.c
glocalfileiostream.h
glocalfilemonitor.c
glocalfilemonitor.h
glocalfileoutputstream.c
glocalfileoutputstream.h
glocalvfs.c
glocalvfs.h
gmarshal-internal.c
gmarshal-internal.h
gmarshal-internal.list
gmemoryinputstream.c
gmemoryinputstream.h
gmemorymonitor.c
gmemorymonitor.h
gmemorymonitordbus.c
gmemorymonitordbus.h
gmemorymonitorportal.c
gmemorymonitorportal.h
gmemoryoutputstream.c
gmemoryoutputstream.h
gmemorysettingsbackend.c
gmenu.c
gmenu.h
gmenuexporter.c
gmenuexporter.h
gmenumodel.c
gmenumodel.h
gmount.c
gmount.h
gmountoperation.c
gmountoperation.h
gmountprivate.h
gnativesocketaddress.c
gnativesocketaddress.h
gnativevolumemonitor.c
gnativevolumemonitor.h
gnetworkaddress.c
gnetworkaddress.h
gnetworking.c
gnetworking.h.in
gnetworkingprivate.h
gnetworkmonitor.c
gnetworkmonitor.h
gnetworkmonitorbase.c
gnetworkmonitorbase.h
gnetworkmonitornetlink.c
gnetworkmonitornetlink.h
gnetworkmonitornm.c
gnetworkmonitornm.h
gnetworkmonitorportal.c
gnetworkmonitorportal.h
gnetworkservice.c
gnetworkservice.h
gnextstepsettingsbackend.m
gnotification-private.h
gnotification.c
gnotification.h
gnotificationbackend.c
gnotificationbackend.h
gnullsettingsbackend.c
gopenuriportal.c
gopenuriportal.h
gosxappinfo.h
gosxappinfo.m
gosxcontenttype.m
goutputstream.c
goutputstream.h
gpermission.c
gpermission.h
gpollableinputstream.c
gpollableinputstream.h
gpollableoutputstream.c
gpollableoutputstream.h
gpollableutils.c
gpollableutils.h
gpollfilemonitor.c
gpollfilemonitor.h
gportalnotificationbackend.c
gportalsupport.c
gportalsupport.h
gpropertyaction.c
gpropertyaction.h
gproxy.c
gproxy.h
gproxyaddress.c
gproxyaddress.h
gproxyaddressenumerator.c
gproxyaddressenumerator.h
gproxyresolver.c
gproxyresolver.h
gproxyresolverportal.c
gproxyresolverportal.h
gregistrysettingsbackend.c
gregistrysettingsbackend.h
gremoteactiongroup.c
gremoteactiongroup.h
gresolver.c
gresolver.h
gresource-tool.c
gresource.c
gresource.h
gresourcefile.c
gresourcefile.h
gschema.dtd
gschema.its
gschema.loc
gseekable.c
gseekable.h
gsettings-mapping.c
gsettings-mapping.h
gsettings-tool.c
gsettings.c
gsettings.h
gsettingsbackend.c
gsettingsbackend.h
gsettingsbackendinternal.h
gsettingsschema-internal.h
gsettingsschema.c
gsettingsschema.h
gsimpleaction.c
gsimpleaction.h
gsimpleactiongroup.c
gsimpleactiongroup.h
gsimpleasyncresult.c
gsimpleasyncresult.h
gsimpleiostream.c
gsimpleiostream.h
gsimplepermission.c
gsimplepermission.h
gsimpleproxyresolver.c
gsimpleproxyresolver.h
gsocket.c
gsocket.h
gsocketaddress.c
gsocketaddress.h
gsocketaddressenumerator.c
gsocketaddressenumerator.h
gsocketclient.c
gsocketclient.h
gsocketconnectable.c
gsocketconnectable.h
gsocketconnection.c
gsocketconnection.h
gsocketcontrolmessage.c
gsocketcontrolmessage.h
gsocketinputstream.c
gsocketinputstream.h
gsocketlistener.c
gsocketlistener.h
gsocketoutputstream.c
gsocketoutputstream.h
gsocketservice.c
gsocketservice.h
gsocks4aproxy.c
gsocks4aproxy.h
gsocks4proxy.c
gsocks4proxy.h
gsocks5proxy.c
gsocks5proxy.h
gsrvtarget.c
gsrvtarget.h
gsubprocess.c
gsubprocess.h
gsubprocesslauncher-private.h
gsubprocesslauncher.c
gsubprocesslauncher.h
gtask.c
gtask.h
gtcpconnection.c
gtcpconnection.h
gtcpwrapperconnection.c
gtcpwrapperconnection.h
gtestdbus.c
gtestdbus.h
gthemedicon.c
gthemedicon.h
gthreadedresolver.c
gthreadedresolver.h
gthreadedsocketservice.c
gthreadedsocketservice.h
gtlsbackend.c
gtlsbackend.h
gtlscertificate.c
gtlscertificate.h
gtlsclientconnection.c
gtlsclientconnection.h
gtlsconnection.c
gtlsconnection.h
gtlsdatabase.c
gtlsdatabase.h
gtlsfiledatabase.c
gtlsfiledatabase.h
gtlsinteraction.c
gtlsinteraction.h
gtlspassword.c
gtlspassword.h
gtlsserverconnection.c
gtlsserverconnection.h
gtrashportal.c
gtrashportal.h
gunionvolumemonitor.c
gunionvolumemonitor.h
gunixconnection.c
gunixconnection.h
gunixcredentialsmessage.c
gunixcredentialsmessage.h
gunixfdlist.c
gunixfdlist.h
gunixfdmessage.c
gunixfdmessage.h
gunixinputstream.c
gunixinputstream.h
gunixmount.c
gunixmount.h
gunixmounts.c
gunixmounts.h
gunixoutputstream.c
gunixoutputstream.h
gunixsocketaddress.c
gunixsocketaddress.h
gunixvolume.c
gunixvolume.h
gunixvolumemonitor.c
gunixvolumemonitor.h
gvfs.c
gvfs.h
gvolume.c
gvolume.h
gvolumemonitor.c
gvolumemonitor.h
gwin32api-application-activation-manager.h
gwin32api-iterator.h
gwin32api-misc.h
gwin32api-package.h
gwin32api-storage.h
gwin32appinfo.c
gwin32appinfo.h
gwin32file-sync-stream.c
gwin32file-sync-stream.h
gwin32inputstream.c
gwin32inputstream.h
gwin32mount.c
gwin32mount.h
gwin32networkmonitor.c
gwin32networkmonitor.h
gwin32notificationbackend.c
gwin32outputstream.c
gwin32outputstream.h
gwin32packageparser.c
gwin32packageparser.h
gwin32registrykey.c
gwin32registrykey.h
gwin32volumemonitor.c
gwin32volumemonitor.h
gzlibcompressor.c
gzlibcompressor.h
gzlibdecompressor.c
gzlibdecompressor.h
meson.build
org.freedesktop.portal.Documents.xml
org.freedesktop.portal.OpenURI.xml
org.freedesktop.portal.ProxyResolver.xml
org.freedesktop.portal.Trash.xml
strinfo.c
thumbnail-verify.c
thumbnail-verify.h
glib
gmodule
gobject
gthread
m4macros
po
subprojects
tests
.clang-format
.dir-locals.el
.gitattributes
.gitignore
.gitlab-ci.yml
AUTHORS
CONTRIBUTING.md
COPYING
HACKING
INSTALL.in
NEWS
NEWS.pre-1-3
README
README.md
README.rationale
README.win32
README.win32.md
check-abis.sh
clang-format-diff.py
glib-gettextize.in
glib.doap
glib.supp
meson.build
meson_options.txt
msvc_recommended_pragmas.h
template-tap.test.in
template.test.in
glib/gio/tests/resources.c

1073 lines
32 KiB
C
Raw Normal View History

2011-12-21 21:45:16 +01:00
/* GLib testing framework examples and tests
*
* Copyright (C) 2011 Red Hat, Inc.
*
* 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.
2011-12-21 21:45:16 +01:00
*
* 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
2014-01-23 12:58:29 +01:00
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
2011-12-21 21:45:16 +01:00
*/
2012-08-19 02:25:37 -04:00
#include <string.h>
2011-12-21 21:45:16 +01:00
#include <gio/gio.h>
#include <glibconfig.h>
#include "gconstructor.h"
2011-12-21 21:45:16 +01:00
#include "test_resources2.h"
2019-02-20 10:38:29 +00:00
#include "digit_test_resources.h"
2011-12-21 21:45:16 +01:00
#ifdef _MSC_VER
# define MODULE_FILENAME_PREFIX ""
#else
# define MODULE_FILENAME_PREFIX "lib"
#endif
2011-12-21 21:45:16 +01:00
static void
test_resource (GResource *resource)
{
GError *error = NULL;
gboolean found, success;
gsize size;
guint32 flags;
GBytes *data;
char **children;
GInputStream *in;
char buffer[128];
const gchar *not_found_paths[] =
{
"/not/there",
"/",
"",
};
gsize i;
2011-12-21 21:45:16 +01:00
for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
{
found = g_resource_get_info (resource,
not_found_paths[i],
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
g_assert_false (found);
}
2011-12-21 21:45:16 +01:00
found = g_resource_get_info (resource,
"/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
2011-12-21 21:45:16 +01:00
found = g_resource_get_info (resource,
"/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_true (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 0);
g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
2011-12-21 21:45:16 +01:00
found = g_resource_get_info (resource,
"/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, 0);
2011-12-21 21:45:16 +01:00
found = g_resource_get_info (resource,
"/a_prefix/test2-alias.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, 0);
2011-12-21 21:45:16 +01:00
for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
{
data = g_resource_lookup_data (resource,
not_found_paths[i],
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
g_assert_null (data);
}
2011-12-21 21:45:16 +01:00
data = g_resource_lookup_data (resource,
"/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_assert_no_error (error);
g_bytes_unref (data);
data = g_resource_lookup_data (resource,
"/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_cmpuint (g_bytes_get_size (data), ==, 0);
g_assert_no_error (error);
g_bytes_unref (data);
for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
{
in = g_resource_open_stream (resource,
not_found_paths[i],
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
g_assert_null (in);
}
2011-12-21 21:45:16 +01:00
in = g_resource_open_stream (resource,
"/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (in != NULL);
g_assert_no_error (error);
success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
&size,
NULL, &error);
g_assert (success);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
buffer[size] = 0;
g_assert_cmpstr (buffer, ==, "test1\n");
g_input_stream_close (in, NULL, &error);
g_assert_no_error (error);
g_clear_object (&in);
2011-12-21 21:45:16 +01:00
in = g_resource_open_stream (resource,
"/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert_nonnull (in);
success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
&size,
NULL, &error);
g_assert_no_error (error);
g_assert_true (success);
g_assert_cmpint (size, ==, 0);
g_input_stream_close (in, NULL, &error);
g_assert_no_error (error);
g_clear_object (&in);
2011-12-21 21:45:16 +01:00
data = g_resource_lookup_data (resource,
"/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
g_bytes_unref (data);
data = g_resource_lookup_data (resource,
"/a_prefix/test2-alias.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
g_bytes_unref (data);
for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
{
if (g_str_equal (not_found_paths[i], "/"))
continue;
children = g_resource_enumerate_children (resource,
not_found_paths[i],
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
g_assert_null (children);
}
2011-12-21 21:45:16 +01:00
children = g_resource_enumerate_children (resource,
"/a_prefix",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children != NULL);
g_assert_no_error (error);
g_assert_cmpint (g_strv_length (children), ==, 2);
2011-12-21 21:45:16 +01:00
g_strfreev (children);
/* Test the preferred lookup where we have a trailing slash. */
children = g_resource_enumerate_children (resource,
"/a_prefix/",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children != NULL);
g_assert_no_error (error);
g_assert_cmpint (g_strv_length (children), ==, 2);
g_strfreev (children);
/* test with a path > 256 and no trailing slash to test the
* slow path of resources where we allocate a modified path.
*/
children = g_resource_enumerate_children (resource,
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/with/no/trailing/slash",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children == NULL);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
2011-12-21 21:45:16 +01:00
}
static void
test_resource_file (void)
{
GResource *resource;
GError *error = NULL;
resource = g_resource_load ("not-there", &error);
g_assert (resource == NULL);
g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
g_clear_error (&error);
resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
2011-12-21 21:45:16 +01:00
g_assert (resource != NULL);
g_assert_no_error (error);
test_resource (resource);
g_resource_unref (resource);
}
static void
test_resource_file_path (void)
{
static const struct {
const gchar *input;
const gchar *expected;
} test_uris[] = {
{ "resource://", "resource:///" },
{ "resource:///", "resource:///" },
{ "resource://////", "resource:///" },
{ "resource:///../../../", "resource:///" },
{ "resource:///../../..", "resource:///" },
{ "resource://abc", "resource:///abc" },
{ "resource:///abc/", "resource:///abc" },
{ "resource:/a/b/../c/", "resource:///a/c" },
{ "resource://../a/b/../c/../", "resource:///a" },
{ "resource://a/b/cc//bb//a///", "resource:///a/b/cc/bb/a" },
{ "resource://././././", "resource:///" },
{ "resource://././././../", "resource:///" },
{ "resource://a/b/c/d.png", "resource:///a/b/c/d.png" },
{ "resource://a/b/c/..png", "resource:///a/b/c/..png" },
{ "resource://a/b/c/./png", "resource:///a/b/c/png" },
};
guint i;
for (i = 0; i < G_N_ELEMENTS (test_uris); i++)
{
GFile *file;
gchar *uri;
file = g_file_new_for_uri (test_uris[i].input);
g_assert (file != NULL);
uri = g_file_get_uri (file);
g_assert (uri != NULL);
g_assert_cmpstr (uri, ==, test_uris[i].expected);
g_object_unref (file);
g_free (uri);
}
}
2011-12-21 21:45:16 +01:00
static void
test_resource_data (void)
{
GResource *resource;
GError *error = NULL;
gboolean loaded_file;
char *content;
gsize content_size;
GBytes *data;
loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
&content, &content_size, NULL);
2011-12-21 21:45:16 +01:00
g_assert (loaded_file);
data = g_bytes_new_take (content, content_size);
resource = g_resource_new_from_data (data, &error);
g_bytes_unref (data);
2011-12-21 21:45:16 +01:00
g_assert (resource != NULL);
g_assert_no_error (error);
test_resource (resource);
g_resource_unref (resource);
}
static void
test_resource_data_unaligned (void)
{
GResource *resource;
GError *error = NULL;
gboolean loaded_file;
char *content, *content_copy;
gsize content_size;
GBytes *data;
loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
&content, &content_size, NULL);
g_assert (loaded_file);
content_copy = g_new (char, content_size + 1);
memcpy (content_copy + 1, content, content_size);
data = g_bytes_new_with_free_func (content_copy + 1, content_size,
(GDestroyNotify) g_free, content_copy);
g_free (content);
resource = g_resource_new_from_data (data, &error);
g_bytes_unref (data);
g_assert (resource != NULL);
g_assert_no_error (error);
test_resource (resource);
g_resource_unref (resource);
}
/* Test error handling for corrupt GResource files (specifically, a corrupt
* GVDB header). */
static void
test_resource_data_corrupt (void)
{
/* A GVDB header is 6 guint32s, and requires a magic number in the first two
* guint32s. A set of zero bytes of a greater length is considered corrupt. */
static const guint8 data[sizeof (guint32) * 7] = { 0, };
GBytes *bytes = NULL;
GResource *resource = NULL;
GError *local_error = NULL;
bytes = g_bytes_new_static (data, sizeof (data));
resource = g_resource_new_from_data (bytes, &local_error);
g_bytes_unref (bytes);
g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL);
g_assert_null (resource);
g_clear_error (&local_error);
}
/* Test handling for empty GResource files. They should also be treated as
* corrupt. */
static void
test_resource_data_empty (void)
{
GBytes *bytes = NULL;
GResource *resource = NULL;
GError *local_error = NULL;
bytes = g_bytes_new_static (NULL, 0);
resource = g_resource_new_from_data (bytes, &local_error);
g_bytes_unref (bytes);
g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL);
g_assert_null (resource);
g_clear_error (&local_error);
}
2011-12-21 21:45:16 +01:00
static void
2012-06-04 06:00:45 -04:00
test_resource_registered (void)
2011-12-21 21:45:16 +01:00
{
GResource *resource;
GError *error = NULL;
gboolean found, success;
gsize size;
guint32 flags;
GBytes *data;
char **children;
GInputStream *in;
char buffer[128];
resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
2011-12-21 21:45:16 +01:00
g_assert (resource != NULL);
g_assert_no_error (error);
found = g_resources_get_info ("/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (!found);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
g_resources_register (resource);
found = g_resources_get_info ("/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
found = g_resources_get_info ("/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_no_error (error);
g_assert_true (found);
g_assert_cmpint (size, ==, 0);
g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
2011-12-21 21:45:16 +01:00
found = g_resources_get_info ("/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpint (flags, ==, 0);
2011-12-21 21:45:16 +01:00
found = g_resources_get_info ("/a_prefix/test2-alias.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, 0);
2011-12-21 21:45:16 +01:00
data = g_resources_lookup_data ("/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_assert_no_error (error);
g_bytes_unref (data);
in = g_resources_open_stream ("/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (in != NULL);
g_assert_no_error (error);
success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
&size,
NULL, &error);
g_assert (success);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
buffer[size] = 0;
g_assert_cmpstr (buffer, ==, "test1\n");
g_input_stream_close (in, NULL, &error);
g_assert_no_error (error);
g_clear_object (&in);
2011-12-21 21:45:16 +01:00
data = g_resources_lookup_data ("/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert_cmpuint (g_bytes_get_size (data), ==, 0);
g_bytes_unref (data);
in = g_resources_open_stream ("/empty.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_no_error (error);
g_assert_nonnull (in);
success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
&size,
NULL, &error);
g_assert_no_error (error);
g_assert_true (success);
g_assert_cmpint (size, ==, 0);
g_input_stream_close (in, NULL, &error);
g_assert_no_error (error);
g_clear_object (&in);
2011-12-21 21:45:16 +01:00
data = g_resources_lookup_data ("/a_prefix/test2.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
g_bytes_unref (data);
data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
g_bytes_unref (data);
children = g_resources_enumerate_children ("/not/here",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children == NULL);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
children = g_resources_enumerate_children ("/a_prefix",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children != NULL);
g_assert_no_error (error);
g_assert_cmpint (g_strv_length (children), ==, 2);
g_strfreev (children);
2011-12-21 21:45:16 +01:00
g_resources_unregister (resource);
g_resource_unref (resource);
2011-12-21 21:45:16 +01:00
found = g_resources_get_info ("/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (!found);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
}
static void
test_resource_automatic (void)
{
GError *error = NULL;
gboolean found;
gsize size;
guint32 flags;
GBytes *data;
found = g_resources_get_info ("/auto_loaded/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpint (flags, ==, 0);
2011-12-21 21:45:16 +01:00
data = g_resources_lookup_data ("/auto_loaded/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_bytes_unref (data);
}
static void
test_resource_manual (void)
{
GError *error = NULL;
gboolean found;
gsize size;
guint32 flags;
GBytes *data;
found = g_resources_get_info ("/manual_loaded/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, 0);
2011-12-21 21:45:16 +01:00
data = g_resources_lookup_data ("/manual_loaded/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_bytes_unref (data);
}
2012-08-19 02:25:37 -04:00
static void
test_resource_manual2 (void)
{
GResource *resource;
GBytes *data;
gsize size;
GError *error = NULL;
resource = _g_test2_get_resource ();
data = g_resource_lookup_data (resource,
"/manual_loaded/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_bytes_unref (data);
g_resource_unref (resource);
}
/* Test building resources with external data option,
* where data is linked in as binary instead of compiled in.
* Checks if resources are automatically registered and
* data can be found and read. */
static void
test_resource_binary_linked (void)
{
#ifndef __linux__
g_test_skip ("--external-data test only works on Linux");
return;
#else /* if __linux__ */
GError *error = NULL;
gboolean found;
gsize size;
guint32 flags;
GBytes *data;
found = g_resources_get_info ("/binary_linked/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_true (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, 0);
data = g_resources_lookup_data ("/binary_linked/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_nonnull (data);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_bytes_unref (data);
#endif /* if __linux__ */
}
2019-02-20 10:38:29 +00:00
/* Test resource whose xml file starts with more than one digit
* and where no explicit c-name is given
* Checks if resources are successfully registered and
2019-02-20 10:38:29 +00:00
* data can be found and read. */
static void
test_resource_digits (void)
{
GError *error = NULL;
gboolean found;
gsize size;
guint32 flags;
GBytes *data;
found = g_resources_get_info ("/digit_test/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_true (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, 0);
data = g_resources_lookup_data ("/digit_test/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_nonnull (data);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_bytes_unref (data);
}
2011-12-21 21:45:16 +01:00
static void
test_resource_module (void)
{
GIOModule *module;
gboolean found;
gsize size;
guint32 flags;
GBytes *data;
GError *error;
#ifdef GLIB_STATIC_COMPILATION
/* The resource module is statically linked with a separate copy
* of a GLib so g_static_resource_init won't work as expected. */
g_test_skip ("Resource modules aren't supported in static builds.");
return;
#endif
2011-12-21 21:45:16 +01:00
if (g_module_supported ())
{
module = g_io_module_new (g_test_get_filename (G_TEST_BUILT,
MODULE_FILENAME_PREFIX "resourceplugin",
NULL));
2011-12-21 21:45:16 +01:00
error = NULL;
found = g_resources_get_info ("/resourceplugin/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (!found);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
g_type_module_use (G_TYPE_MODULE (module));
found = g_resources_get_info ("/resourceplugin/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert_no_error (error);
g_assert_cmpint (size, ==, 6);
g_assert_cmpuint (flags, ==, 0);
2011-12-21 21:45:16 +01:00
data = g_resources_lookup_data ("/resourceplugin/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, 6);
2011-12-21 21:45:16 +01:00
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
g_bytes_unref (data);
g_type_module_unuse (G_TYPE_MODULE (module));
found = g_resources_get_info ("/resourceplugin/test1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (!found);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
}
}
2011-12-21 23:37:56 +01:00
static void
test_uri_query_info (void)
{
GResource *resource;
GError *error = NULL;
gboolean loaded_file;
char *content;
gsize content_size;
GBytes *data;
GFile *file;
GFileInfo *info;
const char *content_type;
gchar *mime_type = NULL;
2015-08-21 00:08:57 -04:00
const char *fs_type;
gboolean readonly;
2011-12-21 23:37:56 +01:00
loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
&content, &content_size, NULL);
2011-12-21 23:37:56 +01:00
g_assert (loaded_file);
data = g_bytes_new_take (content, content_size);
resource = g_resource_new_from_data (data, &error);
g_bytes_unref (data);
2011-12-21 23:37:56 +01:00
g_assert (resource != NULL);
g_assert_no_error (error);
g_resources_register (resource);
file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
info = g_file_query_info (file, "*", 0, NULL, &error);
g_assert_no_error (error);
content_type = g_file_info_get_content_type (info);
g_assert (content_type);
mime_type = g_content_type_get_mime_type (content_type);
g_assert (mime_type);
g_assert_cmpstr (mime_type, ==, "text/plain");
g_free (mime_type);
2011-12-21 23:37:56 +01:00
g_object_unref (info);
2015-08-21 00:08:57 -04:00
info = g_file_query_filesystem_info (file, "*", NULL, &error);
g_assert_no_error (error);
fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
g_assert_cmpstr (fs_type, ==, "resource");
readonly = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY);
g_assert_true (readonly);
g_object_unref (info);
2013-12-24 00:06:00 -05:00
g_assert_cmpuint (g_file_hash (file), !=, 0);
2012-06-04 06:00:45 -04:00
g_object_unref (file);
g_resources_unregister (resource);
g_resource_unref (resource);
}
static void
2012-06-04 06:00:45 -04:00
test_uri_file (void)
{
GResource *resource;
GError *error = NULL;
gboolean loaded_file;
char *content;
gsize content_size;
GBytes *data;
GFile *file;
GFileInfo *info;
gchar *name;
GFile *file2, *parent;
GFileEnumerator *enumerator;
gchar *scheme;
GFileAttributeInfoList *attrs;
2012-08-19 02:25:37 -04:00
GInputStream *stream;
gchar buf[1024];
gboolean ret;
gssize skipped;
2012-06-04 06:00:45 -04:00
loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
&content, &content_size, NULL);
2012-06-04 06:00:45 -04:00
g_assert (loaded_file);
data = g_bytes_new_take (content, content_size);
resource = g_resource_new_from_data (data, &error);
g_bytes_unref (data);
g_assert (resource != NULL);
g_assert_no_error (error);
g_resources_register (resource);
file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
g_assert (g_file_get_path (file) == NULL);
name = g_file_get_parse_name (file);
g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
g_free (name);
name = g_file_get_uri (file);
g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
g_free (name);
g_assert (!g_file_is_native (file));
g_assert (!g_file_has_uri_scheme (file, "http"));
g_assert (g_file_has_uri_scheme (file, "resource"));
scheme = g_file_get_uri_scheme (file);
g_assert_cmpstr (scheme, ==, "resource");
g_free (scheme);
file2 = g_file_dup (file);
g_assert (g_file_equal (file, file2));
g_object_unref (file2);
parent = g_file_get_parent (file);
enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
g_assert_no_error (error);
file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
g_assert_no_error (error);
g_assert (g_file_equal (file, file2));
g_object_unref (file2);
info = g_file_enumerator_next_file (enumerator, NULL, &error);
g_assert_no_error (error);
g_assert (info != NULL);
g_object_unref (info);
info = g_file_enumerator_next_file (enumerator, NULL, &error);
g_assert_no_error (error);
g_assert (info != NULL);
g_object_unref (info);
info = g_file_enumerator_next_file (enumerator, NULL, &error);
g_assert_no_error (error);
g_assert (info == NULL);
g_file_enumerator_close (enumerator, NULL, &error);
g_assert_no_error (error);
g_object_unref (enumerator);
file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
g_assert (g_file_equal (file, file2));
g_assert (g_file_has_prefix (file, parent));
name = g_file_get_relative_path (parent, file);
g_assert_cmpstr (name, ==, "test2-alias.txt");
g_free (name);
g_object_unref (parent);
attrs = g_file_query_settable_attributes (file, NULL, &error);
g_assert_no_error (error);
g_file_attribute_info_list_unref (attrs);
attrs = g_file_query_writable_namespaces (file, NULL, &error);
g_assert_no_error (error);
g_file_attribute_info_list_unref (attrs);
2012-08-19 02:25:37 -04:00
stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
g_assert_no_error (error);
g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
g_assert (ret);
g_assert_no_error (error);
skipped = g_input_stream_skip (stream, 1, NULL, &error);
g_assert_cmpint (skipped, ==, 1);
g_assert_no_error (error);
memset (buf, 0, 1024);
ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
g_assert (ret);
g_assert_no_error (error);
g_assert_cmpstr (buf, ==, "st2\n");
info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
G_FILE_ATTRIBUTE_STANDARD_SIZE,
NULL,
&error);
g_assert_no_error (error);
g_assert (info != NULL);
g_assert_cmpint (g_file_info_get_size (info), ==, 6);
g_object_unref (info);
ret = g_input_stream_close (stream, NULL, &error);
g_assert (ret);
g_assert_no_error (error);
g_object_unref (stream);
2012-06-04 06:00:45 -04:00
g_object_unref (file);
2012-08-19 02:25:37 -04:00
g_object_unref (file2);
2012-06-04 06:00:45 -04:00
2011-12-21 23:37:56 +01:00
g_resources_unregister (resource);
g_resource_unref (resource);
}
2011-12-21 21:45:16 +01:00
static void
test_resource_64k (void)
{
GError *error = NULL;
gboolean found;
gsize size;
guint32 flags;
GBytes *data;
gchar **tokens;
found = g_resources_get_info ("/big_prefix/gresource-big-test.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert_true (found);
g_assert_no_error (error);
/* Check size: 100 of all lower case letters + newline char +
* 100 all upper case letters + newline char +
* 100 of all numbers between 0 to 9 + newline char
* (for 12 iterations)
*/
g_assert_cmpint (size, ==, (26 + 26 + 10) * (100 + 1) * 12);
g_assert_cmpuint (flags, ==, 0);
data = g_resources_lookup_data ("/big_prefix/gresource-big-test.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert_nonnull (data);
g_assert_no_error (error);
size = g_bytes_get_size (data);
g_assert_cmpint (size, ==, (26 + 26 + 10) * (100 + 1) * 12);
tokens = g_strsplit ((const gchar *) g_bytes_get_data (data, NULL), "\n", -1);
/* check tokens[x] == entry at gresource-big-test.txt's line, where x = line - 1 */
g_assert_cmpstr (tokens[0], ==, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
g_assert_cmpstr (tokens[27], ==, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
g_assert_cmpstr (tokens[183], ==, "7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
g_assert_cmpstr (tokens[600], ==, "QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
g_assert_cmpstr (tokens[742], ==, "8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888");
g_strfreev (tokens);
g_bytes_unref (data);
}
/* Check that g_resources_get_info() respects G_RESOURCE_OVERLAYS */
static void
test_overlay (void)
{
if (g_test_subprocess ())
{
GError *error = NULL;
gboolean res;
gsize size;
char *overlay;
char *path;
path = g_test_build_filename (G_TEST_DIST, "test1.overlay", NULL);
overlay = g_strconcat ("/auto_loaded/test1.txt=", path, NULL);
g_setenv ("G_RESOURCE_OVERLAYS", overlay, TRUE);
res = g_resources_get_info ("/auto_loaded/test1.txt", 0, &size, NULL, &error);
g_assert_true (res);
g_assert_no_error (error);
/* test1.txt is 6 bytes, test1.overlay is 23 */
g_assert_cmpint (size, ==, 23);
g_free (overlay);
g_free (path);
return;
}
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDERR);
g_test_trap_assert_passed ();
}
2011-12-21 21:45:16 +01:00
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
_g_test2_register_resource ();
2019-02-20 10:38:29 +00:00
_digit_test_register_resource ();
2011-12-21 21:45:16 +01:00
g_test_add_func ("/resource/file", test_resource_file);
g_test_add_func ("/resource/file-path", test_resource_file_path);
2011-12-21 21:45:16 +01:00
g_test_add_func ("/resource/data", test_resource_data);
g_test_add_func ("/resource/data_unaligned", test_resource_data_unaligned);
g_test_add_func ("/resource/data-corrupt", test_resource_data_corrupt);
g_test_add_func ("/resource/data-empty", test_resource_data_empty);
2012-06-04 06:00:45 -04:00
g_test_add_func ("/resource/registered", test_resource_registered);
2011-12-21 21:45:16 +01:00
g_test_add_func ("/resource/manual", test_resource_manual);
2012-08-19 02:25:37 -04:00
g_test_add_func ("/resource/manual2", test_resource_manual2);
2011-12-21 21:45:16 +01:00
#ifdef G_HAS_CONSTRUCTORS
g_test_add_func ("/resource/automatic", test_resource_automatic);
/* This only uses automatic resources too, so it tests the constructors and destructors */
g_test_add_func ("/resource/module", test_resource_module);
g_test_add_func ("/resource/binary-linked", test_resource_binary_linked);
2011-12-21 21:45:16 +01:00
#endif
2011-12-21 23:37:56 +01:00
g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
2012-06-04 06:00:45 -04:00
g_test_add_func ("/resource/uri/file", test_uri_file);
g_test_add_func ("/resource/64k", test_resource_64k);
g_test_add_func ("/resource/overlay", test_overlay);
2019-02-20 10:38:29 +00:00
g_test_add_func ("/resource/digits", test_resource_digits);
2011-12-21 21:45:16 +01:00
return g_test_run();
}