|
| 1 | +#![cfg(target_os = "windows")] |
| 2 | + |
| 3 | +use desktop_lib::browser_details::{get_chrome_profiles, Browsers}; |
| 4 | +use std::fs; |
| 5 | +use std::io::Write; |
| 6 | +use std::path::PathBuf; |
| 7 | +use tempfile::TempDir; |
| 8 | + |
| 9 | +fn write_local_state(temp: &TempDir, relative: &str, contents: &str) -> PathBuf { |
| 10 | + let mut path = temp.path().to_path_buf(); |
| 11 | + path.push(relative); |
| 12 | + if let Some(parent) = path.parent() { |
| 13 | + fs::create_dir_all(parent).expect("failed to create Local State parent"); |
| 14 | + } |
| 15 | + let mut file = fs::File::create(&path).expect("failed to create Local State file"); |
| 16 | + file.write_all(contents.as_bytes()) |
| 17 | + .expect("failed to write Local State"); |
| 18 | + path |
| 19 | +} |
| 20 | + |
| 21 | +fn install_local_state(temp: &TempDir, browser: Browsers, json: &str) { |
| 22 | + let relative = match browser { |
| 23 | + Browsers::Chrome => "Google/Chrome/User Data/Local State", |
| 24 | + Browsers::Edge => "Microsoft/Edge/User Data/Local State", |
| 25 | + Browsers::Brave => "BraveSoftware/Brave-Browser/User Data/Local State", |
| 26 | + _ => panic!("unexpected browser for chrome profile test"), |
| 27 | + }; |
| 28 | + write_local_state(temp, relative, json); |
| 29 | +} |
| 30 | + |
| 31 | +fn with_localappdata<F: FnOnce()>(temp: &TempDir, f: F) { |
| 32 | + use std::env; |
| 33 | + let original = env::var_os("LOCALAPPDATA"); |
| 34 | + env::set_var("LOCALAPPDATA", temp.path()); |
| 35 | + f(); |
| 36 | + match original { |
| 37 | + Some(val) => env::set_var("LOCALAPPDATA", val), |
| 38 | + None => env::remove_var("LOCALAPPDATA"), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn reads_profiles_and_adds_default_when_missing() { |
| 44 | + let temp = TempDir::new().expect("temp dir"); |
| 45 | + let local_state = r#"{ |
| 46 | + "profile": { |
| 47 | + "info_cache": { |
| 48 | + "Profile 1": { |
| 49 | + "profile_dir": "Profile 1", |
| 50 | + "name": "Personal" |
| 51 | + }, |
| 52 | + "Profile 2": { |
| 53 | + "gaia_name": "Work" |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + }"#; |
| 58 | + install_local_state(&temp, Browsers::Chrome, local_state); |
| 59 | + |
| 60 | + with_localappdata(&temp, || { |
| 61 | + let profiles = get_chrome_profiles(Browsers::Chrome).expect("profiles"); |
| 62 | + let labels: Vec<_> = profiles.iter().map(|p| &p.display_name).collect(); |
| 63 | + assert!(labels.contains(&"Personal".to_string())); |
| 64 | + assert!(labels.contains(&"Work".to_string())); |
| 65 | + assert!(labels.contains(&"Default".to_string())); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +fn deduplicates_profile_directories() { |
| 71 | + let temp = TempDir::new().expect("temp dir"); |
| 72 | + let local_state = r#"{ |
| 73 | + "profile": { |
| 74 | + "info_cache": { |
| 75 | + "Profile 1": { |
| 76 | + "profile_dir": "Profile 1", |
| 77 | + "name": "Personal" |
| 78 | + }, |
| 79 | + "Duplicate": { |
| 80 | + "profile_dir": "Profile 1", |
| 81 | + "name": "Duplicate" |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }"#; |
| 86 | + install_local_state(&temp, Browsers::Chrome, local_state); |
| 87 | + |
| 88 | + with_localappdata(&temp, || { |
| 89 | + let profiles = get_chrome_profiles(Browsers::Chrome).expect("profiles"); |
| 90 | + let dirs: Vec<_> = profiles.iter().map(|p| &p.directory).collect(); |
| 91 | + assert_eq!(dirs.iter().filter(|d| d.as_str() == "Profile 1").count(), 1); |
| 92 | + }); |
| 93 | +} |
0 commit comments