Implement helper for wasm32 MilliSecondsSinceUnixEpoch

This commit is contained in:
Benjamin Kampmann
2021-11-25 15:52:03 +01:00
parent 42680164ea
commit 4c60db94a2
10 changed files with 34 additions and 27 deletions

View File

@@ -15,6 +15,7 @@ pub use uuid;
pub mod deserialized_responses;
pub mod executor;
pub mod locks;
pub mod util;
/// Super trait that is used for our store traits, this trait will differ if
/// it's used on WASM. WASM targets will not require `Send` and `Sync` to have

View File

@@ -0,0 +1,14 @@
use ruma::MilliSecondsSinceUnixEpoch;
use instant::SystemTime;
/// Platform agnostic helper function to create MilliSecondsSinceUnixEpoch
pub fn milli_seconds_since_unix_epoch() -> MilliSecondsSinceUnixEpoch {
let duration = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("now is always higher");
let millis = duration.
as_millis()
.try_into()
.expect("can't convert miliseconds since UNIXEPOCH");
MilliSecondsSinceUnixEpoch(millis)
}

View File

@@ -25,6 +25,7 @@ use matrix_sdk_common::{
deserialized_responses::{AlgorithmInfo, EncryptionInfo, SyncRoomEvent, VerificationState},
locks::Mutex,
uuid::Uuid,
util::milli_seconds_since_unix_epoch,
};
use ruma::{
api::client::r0::{
@@ -1569,6 +1570,7 @@ pub(crate) mod test {
verification::test::{outgoing_request_to_event, request_to_event},
EncryptionSettings, ReadOnlyDevice, ToDeviceRequest,
};
use matrix_sdk_common::util::milli_seconds_since_unix_epoch;
/// These keys need to be periodically uploaded to the server.
type OneTimeKeys = BTreeMap<DeviceKeyId, OneTimeKey>;
@@ -2020,7 +2022,7 @@ pub(crate) mod test {
let event = SyncMessageEvent {
event_id: event_id!("$xxxxx:example.org"),
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
origin_server_ts: milli_seconds_since_unix_epoch(),
sender: alice.user_id().clone(),
content: encrypted_content,
unsigned: Unsigned::default(),

View File

@@ -164,9 +164,11 @@ mod test {
use matrix_sdk_test::async_test;
use std::{
sync::Arc,
time::{Duration, Instant},
time::Duration,
};
use matrix_sdk_common::instant::Instant;
use ruma::{events::room::message::RoomMessageEventContent, room_id, user_id};
use super::EncryptionSettings;

View File

@@ -32,8 +32,8 @@ use tracing::trace;
use uuid::Uuid;
use super::{
caches::SessionStore, Changes, CryptoStore, CryptoStoreError, InboundGroupSession, PickleKey,
ReadOnlyAccount, Result, Session, EncryptedPickleKey,
caches::SessionStore, BackupKeys, Changes, CryptoStore, CryptoStoreError, InboundGroupSession,
PickleKey, EncryptedPickleKey, ReadOnlyAccount, Result, RoomKeyCounts, Session,
};
use crate::{
gossiping::{GossipRequest, SecretInfo},

View File

@@ -28,6 +28,7 @@ use ruma::{
DeviceId, DeviceIdBox, EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId,
};
use tracing::{info, trace, warn};
use matrix_sdk_common::util::milli_seconds_since_unix_epoch;
use super::{
cache::VerificationCache,
@@ -190,7 +191,6 @@ impl VerificationMachine {
self.verifications.get_sas(user_id, flow_id)
}
#[cfg(not(target_arch = "wasm32"))]
fn is_timestamp_valid(timestamp: &MilliSecondsSinceUnixEpoch) -> bool {
use ruma::{uint, UInt};
@@ -201,21 +201,12 @@ impl VerificationMachine {
let timestamp_threshold: UInt = uint!(300);
let timestamp = timestamp.as_secs();
let now = MilliSecondsSinceUnixEpoch::now().as_secs();
let now = milli_seconds_since_unix_epoch().as_secs();
!(now.saturating_sub(timestamp) > old_timestamp_threshold
|| timestamp.saturating_sub(now) > timestamp_threshold)
}
#[cfg(target_arch = "wasm32")]
fn is_timestamp_valid(timestamp: &MilliSecondsSinceUnixEpoch) -> bool {
// TODO the non-wasm method with the same name uses
// `MilliSecondsSinceUnixEpoch::now()` which internally uses
// `SystemTime::now()` this panics under WASM, thus we're returning here
// true for now.
true
}
fn queue_up_content(
&self,
recipient: &UserId,
@@ -519,10 +510,9 @@ mod test {
use std::{
convert::TryFrom,
sync::Arc,
time::{Duration, Instant},
time::Duration,
};
use matrix_sdk_common::locks::Mutex;
use matrix_sdk_common::{instant::Instant, locks::Mutex};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test;

View File

@@ -19,7 +19,7 @@ use std::{
#[cfg(feature = "qrcode")]
use matrix_qrcode::QrVerificationData;
use matrix_sdk_common::{instant::Instant, uuid::Uuid};
use matrix_sdk_common::{instant::Instant, uuid::Uuid, util::milli_seconds_since_unix_epoch};
#[cfg(feature = "qrcode")]
use ruma::DeviceKeyAlgorithm;
use ruma::{
@@ -35,7 +35,7 @@ use ruma::{
AnyMessageEventContent, AnyToDeviceEventContent,
},
to_device::DeviceIdOrAllDevices,
DeviceId, DeviceIdBox, MilliSecondsSinceUnixEpoch, RoomId, UserId,
DeviceId, DeviceIdBox, RoomId, UserId,
};
use tracing::{info, trace, warn};
@@ -161,7 +161,7 @@ impl VerificationRequest {
self.account.device_id().into(),
self.flow_id().as_str().to_string(),
methods,
MilliSecondsSinceUnixEpoch::now(),
milli_seconds_since_unix_epoch(),
);
ToDeviceRequest::new_for_recipients(

View File

@@ -14,7 +14,7 @@
use std::sync::Arc;
#[cfg(test)]
use std::time::Instant;
use matrix_sdk_common::instant::Instant;
use ruma::{
events::key::verification::{cancel::CancelCode, ShortAuthenticationString},

View File

@@ -18,7 +18,7 @@ mod sas_state;
use std::sync::{Arc, Mutex};
#[cfg(test)]
use std::time::Instant;
use matrix_sdk_common::instant::Instant;
use inner_sas::InnerSas;
use matrix_sdk_common::uuid::Uuid;

View File

@@ -16,8 +16,9 @@ use std::{
convert::{TryFrom, TryInto},
matches,
sync::{Arc, Mutex},
time::{Duration, Instant},
time::{Duration},
};
use matrix_sdk_common::instant::Instant;
use matrix_sdk_common::uuid::Uuid;
use olm_rs::sas::OlmSas;
@@ -1136,9 +1137,6 @@ impl SasState<Cancelled> {
#[cfg(test)]
mod test {
use std::convert::TryFrom;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test;
use matrix_sdk_test::async_test;
use ruma::{