termscp 1.0.0

termscp is a feature rich terminal file transfer and explorer with support for SCP/SFTP/FTP/Kube/S3/WebDAV
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
//! ## BookmarksClient
//!
//! `bookmarks_client` is the module which provides an API between the Bookmarks module and the system

use std::fs::OpenOptions;
use std::path::{Path, PathBuf};
use std::string::ToString;
use std::time::SystemTime;

use super::keys::filestorage::FileStorage;
use super::keys::keyringstorage::KeyringStorage;
use super::keys::{KeyStorage, KeyStorageError};
use crate::config::bookmarks::{Bookmark, UserHosts};
use crate::config::serialization::{SerializerError, SerializerErrorKind, deserialize, serialize};
use crate::filetransfer::FileTransferParams;
use crate::utils::crypto;
use crate::utils::fmt::fmt_time;
use crate::utils::random::random_alphanumeric_with_len;

/// BookmarksClient provides a layer between the host system and the bookmarks module
pub struct BookmarksClient {
    hosts: UserHosts,
    bookmarks_file: PathBuf,
    key: String,
    recents_size: usize,
}

impl BookmarksClient {
    /// Instantiates a new BookmarksClient
    /// Bookmarks file path must be provided
    /// Storage path for file provider must be provided
    pub fn new(
        bookmarks_file: &Path,
        storage_path: &Path,
        recents_size: usize,
        keyring: bool,
    ) -> Result<BookmarksClient, SerializerError> {
        // Create default hosts
        let default_hosts: UserHosts = UserHosts::default();
        debug!("Setting up bookmarks client...");
        // Get key storage
        let (key_storage, service_id) = Self::keyring(storage_path, keyring);
        // Load key
        let key: String = match key_storage.get_key(service_id) {
            Ok(k) => {
                debug!("Key loaded with success");
                k
            }
            Err(e) => match e {
                KeyStorageError::NoSuchKey => {
                    // If no such key, generate key and set it into the storage
                    let key: String = Self::generate_key();
                    debug!("Key doesn't exist yet or could not be loaded; generated a new key");
                    if let Err(e) = key_storage.set_key(service_id, key.as_str()) {
                        error!("Failed to set new key into storage: {}", e);
                        return Err(SerializerError::new_ex(
                            SerializerErrorKind::Io,
                            format!("Could not write key to storage: {e}"),
                        ));
                    }
                    // Return key
                    key
                }
                _ => {
                    error!("Failed to get key from storage: {}", e);
                    return Err(SerializerError::new_ex(
                        SerializerErrorKind::Io,
                        format!("Could not get key from storage: {e}"),
                    ));
                }
            },
        };
        let mut client: BookmarksClient = BookmarksClient {
            hosts: default_hosts,
            bookmarks_file: PathBuf::from(bookmarks_file),
            key,
            recents_size,
        };
        // If bookmark file doesn't exist, initialize it
        if !bookmarks_file.exists() {
            info!("Bookmarks file doesn't exist yet; creating it...");
            if let Err(err) = client.write_bookmarks() {
                error!("Failed to create bookmarks file: {}", err);
                return Err(err);
            }
        } else {
            // Load bookmarks from file
            if let Err(err) = client.read_bookmarks() {
                error!("Failed to load bookmarks: {}", err);
                return Err(err);
            }
        }
        info!("Bookmarks client initialized");
        // Load key
        Ok(client)
    }

    /// Get the key storage
    fn keyring(storage_path: &Path, keyring: bool) -> (Box<dyn KeyStorage>, &'static str) {
        if keyring && cfg!(feature = "keyring") {
            debug!("Setting up KeyStorage");
            let username = whoami::username().unwrap_or_default();
            let storage: KeyringStorage = KeyringStorage::new(username.as_str());
            // Check if keyring storage is supported
            #[cfg(not(test))]
            let app_name: &str = "termscp";
            #[cfg(test)] // NOTE: when running test, add -test
            let app_name: &str = "termscp-test";
            match storage.is_supported() {
                true => {
                    debug!("Using KeyringStorage");
                    (Box::new(storage), app_name)
                }
                false => {
                    warn!("KeyringStorage is not supported; using FileStorage");
                    (Box::new(FileStorage::new(storage_path)), "bookmarks")
                }
            }
        } else {
            #[cfg(not(test))]
            let app_name: &str = "bookmarks";
            #[cfg(test)] // NOTE: when running test, add -test
            let app_name: &str = "bookmarks-test";
            debug!("Using FileStorage");
            (Box::new(FileStorage::new(storage_path)), app_name)
        }
    }

