summaryrefslogtreecommitdiff
path: root/tst/stubs/gio/gio-stub.c
blob: 6cf8fb1a1540ca29b26fcdc16008c3a7f5c082a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* NHM - NodeHealthMonitor
 *
 * Implementation of stubs for gio
 *
 * Author: Jean-Pierre Bogler <Jean-Pierre.Bogler@continental-corporation.com>
 *
 * Copyright (C) 2013 Continental Automotive Systems, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 */

/*******************************************************************************
*
* Header includes
*
*******************************************************************************/

#include <stdio.h>                  /* NULL               */
#include <string.h>                 /* strcmp             */
#include <gio/gio.h>                /* Header of real gio */
#include <tst/stubs/gio/gio-stub.h> /* Header of stub gio */


/*******************************************************************************
*
* Exported variables and constants
*
*******************************************************************************/

gboolean  g_bus_get_sync_set_error                              = FALSE;
gboolean  g_dbus_interface_skeleton_export_stub_set_error       = FALSE;
gboolean  g_main_loop_quit_stub_called                          = FALSE;
guint     g_timeout_add_seconds_called_interval                 = 0;
gboolean  g_timeout_add_seconds_called                          = FALSE;
gboolean  g_dbus_connection_new_for_address_sync_stub_set_error = FALSE;
GdbusConnectionCallSyncStubControl g_dbus_connection_call_sync_stub_control;


/*******************************************************************************
*
* File local variables and constants
*
*******************************************************************************/

static guint  folder_file_idx = 0;
static gchar *folder_files[]  = {"0000", "0001", NULL};

/*******************************************************************************
*
* Interfaces. Exported functions.
*
*******************************************************************************/

/**
 * g_file_test_stub:
 *
 * Stub for g_file_test()
 */
gboolean
g_file_test_stub(const gchar *filename,
                 GFileTest    test)
{
  return (g_strcmp0(filename, "existing_file") == 0);
}

/**
 * g_file_read_link_stub:
 *
 * Stub for g_file_read_link()
 */
gchar*
g_file_read_link_stub(const gchar  *filename,
                      GError      **error)
{
  gchar *linked_file = NULL;

  if(strcmp("/proc/0000/exe", filename) == 0)
  {
    linked_file = g_strdup("/usr/bin/valid_prog1");
  }
  else if(strcmp("/proc/0001/exe", filename) == 0)
  {
    linked_file = g_strdup("/usr/bin/valid_prog2");
  }
  else
  {
    linked_file = NULL;
  }

  return linked_file;
}

/**
 * g_dir_open_stub:
 *
 * Stub for g_dir_open()
 */
GDir*
g_dir_open_stub(const gchar *path,
                guint        flags,
                GError     **error)
{
  folder_file_idx = 0;
  return (GDir*) folder_files;
}

/**
 * g_dir_read_name_stub:
 *
 * Stub for g_dir_read_name()
 */
const gchar*
g_dir_read_name_stub(GDir *dir)
{
  gchar *file_name = NULL;

  if(folder_file_idx < sizeof(folder_files)/sizeof(gchar*))
  {
    file_name = folder_files[folder_file_idx];
    folder_file_idx++;
  }

  return file_name;
}

/**
 * g_dir_close_stub:
 *
 * Stub for g_dir_close()
 */
void
g_dir_close_stub(GDir *dir)
{
  folder_file_idx = 0;
}


/**
 * g_bus_get_sync_stub:
 *
 * Stub for g_bus_get_sync()
 */
GDBusConnection*
g_bus_get_sync_stub(GBusType       bus_type,
                    GCancellable  *cancellable,
                    GError       **error)
{
  gpointer retval = FALSE;

  if(g_bus_get_sync_set_error == FALSE)
  {
    retval = g_object_new(G_TYPE_DBUS_CONNECTION, NULL);
  }
  else
  {
    retval = NULL;
    g_set_error(error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, NULL);
  }

  return retval;
}

/**
 * g_bus_own_name_stub:
 *
 * Stub for g_bus_own_name()
 */
guint
g_bus_own_name_stub(GBusType                  bus_type,
                    const gchar              *name,
                    GBusNameOwnerFlags        flags,
                    GBusAcquiredCallback      bus_acquired_handler,
                    GBusNameAcquiredCallback  name_acquired_handler,
                    GBusNameLostCallback      name_lost_handler,
                    gpointer                  user_data,
                    GDestroyNotify            user_data_free_func)
{
  return 0;
}

/**
 * g_dbus_connection_get_unique_name_stub:
 *
 * Stub for g_dbus_connection_get_unique_name()
 */
const gchar*
g_dbus_connection_get_unique_name_stub(GDBusConnection *connection)
{
  return NULL;
}

/**
 * g_dbus_interface_skeleton_export_stub:
 *
 * Stub for g_dbus_interface_skeleton_export()
 */
gboolean
g_dbus_interface_skeleton_export_stub(GDBusInterfaceSkeleton *interface_,
                                      GDBusConnection        *connection,
                                      const gchar            *object_path,
                                      GError                **error)
{
  gboolean retval = FALSE;

  if(g_dbus_interface_skeleton_export_stub_set_error == FALSE)
  {
    retval = TRUE;
  }
  else
  {
    retval = FALSE;
    g_set_error(error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, NULL);
  }

  return retval;
}


