keeper-secrets-manager-core 17.2.0

Rust SDK for Keeper Secrets 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
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
// -*- coding: utf-8 -*-
//  _  __
// | |/ /___ ___ _ __  ___ _ _ (R)
// | ' </ -_) -_) '_ \/ -_) '_|
// |_|\_\___\___| .__/\___|_|
//              |_|
//
// Keeper Secrets Manager
// Copyright 2024 Keeper Security Inc.
// Contact: sm@keepersecurity.com
//

#[cfg(test)]
mod str_to_bool_tests {
    use crate::utils::str_to_bool;

    #[test]
    fn test_str_to_bool_true_values() {
        // Test all inputs that should return `Ok(true)`
        let true_values = vec!["y", "yes", "t", "true", "on", "1"];

        for val in true_values {
            assert_eq!(
                str_to_bool(val),
                Ok(true),
                "Expected true for input: {}",
                val
            );
        }
    }

    #[test]
    fn test_str_to_bool_false_values() {
        // Test all inputs that should return `Ok(false)`
        let false_values = vec!["n", "no", "f", "false", "off", "0"];

        for val in false_values {
            assert_eq!(
                str_to_bool(val),
                Ok(false),
                "Expected false for input: {}",
                val
            );
        }
    }

    #[test]
    fn test_str_to_bool_case_insensitivity() {
        // Test case-insensitive handling
        let mixed_case_true = vec!["Y", "Yes", "T", "True", "ON", "1"];
        let mixed_case_false = vec!["N", "NO", "F", "False", "OFF", "0"];

        for val in mixed_case_true {
            assert_eq!(
                str_to_bool(val),
                Ok(true),
                "Expected true for input: {}",
                val
            );
        }

        for val in mixed_case_false {
            assert_eq!(
                str_to_bool(val),
                Ok(false),
                "Expected false for input: {}",
                val
            );
        }
    }

    #[test]
    fn test_str_to_bool_invalid_values() {
        // Test invalid inputs that should return an Err
        let invalid_values = vec!["maybe", "onoff", "2", "tru", "yesno", ""];

        for val in invalid_values {
            assert!(
                str_to_bool(val).is_err(),
                "Expected error for input: {}",
                val
            );
        }
    }

    #[test]
    fn test_str_to_bool_error_message() {
        // Test the exact error message for an invalid value
        let invalid_input = "invalid";
        let expected_error = format!("invalid truth value {:?}", invalid_input.to_lowercase());
        assert_eq!(str_to_bool(invalid_input), Err(expected_error));
    }
}

#[cfg(test)]
mod get_os_tests {
    #[cfg(test)]
    use crate::utils::determine_os;

    #[test]
    fn test_get_os_linux() {
        let os = determine_os("linux");
        assert_eq!(os, "linux");
    }

    #[test]
    fn test_get_os_macos() {
        let os = determine_os("macos");
        assert_eq!(os, "macOS");
    }

    #[test]
    fn test_get_os_windows_win32() {
        // Simulate 32-bit Windows
        if cfg!(target_os = "windows") {
            let os = determine_os("windows");
            let windows_32 = os == "win32";
            let windows_64 = os == "win64";
            assert_eq!(true, windows_32 || windows_64);
        }
    }

    #[test]
    fn test_get_os_windows_win64() {
        // Simulate 64-bit Windows
        if cfg!(target_arch = "x86_64") {
            let os = determine_os("windows");
            let windows_32 = os == "win32";
            let windows_64 = os == "win64";
            assert_eq!(true, windows_32 || windows_64);
        }
    }

    #[test]
    fn test_get_os_other_os() {
        let os = determine_os("freebsd");
        assert_eq!(os, "freebsd");

        let os = determine_os("solaris");
        assert_eq!(os, "solaris");

        let os = determine_os("unknown");
        assert_eq!(os, "unknown");
    }
}

#[cfg(test)]
mod bytes_to_string_tests {
    use crate::utils::bytes_to_string;

    #[test]
    fn test_bytes_to_string_valid_utf8() {
        let bytes = b"hello";
        let result = bytes_to_string(bytes);
        assert_eq!(result, Ok("hello".to_string()));

        let bytes = b"world";
        let result = bytes_to_string(bytes);
        assert_eq!(result, Ok("world".to_string()));

        let bytes = b"rust programming";
        let result = bytes_to_string(bytes);
        assert_eq!(result, Ok("rust programming".to_string()));
    }

    #[test]
    fn test_bytes_to_string_invalid_utf8() {
        let bytes: &[u8] = &[0, 159, 146, 150]; // Invalid UTF-8 sequence
        let result = bytes_to_string(bytes);
        assert!(result.is_err());
    }

