summaryrefslogtreecommitdiff
path: root/tst/nhm-systemd-test.c
blob: 1486eb3d78c5dc516119408d8abe3f7b043c462c (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/* NHM - NodeHealthMonitor
 *
 * 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/.
 *
 * Author: Jean-Pierre Bogler <Jean-Pierre.Bogler@continental-corporation.com>
 */

/**
 * SECTION:nhm-unit-test
 * @title: NodeHealthMonitor (NHM) unit test
 * @short_description: Unit test for an automatic check of the NHM systemd
 *                     observation.
 *
 * The unit test will stimulate the NHM systemd observation and check for the
 * expected reactions.
 */


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

/* System header files                   */
#include <stdio.h>         /* NULL       */
#include <glib-2.0/glib.h> /* use gtypes */

/* Include the stubbed main file of the NHM. Its functions will be tested! */
#include "nhm-systemd-test.h"


/*******************************************************************************
*
* Local variables and constants
*
*******************************************************************************/

/* Variables to check callback on unit state change */
static gboolean        nhm_systemd_test_app_state_changed_cb_called = FALSE;
static gchar          *nhm_systemd_test_app_state_changed_cb_name   = NULL;
static NhmAppStatus_e  nhm_systemd_test_app_state_changed_cb_status = NhmAppStatus_Ok;


/*******************************************************************************
*
* Prototypes for file local functions (see implementation for description)
*
*******************************************************************************/


/*******************************************************************************
*
* Local (static) functions
*
*******************************************************************************/

/**
 * nhm_systemd_test_app_state_changed_cb:
 * @name:   Name of the application, whose state changed
 * @status: New status of the application.
 *
 * The function is not  a test case, but a callback that will be used during
 * the tests.
 */
static void
nhm_systemd_test_app_state_changed_cb(const gchar    *name,
                                      NhmAppStatus_e  status)
{
  /* Remember that callback was called and store passed parameters */
  nhm_systemd_test_app_state_changed_cb_called = TRUE;
  nhm_systemd_test_app_state_changed_cb_name   = (gchar*) name;
  nhm_systemd_test_app_state_changed_cb_status = status;
}


/**
 * nhm_test_systemd_connect:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_connect() function.
 */
static gint
nhm_test_systemd_connect(void)
{
  gint                              retval   = 0;
  NhmSystemdAppStatusCb             callback = NULL;
  GdbusConnectionCallSyncStubCalls  g_dbus_connection_call_sync_stub_calls[3];
  GVariantBuilder                  *builder;

  /* Check 1: Callback nok. */
  callback = NULL;
  retval = (nhm_systemd_connect(callback) == FALSE) ? 0 : -1;

  /* Check 2: Callback ok. Bus conn. nok. */
  if(retval == 0)
  {
    callback = &nhm_systemd_test_app_state_changed_cb;
    g_bus_get_sync_set_error = TRUE;

    retval = (nhm_systemd_connect(callback) == FALSE) ? 0 : -1;
  }

  /* Check 3: Callback ok. Bus conn. ok. Subscribe nok. */
  if(retval == 0)
  {
    callback = &nhm_systemd_test_app_state_changed_cb;
    g_bus_get_sync_set_error = FALSE;

    g_dbus_connection_call_sync_stub_control.count    = 1;
    g_dbus_connection_call_sync_stub_calls[0].method  = "Subscribe";
    g_dbus_connection_call_sync_stub_calls[0].rval    = NULL;
    g_dbus_connection_call_sync_stub_control.calls    = g_dbus_connection_call_sync_stub_calls;

    retval = (nhm_systemd_connect(callback) == FALSE) ? 0 : -1;
  }

  /* Check 4: Callback ok. Bus conn. ok. Subscribe ok. ListUnits nok */
  if(retval == 0)
  {
    callback = &nhm_systemd_test_app_state_changed_cb;

    g_bus_get_sync_set_error = FALSE;

    g_dbus_connection_call_sync_stub_control.count   = 1;
    g_dbus_connection_call_sync_stub_calls[0].method = "Subscribe";
    g_dbus_connection_call_sync_stub_calls[0].rval   = g_variant_new("()");
    g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

    g_dbus_connection_call_sync_stub_control.count   = 2;
    g_dbus_connection_call_sync_stub_calls[1].method = "ListUnits";
    g_dbus_connection_call_sync_stub_calls[1].rval   = NULL;
    g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

    g_dbus_connection_call_sync_stub_control.count   = 3;
    g_dbus_connection_call_sync_stub_calls[2].method = "Unsubscribe";
    g_dbus_connection_call_sync_stub_calls[2].rval   = NULL;
    g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

    retval = (nhm_systemd_connect(callback) == FALSE) ? 0 : -1;
  }

  /* Check 5: Callback ok. Bus conn. ok. Subscribe ok. ListUnits ok */
  if(retval == 0)
  {
    /* Callback ok */
    callback = &nhm_systemd_test_app_state_changed_cb;

    /* Bus conn. ok */
    g_bus_get_sync_set_error = FALSE;

    /* Subscribe ok */
    g_dbus_connection_call_sync_stub_control.count   = 1;
    g_dbus_connection_call_sync_stub_calls[0].method = "Subscribe";
    g_dbus_connection_call_sync_stub_calls[0].rval   = g_variant_new("()");
    g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

    /* ListUnits ok */
    g_dbus_connection_call_sync_stub_control.count   = 2;
    g_dbus_connection_call_sync_stub_calls[1].method = "ListUnits";

    builder = g_variant_builder_new (G_VARIANT_TYPE("a(ssssssouso)"));
    g_variant_builder_add (builder, "(ssssssouso)",  "1", "2", "3", "4", "5", "6", "/a/b/c", 100, "7", "/a/b/c");
    g_variant_builder_add (builder, "(ssssssouso)",  "1.service", "2", "3", "4", "5", "6", "/a/b/c", 100, "7", "/a/b/c");
    g_dbus_connection_call_sync_stub_calls[1].rval = g_variant_new("(a(ssssssouso))", builder);
    g_variant_builder_unref (builder);

    g_dbus_connection_call_sync_stub_control.calls = g_dbus_connection_call_sync_stub_calls;

    /* Call */
    retval = (nhm_systemd_connect(callback) == TRUE) ? 0 : -1;

    /* Free created unit object. Destroy bus conn. */
    g_slist_free_full(nhm_systemd_observed_units, &nhm_systemd_free_unit);
    g_object_unref(nhm_systemd_conn);
  }

  return retval;
}


/**
 * nhm_test_systemd_disconnect:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_disconnect() function.
 */
static gint
nhm_test_systemd_disconnect(void)
{
  NhmSystemdUnit                   *unit = NULL;
  GdbusConnectionCallSyncStubCalls  g_dbus_connection_call_sync_stub_calls[1];

  /* Check 1: Sub nok. No UnitAdd sig. No UnitRem sig. No obs. units. No systemd conn. */
  nhm_systemd_events_subscribed = FALSE;
  g_dbus_connection_call_sync_stub_control.count   = 1;
  g_dbus_connection_call_sync_stub_calls[0].method = "Unsubscribe";
  g_dbus_connection_call_sync_stub_calls[0].rval   = NULL;
  g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

  nhm_systemd_unit_add_sig_id = 0;
  nhm_systemd_unit_rem_sig_id = 0;
  nhm_systemd_observed_units  = NULL;
  nhm_systemd_conn            = NULL;

  nhm_systemd_disconnect();

  /* Check 2: Sub ok. Unsub. nok. No UnitAdd sig. No UnitRem sig. No obs. units. No systemd conn. */
  nhm_systemd_events_subscribed = TRUE;
  g_dbus_connection_call_sync_stub_control.count   = 1;
  g_dbus_connection_call_sync_stub_calls[0].method = "Unsubscribe";
  g_dbus_connection_call_sync_stub_calls[0].rval   = NULL;
  g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

  nhm_systemd_unit_add_sig_id = 0;
  nhm_systemd_unit_rem_sig_id = 0;
  nhm_systemd_observed_units  = NULL;
  nhm_systemd_conn            = NULL;

  nhm_systemd_disconnect();

  /* Check 3: Unsub. ok. UnitAdd sig. UnitRem sig. Obs. units. Systemd conn. */
  nhm_systemd_events_subscribed = TRUE;
  g_dbus_connection_call_sync_stub_control.count   = 1;
  g_dbus_connection_call_sync_stub_calls[0].method = "Unsubscribe";
  g_dbus_connection_call_sync_stub_calls[0].rval   = g_variant_new("()");
  g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

  nhm_systemd_unit_add_sig_id = 1;
  nhm_systemd_unit_rem_sig_id = 1;

  unit = g_new(NhmSystemdUnit, 1);
  unit->active_state = NHM_ACTIVE_STATE_UNKNOWN;
  unit->name         = g_strdup("Unit");
  unit->path         = g_strdup("/a/unit/to/destroy");
  unit->sig_sub_id   = 0;
  nhm_systemd_observed_units  = g_slist_append(nhm_systemd_observed_units, unit);

  nhm_systemd_conn = g_object_new(G_TYPE_DBUS_CONNECTION, NULL);

  nhm_systemd_disconnect();

  return 0;
}


/**
 * nhm_systemd_test_active_state_string_to_enum:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_active_state_string_to_enum() function.
 */
static gint
nhm_systemd_test_active_state_string_to_enum(void)
{
  gint retval = 0;

  /* Check 1: active -> NHM_ACTIVE_STATE_ACTIVE */
  retval = (   nhm_systemd_active_state_string_to_enum("active")
            == NHM_ACTIVE_STATE_ACTIVE) ? 0 : -1;

  /* Check 2: inactive -> NHM_ACTIVE_STATE_INACTIVE */
  if(retval == 0)
  {
    retval = (nhm_systemd_active_state_string_to_enum("inactive")
              == NHM_ACTIVE_STATE_INACTIVE) ? 0 : -1;
  }

  /* Check 3: activating -> NHM_ACTIVE_STATE_ACTIVATING */
  if(retval == 0)
  {
    retval = (nhm_systemd_active_state_string_to_enum("activating")
              == NHM_ACTIVE_STATE_ACTIVATING) ? 0 : -1;
  }

  /* Check 4: deactivating -> NHM_ACTIVE_STATE_DEACTIVATING */
  if(retval == 0)
  {
    retval = (nhm_systemd_active_state_string_to_enum("deactivating")
              == NHM_ACTIVE_STATE_DEACTIVATING) ? 0 : -1;
  }

  /* Check 5: failed -> NHM_ACTIVE_STATE_FAILED */
  if(retval == 0)
  {
    retval = (nhm_systemd_active_state_string_to_enum("failed")
              == NHM_ACTIVE_STATE_FAILED) ? 0 : -1;
  }

  /* Check 6: reloading -> NHM_ACTIVE_STATE_RELOADING */
  if(retval == 0)
  {
    retval = (nhm_systemd_active_state_string_to_enum("reloading")
              == NHM_ACTIVE_STATE_RELOADING) ? 0 : -1;
  }

  /* Check 6: ? -> NHM_ACTIVE_STATE_UNKNOWN */
  if(retval == 0)
  {
    retval = (nhm_systemd_active_state_string_to_enum("")
              == NHM_ACTIVE_STATE_UNKNOWN) ? 0 : -1;
  }

  return retval;
}


/**
 * nhm_systemd_test_subscribe_properties_changed:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_subscribe_properties_changed() function.
 */
static gint
nhm_systemd_test_subscribe_properties_changed(void)
{
  NhmSystemdUnit unit;

  /* Check 1: Do a subscription */
  return (nhm_systemd_subscribe_properties_changed(&unit) == 0) ? 0 : -1;
}


/**
 * nhm_systemd_test_find_unit_by_name:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_find_unit_by_name() function.
 */
static gint
nhm_systemd_test_find_unit_by_name(void)
{
  gint           retval = 0;
  NhmSystemdUnit u1;
  NhmSystemdUnit u2;

  /* Check 1: Valid compare */
  u1.name = "Unit1";
  u2.name = "Unit1";
  retval = (nhm_systemd_find_unit_by_name(&u1, &u2) == 0) ? 0 : -1;

  /* Check 2: Invalid compare I */
  if(retval == 0)
  {
    u1.name = "Unit1";
    u2.name = "Unit2";
    retval = (nhm_systemd_find_unit_by_name(&u1, &u2) == -1) ? 0 : -1;
  }

  /* Check 3: Invalid compare II */
  if(retval == 0)
  {
    u1.name = "Unit2";
    u2.name = "Unit1";
    retval = (nhm_systemd_find_unit_by_name(&u1, &u2) == 1) ? 0 : -1;
  }

  return retval;
}


/**
 * nhm_systemd_test_free_unit:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_free_unit() function.
 */
static gint
nhm_systemd_test_free_unit(void)
{
  NhmSystemdUnit *unit = g_new(NhmSystemdUnit, 1);

  /* Check 1: Free normal unit object */
  unit->active_state = NHM_ACTIVE_STATE_UNKNOWN;
  unit->name         = g_strdup("Unit");
  unit->path         = g_strdup("/path/to/unit");
  unit->sig_sub_id   = 0;

  nhm_systemd_free_unit(unit);

  return 0;
}


/**
 * nhm_systemd_test_unit_active_state_changed:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_unit_active_state_changed() function.
 */
static gint
nhm_systemd_test_unit_active_state_changed(void)
{
  gint           retval = 0;
  NhmSystemdUnit unit   = {"Unit", "Path", NHM_ACTIVE_STATE_UNKNOWN, 0};

  /* Check 1: Change NHM_ACTIVE_STATE_UNKNOWN -> NHM_ACTIVE_STATE_UNKNOWN */
  nhm_systemd_app_status_cb = &nhm_systemd_test_app_state_changed_cb;
  nhm_systemd_test_app_state_changed_cb_called = FALSE;
  nhm_systemd_test_app_state_changed_cb_name   = NULL;
  nhm_systemd_test_app_state_changed_cb_status = NhmAppStatus_Ok;

  nhm_systemd_unit_active_state_changed(&unit, NHM_ACTIVE_STATE_UNKNOWN);

  /* Check that no callback occured */
  retval = (nhm_systemd_test_app_state_changed_cb_called == FALSE) ? 0 : -1;

  /* Check 2: Change NHM_ACTIVE_STATE_UNKNOWN -> NHM_ACTIVE_STATE_ACTIVE */
  if(retval == 0)
  {
    nhm_systemd_app_status_cb                    = &nhm_systemd_test_app_state_changed_cb;
    nhm_systemd_test_app_state_changed_cb_called = FALSE;
    nhm_systemd_test_app_state_changed_cb_name   = NULL;
    nhm_systemd_test_app_state_changed_cb_status = NhmAppStatus_Failed;

    nhm_systemd_unit_active_state_changed(&unit, NHM_ACTIVE_STATE_ACTIVE);

    /* Assert that callback occurred */
    retval = (   (nhm_systemd_test_app_state_changed_cb_called == TRUE)
              && (g_strcmp0(nhm_systemd_test_app_state_changed_cb_name, unit.name) == 0)
              && (nhm_systemd_test_app_state_changed_cb_status == NhmAppStatus_Ok))
             ? 0 : -1;
  }

  /* Check 2: Change NHM_ACTIVE_STATE_UNKNOWN -> NHM_ACTIVE_STATE_ACTIVE */
  if(retval == 0)
  {
    nhm_systemd_app_status_cb                    = &nhm_systemd_test_app_state_changed_cb;
    nhm_systemd_test_app_state_changed_cb_called = FALSE;
    nhm_systemd_test_app_state_changed_cb_name   = NULL;
    nhm_systemd_test_app_state_changed_cb_status = NhmAppStatus_Ok;

    nhm_systemd_unit_active_state_changed(&unit, NHM_ACTIVE_STATE_FAILED);

    /* Assert that callback occurred */
    retval = (   (nhm_systemd_test_app_state_changed_cb_called == TRUE)
              && (g_strcmp0(nhm_systemd_test_app_state_changed_cb_name, unit.name) == 0)
              && (nhm_systemd_test_app_state_changed_cb_status == NhmAppStatus_Failed))
             ? 0 : -1;
  }

  return retval;
}


/**
 * nhm_systemd_test_unit_get_active_state:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_unit_get_active_state() function.
 */
static gint
nhm_systemd_test_unit_get_active_state(void)
{
  gint                             retval             = 0;
  GVariant                        *unit_state_variant = NULL;
  NhmSystemdUnit                   unit               = {"Name", "Path", NHM_ACTIVE_STATE_ACTIVE, 0};
  GdbusConnectionCallSyncStubCalls g_dbus_connection_call_sync_stub_calls[1];

  /* Check 1: D-Bus error getting ActiveState property */
  g_dbus_connection_call_sync_stub_control.count   = 1;
  g_dbus_connection_call_sync_stub_calls[0].method = "Get";
  g_dbus_connection_call_sync_stub_calls[0].rval   = NULL;
  g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

  retval = (nhm_systemd_unit_get_active_state(&unit) == NHM_ACTIVE_STATE_UNKNOWN)
           ? 0 : -1;

  /* Check 2: Retrieve D-Bus property value successful */
  if(retval == 0)
  {
    g_dbus_connection_call_sync_stub_control.count   = 1;
    g_dbus_connection_call_sync_stub_calls[0].method = "Get";

    unit_state_variant = g_variant_new_string("active");
    unit_state_variant = g_variant_new_variant(unit_state_variant);
    unit_state_variant = g_variant_new_tuple(&unit_state_variant, 1);
    g_dbus_connection_call_sync_stub_calls[0].rval  = unit_state_variant;

    g_dbus_connection_call_sync_stub_control.calls   = g_dbus_connection_call_sync_stub_calls;

    retval = (nhm_systemd_unit_get_active_state(&unit) == NHM_ACTIVE_STATE_ACTIVE)
             ? 0 : -1;
  }

  return retval;
}


/**
 * nhm_systemd_test_unit_added:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_unit_added() function.
 */
static gint
nhm_systemd_test_unit_added(void)
{
  gint                              retval             = 0;
  GVariant                         *param              = NULL;
  GVariant                         *unit_state_variant = NULL;
  NhmSystemdUnit                   *unit               = NULL;
  GdbusConnectionCallSyncStubCalls  g_dbus_connection_call_sync_stub_calls[1];

  /* Check 1: Wrong parameter format */
  nhm_systemd_observed_units = NULL;
  param = g_variant_new("(uss)", 10, "Wrong", "Unit");

  nhm_systemd_unit_added(NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         param,
                         NULL);

  retval = (nhm_systemd_observed_units == NULL) ? 0 : -1;

  /* Check 2: New unit added, but no service */
  if(retval == 0)
  {
    nhm_systemd_observed_units = NULL;
    param = g_variant_new("(so)", "Unit", "/Path/to/Unit");

    /* Function will retrieve unit's active state */
    g_dbus_connection_call_sync_stub_control.count   = 1;
    g_dbus_connection_call_sync_stub_calls[0].method = "Get";

    unit_state_variant = g_variant_new_string("active");
    unit_state_variant = g_variant_new_variant(unit_state_variant);
    unit_state_variant = g_variant_new_tuple(&unit_state_variant, 1);
    g_dbus_connection_call_sync_stub_calls[0].rval  = unit_state_variant;

    g_dbus_connection_call_sync_stub_control.calls = g_dbus_connection_call_sync_stub_calls;

    nhm_systemd_unit_added(NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           param,
                           NULL);
    g_variant_unref(param);
    /* Check that unit was not added */
    retval = (nhm_systemd_observed_units == NULL) ? 0 : -1;
  }

  /* Check 3: Add new service */
  if(retval == 0)
  {
    nhm_systemd_observed_units = NULL;
    param = g_variant_new("(so)", "Unit.service", "/Path/to/Unit");

    /* Function will retrieve unit's active state */
    g_dbus_connection_call_sync_stub_control.count   = 1;
    g_dbus_connection_call_sync_stub_calls[0].method = "Get";

    unit_state_variant = g_variant_new_string("active");
    unit_state_variant = g_variant_new_variant(unit_state_variant);
    unit_state_variant = g_variant_new_tuple(&unit_state_variant, 1);
    g_dbus_connection_call_sync_stub_calls[0].rval  = unit_state_variant;

    g_dbus_connection_call_sync_stub_control.calls = g_dbus_connection_call_sync_stub_calls;

    nhm_systemd_unit_added(NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           param,
                           NULL);

    g_variant_unref(param);
    /* Check that unit was not added */
    unit = (NhmSystemdUnit*) nhm_systemd_observed_units->data;
    retval = (   (g_strcmp0(unit->name, "Unit.service") == 0)
              && (g_strcmp0(unit->path, "/Path/to/Unit") == 0)
              && (unit->active_state == NHM_ACTIVE_STATE_ACTIVE)
              && (unit->sig_sub_id   == 0)) ? 0 : -1;

    g_slist_free_full(nhm_systemd_observed_units, &nhm_systemd_free_unit);
  }

  /* Check 4: Add same service */
  if(retval == 0)
  {
    param = g_variant_new("(so)", "Unit.service", "/Path/to/Unit");

    /* Add unit to the list */
    nhm_systemd_observed_units = NULL;
    unit = g_new(NhmSystemdUnit, 1);
    unit->name         = g_strdup("Unit.service");
    unit->path         = g_strdup("/Path/to/Unit");
    unit->active_state = NHM_ACTIVE_STATE_UNKNOWN;
    unit->sig_sub_id   = 0;
    nhm_systemd_observed_units = g_slist_append(nhm_systemd_observed_units, unit);

    nhm_systemd_unit_added(NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           param,
                           NULL);

    g_variant_unref(param);
    retval = (g_slist_length(nhm_systemd_observed_units) == 1) ? 0 : -1;

    g_slist_free_full(nhm_systemd_observed_units, &nhm_systemd_free_unit);
  }

  return retval;
}


/**
 * nhm_systemd_test_unit_removed:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_unit_removed() function.
 */
static gint
nhm_systemd_test_unit_removed(void)
{
  gint            retval = 0;
  GVariant       *param  = NULL;
  NhmSystemdUnit *unit;

  /* Preparation: Build up a list with an observed unit */
  nhm_systemd_observed_units = NULL;
  unit = g_new(NhmSystemdUnit, 1);
  unit->name         = g_strdup("Unit.service");
  unit->path         = g_strdup("/Path/to/Unit");
  unit->active_state = NHM_ACTIVE_STATE_UNKNOWN;
  unit->sig_sub_id   = 0;
  nhm_systemd_observed_units = g_slist_append(nhm_systemd_observed_units, unit);

  /* Reuse unit variable to point on first list element */
  unit = (NhmSystemdUnit*) (nhm_systemd_observed_units->data);


  /* Check 1: Invalid parameter format  */
  param = g_variant_new("(uss)", 10, "Unit", "/Path/to/Unit");

  nhm_systemd_unit_removed(NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           param,
                           NULL);
  g_variant_unref(param);
  /* Assert unit still on the list */
  retval = (   (g_strcmp0(unit->name, "Unit.service") == 0)
            && (g_strcmp0(unit->path, "/Path/to/Unit") == 0)
            && (unit->active_state == NHM_ACTIVE_STATE_UNKNOWN)
            && (unit->sig_sub_id   == 0)) ? 0 : -1;

  /* Check 2: No service removed */
  if(retval == 0)
  {
    param = g_variant_new("(so)", "Unit1", "/Path/to/Unit");

    nhm_systemd_unit_removed(NULL,
                             NULL,
                             NULL,
                             NULL,
                             NULL,
                             param,
                             NULL);
    g_variant_unref(param);
    /* Assert unit still on the list */
    retval = (   (g_strcmp0(unit->name, "Unit.service") == 0)
              && (g_strcmp0(unit->path, "/Path/to/Unit") == 0)
              && (unit->active_state == NHM_ACTIVE_STATE_UNKNOWN)
              && (unit->sig_sub_id   == 0)) ? 0 : -1;
  }

  /* Check 3: Unknown unit removed */
  if(retval == 0)
  {
    param = g_variant_new("(so)", "Unit1.service", "/Path/to/Unit");

    nhm_systemd_unit_removed(NULL,
                             NULL,
                             NULL,
                             NULL,
                             NULL,
                             param,
                             NULL);
    g_variant_unref(param);
    /* Assert unit still on the list */
    retval = (   (g_strcmp0(unit->name, "Unit.service") == 0)
              && (g_strcmp0(unit->path, "/Path/to/Unit") == 0)
              && (unit->active_state == NHM_ACTIVE_STATE_UNKNOWN)
              && (unit->sig_sub_id   == 0)) ? 0 : -1;
  }

  /* Check 4: Unit removed */
  if(retval == 0)
  {
    param = g_variant_new("(so)", "Unit.service", "/Path/to/Unit");

    nhm_systemd_unit_removed(NULL,
                             NULL,
                             NULL,
                             NULL,
                             NULL,
                             param,
                             NULL);
    g_variant_unref(param);
    retval = (nhm_systemd_observed_units == NULL) ? 0 : -1;
  }

  return retval;
}


/**
 * nhm_systemd_test_unit_properties_changed:
 * @Return: 0, if test succeeded. Otherwise -1.
 *
 * Test nhm_systemd_unit_properties_changed() function.
 */
static gint
nhm_systemd_test_unit_properties_changed(void)
{
  gint                              retval             = 0;
  GVariant                         *param              = NULL;
  GVariant                         *unit_state_variant = NULL;
  gchar                            *inv_prop[2]        = {"state", NULL};
  NhmSystemdUnit                    unit;
  GdbusConnectionCallSyncStubCalls  g_dbus_connection_call_sync_stub_calls[1];

  /* Check 1: Parameter format nok. */
  param = g_variant_new("(s)", "Test");
  nhm_systemd_app_status_cb = &nhm_systemd_test_app_state_changed_cb;
  nhm_systemd_test_app_state_changed_cb_called = FALSE;

  nhm_systemd_unit_properties_changed(NULL,
                                      NULL,
                                      NULL,
                                      NULL,
                                      NULL,
                                      param,
                                      NULL);
  g_variant_unref(param);
  retval = (nhm_systemd_test_app_state_changed_cb_called == FALSE) ? 0 : -1;

  /* Check 2: Parameter format ok. ActiveState unchanged. */
  if(retval == 0)
  {
    inv_prop[0] = "Invalid";
    param = g_variant_new("(sa{sv}^as)", "Test", NULL, inv_prop);

    nhm_systemd_app_status_cb = &nhm_systemd_test_app_state_changed_cb;
    nhm_systemd_test_app_state_changed_cb_called = FALSE;

    nhm_systemd_unit_properties_changed(NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        param,
                                        NULL);
    g_variant_unref(param);
    retval = (nhm_systemd_test_app_state_changed_cb_called == FALSE) ? 0 : -1;
  }

  /* Check 3: Parameter format ok. Same ActiveState. */
  if(retval == 0)
  {
    inv_prop[0] = "ActiveState";
    param = g_variant_new("(sa{sv}^as)", "Test", NULL, inv_prop);

    unit.name = "Unit";
    unit.path = "Path";
    unit.active_state = NHM_ACTIVE_STATE_ACTIVE;
    unit.sig_sub_id = 0;

    g_dbus_connection_call_sync_stub_control.count    = 1;
    g_dbus_connection_call_sync_stub_calls[0].method  = "Get";
    unit_state_variant = g_variant_new_string("active");
    unit_state_variant = g_variant_new_variant(unit_state_variant);
    unit_state_variant = g_variant_new_tuple(&unit_state_variant, 1);
    g_dbus_connection_call_sync_stub_calls[0].rval  = unit_state_variant;
    g_dbus_connection_call_sync_stub_control.calls  = g_dbus_connection_call_sync_stub_calls;

    nhm_systemd_app_status_cb = &nhm_systemd_test_app_state_changed_cb;
    nhm_systemd_test_app_state_changed_cb_called = FALSE;

    nhm_systemd_unit_properties_changed(NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        param,
                                        &unit);
    g_variant_unref(param);
    retval = (nhm_systemd_test_app_state_changed_cb_called == FALSE) ? 0 : -1;
  }

  /* Check 4: Parameter format ok. ActiveState changed. */
  if(retval == 0)
  {
    inv_prop[0] = "ActiveState";
    param = g_variant_new("(sa{sv}^as)", "Test", NULL, inv_prop);

    unit.name = "Unit";
    unit.path = "Path";
    unit.active_state = NHM_ACTIVE_STATE_FAILED;
    unit.sig_sub_id = 0;
    g_dbus_connection_call_sync_stub_control.count = 1;
    g_dbus_connection_call_sync_stub_calls[0].method  = "Get";
    unit_state_variant = g_variant_new_string("active");
    unit_state_variant = g_variant_new_variant(unit_state_variant);
    unit_state_variant = g_variant_new_tuple(&unit_state_variant, 1);
    g_dbus_connection_call_sync_stub_calls[0].rval  = unit_state_variant;
    g_dbus_connection_call_sync_stub_control.calls  = g_dbus_connection_call_sync_stub_calls;

    nhm_systemd_test_app_state_changed_cb_called = FALSE;
    nhm_systemd_app_status_cb = &nhm_systemd_test_app_state_changed_cb;
    nhm_systemd_unit_properties_changed(NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        param,
                                        &unit);
    g_variant_unref(param);
    retval = (nhm_systemd_test_app_state_changed_cb_called == TRUE) ? 0 : -1;
  }

  return retval;
}


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

/**
 * main:
 *
 * Main function of the unit test.
 *
 * Return value: 0 if all tests succeeded. Otherwise -1.
 */
int
main(void)
{
  int retval = 0;

  g_type_init();

  /* Test interfaces */
  retval = nhm_test_systemd_connect();
  retval = (retval == 0) ? nhm_test_systemd_disconnect()                   : -1;

  /* Test static functions */
  retval = (retval == 0) ? nhm_systemd_test_active_state_string_to_enum()  : -1;

  /* Test helper functions */
  retval = (retval == 0) ? nhm_systemd_test_subscribe_properties_changed() : -1;
  retval = (retval == 0) ? nhm_systemd_test_find_unit_by_name()            : -1;
  retval = (retval == 0) ? nhm_systemd_test_free_unit()                    : -1;

  /* Test unit state change chain */
  retval = (retval == 0) ? nhm_systemd_test_unit_active_state_changed()    : -1;
  retval = (retval == 0) ? nhm_systemd_test_unit_get_active_state()        : -1;
  retval = (retval == 0) ? nhm_systemd_test_unit_added()                   : -1;
  retval = (retval == 0) ? nhm_systemd_test_unit_removed()                 : -1;
  retval = (retval == 0) ? nhm_systemd_test_unit_properties_changed()      : -1;

  return retval;
}