sysd-manager-comcontroler 2.19.4

Communication controller for sysd-manager.
Documentation
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
use super::*;

use crate::{
    SystemdUnitFile,
    enums::{DependencyType, StartStopMode},
};
use test_base::{TEST_SERVICE, init_logs};
use zvariant::Value;

#[ignore = "need a connection to a service"]
#[test]
fn stop_service_test() -> Result<(), SystemdErrors> {
    crate::stop_unit(UnitDBusLevel::System, TEST_SERVICE, StartStopMode::Fail)?;
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_get_unit_file_state() {
    init_logs();
    let file1: &str = TEST_SERVICE;

    let status = get_unit_file_state(UnitDBusLevel::System, file1);
    debug!("Status: {status:?}");
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_list_unit_files_system() -> Result<(), SystemdErrors> {
    init_logs();

    let level = UnitDBusLevel::System;
    let unit_files = fill_list_unit_files(level).await?;

    info!("Unit file returned {}", unit_files.len());

    for (idx, unit_file) in unit_files.iter().enumerate() {
        debug!("{idx} - {}", unit_file.full_name);
        debug!("{}", unit_file.file_path);
    }

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_list_unit_files_remote() -> Result<(), SystemdErrors> {
    init_logs();

    // let stream = UnixStream::connect("plr@192.168.0.114").await?;
    let level = UnitDBusLevel::System;
    let unit_files = fill_list_unit_files(level).await?;

    info!("Unit file returned {}", unit_files.len());

    for (idx, unit_file) in unit_files.iter().enumerate() {
        debug!("{idx} - {}", unit_file.full_name);
        debug!("{}", unit_file.file_path);
    }

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_list_unit_files_system_raw() -> Result<(), SystemdErrors> {
    init_logs();

    let level = UnitDBusLevel::System;

    let array: Vec<ListedUnitFile> = systemd_manager_async(level)
        .await?
        .list_unit_files()
        .await?;

    for (idx, unit_file) in array.iter().enumerate() {
        debug!(
            "{idx} - {} - {}",
            unit_file.enablement_status, unit_file.unit_file_path
        );
    }

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_list_units_by_patterns() -> Result<(), SystemdErrors> {
    init_logs();

    let array: Vec<ListedLoadedUnit> = systemd_manager_async(UnitDBusLevel::System)
        .await?
        .list_units_by_patterns(&["active", "inactive"], &["*.timer"])
        .await?;

    for (idx, loaded_unit) in array.iter().enumerate() {
        debug!(
            "{idx} - {} - {}",
            loaded_unit.primary_unit_name, loaded_unit.load_state
        );
    }

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_list_units_late_filter_timer() -> Result<(), SystemdErrors> {
    init_logs();

    let array: Vec<ListedLoadedUnit> = systemd_manager_async(UnitDBusLevel::System)
        .await?
        .list_units()
        .await?;

    for (idx, loaded_unit) in array
        .iter()
        .filter(|lunit| lunit.primary_unit_name.ends_with("timer"))
        .enumerate()
    {
        debug!(
            "{idx} - {} - {}",
            loaded_unit.primary_unit_name, loaded_unit.active_state
        );
    }

    Ok(())
}

/*
#[ignore = "need a connection to a service"]
#[test]
fn test_list_unit_files_system() -> Result<(), SystemdErrors> {
init();
let units = list_unit_files_user_test(UnitDBusLevel::System)?;

let serv = units.iter().find(|ud| ud.full_name == TEST_SERVICE);

debug!("{serv:#?}");
Ok(())
} */

/*
#[ignore = "need a connection to a service"]
#[test]
fn test_list_units() -> Result<(), SystemdErrors> {
    let units = list_units_description(
        &get_connection(UnitDBusLevel::System)?,
        UnitDBusLevel::System,
    )?;

    let serv = units.get(TEST_SERVICE);
    debug!("{:#?}", serv);
    Ok(())
} */

#[ignore = "need a connection to a service"]
#[test]
pub fn test_get_unit_path() -> Result<(), SystemdErrors> {
    init_logs();
    let unit_file: &str = "tiny_daemon.service";

    let connection = get_blocking_connection(UnitDBusLevel::System)?;

    let message = connection.call_method(
        Some(DESTINATION_SYSTEMD),
        PATH_SYSTEMD,
        Some(INTERFACE_SYSTEMD_MANAGER),
        "GetUnit",
        &(unit_file),
    )?;

    info!("message {message:?}");

    let body = message.body();

    let z: zvariant::ObjectPath = body.deserialize()?;
    //let z :String = body.deserialize()?;

    info!("obj {:?}", z.as_str());

    /*         let body = message.body();

    let des = body.deserialize();

    println!("{:#?}", des); */
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
pub fn test_fetch_system_unit_info() -> Result<(), SystemdErrors> {
    init_logs();

    let btree_map = fetch_system_unit_info(
        UnitDBusLevel::System,
        "/org/freedesktop/systemd1/unit/tiny_5fdaemon_2eservice",
        UnitType::Service,
    )?;

    debug!("ALL PARAM: {btree_map:#?}");
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_info() -> Result<(), SystemdErrors> {
    init_logs();

    let path = unit_dbus_path_from_name(TEST_SERVICE);

    println!("unit {TEST_SERVICE} Path {path}");
    let map = fetch_system_unit_info(UnitDBusLevel::System, &path, UnitType::Service)?;

    println!("{map:#?}");
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_system_info() -> Result<(), SystemdErrors> {
    init_logs();

    let map = fetch_system_info(UnitDBusLevel::System)?;

    info!("{map:#?}");
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_unit() -> Result<(), SystemdErrors> {
    init_logs();

    let unit = fetch_unit(UnitDBusLevel::System, TEST_SERVICE)?;

    info!("{unit:#?}");
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_unit_user_session() -> Result<(), SystemdErrors> {
    init_logs();

    let unit = fetch_unit(UnitDBusLevel::UserSession, TEST_SERVICE)?;

    info!("{unit:#?}");
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_unit_wrong_bus() -> Result<(), SystemdErrors> {
    init_logs();

    let unit = fetch_unit(UnitDBusLevel::UserSession, TEST_SERVICE)?;

    info!("{}", unit.debug());
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_unit_dependencies() -> Result<(), SystemdErrors> {
    init_logs();

    let path = unit_dbus_path_from_name(TEST_SERVICE);
    let res = unit_get_dependencies(
        UnitDBusLevel::System,
        TEST_SERVICE,
        &path,
        DependencyType::Forward,
        false,
    );

    info!("{:#?}", res.unwrap());
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_unit_reverse_dependencies() -> Result<(), SystemdErrors> {
    init_logs();

    let path = unit_dbus_path_from_name(TEST_SERVICE);
    let res = unit_get_dependencies(
        UnitDBusLevel::System,
        TEST_SERVICE,
        &path,
        DependencyType::Reverse,
        false,
    );

    info!("{res:#?}");
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_fetch_unit_fail_wrong_name() -> Result<(), SystemdErrors> {
    init_logs();

    let fake = format!("{TEST_SERVICE}_fake");
    match fetch_unit(UnitDBusLevel::System, &fake) {
        Ok(_) => todo!(),
        Err(e) => {
            warn!("{e:?}");
            if let SystemdErrors::ZNoSuchUnit(_method, _message) = e {
                Ok(())
            } else {
                Err(SystemdErrors::Custom("Wrong expected Error".to_owned()))
            }
        }
    }
}

#[test]
fn test_name_convertion() {
    let tests = [
        ("tiny_daemon.service", "tiny_5fdaemon_2eservice"),
        ("-.mount", "_2d_2emount"),
        //("sys-devices-pci0000:00-0000:00:1d.0-0000:3d:00.0-nvme-nvme0-nvme0n1-nvme0n1p1.device", "sys_2ddevices_2dpci0000_3a00_2d0000_3a00_3a1d_2e0_2d0000_3a3d_3a00_2e0_2dnvme_2dnvme0_2dnvme0n1_2dnvme0n1p1_2edevice"),
        ("1first", "_31first"),
    ];

    for (origin, expected) in tests {
        let convertion = bus_label_escape(origin);
        assert_eq!(convertion, expected);
    }
}

#[ignore = "need a connection to a service"]
#[test]
fn test_get_unit_processes() -> Result<(), SystemdErrors> {
    let unit_file: &str = "system.slice";

    let list = retreive_unit_processes(UnitDBusLevel::System, unit_file)?;

    for up in list {
        println!("{up:#?}")
    }

    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_get_unit_active_state() -> Result<(), SystemdErrors> {
    let unit_object = unit_dbus_path_from_name(TEST_SERVICE);

    println!("path : {unit_object}");
    let state = get_unit_active_state(UnitDBusLevel::System, &unit_object)?;

    println!("state of {TEST_SERVICE} is {state:?}");

    Ok(())
}

async fn get_unit_list_test(level: UnitDBusLevel) -> Result<Vec<ListedLoadedUnit>, SystemdErrors> {
    let connection = get_connection(level).await?;

    let r = list_units_list_async(connection).await?;

    info!("Returned units count: {}", r.len());

    Ok(r)
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_get_unit_list_system() -> Result<(), SystemdErrors> {
    init_logs();
    let _map = get_unit_list_test(UnitDBusLevel::System).await?;
    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_get_unit_list_user() -> Result<(), SystemdErrors> {
    init_logs();
    let _map = get_unit_list_test(UnitDBusLevel::UserSession).await?;
    Ok(())
}

async fn get_unit_file_list_test(
    level: UnitDBusLevel,
) -> Result<Vec<SystemdUnitFile>, SystemdErrors> {
    let r = fill_list_unit_files(level).await?;

    info!("Returned units count: {}", r.len());

    Ok(r)
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_get_unit_file_list_system() -> Result<(), SystemdErrors> {
    init_logs();
    let _map = get_unit_file_list_test(UnitDBusLevel::System).await?;
    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_get_unit_file_list_user() -> Result<(), SystemdErrors> {
    init_logs();
    let _map = get_unit_list_test(UnitDBusLevel::UserSession).await?;
    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_get_list() -> Result<(), SystemdErrors> {
    init_logs();
    let connection = get_connection(UnitDBusLevel::System).await?;

    let connection2 = get_connection(UnitDBusLevel::UserSession).await?;

    use std::time::Instant;
    let now = Instant::now();
    let t1 = tokio::spawn(list_units_list_async(connection.clone()));
    let t2 = tokio::spawn(fill_list_unit_files(UnitDBusLevel::System));
    let t3 = tokio::spawn(list_units_list_async(connection2.clone()));
    let t4 = tokio::spawn(fill_list_unit_files(UnitDBusLevel::UserSession));

    let _asdf = tokio::join!(t1, t2, t3, t4);

    let elapsed = now.elapsed();
    println!("Elapsed: {elapsed:.2?}");
    /*        let a = asdf.0;
    let b = asdf.1;

    println!("{:?}", a);
    println!("{:?}", b); */

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_get_list2() -> Result<(), SystemdErrors> {
    let connection = get_connection(UnitDBusLevel::System).await?;

    let connection2 = get_connection(UnitDBusLevel::UserSession).await?;

    use std::time::Instant;
    let now = Instant::now();
    let t1 = list_units_list_async(connection.clone());
    let t2 = fill_list_unit_files(UnitDBusLevel::System);
    let t3 = list_units_list_async(connection2.clone());
    let t4 = fill_list_unit_files(UnitDBusLevel::UserSession);

    let joined_result = tokio::join!(t1, t2, t3, t4);

    let elapsed = now.elapsed();
    println!("Elapsed: {elapsed:.2?}");

    let r1 = joined_result.0.unwrap();
    let r2 = joined_result.1.unwrap();
    let r3 = joined_result.2.unwrap();
    let r4 = joined_result.3.unwrap();

    println!("System unit description size {}", r1.len());
    println!("System unit file size {}", r2.len());
    println!("Session unit description size {}", r3.len());
    println!("Session unit file size {}", r4.len());

    //check system collision
    /*     for (key, _val) in r1 {
        if r3.contains_key(&key) {
            println!("collision description on key {key}");
        }
    } */

    /*         let a = asdf.0;
           let b = asdf.1;

           println!("{:?}", a);
           println!("{:?}", b);
    */
    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_get_properties() -> Result<(), SystemdErrors> {
    init_logs();
    let connection = get_blocking_connection(UnitDBusLevel::System)?;

    let object_path = unit_dbus_path_from_name(TEST_SERVICE);
    debug!("Unit path: {object_path}");
    let properties_proxy: zbus::blocking::fdo::PropertiesProxy =
        fdo::PropertiesProxy::builder(&connection)
            .destination(DESTINATION_SYSTEMD)?
            .path(object_path)?
            // .interface(INTERFACE_SYSTEMD_UNIT)?
            //  .interface(UnitType::Service.interface())?
            .build()?;

    let unit_type = UnitType::Service;
    let unit_interface = unit_type.interface();

    //let unit_interface_name = InterfaceName::try_from(INTERFACE_SYSTEMD_UNIT).unwrap();
    let unit_interface_name = InterfaceName::try_from(INTERFACE_SYSTEMD_UNIT).unwrap();

    let mut unit_properties: HashMap<String, OwnedValue> =
        properties_proxy.get_all(unit_interface_name)?;

    let interface_name = InterfaceName::try_from(unit_interface).unwrap();

    let properties: HashMap<String, OwnedValue> = properties_proxy.get_all(interface_name)?;

    info!("Properties size {}", properties.len());

    info!("Unit Properties size {}", unit_properties.len());

    for k in properties.into_keys() {
        unit_properties.remove(&k);
    }

    info!("Unit Properties size {}", unit_properties.len());

    /*      for (k, v) in unit_properties {
        info!("{k} {:?}", v);
    } */

    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_kill_unit() -> Result<(), SystemdErrors> {
    init_logs();
    let unit_name: &str = TEST_SERVICE;

    kill_unit(UnitDBusLevel::System, unit_name, KillWho::Main, 1)
}

#[ignore = "need a connection to a service"]
#[test]
fn test_queue_signal_unit() -> Result<(), SystemdErrors> {
    init_logs();
    let unit_name: &str = TEST_SERVICE;
    let val: i32 = libc::SIGRTMIN();
    let val2 = libc::SIGRTMAX();

    println!("{val} {val2}");

    queue_signal_unit(UnitDBusLevel::System, unit_name, KillWho::Main, 9, 0)
}

#[ignore = "need a connection to a service"]
#[test]
pub(super) fn test_unit_clean() -> Result<(), SystemdErrors> {
    init_logs();
    let handle_answer = |_method: &str, _return_message: &Message| {
        info!("Clean Unit SUCCESS");

        Ok(())
    };
    let path = unit_dbus_path_from_name(TEST_SERVICE);
    let what = ["logs"];

    let r = send_unit_message(
        UnitDBusLevel::System,
        "Clean",
        &(&what),
        handle_answer,
        &path,
    );

    if let Err(ref e) = r {
        error!("{e:?}");
    }
    r
}

fn send_unit_message<T, U>(
    level: UnitDBusLevel,
    method: &str,
    body: &T,
    handler: impl Fn(&str, &Message) -> Result<U, SystemdErrors>,
    path: &str,
) -> Result<U, SystemdErrors>
where
    T: serde::ser::Serialize + DynamicType,
    U: std::fmt::Debug,
{
    let message = Message::method_call(path, method)?
        .with_flags(Flags::AllowInteractiveAuth)?
        .destination(DESTINATION_SYSTEMD)?
        .interface(INTERFACE_SYSTEMD_UNIT)?
        .build(body)?;

    let connection = get_blocking_connection(level)?;

    connection.send(&message)?;

    let message_it = MessageIterator::from(connection);

    for message_res in message_it {
        debug!("Message response {message_res:?}");
        let return_message = message_res?;

        match return_message.message_type() {
            zbus::message::Type::MethodReturn => {
                info!("{method} Response");
                let result = handler(method, &return_message);
                return result;
            }
            zbus::message::Type::MethodCall => {
                warn!("Not supposed to happen: {return_message:?}");
                break;
            }
            zbus::message::Type::Error => {
                let error = zbus::Error::from(return_message);
                {
                    match error {
                        zbus::Error::MethodError(
                            ref owned_error_name,
                            ref details,
                            ref message,
                        ) => {
                            warn!(
                                "Method error: {}\nDetails: {}\n{:?}",
                                owned_error_name.as_str(),
                                details.as_ref().map(|s| s.as_str()).unwrap_or_default(),
                                message
                            )
                        }
                        _ => warn!("Bus error: {error:?}"),
                    }
                }
                return Err(SystemdErrors::from(error));
            }
            zbus::message::Type::Signal => {
                info!("Signal: {return_message:?}");
                continue;
            }
        }
    }

    let msg = format!("{method:?} ????, response supposed to be Unreachable");
    warn!("{msg}");
    Err(SystemdErrors::Malformed(
        msg,
        "sequences of messages".to_owned(),
    ))
}

#[ignore = "need a connection to a service"]
#[test]
fn test_mask_unit_file() -> Result<(), SystemdErrors> {
    init_logs();
    let unit_name: &str = TEST_SERVICE;

    mask_unit_files(UnitDBusLevel::System, &[unit_name], false, false)?;

    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_mask_unit_file2() -> Result<(), SystemdErrors> {
    init_logs();
    let unit_file_name: &str = "/etc/systemd/system/tiny_daemon.service";

    mask_unit_files(UnitDBusLevel::System, &[unit_file_name], false, false)?;

    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_unmask_unit_file() -> Result<(), SystemdErrors> {
    init_logs();
    let unit_name: &str = TEST_SERVICE;

    unmask_unit_files(UnitDBusLevel::System, &[unit_name], false)?;

    Ok(())
}

#[ignore = "need a connection to a service"]
#[test]
fn test_introspect() -> Result<(), SystemdErrors> {
    init_logs();

    let result: Result<(), SystemdErrors> = {
        let connection = get_blocking_connection(UnitDBusLevel::System)?;
        info!("Connect");

        let message = connection.call_method(
            Some(DESTINATION_SYSTEMD),
            //"/org/freedesktop/systemd1/unit/avahi_2ddaemon_2eservice",
            //"/",
            //"/org/freedesktop/systemd1/unit",
            "/org/freedesktop/systemd1/archlinux_2dkeyring_2dwkd_2dsync_2etimer",
            //  Some("org.freedesktop.DBus.Properties"),
            Some("org.freedesktop.DBus.Introspectable"),
            "Introspect",
            // &(UnitType::Service.interface()),
            &(),
        )?;

        info!("message {message:?}");

        let body = message.body();

        info!("signature {:?}", body.signature());

        //let z: Vec<(String, OwnedValue)> = body.deserialize()?;

        let z: String = body.deserialize()?;
        //let z :String = body.deserialize()?;

        info!("obj {:?}", z);

        /*         let body = message.body();

        let des = body.deserialize();

        println!("{:#?}", des); */

        /*     match get_unit_file_state(level, unit_primary_name) {
            Ok(unit_file_status) => unit.set_enable_status(unit_file_status as u8),
            Err(err) => warn!("Fail to get unit file state : {:?}", err),
        } */
        Ok(())
    };

    if let Err(ref e) = result {
        warn!("ASTFGSDFGD {e:?}");
    }
    result
}

#[ignore = "need a connection to a service"]
#[test]
fn test_introspect2() -> Result<(), SystemdErrors> {
    init_logs();

    fn sub() -> Result<(), SystemdErrors> {
        let connection = get_blocking_connection(UnitDBusLevel::System)?;

        let proxy = Proxy::new(
            &connection,
            DESTINATION_SYSTEMD,
            "/org/freedesktop/systemd1/unit/archlinux_2dkeyring_2dwkd_2dsync_2etimer",
            "org.freedesktop.DBus.Introspectable",
        )?;

        info!("Proxy {proxy:?}");

        let xml = proxy.introspect()?;

        let root_node = zbus_xml::Node::from_reader(xml.as_bytes())?;

        for int in root_node.interfaces() {
            info!("Interface {}", int.name());

            for prop in int.properties() {
                info!("\tProp {} {:?}", prop.name(), prop.ty().to_string());
            }
        }

        Ok(())
    }

    sub().map_err(|e| {
        warn!("ASTFGSDFGD {e:?}");
        e
    })
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_introspect3() -> Result<(), SystemdErrors> {
    init_logs();

    let map = fetch_unit_interface_properties().await?;

    for (k, v) in map.iter() {
        info!("{k}\t{}", v.len());
    }

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_introspect_types() -> Result<(), SystemdErrors> {
    init_logs();

    let map = fetch_unit_interface_properties().await?;
    /*
    let mut signatures = BTreeMap::new();

    for unit_property in map.values().flat_map(|v| v) {
        if let Some(v) = signatures.insert(&unit_property.signature, 1) {
            signatures.insert(&unit_property.signature, v + 1);
        }
    }

    println!("{signatures:#?}"); */

    let signatures = map
        .values()
        .flatten()
        .fold(BTreeMap::new(), |mut acc, unit_property| {
            *acc.entry(&unit_property.signature).or_insert(1) += 1;
            acc
        });

    println!("{signatures:#?}");

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_get_properties2() -> Result<(), SystemdErrors> {
    init_logs();

    let connection = get_connection(UnitDBusLevel::System).await?;

    let object_path = unit_dbus_path_from_name(TEST_SERVICE);
    let message = connection
        .call_method(
            Some(DESTINATION_SYSTEMD),
            //"/org/freedesktop/systemd1/unit/avahi_2ddaemon_2eservice",
            //"/",
            //"/org/freedesktop/systemd1/unit",
            // "/org/freedesktop/systemd1/archlinux_2dkeyring_2dwkd_2dsync_2etimer",
            object_path,
            //  Some("org.freedesktop.DBus.Properties"),
            Some("org.freedesktop.DBus.Properties"),
            "Get",
            // &(UnitType::Service.interface()),
            //      &("org.freedesktop.systemd1.Unit", "Id", "ConditionTimestamp"),
            &("org.freedesktop.systemd1.Unit", "ConditionTimestamp"),
        )
        .await?;

    let body = message.body();

    info!("signature {:?}", body.signature().to_string());

    //let z: Vec<(String, OwnedValue)> = body.deserialize()?;

    //let z: OwnedValue = body.deserialize()?;
    let z: Value = body.deserialize()?;
    //let z :String = body.deserialize()?;

    info!("obj {:?}", z);

    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_reboot() -> Result<(), SystemdErrors> {
    let connection = get_connection(UnitDBusLevel::System).await?;
    reboot_async(connection, true).await?;
    Ok(())
}

#[ignore = "need a connection to a service"]
#[tokio::test]
async fn test_power_off() -> Result<(), SystemdErrors> {
    let connection = get_connection(UnitDBusLevel::System).await?;
    power_off_async(connection).await?;
    Ok(())
}

#[test]
fn test_bytes_msg() {
    init_logs();
    let bytes: [u8; 80] = [
        108, 2, 1, 1, 0, 0, 0, 0, 255, 255, 255, 255, 62, 0, 0, 0, 5, 1, 117, 0, 8, 0, 0, 0, 7, 1,
        115, 0, 20, 0, 0, 0, 111, 114, 103, 46, 102, 114, 101, 101, 100, 101, 115, 107, 116, 111,
        112, 46, 68, 66, 117, 115, 0, 0, 0, 0, 6, 1, 115, 0, 6, 0, 0, 0, 58, 49, 46, 51, 52, 53, 0,
        0, 8, 1, 103, 0, 0, 0, 0, 0,
    ];
    info!("String: {}", "ASDFASDFAF");
    let s = String::from_utf8_lossy(&bytes);
    info!("String: {}", s);
    info!("String: {}", s);
    info!("String: {}", s);
    info!("String: {}", s);
}

#[test]
fn test_bytes_msg2() {
    init_logs();

    info!("String: {}", "ASDFASDFAF");
}