    #[test]
    fn test_bytes_to_string_empty_bytes() {
        let bytes: &[u8] = b"";
        let result = bytes_to_string(bytes);
        assert_eq!(result, Ok("".to_string())); // Empty string from empty byte slice
    }

    #[test]
    fn test_bytes_to_string_mixed_bytes() {
        let bytes: &[u8] = b"valid\xFFinvalid"; // Mixed valid and invalid bytes
        let result = bytes_to_string(bytes);
        assert!(result.is_err());
    }
}

#[cfg(test)]
mod bytes_to_int_tests {
    use num_bigint::BigUint;

    use crate::utils::bytes_to_int;

    #[test]
    fn test_bytes_to_int_valid() {
        let bytes = &[0x01, 0x02, 0x03, 0x04]; // 16909060 in decimal
        let result = bytes_to_int(bytes).unwrap();
        assert_eq!(result, BigUint::from(16909060u64));

        let bytes = &[0xFF, 0xFF, 0xFF, 0xFF]; // 4294967295 in decimal
        let result = bytes_to_int(bytes).unwrap();
        assert_eq!(result, BigUint::from(4294967295u64));

        let bytes = &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]; // 1 in decimal
        let result = bytes_to_int(bytes).unwrap();
        assert_eq!(result, BigUint::from(1u64));

        let bytes = &[0x7F, 0xFF, 0xFF, 0xFF, 0xFF]; // 4294967295 in decimal
        let result = bytes_to_int(bytes).unwrap();
        assert_eq!(result, BigUint::from(549755813887u64));
    }

    #[test]
    fn test_bytes_to_int_empty() {
        let bytes: &[u8] = &[];
        let result = bytes_to_int(bytes).unwrap();
        assert_eq!(result, BigUint::from(0u32)); // Should return None for empty byte slice
    }

    #[test]
    fn test_bytes_to_int_single_byte() {
        let bytes = &[0x01]; // Should return 1
        let result = bytes_to_int(bytes).unwrap();
        assert_eq!(result, BigUint::from(1u32));

        let bytes = &[0xFF]; // Should return 255
        let result = bytes_to_int(bytes).unwrap();
        assert_eq!(result, BigUint::from(255u32));
    }
}

#[cfg(test)]
mod bytes_to_base64_tests {
    use crate::utils::bytes_to_base64;

    #[test]
    fn test_bytes_to_base64_basic() {
        let bytes = b"hello";
        let expected = "aGVsbG8="; // Base64 encoded "hello"
        assert_eq!(bytes_to_base64(bytes), expected);
    }

    #[test]
    fn test_bytes_to_base64_empty() {
        let bytes: &[u8] = b""; // Empty input
        let expected = ""; // Base64 encoding of empty input is also empty
        assert_eq!(bytes_to_base64(bytes), expected);
    }

    #[test]
    fn test_bytes_to_base64_non_ascii() {
        let bytes = [0xF0, 0x9F, 0x98, 0x80]; // Unicode smiley face "😀"
        let expected = "8J+YgA==";
        assert_eq!(bytes_to_base64(&bytes), expected);
    }

    #[test]
    fn test_bytes_to_base64_binary_data() {
        let bytes = [0xFF, 0xD8, 0xFF, 0xE0]; // Start of JPEG header
        let expected = "/9j/4A==";
        assert_eq!(bytes_to_base64(&bytes), expected);
    }
}

#[cfg(test)]
mod base64_to_string_tests {
    use crate::utils::base64_to_string;
    use base64::{
        engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD},
        Engine as _,
    };
    #[test]
    fn test_base64_to_string_valid() {
        let base64_input = STANDARD.encode("hello");
        let expected = "hello";
        assert_eq!(base64_to_string(&base64_input).unwrap(), expected);
    }

    #[test]
    fn test_base64_to_string_empty() {
        let base64_input = URL_SAFE_NO_PAD.encode("");
        let expected = "";
        assert_eq!(base64_to_string(&base64_input).unwrap(), expected);
    }

    #[test]
    fn test_base64_to_string_invalid_base64() {
        let base64_input = "!!not_base64!!";
        let result = base64_to_string(base64_input);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Failed to decode Base64 string"));
    }

    #[test]
    fn test_base64_to_string_invalid_utf8() {
        let invalid_utf8_bytes = vec![0xF0, 0x28, 0x8C, 0x28]; // Invalid UTF-8 byte sequence
        let base64_input = URL_SAFE_NO_PAD.encode(&invalid_utf8_bytes);
        let result = base64_to_string(&base64_input);
        assert!(result.is_err());
        assert_eq!(
            result,
            Err(crate::custom_error::KSMRError::DecodeError(
                "Failed to decode Base64 string".to_string()
            ))
        );
    }
}

