4
4
5
5
use serde:: { Deserialize , Serialize } ;
6
6
use std:: io:: { self , Read , Write } ;
7
- use std:: path:: PathBuf ;
8
7
use std:: process:: Command ;
9
8
use url:: Url ;
10
9
@@ -24,7 +23,6 @@ pub enum FirefoxCommand {
24
23
LaunchFirefox { url : String } ,
25
24
LaunchFirefoxPrivate { url : String } ,
26
25
GetVersion { } ,
27
- GetInstallId { } ,
28
26
}
29
27
#[ derive( Serialize , Deserialize ) ]
30
28
// {
@@ -36,14 +34,6 @@ pub struct Response {
36
34
pub result_code : u32 ,
37
35
}
38
36
39
- #[ derive( Serialize , Deserialize ) ]
40
- // {
41
- // "installation_id": "123ABC456",
42
- // }
43
- pub struct InstallationId {
44
- pub installation_id : String ,
45
- }
46
-
47
37
#[ repr( u32 ) ]
48
38
pub enum ResultCode {
49
39
Success = 0 ,
@@ -162,28 +152,6 @@ pub fn process_command(command: &FirefoxCommand) -> std::io::Result<bool> {
162
152
Ok ( true )
163
153
}
164
154
FirefoxCommand :: GetVersion { } => generate_response ( "1" , ResultCode :: Success . into ( ) ) ,
165
- FirefoxCommand :: GetInstallId { } => {
166
- // config_dir() evaluates to ~/Library/Application Support on macOS
167
- // and %RoamingAppData% on Windows.
168
- let mut json_path = match dirs:: config_dir ( ) {
169
- Some ( path) => path,
170
- None => {
171
- return generate_response (
172
- "Config dir could not be found" ,
173
- ResultCode :: Error . into ( ) ,
174
- )
175
- }
176
- } ;
177
- #[ cfg( target_os = "windows" ) ]
178
- json_path. push ( "Mozilla\\ Firefox" ) ;
179
- #[ cfg( target_os = "macos" ) ]
180
- json_path. push ( "Firefox" ) ;
181
-
182
- json_path. push ( "install_id" ) ;
183
- json_path. set_extension ( "json" ) ;
184
- let mut install_id = String :: new ( ) ;
185
- get_install_id ( & mut json_path, & mut install_id)
186
- }
187
155
}
188
156
}
189
157
@@ -260,54 +228,10 @@ fn launch_firefox<C: CommandRunner>(
260
228
command. to_string ( )
261
229
}
262
230
263
- fn get_install_id ( json_path : & mut PathBuf , install_id : & mut String ) -> std:: io:: Result < bool > {
264
- if !json_path. exists ( ) {
265
- return Err ( std:: io:: Error :: new (
266
- std:: io:: ErrorKind :: NotFound ,
267
- "Install ID file does not exist" ,
268
- ) ) ;
269
- }
270
- let json_size = std:: fs:: metadata ( & json_path)
271
- . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , e) ) ?
272
- . len ( ) ;
273
- // Set a 1 KB limit for the file size.
274
- if json_size <= 0 || json_size > 1024 {
275
- return Err ( std:: io:: Error :: new (
276
- std:: io:: ErrorKind :: InvalidData ,
277
- "Install ID file has invalid size" ,
278
- ) ) ;
279
- }
280
- let mut file =
281
- std:: fs:: File :: open ( json_path) . or_else ( |_| -> std:: io:: Result < std:: fs:: File > {
282
- return Err ( std:: io:: Error :: new (
283
- std:: io:: ErrorKind :: NotFound ,
284
- "Failed to open file" ,
285
- ) ) ;
286
- } ) ?;
287
- let mut contents = String :: new ( ) ;
288
- match file. read_to_string ( & mut contents) {
289
- Ok ( _) => match serde_json:: from_str :: < InstallationId > ( & contents) {
290
- Ok ( id) => {
291
- * install_id = id. installation_id . clone ( ) ;
292
- generate_response ( & id. installation_id , ResultCode :: Success . into ( ) )
293
- }
294
- Err ( _) => {
295
- return Err ( std:: io:: Error :: new (
296
- std:: io:: ErrorKind :: InvalidData ,
297
- "Failed to read installation ID" ,
298
- ) )
299
- }
300
- } ,
301
- Err ( _) => generate_response ( "Failed to read file" , ResultCode :: Error . into ( ) ) ,
302
- } ?;
303
- Ok ( true )
304
- }
305
-
306
231
#[ cfg( test) ]
307
232
mod tests {
308
233
use super :: * ;
309
234
use std:: io:: Cursor ;
310
- use tempfile:: NamedTempFile ;
311
235
#[ test]
312
236
fn test_validate_url ( ) {
313
237
let valid_test_cases = vec ! [
@@ -423,90 +347,4 @@ mod tests {
423
347
let correct_url_format = format ! ( "-osint -private-window {}" , url) ;
424
348
assert ! ( command_line. contains( correct_url_format. as_str( ) ) ) ;
425
349
}
426
-
427
- #[ test]
428
- fn test_get_install_id_valid ( ) -> std:: io:: Result < ( ) > {
429
- let mut tempfile = NamedTempFile :: new ( ) . unwrap ( ) ;
430
- let installation_id = InstallationId {
431
- installation_id : "123ABC456" . to_string ( ) ,
432
- } ;
433
- let json_string = serde_json:: to_string ( & installation_id) ;
434
- let _ = tempfile. write_all ( json_string?. as_bytes ( ) ) ;
435
- let mut install_id = String :: new ( ) ;
436
- let result = get_install_id ( & mut tempfile. path ( ) . to_path_buf ( ) , & mut install_id) ;
437
- assert ! ( result. is_ok( ) ) ;
438
- assert_eq ! ( install_id, "123ABC456" ) ;
439
- Ok ( ( ) )
440
- }
441
-
442
- #[ test]
443
- fn test_get_install_id_incorrect_var ( ) -> std:: io:: Result < ( ) > {
444
- #[ derive( Serialize , Deserialize ) ]
445
- pub struct IncorrectJSON {
446
- pub incorrect_var : String ,
447
- }
448
- let mut tempfile = NamedTempFile :: new ( ) . unwrap ( ) ;
449
- let incorrect_json = IncorrectJSON {
450
- incorrect_var : "incorrect_val" . to_string ( ) ,
451
- } ;
452
- let json_string = serde_json:: to_string ( & incorrect_json) ;
453
- let _ = tempfile. write_all ( json_string?. as_bytes ( ) ) ;
454
- let mut install_id = String :: new ( ) ;
455
- let result = get_install_id ( & mut tempfile. path ( ) . to_path_buf ( ) , & mut install_id) ;
456
- assert ! ( result. is_err( ) ) ;
457
- let error = result. err ( ) . unwrap ( ) ;
458
- assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: InvalidData ) ;
459
- Ok ( ( ) )
460
- }
461
-
462
- #[ test]
463
- fn test_get_install_id_partially_correct_vars ( ) -> std:: io:: Result < ( ) > {
464
- #[ derive( Serialize , Deserialize ) ]
465
- pub struct IncorrectJSON {
466
- pub installation_id : String ,
467
- pub incorrect_var : String ,
468
- }
469
- let mut tempfile = NamedTempFile :: new ( ) . unwrap ( ) ;
470
- let incorrect_json = IncorrectJSON {
471
- installation_id : "123ABC456" . to_string ( ) ,
472
- incorrect_var : "incorrect_val" . to_string ( ) ,
473
- } ;
474
- let json_string = serde_json:: to_string ( & incorrect_json) ;
475
- let _ = tempfile. write_all ( json_string?. as_bytes ( ) ) ;
476
- let mut install_id = String :: new ( ) ;
477
- let result = get_install_id ( & mut tempfile. path ( ) . to_path_buf ( ) , & mut install_id) ;
478
- // This still succeeds as the installation_id field is present
479
- assert ! ( result. is_ok( ) ) ;
480
- Ok ( ( ) )
481
- }
482
-
483
- #[ test]
484
- fn test_get_install_id_file_does_not_exist ( ) -> std:: io:: Result < ( ) > {
485
- let tempfile = NamedTempFile :: new ( ) . unwrap ( ) ;
486
- let mut path = tempfile. path ( ) . to_path_buf ( ) ;
487
- tempfile. close ( ) ?;
488
- let mut install_id = String :: new ( ) ;
489
- let result = get_install_id ( & mut path, & mut install_id) ;
490
- assert ! ( result. is_err( ) ) ;
491
- let error = result. err ( ) . unwrap ( ) ;
492
- assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: NotFound ) ;
493
- Ok ( ( ) )
494
- }
495
-
496
- #[ test]
497
- fn test_get_install_id_file_too_large ( ) -> std:: io:: Result < ( ) > {
498
- let mut tempfile = NamedTempFile :: new ( ) . unwrap ( ) ;
499
- let installation_id = InstallationId {
500
- // Create a ~10 KB file
501
- installation_id : String :: from_utf8 ( vec ! [ b'X' ; 10000 ] ) . unwrap ( ) ,
502
- } ;
503
- let json_string = serde_json:: to_string ( & installation_id) ;
504
- let _ = tempfile. write_all ( json_string?. as_bytes ( ) ) ;
505
- let mut install_id = String :: new ( ) ;
506
- let result = get_install_id ( & mut tempfile. path ( ) . to_path_buf ( ) , & mut install_id) ;
507
- assert ! ( result. is_err( ) ) ;
508
- let error = result. err ( ) . unwrap ( ) ;
509
- assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: InvalidData ) ;
510
- Ok ( ( ) )
511
- }
512
350
}
0 commit comments