    /// Iterate over bookmarks keys
    pub fn iter_bookmarks(&self) -> impl Iterator<Item = &String> + '_ {
        Box::new(self.hosts.bookmarks.keys())
    }

    /// Get bookmark associated to key
    pub fn get_bookmark(&self, key: &str) -> Option<FileTransferParams> {
        debug!("Getting bookmark {}", key);
        let mut entry: Bookmark = self.hosts.bookmarks.get(key).cloned()?;
        // Decrypt password first
        if let Some(pwd) = entry.password.as_mut() {
            match self.decrypt_str(pwd.as_str()) {
                Ok(decrypted_pwd) => {
                    *pwd = decrypted_pwd;
                }
                Err(err) => {
                    error!("Failed to decrypt `password` for bookmark {}: {}", key, err);
                }
            }
        }
        // Decrypt AWS-S3 params
        if let Some(s3) = entry.s3.as_mut() {
            // Access key
            if let Some(access_key) = s3.access_key.as_mut() {
                match self.decrypt_str(access_key.as_str()) {
                    Ok(plain) => {
                        *access_key = plain;
                    }
                    Err(err) => {
                        error!(
                            "Failed to decrypt `access_key` for bookmark {}: {}",
                            key, err
                        );
                    }
                }
            }
            // Secret access key
            if let Some(secret_access_key) = s3.secret_access_key.as_mut() {
                match self.decrypt_str(secret_access_key.as_str()) {
                    Ok(plain) => {
                        *secret_access_key = plain;
                    }
                    Err(err) => {
                        error!(
                            "Failed to decrypt `secret_access_key` for bookmark {}: {}",
                            key, err
                        );
                    }
                }
            }
        }
        // Then convert into
        Some(FileTransferParams::from(entry))
    }

    /// Add a new recent to bookmarks
    pub fn add_bookmark<S: AsRef<str>>(
        &mut self,
        name: S,
        params: FileTransferParams,
        save_password: bool,
    ) -> Result<(), SerializerError> {
        let name: String = name.as_ref().to_string();
        if name.is_empty() {
            error!("Bookmark name is empty; ignoring add_bookmark request");
            return Ok(());
        }
        // Make bookmark
        info!("Added bookmark {}", name);
        let mut host: Bookmark = self.make_bookmark(params)?;
        // If not save_password, set secrets to `None`
        if !save_password {
            host.password = None;
            if let Some(s3) = host.s3.as_mut() {
                s3.access_key = None;
                s3.secret_access_key = None;
            }
        }
        self.hosts.bookmarks.insert(name, host);
        Ok(())
    }

    /// Delete entry from bookmarks
    pub fn del_bookmark(&mut self, name: &str) {
        let _ = self.hosts.bookmarks.remove(name);
        info!("Removed bookmark {}", name);
    }
    /// Iterate over recents keys
    pub fn iter_recents(&self) -> impl Iterator<Item = &String> + '_ {
        Box::new(self.hosts.recents.keys())
    }

    /// Get recent associated to key
    pub fn get_recent(&self, key: &str) -> Option<FileTransferParams> {
        // NOTE: password is not decrypted; recents will never have password
        info!("Getting bookmark {}", key);
        let entry: Bookmark = self.hosts.recents.get(key).cloned()?;
        Some(FileTransferParams::from(entry))
    }

    /// Add a new recent to bookmarks
    pub fn add_recent(&mut self, params: FileTransferParams) -> Result<(), SerializerError> {
        // Make bookmark
        let mut host: Bookmark = self.make_bookmark(params)?;
        // Null password for recents
        host.password = None;
        if let Some(s3) = host.s3.as_mut() {
            s3.access_key = None;
            s3.secret_access_key = None;
        }
        // Check if duplicated
        for (key, value) in &self.hosts.recents {
            if *value == host {
                debug!("Discarding recent since duplicated ({})", key);
                // Don't save duplicates
                return Ok(());
            }
        }
        // If hosts size is bigger than self.recents_size; pop last
        if self.hosts.recents.len() >= self.recents_size {
            // Get keys
            let mut keys: Vec<String> = Vec::with_capacity(self.hosts.recents.len());
            for key in self.hosts.recents.keys() {
                keys.push(key.clone());
            }
            // Sort keys; NOTE: most recent is the last element
            keys.sort();
            // Delete keys starting from the last one
            for key in keys.iter() {
                let _ = self.hosts.recents.remove(key);
                debug!("Removed recent bookmark {}", key);
                // If length is < self.recents_size; break
                if self.hosts.recents.len() < self.recents_size {
                    break;
                }
            }
        }
        let name: String = fmt_time(SystemTime::now(), "ISO%Y%m%dT%H%M%S");
        info!("Saved recent host {}", name);
        self.hosts.recents.insert(name, host);
        Ok(())
    }

    /// Delete entry from recents
    pub fn del_recent(&mut self, name: &str) {
        let _ = self.hosts.recents.remove(name);
        info!("Removed recent host {}", name);
    }

    /// Write bookmarks to file
    pub fn write_bookmarks(&self) -> Result<(), SerializerError> {
        // Open file
        debug!("Writing bookmarks");
        match OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(self.bookmarks_file.as_path())
        {
            Ok(writer) => serialize(&self.hosts, Box::new(writer)),
            Err(err) => {
                error!("Failed to write bookmarks: {}", err);
                Err(SerializerError::new_ex(
                    SerializerErrorKind::Io,
                    err.to_string(),
                ))
            }
        }
    }

    /// Read bookmarks from file
    fn read_bookmarks(&mut self) -> Result<(), SerializerError> {
        // Open bookmarks file for read
        debug!("Reading bookmarks");
        match OpenOptions::new()
            .read(true)
            .open(self.bookmarks_file.as_path())
        {
            Ok(reader) => {
                // Deserialize
                match deserialize(Box::new(reader)) {
                    Ok(hosts) => {
                        self.hosts = hosts;
                        Ok(())
                    }
                    Err(err) => Err(err),
                }
            }
            Err(err) => {
                error!("Failed to read bookmarks: {}", err);
                Err(SerializerError::new_ex(
                    SerializerErrorKind::Io,
                    err.to_string(),
                ))
            }
        }
    }

    /// Generate a new AES key
    fn generate_key() -> String {
        // Generate 256 bytes (2048 bits) key
        random_alphanumeric_with_len(256)
    }

    /// Make bookmark from credentials
    fn make_bookmark(&self, params: FileTransferParams) -> Result<Bookmark, SerializerError> {
        let mut bookmark: Bookmark = Bookmark::from(params);
        // Encrypt password
        if let Some(pwd) = bookmark.password {
            bookmark.password = Some(self.encrypt_str(pwd.as_str())?);
        }
        // Encrypt aws s3 params
        if let Some(s3) = bookmark.s3.as_mut() {
            if let Some(access_key) = s3.access_key.as_mut() {
                *access_key = self.encrypt_str(access_key.as_str())?;
            }
            if let Some(secret_access_key) = s3.secret_access_key.as_mut() {
                *secret_access_key = self.encrypt_str(secret_access_key.as_str())?;
            }
        }
        Ok(bookmark)
    }

    /// Encrypt provided string using AES-128. Encrypted buffer is then converted to BASE64
    fn encrypt_str(&self, txt: &str) -> Result<String, SerializerError> {
        crypto::aes128_b64_crypt(self.key.as_str(), txt).map_err(|err| {
            SerializerError::new_ex(SerializerErrorKind::Serialization, err.to_string())
        })
    }

    /// Decrypt provided string using AES-128
    fn decrypt_str(&self, secret: &str) -> Result<String, SerializerError> {
        match crypto::aes128_b64_decrypt(self.key.as_str(), secret) {
            Ok(txt) => Ok(txt),
            Err(err) => Err(SerializerError::new_ex(
                SerializerErrorKind::Syntax,
                err.to_string(),
            )),
        }
    }
}