#[cfg(test)]
mod url_safe_str_to_bytes_tests {
    use crate::{custom_error::KSMRError, utils::url_safe_str_to_bytes};

    #[test]
    fn test_url_safe_str_to_bytes_valid() {
        let input = "dGVzdGxlbjgq"; // This is base64 for "testlen8"
        let result = url_safe_str_to_bytes(input);
        assert_eq!(result, Ok(vec![116, 101, 115, 116, 108, 101, 110, 56, 42]));
        // Valid input
    }

    #[test]
    fn test_url_safe_str_to_bytes_invalid() {
        let input = "invalid_base64_string";
        let result = url_safe_str_to_bytes(input);
        assert_eq!(
            result,
            Err(KSMRError::DecodeError(
                "Invalid input length: 21".to_string()
            ))
        ); // Expect an invalid base64 error
    }

    #[test]
    fn test_url_safe_str_to_bytes_empty() {
        let input = "";
        let result = url_safe_str_to_bytes(input);
        assert_eq!(result, Ok(Vec::new())); // Expect decoded bytes too short error when we are checking for minimum length
                                            // assert_eq!(result, Ok(vec![]));
    }

    #[test]
    fn test_url_safe_str_to_bytes_special_characters() {
        let input = "dGVzdGxlbjgq"; // This is base64 for "star"
        let result = url_safe_str_to_bytes(input);
        // Assuming that this special character results in fewer than 8 bytes
        assert_eq!(result, Ok(vec![116, 101, 115, 116, 108, 101, 110, 56, 42]));
    }
}

#[cfg(test)]
mod url_safe_str_to_int_tests {
    use num_bigint::BigUint;
    use std::str::FromStr;

    use crate::{custom_error::KSMRError, utils::url_safe_str_to_int};

    #[test]
    fn test_url_safe_str_to_int_invalid_base64() {
        let input = "invalid_base64_string!";
        let result = url_safe_str_to_int(input);
        assert_eq!(
            result,
            Err(KSMRError::DecodeError(
                "Invalid symbol 33, offset 21.".to_string()
            ))
        );
    }

    #[test]
    fn test_url_safe_str_to_int_decoded_bytes_too_short() {
        let input = "aA=="; // Base64 for "a" (1 byte)
        let result = url_safe_str_to_int(input);
        assert_eq!(
            result,
            Err(KSMRError::DecodeError("Invalid padding".to_string()))
        ); // Expecting InsufficientBytes error
    }

    #[test]
    fn test_url_safe_str_to_int_empty_string() {
        let input = "";
        let result = url_safe_str_to_int(input);
        assert_ne!(
            result,
            Err(KSMRError::InsufficientBytes(
                "Input string is empty".to_string()
            ))
        );
    }

    #[test]
    fn test_url_safe_str_to_int_special_characters() {
        let input = "4oCU";
        let _expected = BigUint::from_str("14844052").unwrap(); // this is expected if decoder bytes should be of at least 8 bytes long condition is not present.
        let result = url_safe_str_to_int(input);
        assert_eq!(result, Ok(_expected));
    }
}

#[cfg(test)]
mod generate_uid_bytes_tests {
    use crate::utils::generate_uid_bytes;
    use std::collections::HashSet;
    #[test]
    fn test_generate_uid_bytes_with_valid_dash() {
        let uid_bytes = generate_uid_bytes();

        assert!(uid_bytes.len() == 16); // Ensure the length of uid_bytes is 16

        let mut bytes_hashset: HashSet<_> = HashSet::new();
        for _ in 1..=100 {
            bytes_hashset.insert(generate_uid_bytes());
        }

        assert_eq!(bytes_hashset.len(), 100);
    }
}

#[cfg(test)]
mod temp_otp_codes_tests {
    use crate::utils::TotpCode;

    #[test]
    fn test_totp_code_creation() {
        let cases = vec![
            ("123456".to_string(), 30, 60),
            ("654321".to_string(), 10, 30),
            ("111111".to_string(), 0, 60),
            ("999999".to_string(), 5, 45),
        ];

        for (code, time_left, period) in cases {
            let totp = TotpCode::new(code.clone(), time_left, period);
            assert_eq!(totp.get_code(), code);
            assert_eq!(totp.get_time_left(), time_left);
            assert_eq!(totp.get_period(), period);
        }
    }

