Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use regex for OrganizationID/UserID/DeviceName validation to speedup tests #5235

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ rand_07 = { package = "rand", version = "0.7", default-features = false }
rand_08 = { package = "rand", version = "0.8", default-features = false }
rand = { version = "0.8.5", default-features = false }
regex = { version = "1.9.6", default-features = false }
regex-syntax = { version = "0.7.5", default-features = false }
reqwest-eventsource = { version = "0.4.0", default-features = false }
reqwest = { version = "0.11.22", default-features = false }
rmp-serde = { version = "1.1.2", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions libparsec/crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ data-encoding = { workspace = true, features = ["std"] }
url = { workspace = true }
percent-encoding = { workspace = true, features = ["alloc"] }
regex = { workspace = true, features = ["std", "perf", "unicode"] }
regex-syntax = { workspace = true, features = ["unicode-perl"] }
unicode-normalization = { workspace = true, features = ["std"] }
paste = { workspace = true }
flate2 = { workspace = true, features = ["rust_backend"] }
Expand Down
50 changes: 26 additions & 24 deletions libparsec/crates/types/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

use email_address_parser::EmailAddress;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::convert::TryFrom;
Expand Down Expand Up @@ -145,7 +144,7 @@ macro_rules! new_uuid_type {
}

macro_rules! new_string_based_id_type {
(pub $name:ident, $bytes_size:expr, $pattern:expr) => {
(pub $name:ident, $match_fn:expr) => {
#[derive(
Clone, SerializeDisplay, DeserializeFromStr, PartialEq, Eq, Hash, PartialOrd, Ord,
)]
Expand All @@ -157,23 +156,6 @@ macro_rules! new_string_based_id_type {
}
}

impl $name {
const BYTES_SIZE: usize = $bytes_size;

fn pattern() -> &'static Regex {
lazy_static! {
static ref PATTERN: Regex = Regex::new($pattern)
.expect("`pattern` should be a valid regular expression");
}
&PATTERN
}

/// Validate that the input is contained in [`Self::BYTES_SIZE`] and match the regex provided by [`Self::pattern`].
pub fn is_valid(input: &str) -> bool {
input.len() <= Self::BYTES_SIZE && Self::pattern().is_match(input)
}
}

