2017-09-21 22:26:06 +03:00
/*
2017-04-19 02:07:51 +03:00
* Copyright © 2017 Endless Mobile , Inc .
2022-02-17 22:12:18 +03:00
* Copyright ( C ) 2022 Igalia S . L .
2017-04-19 02:07:51 +03:00
*
2018-01-30 22:26:26 +03:00
* SPDX - License - Identifier : LGPL - 2.0 +
*
2017-04-19 02:07:51 +03:00
* 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 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
* License along with this library . If not , see < https : //www.gnu.org/licenses/>.
2017-04-19 02:07:51 +03:00
*
* Authors :
* - Philip Withnall < withnall @ endlessm . com >
*/
# include "config.h"
# include <gio/gio.h>
# include <glib-object.h>
2023-05-01 21:24:29 +03:00
# include <glib.h>
2017-04-19 02:07:51 +03:00
# include <libglnx.h>
# include "test-mock-gio.h"
/**
* SECTION : mock - gio
* @ title : Mock GIO volume interfaces
* @ short_description : Mock implementations of GIO volume , mount and drive
* interfaces
* @ stability : Unstable
* @ include : tests / test - mock - gio . h
*
* A set of classes implementing GIO interfaces for volumes , mounts , drives
* and volume monitoring , which return mock data to the caller when used . These
* are designed for use in unit tests , to mock up removable drives when testing
* code which monitors such drives being added and removed and then queries
* properties of them .
*
* By returning mock drive locations to the caller , for example , the contents of
* a removable drive may be mocked up using temporary files .
*
* Currently , all the mock data returned by these classes to callers is static ,
* set at construction time .
*
* Since : 2017.8
*/
/* Mock volume monitor class. This returns a static set of data to the caller,
* which it was initialised with . */
struct _OstreeMockVolumeMonitor
{
GVolumeMonitor parent_instance ;
GList * mounts ; /* (element-type OstreeMockMount) */
2023-05-01 21:24:29 +03:00
GList * volumes ; /* (element-type OstreeMockVolume) */
2017-04-19 02:07:51 +03:00
} ;
G_DEFINE_TYPE ( OstreeMockVolumeMonitor , ostree_mock_volume_monitor , G_TYPE_VOLUME_MONITOR )
static GList *
ostree_mock_volume_monitor_get_mounts ( GVolumeMonitor * monitor )
{
OstreeMockVolumeMonitor * self = OSTREE_MOCK_VOLUME_MONITOR ( monitor ) ;
2023-05-01 21:24:29 +03:00
return g_list_copy_deep ( self - > mounts , ( GCopyFunc ) g_object_ref , NULL ) ;
2017-04-19 02:07:51 +03:00
}
static GList *
ostree_mock_volume_monitor_get_volumes ( GVolumeMonitor * monitor )
{
OstreeMockVolumeMonitor * self = OSTREE_MOCK_VOLUME_MONITOR ( monitor ) ;
2023-05-01 21:24:29 +03:00
return g_list_copy_deep ( self - > volumes , ( GCopyFunc ) g_object_ref , NULL ) ;
2017-04-19 02:07:51 +03:00
}
static void
ostree_mock_volume_monitor_init ( OstreeMockVolumeMonitor * self )
{
/* Nothing to see here. */
}
static void
ostree_mock_volume_monitor_dispose ( GObject * object )
{
OstreeMockVolumeMonitor * self = OSTREE_MOCK_VOLUME_MONITOR ( object ) ;
g_list_free_full ( self - > volumes , g_object_unref ) ;
self - > volumes = NULL ;
g_list_free_full ( self - > mounts , g_object_unref ) ;
self - > mounts = NULL ;
G_OBJECT_CLASS ( ostree_mock_volume_monitor_parent_class ) - > dispose ( object ) ;
}
static void
ostree_mock_volume_monitor_class_init ( OstreeMockVolumeMonitorClass * klass )
{
GObjectClass * object_class = G_OBJECT_CLASS ( klass ) ;
GVolumeMonitorClass * monitor_class = G_VOLUME_MONITOR_CLASS ( klass ) ;
object_class - > dispose = ostree_mock_volume_monitor_dispose ;
monitor_class - > get_mounts = ostree_mock_volume_monitor_get_mounts ;
monitor_class - > get_volumes = ostree_mock_volume_monitor_get_volumes ;
}
/**
* ostree_mock_volume_monitor_new :
* @ mounts : ( element - type GMount ) ( transfer none ) : list of current # GMounts
* @ volumes : ( element - type GVolume ) ( transfer none ) : list of current # GVolumes
*
* Create a new mock # GVolumeMonitor which will return the given static lists of
* # GMounts and # GVolumes to any caller of g_volume_monitor_get_mounts ( ) or
* g_volume_monitor_get_volumes ( ) .
*
* Typically , the elements of @ mounts will be # OstreeMockMount objects and the
* elements of @ volumes will be # OstreeMockVolume objects ; but this does not
* have to be the case .
*
* Returns : ( transfer full ) : a new # GVolumeMonitor object
* Since : 2017.8
*/
GVolumeMonitor *
2023-05-01 21:24:29 +03:00
ostree_mock_volume_monitor_new ( GList * mounts , GList * volumes )
2017-04-19 02:07:51 +03:00
{
2023-05-01 21:24:29 +03:00
g_autoptr ( OstreeMockVolumeMonitor ) monitor = NULL ;
2017-04-19 02:07:51 +03:00
monitor = g_object_new ( OSTREE_TYPE_MOCK_VOLUME_MONITOR , NULL ) ;
2023-05-01 21:24:29 +03:00
monitor - > mounts = g_list_copy_deep ( mounts , ( GCopyFunc ) g_object_ref , NULL ) ;
monitor - > volumes = g_list_copy_deep ( volumes , ( GCopyFunc ) g_object_ref , NULL ) ;
2017-04-19 02:07:51 +03:00
2022-02-17 22:12:18 +03:00
return ( GVolumeMonitor * ) g_steal_pointer ( & monitor ) ;
2017-04-19 02:07:51 +03:00
}
/* Mock volume class. This returns a static set of data to the caller, which it
* was initialised with . */
struct _OstreeMockVolume
{
GObject parent_instance ;
gchar * name ;
2023-05-01 21:24:29 +03:00
GDrive * drive ; /* (owned) (nullable) */
GMount * mount ; /* (owned) (nullable) */
2017-04-19 02:07:51 +03:00
} ;
static void ostree_mock_volume_iface_init ( GVolumeIface * iface ) ;
G_DEFINE_TYPE_WITH_CODE ( OstreeMockVolume , ostree_mock_volume , G_TYPE_OBJECT ,
G_IMPLEMENT_INTERFACE ( G_TYPE_VOLUME , ostree_mock_volume_iface_init ) )
static gchar *
ostree_mock_volume_get_name ( GVolume * volume )
{
OstreeMockVolume * self = OSTREE_MOCK_VOLUME ( volume ) ;
return g_strdup ( self - > name ) ;
}
static GDrive *
ostree_mock_volume_get_drive ( GVolume * volume )
{
OstreeMockVolume * self = OSTREE_MOCK_VOLUME ( volume ) ;
return ( self - > drive ! = NULL ) ? g_object_ref ( self - > drive ) : NULL ;
}
static GMount *
ostree_mock_volume_get_mount ( GVolume * volume )
{
OstreeMockVolume * self = OSTREE_MOCK_VOLUME ( volume ) ;
return ( self - > mount ! = NULL ) ? g_object_ref ( self - > mount ) : NULL ;
}
static void
ostree_mock_volume_init ( OstreeMockVolume * self )
{
/* Nothing to see here. */
}
static void
ostree_mock_volume_dispose ( GObject * object )
{
OstreeMockVolume * self = OSTREE_MOCK_VOLUME ( object ) ;
g_clear_pointer ( & self - > name , g_free ) ;
g_clear_object ( & self - > drive ) ;
g_clear_object ( & self - > mount ) ;
G_OBJECT_CLASS ( ostree_mock_volume_parent_class ) - > dispose ( object ) ;
}
static void
ostree_mock_volume_class_init ( OstreeMockVolumeClass * klass )
{
GObjectClass * object_class = G_OBJECT_CLASS ( klass ) ;
object_class - > dispose = ostree_mock_volume_dispose ;
}
static void
ostree_mock_volume_iface_init ( GVolumeIface * iface )
{
iface - > get_name = ostree_mock_volume_get_name ;
iface - > get_drive = ostree_mock_volume_get_drive ;
iface - > get_mount = ostree_mock_volume_get_mount ;
}
/**
* ostree_mock_volume_new :
* @ name : volume name
* @ drive : ( transfer none ) ( nullable ) : drive for the volume , or % NULL if none
* should be associated
* @ mount : ( transfer none ) ( nullable ) : mount for the volume , or % NULL if it ’ s
* not mounted
*
* Create a new mock # GVolume which will return the given static @ name , @ drive
* and @ mount to any caller of its getter methods . There is currently no
* provision for changing these values dynamically . There is also currently no
* provision for mocking the other getters of # GVolume .
*
* Typically , @ drive will be an # OstreeMockDrive object and @ mount will be an
* # OstreeMockMount object ; but this does not have to be the case .
*
* Returns : ( transfer full ) : a new # GVolume object
* Since : 2017.8
*/
OstreeMockVolume *
2023-05-01 21:24:29 +03:00
ostree_mock_volume_new ( const gchar * name , GDrive * drive , GMount * mount )
2017-04-19 02:07:51 +03:00
{
2023-05-01 21:24:29 +03:00
g_autoptr ( OstreeMockVolume ) volume = NULL ;
2017-04-19 02:07:51 +03:00
volume = g_object_new ( OSTREE_TYPE_MOCK_VOLUME , NULL ) ;
volume - > name = g_strdup ( name ) ;
volume - > drive = ( drive ! = NULL ) ? g_object_ref ( drive ) : NULL ;
volume - > mount = ( mount ! = NULL ) ? g_object_ref ( mount ) : NULL ;
return g_steal_pointer ( & volume ) ;
}
/* Mock drive class. This returns a static set of data to the caller, which it
* was initialised with . */
struct _OstreeMockDrive
{
GObject parent_instance ;
gboolean is_removable ;
} ;
static void ostree_mock_drive_iface_init ( GDriveIface * iface ) ;
G_DEFINE_TYPE_WITH_CODE ( OstreeMockDrive , ostree_mock_drive , G_TYPE_OBJECT ,
G_IMPLEMENT_INTERFACE ( G_TYPE_DRIVE , ostree_mock_drive_iface_init ) )
# if GLIB_CHECK_VERSION(2, 50, 0)
static gboolean
ostree_mock_drive_is_removable ( GDrive * drive )
{
OstreeMockDrive * self = OSTREE_MOCK_DRIVE ( drive ) ;
return self - > is_removable ;
}
# endif
static void
ostree_mock_drive_init ( OstreeMockDrive * self )
{
/* Nothing to see here. */
}
static void
ostree_mock_drive_class_init ( OstreeMockDriveClass * klass )
{
/* Nothing to see here. */
}
static void
ostree_mock_drive_iface_init ( GDriveIface * iface )
{
# if GLIB_CHECK_VERSION(2, 50, 0)
iface - > is_removable = ostree_mock_drive_is_removable ;
# endif
}
/**
* ostree_mock_drive_new :
* @ is_removable : % TRUE if the drive is removable ; % FALSE otherwise
*
* Create a new mock # GDrive which will return the given static @ is_removable to
* any caller of its getter methods . There is currently no provision for mocking
* the other getters of # GDrive .
*
* Returns : ( transfer full ) : a new # GDrive object
* Since : 2017.8
*/
OstreeMockDrive *
ostree_mock_drive_new ( gboolean is_removable )
{
2023-05-01 21:24:29 +03:00
g_autoptr ( OstreeMockDrive ) drive = NULL ;
2017-04-19 02:07:51 +03:00
drive = g_object_new ( OSTREE_TYPE_MOCK_DRIVE , NULL ) ;
drive - > is_removable = is_removable ;
return g_steal_pointer ( & drive ) ;
}
/* Mock mount class. This returns a static set of data to the caller, which it
* was initialised with . */
struct _OstreeMockMount
{
GObject parent_instance ;
2023-05-01 21:24:29 +03:00
gchar * name ; /* (owned) */
GFile * root ; /* (owned) */
2017-04-19 02:07:51 +03:00
} ;
static void ostree_mock_mount_iface_init ( GMountIface * iface ) ;
G_DEFINE_TYPE_WITH_CODE ( OstreeMockMount , ostree_mock_mount , G_TYPE_OBJECT ,
G_IMPLEMENT_INTERFACE ( G_TYPE_MOUNT , ostree_mock_mount_iface_init ) )
static gchar *
ostree_mock_mount_get_name ( GMount * mount )
{
OstreeMockMount * self = OSTREE_MOCK_MOUNT ( mount ) ;
return g_strdup ( self - > name ) ;
}
static GFile *
ostree_mock_mount_get_root ( GMount * mount )
{
OstreeMockMount * self = OSTREE_MOCK_MOUNT ( mount ) ;
return g_object_ref ( self - > root ) ;
}
static void
ostree_mock_mount_init ( OstreeMockMount * self )
{
/* Nothing to see here. */
}
static void
ostree_mock_mount_dispose ( GObject * object )
{
OstreeMockMount * self = OSTREE_MOCK_MOUNT ( object ) ;
g_clear_pointer ( & self - > name , g_free ) ;
g_clear_object ( & self - > root ) ;
G_OBJECT_CLASS ( ostree_mock_mount_parent_class ) - > dispose ( object ) ;
}
static void
ostree_mock_mount_class_init ( OstreeMockMountClass * klass )
{
GObjectClass * object_class = G_OBJECT_CLASS ( klass ) ;
object_class - > dispose = ostree_mock_mount_dispose ;
}
static void
ostree_mock_mount_iface_init ( GMountIface * iface )
{
iface - > get_name = ostree_mock_mount_get_name ;
iface - > get_root = ostree_mock_mount_get_root ;
}
/**
* ostree_mock_mount_new :
* @ name : mount name
* @ root : ( transfer none ) : root path for the mounted file system
*
* Create a new mock # GMount which will return the given static @ name and @ root
* to any caller of its getter methods . There is currently no provision for
* mocking the other getters of # GMount .
*
* Typically , @ root will point to a temporary directory where a mocked file
* system is present ; but this does not have to be the case .
*
* Returns : ( transfer full ) : a new # GMount object
* Since : 2017.8
*/
OstreeMockMount *
2023-05-01 21:24:29 +03:00
ostree_mock_mount_new ( const gchar * name , GFile * root )
2017-04-19 02:07:51 +03:00
{
2023-05-01 21:24:29 +03:00
g_autoptr ( OstreeMockMount ) mount = NULL ;
2017-04-19 02:07:51 +03:00
mount = g_object_new ( OSTREE_TYPE_MOCK_MOUNT , NULL ) ;
mount - > name = g_strdup ( name ) ;
mount - > root = g_object_ref ( root ) ;
return g_steal_pointer ( & mount ) ;
}