    #[test]
    fn test_get_code() {
        let cases = vec![
            ("123456".to_string(), 30, 60, "123456"),
            ("654321".to_string(), 10, 30, "654321"),
            ("000000".to_string(), 20, 60, "000000"),
        ];

        for (code, time_left, period, expected_code) in cases {
            let totp = TotpCode::new(code, time_left, period);
            assert_eq!(totp.get_code(), expected_code);
        }
    }

    #[test]
    fn test_get_time_left() {
        let cases = vec![
            ("123456".to_string(), 30, 60, 30),
            ("654321".to_string(), 10, 30, 10),
            ("000000".to_string(), 45, 60, 45),
        ];

        for (code, time_left, period, expected_time_left) in cases {
            let totp = TotpCode::new(code, time_left, period);
            assert_eq!(totp.get_time_left(), expected_time_left);
        }
    }

    #[test]
    fn test_get_period() {
        let cases = vec![
            ("123456".to_string(), 30, 60, 60),
            ("654321".to_string(), 10, 30, 30),
            ("000000".to_string(), 45, 45, 45),
        ];

        for (code, time_left, period, expected_period) in cases {
            let totp = TotpCode::new(code, time_left, period);
            assert_eq!(totp.get_period(), expected_period);
        }
    }
}

#[cfg(test)]
mod get_totp_code_tests {
    use crate::utils::get_totp_code;
    use crate::utils::TotpCode;

    #[test]
    fn test_valid_totp_code_sha1() {
        let url: &str =
            "otpauth://totp/Test?secret=JBSWY3DPEHPK3PXP&algorithm=SHA1&digits=6&period=30";
        let totp: TotpCode = get_totp_code(url).expect("Expected a valid TOTP code");
        assert_eq!(totp.get_code().len(), 6);
        assert_eq!(totp.get_period(), 30);
    }

    #[test]
    fn test_valid_totp_code_sha256() {
        let url: &str =
            "otpauth://totp/Test?secret=JBSWY3DPEHPK3PXP&algorithm=SHA256&digits=8&period=60";
        let totp: TotpCode = get_totp_code(url).expect("Expected a valid TOTP code");
        assert_eq!(totp.get_code().len(), 8);
        assert_eq!(totp.get_period(), 60);
    }

    #[test]
    fn test_valid_totp_code_sha512() {
        let url: &str =
            "otpauth://totp/Test?secret=JBSWY3DPEHPK3PXP&algorithm=SHA512&digits=8&period=60";
        let totp: TotpCode = get_totp_code(url).expect("Expected a valid TOTP code");
        assert_eq!(totp.get_code().len(), 8);
        assert_eq!(totp.get_period(), 60);
    }

    // #[test]
    // fn test_invalid_scheme() {
    //     let url: &str = "http://totp/Test?secret=JBSWY3DPEHPK3PXP&algorithm=SHA1";
    //     let err: String = get_totp_code(url).expect_err("Expected error due to invalid scheme");
    //     assert_eq!(err, "Not an otpauth URI");
    // }

    // #[test]
    // fn test_missing_secret() {
    //     let url: &str = "otpauth://totp/Test?algorithm=SHA1";
    //     let err: String = get_totp_code(url).expect_err("Expected error due to missing secret");
    //     assert_eq!(err, "TOTP secret not found in URI");
    // }

    // #[test]
    // fn test_invalid_secret_format() {
    //     let url: &str = "otpauth://totp/Test?secret=INVALIDSECRET&algorithm=SHA1";
    //     let err: String =
    //         get_totp_code(url).expect_err("Expected error due to invalid secret format");
    //     assert_eq!(err, "Invalid secret format");
    // }

    // #[test]
    // fn test_invalid_algorithm() {
    //     let url: &str = "otpauth://totp/Test?secret=JBSWY3DPEHPK3PXP&algorithm=MD5";
    //     let err: String = get_totp_code(url).expect_err("Expected error due to invalid algorithm");
    //     assert_eq!(err, "Invalid algorithm: MD5");
    // }

    #[test]
    fn test_custom_digits_and_period() {
        let url: &str =
            "otpauth://totp/Test?secret=JBSWY3DPEHPK3PXP&algorithm=SHA1&digits=8&period=45";
        let totp: TotpCode = get_totp_code(url).expect("Expected a valid TOTP code");
        assert_eq!(totp.get_code().len(), 8);
        assert_eq!(totp.get_period(), 45);
    }
}

#[cfg(test)]
mod random_sample_tests {
    use crate::utils::random_sample;

    #[test]
    fn test_sample_length_zero() {
        // Case: sample_length is zero, should return an empty string
        let result = random_sample(0, "abc").expect("Expected Ok result");
        assert_eq!(result, "");
    }

