@@ -2641,342 +2641,4 @@ async fn add_all_recipients_as_contacts(
2641
2641
}
2642
2642
2643
2643
#[ cfg( test) ]
2644
- mod tests {
2645
- use super :: * ;
2646
- use crate :: test_utils:: TestContext ;
2647
-
2648
- #[ test]
2649
- fn test_get_folder_meaning_by_name ( ) {
2650
- assert_eq ! ( get_folder_meaning_by_name( "Gesendet" ) , FolderMeaning :: Sent ) ;
2651
- assert_eq ! ( get_folder_meaning_by_name( "GESENDET" ) , FolderMeaning :: Sent ) ;
2652
- assert_eq ! ( get_folder_meaning_by_name( "gesendet" ) , FolderMeaning :: Sent ) ;
2653
- assert_eq ! (
2654
- get_folder_meaning_by_name( "Messages envoyés" ) ,
2655
- FolderMeaning :: Sent
2656
- ) ;
2657
- assert_eq ! (
2658
- get_folder_meaning_by_name( "mEsSaGes envoyÉs" ) ,
2659
- FolderMeaning :: Sent
2660
- ) ;
2661
- assert_eq ! ( get_folder_meaning_by_name( "xxx" ) , FolderMeaning :: Unknown ) ;
2662
- assert_eq ! ( get_folder_meaning_by_name( "SPAM" ) , FolderMeaning :: Spam ) ;
2663
- assert_eq ! ( get_folder_meaning_by_name( "Trash" ) , FolderMeaning :: Trash ) ;
2664
- }
2665
-
2666
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
2667
- async fn test_set_uid_next_validity ( ) {
2668
- let t = TestContext :: new_alice ( ) . await ;
2669
- assert_eq ! ( get_uid_next( & t. ctx, "Inbox" ) . await . unwrap( ) , 0 ) ;
2670
- assert_eq ! ( get_uidvalidity( & t. ctx, "Inbox" ) . await . unwrap( ) , 0 ) ;
2671
-
2672
- set_uidvalidity ( & t. ctx , "Inbox" , 7 ) . await . unwrap ( ) ;
2673
- assert_eq ! ( get_uidvalidity( & t. ctx, "Inbox" ) . await . unwrap( ) , 7 ) ;
2674
- assert_eq ! ( get_uid_next( & t. ctx, "Inbox" ) . await . unwrap( ) , 0 ) ;
2675
-
2676
- set_uid_next ( & t. ctx , "Inbox" , 5 ) . await . unwrap ( ) ;
2677
- set_uidvalidity ( & t. ctx , "Inbox" , 6 ) . await . unwrap ( ) ;
2678
- assert_eq ! ( get_uid_next( & t. ctx, "Inbox" ) . await . unwrap( ) , 5 ) ;
2679
- assert_eq ! ( get_uidvalidity( & t. ctx, "Inbox" ) . await . unwrap( ) , 6 ) ;
2680
- }
2681
-
2682
- #[ test]
2683
- fn test_build_sequence_sets ( ) {
2684
- assert_eq ! ( build_sequence_sets( & [ ] ) . unwrap( ) , vec![ ] ) ;
2685
-
2686
- let cases = vec ! [
2687
- ( vec![ 1 ] , "1" ) ,
2688
- ( vec![ 3291 ] , "3291" ) ,
2689
- ( vec![ 1 , 3 , 5 , 7 , 9 , 11 ] , "1,3,5,7,9,11" ) ,
2690
- ( vec![ 1 , 2 , 3 ] , "1:3" ) ,
2691
- ( vec![ 1 , 4 , 5 , 6 ] , "1,4:6" ) ,
2692
- ( ( 1 ..=500 ) . collect( ) , "1:500" ) ,
2693
- ( vec![ 3 , 4 , 8 , 9 , 10 , 11 , 39 , 50 , 2 ] , "3:4,8:11,39,50,2" ) ,
2694
- ] ;
2695
- for ( input, s) in cases {
2696
- assert_eq ! (
2697
- build_sequence_sets( & input) . unwrap( ) ,
2698
- vec![ ( input, s. into( ) ) ]
2699
- ) ;
2700
- }
2701
-
2702
- let has_number = |( uids, s) : & ( Vec < u32 > , String ) , number| {
2703
- uids. iter ( ) . any ( |& n| n == number)
2704
- && s. split ( ',' ) . any ( |n| n. parse :: < u32 > ( ) . unwrap ( ) == number)
2705
- } ;
2706
-
2707
- let numbers: Vec < _ > = ( 2 ..=500 ) . step_by ( 2 ) . collect ( ) ;
2708
- let result = build_sequence_sets ( & numbers) . unwrap ( ) ;
2709
- for ( _, set) in & result {
2710
- assert ! ( set. len( ) < 1010 ) ;
2711
- assert ! ( !set. ends_with( ',' ) ) ;
2712
- assert ! ( !set. starts_with( ',' ) ) ;
2713
- }
2714
- assert ! ( result. len( ) == 1 ) ; // these UIDs fit in one set
2715
- for & number in & numbers {
2716
- assert ! ( result. iter( ) . any( |r| has_number( r, number) ) ) ;
2717
- }
2718
-
2719
- let numbers: Vec < _ > = ( 1 ..=1000 ) . step_by ( 3 ) . collect ( ) ;
2720
- let result = build_sequence_sets ( & numbers) . unwrap ( ) ;
2721
- for ( _, set) in & result {
2722
- assert ! ( set. len( ) < 1010 ) ;
2723
- assert ! ( !set. ends_with( ',' ) ) ;
2724
- assert ! ( !set. starts_with( ',' ) ) ;
2725
- }
2726
- let ( last_uids, last_str) = result. last ( ) . unwrap ( ) ;
2727
- assert_eq ! (
2728
- last_uids. get( ( last_uids. len( ) - 2 ) ..) . unwrap( ) ,
2729
- & [ 997 , 1000 ]
2730
- ) ;
2731
- assert ! ( last_str. ends_with( "997,1000" ) ) ;
2732
- assert ! ( result. len( ) == 2 ) ; // This time we need 2 sets
2733
- for & number in & numbers {
2734
- assert ! ( result. iter( ) . any( |r| has_number( r, number) ) ) ;
2735
- }
2736
-
2737
- let numbers: Vec < _ > = ( 30000000 ..=30002500 ) . step_by ( 4 ) . collect ( ) ;
2738
- let result = build_sequence_sets ( & numbers) . unwrap ( ) ;
2739
- for ( _, set) in & result {
2740
- assert ! ( set. len( ) < 1010 ) ;
2741
- assert ! ( !set. ends_with( ',' ) ) ;
2742
- assert ! ( !set. starts_with( ',' ) ) ;
2743
- }
2744
- assert_eq ! ( result. len( ) , 6 ) ;
2745
- for & number in & numbers {
2746
- assert ! ( result. iter( ) . any( |r| has_number( r, number) ) ) ;
2747
- }
2748
- }
2749
-
2750
- async fn check_target_folder_combination (
2751
- folder : & str ,
2752
- mvbox_move : bool ,
2753
- chat_msg : bool ,
2754
- expected_destination : & str ,
2755
- accepted_chat : bool ,
2756
- outgoing : bool ,
2757
- setupmessage : bool ,
2758
- ) -> Result < ( ) > {
2759
- println ! ( "Testing: For folder {folder}, mvbox_move {mvbox_move}, chat_msg {chat_msg}, accepted {accepted_chat}, outgoing {outgoing}, setupmessage {setupmessage}" ) ;
2760
-
2761
- let t = TestContext :: new_alice ( ) . await ;
2762
- t. ctx
2763
- . set_config ( Config :: ConfiguredMvboxFolder , Some ( "DeltaChat" ) )
2764
- . await ?;
2765
- t. ctx
2766
- . set_config ( Config :: ConfiguredSentboxFolder , Some ( "Sent" ) )
2767
- . await ?;
2768
- t. ctx
2769
- . set_config ( Config :: MvboxMove , Some ( if mvbox_move { "1" } else { "0" } ) )
2770
- . await ?;
2771
-
2772
- if accepted_chat {
2773
- let contact_id = Contact :: create ( & t. ctx , "" , "bob@example.net" ) . await ?;
2774
- ChatId :: create_for_contact ( & t. ctx , contact_id) . await ?;
2775
- }
2776
- let temp;
2777
-
2778
- let bytes = if setupmessage {
2779
- include_bytes ! ( "../test-data/message/AutocryptSetupMessage.eml" )
2780
- } else {
2781
- temp = format ! (
2782
- "Received: (Postfix, from userid 1000); Mon, 4 Dec 2006 14:51:39 +0100 (CET)\n \
2783
- {}\
2784
- Subject: foo\n \
2785
- Message-ID: <abc@example.com>\n \
2786
- {}\
2787
- Date: Sun, 22 Mar 2020 22:37:57 +0000\n \
2788
- \n \
2789
- hello\n ",
2790
- if outgoing {
2791
- "From: alice@example.org\n To: bob@example.net\n "
2792
- } else {
2793
- "From: bob@example.net\n To: alice@example.org\n "
2794
- } ,
2795
- if chat_msg { "Chat-Version: 1.0\n " } else { "" } ,
2796
- ) ;
2797
- temp. as_bytes ( )
2798
- } ;
2799
-
2800
- let ( headers, _) = mailparse:: parse_headers ( bytes) ?;
2801
- let actual = if let Some ( config) =
2802
- target_folder_cfg ( & t, folder, get_folder_meaning_by_name ( folder) , & headers) . await ?
2803
- {
2804
- t. get_config ( config) . await ?
2805
- } else {
2806
- None
2807
- } ;
2808
-
2809
- let expected = if expected_destination == folder {
2810
- None
2811
- } else {
2812
- Some ( expected_destination)
2813
- } ;
2814
- assert_eq ! ( expected, actual. as_deref( ) , "For folder {folder}, mvbox_move {mvbox_move}, chat_msg {chat_msg}, accepted {accepted_chat}, outgoing {outgoing}, setupmessage {setupmessage}: expected {expected:?}, got {actual:?}" ) ;
2815
- Ok ( ( ) )
2816
- }
2817
-
2818
- // chat_msg means that the message was sent by Delta Chat
2819
- // The tuples are (folder, mvbox_move, chat_msg, expected_destination)
2820
- const COMBINATIONS_ACCEPTED_CHAT : & [ ( & str , bool , bool , & str ) ] = & [
2821
- ( "INBOX" , false , false , "INBOX" ) ,
2822
- ( "INBOX" , false , true , "INBOX" ) ,
2823
- ( "INBOX" , true , false , "INBOX" ) ,
2824
- ( "INBOX" , true , true , "DeltaChat" ) ,
2825
- ( "Sent" , false , false , "Sent" ) ,
2826
- ( "Sent" , false , true , "Sent" ) ,
2827
- ( "Sent" , true , false , "Sent" ) ,
2828
- ( "Sent" , true , true , "DeltaChat" ) ,
2829
- ( "Spam" , false , false , "INBOX" ) , // Move classical emails in accepted chats from Spam to Inbox, not 100% sure on this, we could also just never move non-chat-msgs
2830
- ( "Spam" , false , true , "INBOX" ) ,
2831
- ( "Spam" , true , false , "INBOX" ) , // Move classical emails in accepted chats from Spam to Inbox, not 100% sure on this, we could also just never move non-chat-msgs
2832
- ( "Spam" , true , true , "DeltaChat" ) ,
2833
- ] ;
2834
-
2835
- // These are the same as above, but non-chat messages in Spam stay in Spam
2836
- const COMBINATIONS_REQUEST : & [ ( & str , bool , bool , & str ) ] = & [
2837
- ( "INBOX" , false , false , "INBOX" ) ,
2838
- ( "INBOX" , false , true , "INBOX" ) ,
2839
- ( "INBOX" , true , false , "INBOX" ) ,
2840
- ( "INBOX" , true , true , "DeltaChat" ) ,
2841
- ( "Sent" , false , false , "Sent" ) ,
2842
- ( "Sent" , false , true , "Sent" ) ,
2843
- ( "Sent" , true , false , "Sent" ) ,
2844
- ( "Sent" , true , true , "DeltaChat" ) ,
2845
- ( "Spam" , false , false , "Spam" ) ,
2846
- ( "Spam" , false , true , "INBOX" ) ,
2847
- ( "Spam" , true , false , "Spam" ) ,
2848
- ( "Spam" , true , true , "DeltaChat" ) ,
2849
- ] ;
2850
-
2851
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
2852
- async fn test_target_folder_incoming_accepted ( ) -> Result < ( ) > {
2853
- for ( folder, mvbox_move, chat_msg, expected_destination) in COMBINATIONS_ACCEPTED_CHAT {
2854
- check_target_folder_combination (
2855
- folder,
2856
- * mvbox_move,
2857
- * chat_msg,
2858
- expected_destination,
2859
- true ,
2860
- false ,
2861
- false ,
2862
- )
2863
- . await ?;
2864
- }
2865
- Ok ( ( ) )
2866
- }
2867
-
2868
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
2869
- async fn test_target_folder_incoming_request ( ) -> Result < ( ) > {
2870
- for ( folder, mvbox_move, chat_msg, expected_destination) in COMBINATIONS_REQUEST {
2871
- check_target_folder_combination (
2872
- folder,
2873
- * mvbox_move,
2874
- * chat_msg,
2875
- expected_destination,
2876
- false ,
2877
- false ,
2878
- false ,
2879
- )
2880
- . await ?;
2881
- }
2882
- Ok ( ( ) )
2883
- }
2884
-
2885
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
2886
- async fn test_target_folder_outgoing ( ) -> Result < ( ) > {
2887
- // Test outgoing emails
2888
- for ( folder, mvbox_move, chat_msg, expected_destination) in COMBINATIONS_ACCEPTED_CHAT {
2889
- check_target_folder_combination (
2890
- folder,
2891
- * mvbox_move,
2892
- * chat_msg,
2893
- expected_destination,
2894
- true ,
2895
- true ,
2896
- false ,
2897
- )
2898
- . await ?;
2899
- }
2900
- Ok ( ( ) )
2901
- }
2902
-
2903
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
2904
- async fn test_target_folder_setupmsg ( ) -> Result < ( ) > {
2905
- // Test setupmessages
2906
- for ( folder, mvbox_move, chat_msg, _expected_destination) in COMBINATIONS_ACCEPTED_CHAT {
2907
- check_target_folder_combination (
2908
- folder,
2909
- * mvbox_move,
2910
- * chat_msg,
2911
- if folder == & "Spam" { "INBOX" } else { folder } , // Never move setup messages, except if they are in "Spam"
2912
- false ,
2913
- true ,
2914
- true ,
2915
- )
2916
- . await ?;
2917
- }
2918
- Ok ( ( ) )
2919
- }
2920
-
2921
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
2922
- async fn test_get_imap_search_command ( ) -> Result < ( ) > {
2923
- let t = TestContext :: new_alice ( ) . await ;
2924
- assert_eq ! (
2925
- get_imap_self_sent_search_command( & t. ctx) . await ?,
2926
- r#"FROM "alice@example.org""#
2927
- ) ;
2928
-
2929
- t. ctx . set_primary_self_addr ( "alice@another.com" ) . await ?;
2930
- assert_eq ! (
2931
- get_imap_self_sent_search_command( & t. ctx) . await ?,
2932
- r#"OR (FROM "alice@another.com") (FROM "alice@example.org")"#
2933
- ) ;
2934
-
2935
- t. ctx . set_primary_self_addr ( "alice@third.com" ) . await ?;
2936
- assert_eq ! (
2937
- get_imap_self_sent_search_command( & t. ctx) . await ?,
2938
- r#"OR (OR (FROM "alice@third.com") (FROM "alice@another.com")) (FROM "alice@example.org")"#
2939
- ) ;
2940
-
2941
- Ok ( ( ) )
2942
- }
2943
-
2944
- #[ test]
2945
- fn test_uid_grouper ( ) {
2946
- // Input: sequence of (rowid: i64, uid: u32, target: String)
2947
- // Output: sequence of (target: String, rowid_set: Vec<i64>, uid_set: String)
2948
- let grouper = UidGrouper :: from ( [ ( 1 , 2 , "INBOX" . to_string ( ) ) ] ) ;
2949
- let res: Vec < ( String , Vec < i64 > , String ) > = grouper. into_iter ( ) . collect ( ) ;
2950
- assert_eq ! ( res, vec![ ( "INBOX" . to_string( ) , vec![ 1 ] , "2" . to_string( ) ) ] ) ;
2951
-
2952
- let grouper = UidGrouper :: from ( [ ( 1 , 2 , "INBOX" . to_string ( ) ) , ( 2 , 3 , "INBOX" . to_string ( ) ) ] ) ;
2953
- let res: Vec < ( String , Vec < i64 > , String ) > = grouper. into_iter ( ) . collect ( ) ;
2954
- assert_eq ! (
2955
- res,
2956
- vec![ ( "INBOX" . to_string( ) , vec![ 1 , 2 ] , "2:3" . to_string( ) ) ]
2957
- ) ;
2958
-
2959
- let grouper = UidGrouper :: from ( [
2960
- ( 1 , 2 , "INBOX" . to_string ( ) ) ,
2961
- ( 2 , 2 , "INBOX" . to_string ( ) ) ,
2962
- ( 3 , 3 , "INBOX" . to_string ( ) ) ,
2963
- ] ) ;
2964
- let res: Vec < ( String , Vec < i64 > , String ) > = grouper. into_iter ( ) . collect ( ) ;
2965
- assert_eq ! (
2966
- res,
2967
- vec![ ( "INBOX" . to_string( ) , vec![ 1 , 2 , 3 ] , "2:3" . to_string( ) ) ]
2968
- ) ;
2969
- }
2970
-
2971
- #[ test]
2972
- fn test_setmetadata_device_token ( ) {
2973
- assert_eq ! (
2974
- format_setmetadata( "INBOX" , "foobarbaz" ) ,
2975
- "SETMETADATA \" INBOX\" (/private/devicetoken {9+}\r \n foobarbaz)"
2976
- ) ;
2977
- assert_eq ! (
2978
- format_setmetadata( "INBOX" , "foo\r \n bar\r \n baz\r \n " ) ,
2979
- "SETMETADATA \" INBOX\" (/private/devicetoken {15+}\r \n foo\r \n bar\r \n baz\r \n )"
2980
- ) ;
2981
- }
2982
- }
2644
+ mod imap_tests;
0 commit comments