/**
 * g_dbus_connection_new_for_address_sync_stub:
 *
 * Stub for g_dbus_connection_new_for_address_sync()
 */
GDBusConnection*
g_dbus_connection_new_for_address_sync_stub(const gchar          *address,
                                            GDBusConnectionFlags  flags,
                                            GDBusAuthObserver    *observer,
                                            GCancellable         *cancellable,
                                            GError              **error)
{
  GDBusConnection *retval = NULL;

  if(g_dbus_connection_new_for_address_sync_stub_set_error == FALSE)
  {
    retval = g_object_new(G_TYPE_DBUS_CONNECTION, NULL);
  }
  else
  {
    retval = NULL;
    g_set_error(error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, NULL);
  }

  return retval;
}


/**
 * g_dbus_connection_call_sync_stub:
 *
 * Stub for g_dbus_connection_call_sync()
 */
GVariant*
g_dbus_connection_call_sync_stub(GDBusConnection    *connection,
                                 const gchar        *bus_name,
                                 const gchar        *object_path,
                                 const gchar        *interface_name,
                                 const gchar        *method_name,
                                 GVariant           *parameters,
                                 const GVariantType *reply_type,
                                 GDBusCallFlags      flags,
                                 gint                timeout_msec,
                                 GCancellable       *cancellable,
                                 GError            **error)
{
  GVariant                         *rval              = NULL;
  guint                             control_array_idx = 0;
  GdbusConnectionCallSyncStubCalls *call              = NULL;

  for(control_array_idx = 0;
         (control_array_idx < g_dbus_connection_call_sync_stub_control.count)
      && (call == NULL);
      control_array_idx++)
  {
    if(g_strcmp0(g_dbus_connection_call_sync_stub_control.calls[control_array_idx].method,
        method_name) == 0)
    {
      call = &g_dbus_connection_call_sync_stub_control.calls[control_array_idx];
    }
  }

  if(call != NULL)
  {
    rval = call->rval;

    if((rval == NULL) && (error != NULL))
    {
      g_set_error(error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, NULL);
    }
  }

  return rval;
}


/**
 * g_dbus_connection_signal_subscribe_stub:
 *
 * Stub for g_dbus_connection_signal_subscribe()
 */
guint
g_dbus_connection_signal_subscribe_stub(GDBusConnection     *connection,
                                        const gchar         *sender,
                                        const gchar         *interface_name,
                                        const gchar         *member,
                                        const gchar         *object_path,
                                        const gchar         *arg0,
                                        GDBusSignalFlags     flags,
                                        GDBusSignalCallback  callback,
                                        gpointer             user_data,
                                        GDestroyNotify       user_data_free_func)
{
  return 0;
}


/**
 * g_dbus_connection_signal_unsubscribe_stub:
 *
 * Stub for g_dbus_connection_signal_unsubscribe()
 */
void
g_dbus_connection_signal_unsubscribe_stub(GDBusConnection *connection,
                                          guint            subscription_id)
{

}


/**
 * g_main_loop_run_stub:
 *
 * Stub for g_main_loop_run()
 */
void
g_main_loop_run_stub(GMainLoop *loop)
{
}

/**
 * g_main_loop_quit_stub:
 *
 * Stub for g_main_loop_quit()
 */
void
g_main_loop_quit_stub(GMainLoop *loop)
{
  g_main_loop_quit_stub_called = TRUE;
}

/**
 * g_timeout_add_seconds_stub:
 *
 * Stub for g_timeout_add_seconds()
 */
guint
g_timeout_add_seconds_stub(guint       interval,
                           GSourceFunc function,
                           gpointer    data)
{
  g_timeout_add_seconds_called_interval = interval;
  g_timeout_add_seconds_called          = TRUE;

  return 0;
}

/**
 * g_signal_connect_data_stub:
 *
 * Stub for g_signal_connect_data()
 */
gulong
g_signal_connect_data_stub(gpointer       instance,
                           const gchar   *detailed_signal,
                           GCallback      c_handler,
                           gpointer       data,
                           GClosureNotify destroy_data,
                           GConnectFlags  connect_flags)
{
  return 0;
}

/**
 * g_spawn_sync_stub:
 *
 * Stub for g_spawn_sync()
 */
gboolean
g_spawn_sync_stub(const gchar          *working_directory,
                  gchar               **argv,
                  gchar               **envp,
                  GSpawnFlags           flags,
                  GSpawnChildSetupFunc  child_setup,
                  gpointer              user_data,
                  gchar               **standard_output,
                  gchar               **standard_error,
                  gint                 *exit_status,
                  GError              **error)
{
  gboolean retval = FALSE;

  if(strcmp(argv[0], "valid_proc") == 0)
  {
    retval       = TRUE;
    *exit_status = 0;
    *error       = NULL;
  }
  else if(strcmp(argv[0], "failing_proc") == 0)
  {
    retval       = TRUE;
    *exit_status = -1;
    *error       = NULL;
  }
  else
  {
    retval = FALSE;
    g_set_error(error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, NULL);
  }

  return retval;
}