    #[test]
    fn test_sample_string_empty() {
        // Case: sample_string is empty, should return an error
        let result = random_sample(5, "");
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "sample_string must not be empty"
        );
    }

    #[test]
    fn test_sample_length_with_valid_string() {
        // Case: Generate a sample of specified length with a non-empty string
        let sample_length = 5;
        let result = random_sample(sample_length, "abcdef").expect("Expected Ok result");
        assert_eq!(result.len(), sample_length);

        // Check that all characters in result are from sample_string
        for char in result.chars() {
            assert!("abcdef".contains(char));
        }
    }

    #[test]
    fn test_sample_length_exceeds_string_length() {
        // Case: Generate a sample longer than sample_string length
        let sample_length = 10;
        let result = random_sample(sample_length, "abc").expect("Expected Ok result");
        assert_eq!(result.len(), sample_length);

        // Check that all characters in result are from sample_string
        for char in result.chars() {
            assert!("abc".contains(char));
        }
    }

    #[test]
    fn test_single_character_string() {
        // Case: sample_string has only one character
        let result = random_sample(5, "a").expect("Expected Ok result");
        assert_eq!(result, "aaaaa");
    }
}

#[cfg(test)]
#[cfg(target_os = "windows")]
mod get_windows_user_sid_and_name_tests {
    use crate::utils::get_windows_user_sid_and_name;
    use std::os::windows::process::ExitStatusExt;
    use std::process::Output;

    #[test]
    fn test_get_windows_user_sid_and_name_with_mock() {
        let mock_command = || Output {
            status: std::process::ExitStatus::from_raw(0),
            stdout: b"Domain\\Username S-1-5-21-3623811015-3361044348-30300820-1013".to_vec(),
            stderr: Vec::new(),
        };

        let (sid, username) = get_windows_user_sid_and_name(Some(mock_command));
        assert_eq!(
            sid,
            Some("S-1-5-21-3623811015-3361044348-30300820-1013".to_string())
        );
        assert_eq!(username, Some("Username".to_string()));
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn test_get_windows_user_sid_and_name_real_command() {
        let (sid, username) = get_windows_user_sid_and_name::<fn() -> Output>(None);
        assert!(sid.is_some());
        assert!(username.is_some());
    }
}

#[cfg(test)]
mod set_config_mode_tests {
    use crate::utils::set_config_mode;
    use serial_test::serial;
    use std::env;
    use std::fs::{self, File};
    #[cfg(target_os = "windows")]
    use std::io;
    #[cfg(unix)]
    use std::os::unix::fs::PermissionsExt;

    #[serial]
    #[cfg(target_os = "windows")]
    #[test]
    fn test_set_config_mode_windows_skip_mode() {
        env::set_var("KSM_CONFIG_SKIP_MODE", "true");

        // Mock file path
        let file_path = "C:\\path\\to\\config_file.txt";

        // If `KSM_CONFIG_SKIP_MODE` is true, function should return Ok without setting mode.
        assert!(set_config_mode(file_path, None).is_ok());

        env::remove_var("KSM_CONFIG_SKIP_MODE");
    }

    #[serial]
    #[cfg(target_os = "windows")]
    #[test]
    fn test_set_config_mode_windows_with_icacls() {
        // Mock file path
        let file_path = "C:\\path\\to\\config_file.txt";

        // Mock SID retrieval function, mock command execution to simulate success/failure
        let _mock_get_windows_user_sid_and_name = || {
            // Return a mock SID and username
            (
                Some("S-1-5-21-3623811015-3361044348-30300820-1013".to_string()),
                Some("Username".to_string()),
            )
        };

        // Ensure permissions are correctly set and check error cases
        // Simulate the command's output and responses

        let result = set_config_mode(file_path, None);
        // Based on the mock, add assertions for expected outcomes
        assert!(result.is_ok());
    }

    #[serial]
    #[cfg(target_family = "unix")]
    #[test]
    fn test_set_config_mode_unix() {
        // Create a temporary file to test Unix permissions
        let file_path = "/tmp/test_config_file.txt";
        File::create(file_path).expect("Failed to create test file");

        // Execute the function to set Unix permissions
        let result = set_config_mode(file_path);

        // Assert that the function completed successfully
        assert!(result.is_ok());

        // Check file permissions
        let metadata = fs::metadata(file_path).expect("Failed to read metadata");
        let permissions = metadata.permissions();

        // Ensure the file permissions are set to 0o600
        assert_eq!(permissions.mode() & 0o777, 0o600);

        // Clean up
        fs::remove_file(file_path).expect("Failed to delete test file");
    }