#[cfg(test)]
#[cfg(not(target_os = "macos"))] // CI/CD blocks
mod tests {

    use std::thread::sleep;
    use std::time::Duration;

    use pretty_assertions::assert_eq;
    use tempfile::TempDir;

    use super::*;
    use crate::filetransfer::params::{AwsS3Params, GenericProtocolParams};
    use crate::filetransfer::{FileTransferProtocol, ProtocolParams};

    #[test]

    fn test_system_bookmarks_new() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Verify client
        assert_eq!(client.hosts.bookmarks.len(), 0);
        assert_eq!(client.hosts.recents.len(), 0);
        assert_eq!(client.key.len(), 256);
        assert_eq!(client.bookmarks_file, cfg_path);
        assert_eq!(client.recents_size, 16);
    }

    #[test]

    fn test_system_bookmarks_new_from_existing() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add some bookmarks
        assert!(
            client
                .add_bookmark(
                    "raspberry",
                    make_generic_ftparams(
                        FileTransferProtocol::Sftp,
                        "192.168.1.31",
                        22,
                        "pi",
                        Some("mypassword"),
                    ),
                    true,
                )
                .is_ok()
        );
        assert!(
            client
                .add_recent(make_generic_ftparams(
                    FileTransferProtocol::Sftp,
                    "192.168.1.31",
                    22,
                    "pi",
                    Some("mypassword"),
                ))
                .is_ok()
        );
        let recent_key: String = String::from(client.iter_recents().next().unwrap());
        assert!(client.write_bookmarks().is_ok());
        let key: String = client.key.clone();
        // Re-initialize a client
        let client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Verify it loaded parameters correctly
        assert_eq!(client.key, key);
        let bookmark = ftparams_to_tup(client.get_bookmark("raspberry").unwrap());
        assert_eq!(bookmark.0, String::from("192.168.1.31"));
        assert_eq!(bookmark.1, 22);
        assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
        assert_eq!(bookmark.3, String::from("pi"));
        assert_eq!(*bookmark.4.as_ref().unwrap(), String::from("mypassword"));
        let bookmark = ftparams_to_tup(client.get_recent(&recent_key).unwrap());
        assert_eq!(bookmark.0, String::from("192.168.1.31"));
        assert_eq!(bookmark.1, 22);
        assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
        assert_eq!(bookmark.3, String::from("pi"));
        assert_eq!(bookmark.4, None);
    }

    #[test]
    fn should_make_s3_bookmark_with_secrets() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add s3 bookmark
        assert!(
            client
                .add_bookmark("my-bucket", make_s3_ftparams(), true)
                .is_ok()
        );
        // Verify bookmark
        let bookmark = client.get_bookmark("my-bucket").unwrap();
        assert_eq!(bookmark.protocol, FileTransferProtocol::AwsS3);
        let params = bookmark.params.s3_params().unwrap();
        assert_eq!(params.access_key.as_deref().unwrap(), "pippo");
        assert_eq!(params.profile.as_deref().unwrap(), "test");
        assert_eq!(params.secret_access_key.as_deref().unwrap(), "pluto");
        assert_eq!(params.bucket_name.as_str(), "omar");
        assert_eq!(params.region.as_deref().unwrap(), "eu-west-1");
    }

    #[test]
    fn should_make_s3_bookmark_without_secrets() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add s3 bookmark
        assert!(
            client
                .add_bookmark("my-bucket", make_s3_ftparams(), false)
                .is_ok()
        );
        // Verify bookmark
        let bookmark = client.get_bookmark("my-bucket").unwrap();
        assert_eq!(bookmark.protocol, FileTransferProtocol::AwsS3);
        let params = bookmark.params.s3_params().unwrap();
        assert_eq!(params.profile.as_deref().unwrap(), "test");
        assert_eq!(params.bucket_name.as_str(), "omar");
        assert_eq!(params.region.as_deref().unwrap(), "eu-west-1");
        // secrets
        assert_eq!(params.access_key, None);
        assert_eq!(params.secret_access_key, None);
    }

    #[test]
    fn should_make_s3_recent() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add s3 bookmark
        assert!(client.add_recent(make_s3_ftparams()).is_ok());
        // Verify bookmark
        let bookmark = client.iter_recents().next().unwrap();
        let bookmark = client.get_recent(bookmark).unwrap();
        assert_eq!(bookmark.protocol, FileTransferProtocol::AwsS3);
        let params = bookmark.params.s3_params().unwrap();
        assert_eq!(params.profile.as_deref().unwrap(), "test");
        assert_eq!(params.bucket_name.as_str(), "omar");
        assert_eq!(params.region.as_deref().unwrap(), "eu-west-1");
        // secrets
        assert_eq!(params.access_key, None);
        assert_eq!(params.secret_access_key, None);
    }

    #[test]

    fn test_system_bookmarks_manipulate_bookmarks() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add bookmark
        assert!(
            client
                .add_bookmark(
                    "raspberry",
                    make_generic_ftparams(
                        FileTransferProtocol::Sftp,
                        "192.168.1.31",
                        22,
                        "pi",
                        Some("mypassword"),
                    ),
                    true,
                )
                .is_ok()
        );
        assert!(
            client
                .add_bookmark(
                    "raspberry2",
                    make_generic_ftparams(
                        FileTransferProtocol::Sftp,
                        "192.168.1.31",
                        22,
                        "pi",
                        Some("mypassword2"),
                    ),
                    true,
                )
                .is_ok()
        );
        // Iter
        assert_eq!(client.iter_bookmarks().count(), 2);
        // Get bookmark
        let bookmark = ftparams_to_tup(client.get_bookmark(&String::from("raspberry")).unwrap());
        assert_eq!(bookmark.0, String::from("192.168.1.31"));
        assert_eq!(bookmark.1, 22);
        assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
        assert_eq!(bookmark.3, String::from("pi"));
        assert_eq!(*bookmark.4.as_ref().unwrap(), String::from("mypassword"));
        // Write bookmarks
        assert!(client.write_bookmarks().is_ok());
        // Delete bookmark
        client.del_bookmark(&String::from("raspberry"));
        // Get unexisting bookmark
        assert!(client.get_bookmark(&String::from("raspberry")).is_none());
        // Write bookmarks
        assert!(client.write_bookmarks().is_ok());
    }

    #[test]
    fn test_system_bookmarks_bad_bookmark_name() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add bookmark with empty name should be silently ignored
        assert!(
            client
                .add_bookmark(
                    "",
                    make_generic_ftparams(
                        FileTransferProtocol::Sftp,
                        "192.168.1.31",
                        22,
                        "pi",
                        Some("mypassword"),
                    ),
                    true,
                )
                .is_ok()
        );
        // No bookmark should have been added
        assert_eq!(client.iter_bookmarks().count(), 0);
    }

    #[test]
    fn save_bookmark_wno_password() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add bookmark
        assert!(
            client
                .add_bookmark(
                    "raspberry",
                    make_generic_ftparams(
                        FileTransferProtocol::Sftp,
                        "192.168.1.31",
                        22,
                        "pi",
                        Some("mypassword"),
                    ),
                    false,
                )
                .is_ok()
        );
        let bookmark = ftparams_to_tup(client.get_bookmark(&String::from("raspberry")).unwrap());
        assert_eq!(bookmark.0, String::from("192.168.1.31"));
        assert_eq!(bookmark.1, 22);
        assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
        assert_eq!(bookmark.3, String::from("pi"));
        assert_eq!(bookmark.4, None);
    }

    #[test]

    fn test_system_bookmarks_manipulate_recents() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add bookmark
        assert!(
            client
                .add_recent(make_generic_ftparams(
                    FileTransferProtocol::Sftp,
                    "192.168.1.31",
                    22,
                    "pi",
                    Some("mypassword"),
                ))
                .is_ok()
        );
        // Iter
        assert_eq!(client.iter_recents().count(), 1);
        let key: String = String::from(client.iter_recents().next().unwrap());
        // Get bookmark
        let bookmark = ftparams_to_tup(client.get_recent(&key).unwrap());
        assert_eq!(bookmark.0, String::from("192.168.1.31"));
        assert_eq!(bookmark.1, 22);
        assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
        assert_eq!(bookmark.3, String::from("pi"));
        assert_eq!(bookmark.4, None);
        // Write bookmarks
        assert!(client.write_bookmarks().is_ok());
        // Delete bookmark
        client.del_recent(&key);
        // Get unexisting bookmark
        assert!(client.get_bookmark(&key).is_none());
        // Write bookmarks
        assert!(client.write_bookmarks().is_ok());
    }

    #[test]

    fn test_system_bookmarks_dup_recent() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add bookmark
        assert!(
            client
                .add_recent(make_generic_ftparams(
                    FileTransferProtocol::Sftp,
                    "192.168.1.31",
                    22,
                    "pi",
                    Some("mypassword"),
                ))
                .is_ok()
        );
        assert!(
            client
                .add_recent(make_generic_ftparams(
                    FileTransferProtocol::Sftp,
                    "192.168.1.31",
                    22,
                    "pi",
                    Some("mypassword"),
                ))
                .is_ok()
        );
        // There should be only one recent
        assert_eq!(client.iter_recents().count(), 1);
    }

    #[test]

    fn test_system_bookmarks_recents_more_than_limit() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 2, true).unwrap();
        // Add recent, wait 1 second for each one (cause the name depends on time)
        // 1
        assert!(
            client
                .add_recent(make_generic_ftparams(
                    FileTransferProtocol::Sftp,
                    "192.168.1.1",
                    22,
                    "pi",
                    Some("mypassword"),
                ))
                .is_ok()
        );
        sleep(Duration::from_secs(1));
        // 2
        assert!(
            client
                .add_recent(make_generic_ftparams(
                    FileTransferProtocol::Sftp,
                    "192.168.1.2",
                    22,
                    "pi",
                    Some("mypassword"),
                ))
                .is_ok()
        );
        sleep(Duration::from_secs(1));
        // 3
        assert!(
            client
                .add_recent(make_generic_ftparams(
                    FileTransferProtocol::Sftp,
                    "192.168.1.3",
                    22,
                    "pi",
                    Some("mypassword"),
                ))
                .is_ok()
        );
        // Limit is 2
        assert_eq!(client.iter_recents().count(), 2);
        // Check that 192.168.1.1 has been removed
        let key: String = client.iter_recents().next().unwrap().to_string();
        assert!(matches!(
            client
                .hosts
                .recents
                .get(&key)
                .unwrap()
                .address
                .as_ref()
                .cloned()
                .unwrap_or_default()
                .as_str(),
            "192.168.1.2" | "192.168.1.3"
        ));
        let key: String = client.iter_recents().nth(1).unwrap().to_string();
        assert!(matches!(
            client
                .hosts
                .recents
                .get(&key)
                .unwrap()
                .address
                .as_ref()
                .cloned()
                .unwrap_or_default()
                .as_str(),
            "192.168.1.2" | "192.168.1.3"
        ));
    }

    #[test]
    fn test_system_bookmarks_add_bookmark_empty() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        // Add bookmark with empty name should be silently ignored
        assert!(
            client
                .add_bookmark(
                    "",
                    make_generic_ftparams(
                        FileTransferProtocol::Sftp,
                        "192.168.1.31",
                        22,
                        "pi",
                        Some("mypassword"),
                    ),
                    true,
                )
                .is_ok()
        );
        // No bookmark should have been added
        assert_eq!(client.iter_bookmarks().count(), 0);
    }

    #[test]
    fn test_system_bookmarks_decrypt_str() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        // Initialize a new bookmarks client
        let mut client: BookmarksClient =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        client.key = "MYSUPERSECRETKEY".to_string();
        assert_eq!(
            client.decrypt_str("z4Z6LpcpYqBW4+bkIok+5A==").ok().unwrap(),
            "Hello world!"
        );
        assert!(client.decrypt_str("bidoof").is_err());
    }

    #[test]
    fn should_return_bookmark_when_password_decryption_fails() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        let mut client =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        let mut bookmark = Bookmark::from(make_generic_ftparams(
            FileTransferProtocol::Sftp,
            "192.168.1.31",
            22,
            "pi",
            Some("mypassword"),
        ));
        bookmark.password = Some(String::from("not-valid-base64"));
        client
            .hosts
            .bookmarks
            .insert(String::from("raspberry"), bookmark);

        let bookmark = ftparams_to_tup(client.get_bookmark("raspberry").unwrap());

        assert_eq!(bookmark.0, String::from("192.168.1.31"));
        assert_eq!(bookmark.1, 22);
        assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
        assert_eq!(bookmark.3, String::from("pi"));
        assert_eq!(bookmark.4.as_deref(), Some("not-valid-base64"));
    }

    #[test]
    fn should_return_s3_bookmark_when_secret_decryption_fails() {
        let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
        let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
        let mut client =
            BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
        let mut bookmark = Bookmark::from(make_s3_ftparams());
        let s3 = bookmark.s3.as_mut().unwrap();
        s3.access_key = Some(String::from("bad-access-key"));
        s3.secret_access_key = Some(String::from("bad-secret-key"));
        client
            .hosts
            .bookmarks
            .insert(String::from("my-bucket"), bookmark);

        let bookmark = client.get_bookmark("my-bucket").unwrap();
        let params = bookmark.params.s3_params().unwrap();

        assert_eq!(bookmark.protocol, FileTransferProtocol::AwsS3);
        assert_eq!(params.bucket_name.as_str(), "omar");
        assert_eq!(params.region.as_deref(), Some("eu-west-1"));
        assert_eq!(params.access_key.as_deref(), Some("bad-access-key"));
        assert_eq!(params.secret_access_key.as_deref(), Some("bad-secret-key"));
    }

    /// Get paths for configuration and key for bookmarks
    fn get_paths(dir: &Path) -> (PathBuf, PathBuf) {
        let k: PathBuf = PathBuf::from(dir);
        let mut c: PathBuf = k.clone();
        c.push("bookmarks.toml");
        (c, k)
    }

    fn make_generic_ftparams(
        protocol: FileTransferProtocol,
        address: &str,
        port: u16,
        username: &str,
        password: Option<&str>,
    ) -> FileTransferParams {
        let params = ProtocolParams::Generic(
            GenericProtocolParams::default()
                .address(address)
                .port(port)
                .username(Some(username))
                .password(password),
        );
        FileTransferParams::new(protocol, params)
    }

    fn make_s3_ftparams() -> FileTransferParams {
        FileTransferParams::new(
            FileTransferProtocol::AwsS3,
            ProtocolParams::AwsS3(
                AwsS3Params::new("omar", Some("eu-west-1"), Some("test"))
                    .endpoint(Some("http://localhost:9000"))
                    .new_path_style(false)
                    .access_key(Some("pippo"))
                    .secret_access_key(Some("pluto"))
                    .security_token(Some("omar"))
                    .session_token(Some("gerry-scotti")),
            ),
        )
    }

    fn ftparams_to_tup(
        params: FileTransferParams,
    ) -> (String, u16, FileTransferProtocol, String, Option<String>) {
        let protocol = params.protocol;
        let p = params.params.generic_params().unwrap();
        (
            p.address.to_string(),
            p.port,
            protocol,
            p.username.as_ref().cloned().unwrap_or_default(),
            p.password.as_ref().cloned(),
        )
    }
}