11use crate :: mode:: Mode ;
22use linkify:: { LinkFinder , LinkKind } ;
3+ use rand:: Rng ;
34use url:: Url ;
45
56#[ cfg( test) ]
@@ -10,60 +11,60 @@ mod find_and_replace {
1011 fn naive ( ) {
1112 assert_eq ! (
1213 find_and_replace(
13- & "https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa" . to_string ( ) ,
14+ "https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa" ,
1415 & Mode :: Remove
1516 ) ,
1617 "https://test.test/"
1718 ) ;
1819 assert_eq ! (
1920 find_and_replace(
20- & "https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa" . to_string ( ) ,
21+ "https://test.test/?fbclid=dsadsa&utm_source=fafa&utm_campaign=fafas&utm_medium=adsa" ,
2122 & Mode :: YourMom
2223 ) ,
2324 "https://test.test/?fbclid=your_mom&utm_source=your_mom&utm_campaign=your_mom&utm_medium=your_mom"
2425 ) ;
26+ assert_ne ! (
27+ find_and_replace(
28+ "https://test.test/?fbclid=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_source=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_campaign=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_medium=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs" ,
29+ & Mode :: Evil
30+ ) ,
31+ "https://test.test/?fbclid=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_source=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_campaign=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs&utm_medium=IwAR3l6qn8TzOT254dIa7jBAM1dG3OHn3f8ZoRGsADTmqG1Zfmmko-oRhE8Qs"
32+ ) ;
2533 }
2634 #[ test]
2735 fn should_preserve_query ( ) {
2836 assert_eq ! (
29- find_and_replace( & "https://test.test/?abc=abc" . to_string ( ) , & Mode :: Remove ) ,
37+ find_and_replace( "https://test.test/?abc=abc" , & Mode :: Remove ) ,
3038 "https://test.test/?abc=abc"
3139 ) ;
3240 assert_eq ! (
33- find_and_replace( & "https://test.test/?abc=abc" . to_string ( ) , & Mode :: YourMom ) ,
41+ find_and_replace( "https://test.test/?abc=abc" , & Mode :: YourMom ) ,
3442 "https://test.test/?abc=abc"
3543 ) ;
3644 }
3745 #[ test]
3846 fn multiple_params ( ) {
3947 assert_eq ! (
40- find_and_replace(
41- & "https://test.test/?abc=abc&fbclid=flksj" . to_string( ) ,
42- & Mode :: Remove
43- ) ,
48+ find_and_replace( "https://test.test/?abc=abc&fbclid=flksj" , & Mode :: Remove ) ,
4449 "https://test.test/?abc=abc"
4550 ) ;
4651 assert_eq ! (
47- find_and_replace(
48- & "https://test.test/?abc=abc&fbclid=flksj" . to_string( ) ,
49- & Mode :: YourMom
50- ) ,
52+ find_and_replace( "https://test.test/?abc=abc&fbclid=flksj" , & Mode :: YourMom ) ,
5153 "https://test.test/?abc=abc&fbclid=your_mom"
5254 ) ;
5355 }
5456 #[ test]
5557 fn multiple_links ( ) {
5658 assert_eq ! (
5759 find_and_replace(
58- & "https://test.test/?abc=abc&fbclid=flksj\n https://test.test/?abc=abc&fbclid=flksj"
59- . to_string( ) ,
60+ "https://test.test/?abc=abc&fbclid=flksj\n https://test.test/?abc=abc&fbclid=flksj" ,
6061 & Mode :: Remove
6162 ) ,
6263 "https://test.test/?abc=abc\n https://test.test/?abc=abc"
6364 ) ;
6465 assert_eq ! (
6566 find_and_replace(
66- & "https://test.test/?abc=abc&fbclid=flksj\n https://test.test/?abc=abc&fbclid=flksj" . to_string ( ) ,
67+ "https://test.test/?abc=abc&fbclid=flksj\n https://test.test/?abc=abc&fbclid=flksj" ,
6768 & Mode :: YourMom
6869 ) ,
6970 "https://test.test/?abc=abc&fbclid=your_mom\n https://test.test/?abc=abc&fbclid=your_mom"
@@ -73,14 +74,14 @@ mod find_and_replace {
7374 fn multiple_links_and_text ( ) {
7475 assert_eq ! (
7576 find_and_replace(
76- & "some text here https://test.test/?abc=abc&fbclid=flksj here \n and herehttps://test.test/?abc=abc&fbclid=flksj" . to_string ( ) ,
77+ "some text here https://test.test/?abc=abc&fbclid=flksj here \n and herehttps://test.test/?abc=abc&fbclid=flksj" ,
7778 & Mode :: Remove
7879 ) ,
7980 "some text here https://test.test/?abc=abc here \n and herehttps://test.test/?abc=abc"
8081 ) ;
8182 assert_eq ! (
8283 find_and_replace(
83- & "some text here https://test.test/?abc=abc&fbclid=flksj here \n and herehttps://test.test/?abc=abc&fbclid=flksj" . to_string ( ) ,
84+ "some text here https://test.test/?abc=abc&fbclid=flksj here \n and herehttps://test.test/?abc=abc&fbclid=flksj" ,
8485 & Mode :: YourMom
8586 ) ,
8687 "some text here https://test.test/?abc=abc&fbclid=your_mom here \n and herehttps://test.test/?abc=abc&fbclid=your_mom"
@@ -126,6 +127,25 @@ fn process_query(query: url::form_urlencoded::Parse<'_>, mode: &Mode) -> Vec<(St
126127 }
127128 } )
128129 . collect ( ) ,
130+ Mode :: Evil => {
131+ let mut rng = rand:: thread_rng ( ) ;
132+ query
133+ . map ( |p| {
134+ if is_hit ( & p. 0 ) {
135+ (
136+ p. 0 . to_string ( ) ,
137+ swap_two_chars (
138+ & p. 1 ,
139+ rng. gen_range ( 0 ..p. 1 . to_string ( ) . len ( ) ) ,
140+ rng. gen_range ( 0 ..p. 1 . to_string ( ) . len ( ) ) ,
141+ ) ,
142+ )
143+ } else {
144+ ( p. 0 . to_string ( ) , p. 1 . to_string ( ) )
145+ }
146+ } )
147+ . collect ( )
148+ }
129149 }
130150}
131151
@@ -144,3 +164,19 @@ mod is_hit {
144164fn is_hit ( p : & str ) -> bool {
145165 p == "fbclid" || p == "utm_source" || p == "utm_campaign" || p == "utm_medium"
146166}
167+
168+ #[ cfg( test) ]
169+ mod swap {
170+ use super :: * ;
171+
172+ #[ test]
173+ fn test_all ( ) {
174+ assert_eq ! ( swap_two_chars( "0123456789" , 2 , 7 ) , "0173456289" ) ;
175+ }
176+ }
177+
178+ fn swap_two_chars ( s : & str , a : usize , b : usize ) -> String {
179+ let mut char_vector: Vec < char > = s. chars ( ) . collect ( ) ;
180+ char_vector. swap ( a, b) ;
181+ char_vector. iter ( ) . collect ( )
182+ }
0 commit comments