    #[serial]
    #[cfg(not(target_os = "windows"))]
    #[test]
    fn test_set_config_mode_env_skip() {
        // Set environment variable to skip setting the mode
        env::set_var("KSM_CONFIG_SKIP_MODE", "true");

        // Create a dummy file for testing
        let file_path = "/tmp/test_config_file_skip.txt";
        File::create(file_path).expect("Failed to create test file");

        // Call the function and expect it to return Ok immediately
        let result = set_config_mode(file_path);
        assert!(result.is_ok());

        // Clean up
        fs::remove_file(file_path).expect("Failed to delete test file");

        // Clear environment variable
        env::remove_var("KSM_CONFIG_SKIP_MODE");
    }
}

#[cfg(test)]
mod check_config_mode_tests {
    use crate::utils::check_config_mode;
    use crate::utils::ConfigError;
    use serial_test::serial;
    use std::env;
    use std::fs;
    // #[cfg(target_os = "windows")]
    // use std::io::{self, Write};
    #[cfg(unix)]
    use std::os::unix::fs::PermissionsExt;
    use std::path::Path;
    // #[cfg(target_os = "windows")]
    // use std::process::Command;
    // #[cfg(target_os = "windows")]
    // use tempfile::NamedTempFile;

    // Helper function to create a temporary file for testing
    fn create_temp_file_with_permissions(content: &str, _mode: u32) -> String {
        // let temp_file = tempfile::NamedTempFile::new().unwrap();
        // let path = temp_file.path().to_str().unwrap().to_string();

        let path = Path::new("./test_temp_file.txt");
        if path.exists() {
            fs::remove_file(path).unwrap();
        }
        fs::write(&path, content).unwrap();

        // Set the file permissions
        #[cfg(unix)]
        {
            let mut permissions = fs::metadata(&path).unwrap().permissions();
            permissions.set_mode(_mode);
            fs::set_permissions(path, permissions).expect("Unable to set permissions");

            // // Adjust ownership (Unix only)
            // Command::new("chown")
            //     .arg("$(whoami)")
            //     .arg(path.to_str().unwrap())
            //     .status()
            //     .expect("Failed to change file ownership");
        }

        return path.to_str().unwrap().to_string();
    }

    #[test]
    #[serial]
    #[cfg(target_os = "windows")]
    fn test_check_config_mode_windows_no_file() {
        // Test for a non-existent file on Windows
        let path = "C:\\path\\to\\nonexistent_file.txt";
        let result = check_config_mode(path);
        assert!(result.is_err());
        if let Err(ConfigError::FileNotFound(ref path)) = result {
            assert_eq!(path, path);
        }
        if Path::new(path).exists() {
            fs::remove_file(path).unwrap();
        }
    }

