Skip to content

Commit 2e655e7

Browse files
authored
Easier mocking of platforms (#1378)
* core: move OS info into config.rs
1 parent 4580dab commit 2e655e7

8 files changed

Lines changed: 25 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Added
1515

1616
- [core] Add `login` (mobile) and `auth_token` retrieval via login5
17+
- [core] Add `OS` and `os_version` to `config.rs`
1718

1819
### Removed
1920

core/src/config.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ pub(crate) const KEYMASTER_CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd";
66
pub(crate) const ANDROID_CLIENT_ID: &str = "9a8d2f0ce77a4e248bb71fefcb557637";
77
pub(crate) const IOS_CLIENT_ID: &str = "58bd3c95768941ea9eb4350aaa033eb3";
88

9+
// Easily adjust the current platform to mock the behavior on it. If for example
10+
// android or ios needs to be mocked, the `os_version` has to be set to a valid version.
11+
// Otherwise, client-token or login5 requests will fail with a generic invalid-credential error.
12+
/// See [std::env::consts::OS]
13+
pub const OS: &str = std::env::consts::OS;
14+
15+
// valid versions for some os:
16+
// 'android': 30
17+
// 'ios': 17
18+
/// See [sysinfo::System::os_version]
19+
pub fn os_version() -> String {
20+
sysinfo::System::os_version().unwrap_or("0".into())
21+
}
22+
923
#[derive(Clone, Debug)]
1024
pub struct SessionConfig {
1125
pub client_id: String,
@@ -39,7 +53,7 @@ impl SessionConfig {
3953

4054
impl Default for SessionConfig {
4155
fn default() -> Self {
42-
Self::default_for_os(std::env::consts::OS)
56+
Self::default_for_os(OS)
4357
}
4458
}
4559

core/src/connection/handshake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ where
110110
let mut client_nonce = vec![0; 0x10];
111111
thread_rng().fill_bytes(&mut client_nonce);
112112

113-
let platform = match std::env::consts::OS {
113+
let platform = match crate::config::OS {
114114
"android" => Platform::PLATFORM_ANDROID_ARM,
115115
"freebsd" | "netbsd" | "openbsd" => match ARCH {
116116
"x86_64" => Platform::PLATFORM_FREEBSD_X86_64,

core/src/connection/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub async fn authenticate(
112112
_ => CpuFamily::CPU_UNKNOWN,
113113
};
114114

115-
let os = match std::env::consts::OS {
115+
let os = match crate::config::OS {
116116
"android" => Os::OS_ANDROID,
117117
"freebsd" | "netbsd" | "openbsd" => Os::OS_FREEBSD,
118118
"ios" => Os::OS_IPHONE,

core/src/http_client.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
collections::HashMap,
3-
env::consts::OS,
43
time::{Duration, Instant},
54
};
65

@@ -21,11 +20,11 @@ use hyper_util::{
2120
use nonzero_ext::nonzero;
2221
use once_cell::sync::OnceCell;
2322
use parking_lot::Mutex;
24-
use sysinfo::System;
2523
use thiserror::Error;
2624
use url::Url;
2725

2826
use crate::{
27+
config::{os_version, OS},
2928
date::Date,
3029
version::{spotify_version, FALLBACK_USER_AGENT, VERSION_STRING},
3130
Error,
@@ -106,12 +105,10 @@ pub struct HttpClient {
106105
impl HttpClient {
107106
pub fn new(proxy_url: Option<&Url>) -> Self {
108107
let zero_str = String::from("0");
109-
let os_version = System::os_version().unwrap_or_else(|| zero_str.clone());
108+
let os_version = os_version();
110109

111110
let (spotify_platform, os_version) = match OS {
112-
// example os_version: 30
113111
"android" => ("Android", os_version),
114-
// example os_version: 17
115112
"ios" => ("iOS", os_version),
116113
"macos" => ("OSX", zero_str),
117114
"windows" => ("Win32", zero_str),

core/src/login5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::config::OS;
12
use crate::spclient::CLIENT_TOKEN;
23
use crate::token::Token;
34
use crate::{util, Error, SessionConfig};
@@ -15,7 +16,6 @@ use librespot_protocol::{
1516
};
1617
use protobuf::well_known_types::duration::Duration as ProtoDuration;
1718
use protobuf::{Message, MessageField};
18-
use std::env::consts::OS;
1919
use std::time::{Duration, Instant};
2020
use thiserror::Error;
2121
use tokio::time::sleep;

core/src/spclient.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
env::consts::OS,
32
fmt::Write,
43
time::{Duration, Instant},
54
};
@@ -18,6 +17,7 @@ use rand::RngCore;
1817
use sysinfo::System;
1918
use thiserror::Error;
2019

20+
use crate::config::{os_version, OS};
2121
use crate::{
2222
apresolve::SocketAddress,
2323
cdn_url::CdnUrl,
@@ -162,7 +162,7 @@ impl SpClient {
162162
.platform_specific_data
163163
.mut_or_insert_default();
164164

165-
let os_version = System::os_version().unwrap_or_else(|| String::from("0"));
165+
let os_version = os_version();
166166
let kernel_version = System::kernel_version().unwrap_or_else(|| String::from("0"));
167167

168168
match os {
@@ -191,12 +191,10 @@ impl SpClient {
191191
ios_data.user_interface_idiom = 0;
192192
ios_data.target_iphone_simulator = false;
193193
ios_data.hw_machine = "iPhone14,5".to_string();
194-
// example system_version: 17
195194
ios_data.system_version = os_version;
196195
}
197196
"android" => {
198197
let android_data = platform_data.mut_android();
199-
// example android_version: 30
200198
android_data.android_version = os_version;
201199
android_data.api_version = 31;
202200
"Pixel".clone_into(&mut android_data.device_name);

core/src/version.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ pub const SPOTIFY_MOBILE_VERSION: &str = "8.6.84";
2929
pub const FALLBACK_USER_AGENT: &str = "Spotify/117300517 Linux/0 (librespot)";
3030

3131
pub fn spotify_version() -> String {
32-
match std::env::consts::OS {
32+
match crate::config::OS {
3333
"android" | "ios" => SPOTIFY_MOBILE_VERSION.to_owned(),
3434
_ => SPOTIFY_VERSION.to_string(),
3535
}
3636
}
3737

3838
pub fn spotify_semantic_version() -> String {
39-
match std::env::consts::OS {
39+
match crate::config::OS {
4040
"android" | "ios" => SPOTIFY_MOBILE_VERSION.to_owned(),
4141
_ => SPOTIFY_SEMANTIC_VERSION.to_string(),
4242
}

0 commit comments

Comments
 (0)