impl std::convert::AsRef<str> for $name {
#[inline]
fn as_ref(&self) -> &str {
Expand All @@ -196,7 +178,7 @@ macro_rules! new_string_based_id_type {
fn try_from(s: &str) -> Result<Self, Self::Error> {
let id: String = s.nfc().collect();

if Self::is_valid(&id) {
if $match_fn(&id) {
Ok(Self(id))
} else {
Err(concat!("Invalid ", stringify!($name)))
Expand Down Expand Up @@ -242,17 +224,28 @@ impl From<ChunkID> for BlockID {
}
}

// Equivalent to pattern "^[\w\-]{1,32}$" on 32 bytes
#[inline]
fn match_legacy_id(id: &str) -> bool {
// `str::len` returns the number of bytes
let bytes_size = id.len();

let is_valid_char = |c: char| c == '-' || regex_syntax::is_word_character(c);

(1..=32).contains(&bytes_size) && id.chars().all(is_valid_char)
}

/*
* OrganizationID
*/

new_string_based_id_type!(pub OrganizationID, 32, r"^[\w\-]{1,32}$");
new_string_based_id_type!(pub OrganizationID, match_legacy_id);

/*
* UserID
*/

new_string_based_id_type!(pub UserID, 32, r"^[\w\-]{1,32}$");
new_string_based_id_type!(pub UserID, match_legacy_id);

impl UserID {
pub fn to_device_id(&self, device_name: DeviceName) -> DeviceID {
Expand All @@ -264,13 +257,22 @@ impl UserID {
* DeviceName
*/

new_string_based_id_type!(pub DeviceName, 32, r"^[\w\-]{1,32}$");
new_string_based_id_type!(pub DeviceName, match_legacy_id);

/*
* DeviceLabel
*/

new_string_based_id_type!(pub DeviceLabel, 255, r"^.+$");
// Equivalent to pattern r"^.+$" (i.e. at least 1 character) on 255 bytes
#[inline]
fn match_device_label(id: &str) -> bool {
// `str::len` returns the number of bytes
let bytes_size = id.len();

(1..=255).contains(&bytes_size)
}

new_string_based_id_type!(pub DeviceLabel, match_device_label);
impl_from_maybe!(Option<DeviceLabel>);

/*
Expand Down
33 changes: 23 additions & 10 deletions libparsec/crates/types/tests/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,44 @@ fn device_label_bad_size() {
}

#[rstest]
#[case("foo42")]
#[case("FOO")]
#[case("f")]
#[case("f-o-o")]
#[case("f_o_o")]
#[case(&"x".repeat(32))]
#[case("三国")]
#[case::text_32_long("This_one_is_exactly_32_char_long")]
#[case::alphanum("01239AbcdDEFA")]
#[case::with_underscore("hello_World")]
#[case::with_hyphen("hello-World")]
// cspell:disable-next-line
#[case::with_unicode_1("ªµºÖØøˆˠˬˮ\u{301}ͶͽͿΆΊ")]
#[case::with_unicode_2("ΌΎΣҁ\u{484}Աՙֈ\u{5c7}\u{5bf}\u{5c1}\u{5af}\u{5c4}")]
#[case::with_unicode_3("ת\u{5ef}\u{610}٩ۓە")]
#[case::with_unicode_4("\u{6ea}\u{6df}ۿܐݍ߀ߺ\u{7fd}ࠀࡀ")]
// cspell:disable-next-line
#[case::hello("𝔅𝔬𝔫𝔧𝔬𝔲𝔯")]
fn organization_id_user_id_and_device_name(#[case] raw: &str) {
use unicode_normalization::UnicodeNormalization;

let nfc_raw = raw.nfc().collect::<String>();
p_assert_eq!(raw, nfc_raw, "raw != nfc_raw (expected `{:#?}`)", nfc_raw);

let organization_id = OrganizationID::from_str(raw).unwrap();
p_assert_eq!(organization_id.to_string(), raw);
p_assert_eq!(organization_id.as_ref(), raw);
p_assert_eq!(organization_id, OrganizationID::from_str(raw).unwrap());

let user_id = UserID::from_str(raw).unwrap();
p_assert_eq!(user_id.to_string(), raw);
p_assert_eq!(user_id.as_ref(), raw);
p_assert_eq!(user_id, UserID::from_str(raw).unwrap());

let device_name = DeviceName::from_str(raw).unwrap();
p_assert_eq!(device_name.to_string(), raw);
p_assert_eq!(device_name.as_ref(), raw);
p_assert_eq!(device_name, DeviceName::from_str(raw).unwrap());
}

#[rstest]
#[case(&"x".repeat(33))]
#[case("F~o")]
#[case("f o")]
#[case::text_33_long("This_text_is_exactly_33_char_long")]
#[case::empty("")]
#[case::invalid_tilde("F~o")]
#[case::invalid_space("f o")]
fn bad_organization_id_user_id_and_device_name(#[case] raw: &str) {
OrganizationID::from_str(raw).unwrap_err();
UserID::from_str(raw).unwrap_err();
Expand Down
4 changes: 2 additions & 2 deletions libparsec/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

/// Validate a label without doing a unicode normalization.
pub fn validate_entry_name(raw: &str) -> bool {
libparsec_types::EntryName::is_valid(raw).is_ok()
libparsec_types::EntryName::from_str(raw).is_ok()
}

pub fn validate_path(raw: &str) -> bool {
Expand All @@ -23,7 +23,7 @@ pub fn validate_email(raw: &str) -> bool {

/// Validate a label without doing a unicode normalization.
pub fn validate_device_label(raw: &str) -> bool {
libparsec_types::DeviceLabel::is_valid(raw)
libparsec_types::DeviceLabel::from_str(raw).is_ok()
}

pub fn validate_invitation_token(raw: &str) -> bool {
Expand Down