    #[serial]
    #[cfg(target_os = "windows")]
    #[test]
    fn test_check_config_mode_windows_permission_denied() {
        // Test for a file that exists but is permission denied
        let path = create_temp_file_with_permissions("content", 0o000); // No permissions
        let result = check_config_mode(&path);
        assert!(result.is_err());
        if let Err(ConfigError::PermissionDenied(ref denied_path)) = result {
            assert_eq!(denied_path, &path);
        }
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[serial]
    #[cfg(target_os = "windows")]
    #[test]
    fn test_check_config_mode_windows_too_open_permissions() {
        // Create a temporary file and set too open permissions
        let path = create_temp_file_with_permissions("content", 0o777); // World writable
        let result = check_config_mode(&path);
        assert!(result.is_err());
        if let Err(ConfigError::PermissionDenied(ref denied_path)) = result {
            assert_eq!(
                denied_path.as_str(),
                format!("File permissions too open for {}", &path)
            );
        }
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[serial]
    #[cfg(target_os = "windows")]
    #[test]
    fn test_check_config_mode_windows_proper_permissions() {
        // Test with a file that has proper permissions
        let path = create_temp_file_with_permissions("content", 0o600); // Owner only
        let result = check_config_mode(&path);
        assert!(result.is_ok());
        assert!(result.unwrap());
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[serial]
    #[test]
    fn test_check_config_mode_unix_no_file() {
        // Test for a non-existent file on Unix
        let result = check_config_mode("/path/to/nonexistent_file.txt");
        assert!(result.is_err());
        if let Err(ConfigError::FileNotFound(ref path)) = result {
            assert_eq!(path, "/path/to/nonexistent_file.txt");
        }
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[serial]
    #[test]
    fn test_check_config_mode_unix_permission_denied() {
        // Test for a file that exists but is permission denied
        let path = create_temp_file_with_permissions("content", 0o000); // No permissions
        let result = check_config_mode(&path);
        assert!(result.is_err());
        if let Err(ConfigError::PermissionDenied(ref denied_path)) = result {
            assert_eq!(denied_path, &path);
        }
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[serial]
    #[test]
    fn test_check_config_mode_unix_too_open_permissions() {
        // Create a temporary file and set too open permissions
        let path = create_temp_file_with_permissions("content", 0o777); // World writable
        let result = check_config_mode(&path);
        assert!(result.is_err());
        if let Err(ConfigError::PermissionDenied(ref denied_path)) = result {
            assert_eq!(
                denied_path.as_str(),
                format!("File permissions too open for {}", &path)
            );
        }
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[serial]
    #[test]
    fn test_check_config_mode_unix_proper_permissions() {
        // Test with a file that has proper permissions
        let path = create_temp_file_with_permissions("content", 0o600); // Owner only
        let result = check_config_mode(&path);
        assert!(result.is_ok());
        assert!(result.unwrap());
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[test]
    #[serial]
    fn test_check_config_mode_skip_mode_check() {
        // Test to skip mode check via environment variable
        env::set_var("KSM_CONFIG_SKIP_MODE", "TRUE");
        let path = create_temp_file_with_permissions("content", 0o600);
        let result = check_config_mode(&path);
        assert!(result.is_ok());
        assert!(result.unwrap());
        env::remove_var("KSM_CONFIG_SKIP_MODE"); // Clean up
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }

    #[test]
    #[serial]
    fn test_check_config_mode_skip_warning() {
        // Test skipping warning
        let path = create_temp_file_with_permissions("content", 0o600);
        env::set_var("KSM_CONFIG_SKIP_MODE_WARNING", "TRUE");
        let result = check_config_mode(&path);
        assert!(result.is_ok());
        assert!(result.unwrap());
        env::remove_var("KSM_CONFIG_SKIP_MODE_WARNING"); // Clean up
        if Path::new("./test_temp_file.txt").exists() {
            fs::remove_file(Path::new("./test_temp_file.txt")).unwrap();
        }
    }
}

#[cfg(test)]
mod generate_password_tests {
    use crate::{
        custom_error::KSMRError,
        utils::{generate_password_with_options, PasswordOptions},
    };

    #[test]
    fn test_generate_password_default_options() {
        let options = PasswordOptions::new().length(32);
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
    }

    #[test]
    fn test_generate_password_with_lowercase() {
        let options = PasswordOptions::new().length(32).lowercase(4);
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
        assert!(password.chars().filter(|c| c.is_lowercase()).count() >= 4);
    }

    #[test]
    fn test_generate_password_with_uppercase() {
        let options = PasswordOptions::new().length(32).uppercase(4);
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
        assert!(password.chars().filter(|c| c.is_uppercase()).count() >= 4);
    }

    #[test]
    fn test_generate_password_with_digits() {
        let options = PasswordOptions::new().length(32).digits(4);
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
        assert!(password.chars().filter(|c| c.is_ascii_digit()).count() >= 4);
    }

    #[test]
    fn test_generate_password_with_special_characters() {
        let options = PasswordOptions::new().length(32).special_characters(4);
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
        assert!(
            password
                .chars()
                .filter(|c| "!@#$%^&*()-_=+[]{};:,.<>?/|QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm".contains(*c))
                .count()
                >= 4
        );
    }

    #[test]
    fn test_generate_password_with_all_character_types() {
        let options = PasswordOptions::new()
            .length(32)
            .lowercase(4)
            .uppercase(4)
            .digits(4)
            .special_characters(4);
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
        assert!(password.chars().filter(|c| c.is_lowercase()).count() >= 4);
        assert!(password.chars().filter(|c| c.is_uppercase()).count() >= 4);
        assert!(password.chars().filter(|c| c.is_ascii_digit()).count() >= 4);
        assert!(
            password
                .chars()
                .filter(|c| "!@#$%^&*()-_=+[]{};:,.<>?/|QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm".contains(*c))
                .count()
                >= 4
        );
    }

    #[test]
    fn test_generate_password_with_exceeding_character_count() {
        let options = PasswordOptions::new()
            .length(20)
            .lowercase(15)
            .uppercase(10)
            .digits(10);
        let result = generate_password_with_options(options);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            KSMRError::PasswordCreationError(
                "The specified character counts (35) exceed the total password length (20)!"
                    .to_string()
            )
        );
    }

    #[test]
    fn test_generate_password_with_zero_length() {
        let options = PasswordOptions::new().length(0);
        let result = generate_password_with_options(options);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 32);
    }

    #[test]
    fn test_generate_password_with_only_special_characters() {
        let options = PasswordOptions::new()
            .length(32)
            .special_characters(32)
            .special_characterset("!@#$%^&*()".to_string());
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
        assert!(password.chars().all(|c| "!@#$%^&*()".contains(c)));
    }

    #[test]
    fn test_generate_password_with_no_constraints() {
        let options = PasswordOptions::new().length(32);
        let password = generate_password_with_options(options).unwrap();
        assert_eq!(password.len(), 32);
        assert!(password.chars().any(|c| c.is_lowercase()
            || c.is_uppercase()
            || c.is_ascii_digit()
            || "!@#$%^&*()-_=+[]{};:,.<>?/|".contains(c)));
    }

    #[test]
    fn test_generate_password_with_exact_counts() {
        // Test exact character counts with negative values (KSM-782)
        let options = PasswordOptions::new()
            .lowercase(-8)
            .uppercase(-8)
            .digits(-6)
            .special_characters(-4);
        let password = generate_password_with_options(options).unwrap();

        // Should be exactly 26 characters (8+8+6+4)
        assert_eq!(password.len(), 26);

        // Should have EXACTLY the specified counts
        assert_eq!(password.chars().filter(|c| c.is_lowercase()).count(), 8);
        assert_eq!(password.chars().filter(|c| c.is_uppercase()).count(), 8);
        assert_eq!(password.chars().filter(|c| c.is_ascii_digit()).count(), 6);

        let special_count = password
            .chars()
            .filter(|c| !c.is_lowercase() && !c.is_uppercase() && !c.is_ascii_digit())
            .count();
        assert_eq!(special_count, 4);
    }

    #[test]
    fn test_generate_password_with_zero_exact_count() {
        // Test zero value (also treated as exact count)
        let options = PasswordOptions::new()
            .lowercase(-10)
            .uppercase(-10)
            .digits(0) // Zero should be treated as exact 0
            .special_characters(-6);
        let password = generate_password_with_options(options).unwrap();

        assert_eq!(password.len(), 26); // 10+10+0+6
        assert_eq!(password.chars().filter(|c| c.is_ascii_digit()).count(), 0);
    }

    #[test]
    fn test_generate_password_mixed_exact_and_minimum() {
        // Test mixing negative (exact) and positive (minimum) values
        let options = PasswordOptions::new()
            .length(30)
            .lowercase(-10) // EXACTLY 10
            .uppercase(5); // AT LEAST 5
        let password = generate_password_with_options(options).unwrap();

        assert_eq!(password.len(), 30);
        assert_eq!(password.chars().filter(|c| c.is_lowercase()).count(), 10);
        assert!(password.chars().filter(|c| c.is_uppercase()).count() >= 5);
    }

    #[test]
    fn test_generate_password_all_exact_counts() {
        // Test when all counts are exact (all negative)
        let options = PasswordOptions::new()
            .lowercase(-12)
            .uppercase(-8)
            .digits(-5)
            .special_characters(-7);
        let password = generate_password_with_options(options).unwrap();

        // Total should be sum of absolute values: 32
        assert_eq!(password.len(), 32);
        assert_eq!(password.chars().filter(|c| c.is_lowercase()).count(), 12);
        assert_eq!(password.chars().filter(|c| c.is_uppercase()).count(), 8);
        assert_eq!(password.chars().filter(|c| c.is_ascii_digit()).count(), 5);

        let special_count = password
            .chars()
            .filter(|c| !c.is_lowercase() && !c.is_uppercase() && !c.is_ascii_digit())
            .count();
        assert_eq!(special_count, 7);
    }

    #[test]
    fn test_generate_password_single_exact_count() {
        // Test single exact count with others minimum
        let options = PasswordOptions::new().length(25).lowercase(-10); // EXACTLY 10, others fill remaining 15
        let password = generate_password_with_options(options).unwrap();

        assert_eq!(password.len(), 25);
        assert_eq!(password.chars().filter(|c| c.is_lowercase()).count(), 10);
    }

    #[test]
    fn test_generate_password_exact_ignores_length_param() {
        // Test that exact mode ignores length parameter (KSM-782)
        let options = PasswordOptions::new()
            .length(10) // Length param should be IGNORED
            .lowercase(-20); // EXACTLY 20 lowercase
        let password = generate_password_with_options(options).unwrap();

        // Password should be 20 chars (sum of exact counts), NOT 10
        assert_eq!(password.len(), 20);
        assert_eq!(password.chars().filter(|c| c.is_lowercase()).count(), 20);
    }
}