1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
// [impl->dsn~uri-data-model-naming~1]
// [impl->req~uri-data-model-proto~1]
use std::hash::{Hash, Hasher};
use std::str::FromStr;
use uriparse::{Authority, URIReference};
pub use crate::up_core_api::uri::UUri;
pub(crate) const WILDCARD_AUTHORITY: &str = "*";
pub(crate) const WILDCARD_ENTITY_ID: u32 = 0x0000_FFFF;
pub(crate) const WILDCARD_ENTITY_VERSION: u32 = 0x0000_00FF;
pub(crate) const WILDCARD_RESOURCE_ID: u32 = 0x0000_FFFF;
pub(crate) const RESOURCE_ID_RESPONSE: u32 = 0;
pub(crate) const RESOURCE_ID_MIN_EVENT: u32 = 0x8000;
#[derive(Debug)]
pub enum UUriError {
SerializationError(String),
ValidationError(String),
}
impl UUriError {
pub fn serialization_error<T>(message: T) -> UUriError
where
T: Into<String>,
{
Self::SerializationError(message.into())
}
pub fn validation_error<T>(message: T) -> UUriError
where
T: Into<String>,
{
Self::ValidationError(message.into())
}
}
impl std::fmt::Display for UUriError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SerializationError(e) => f.write_fmt(format_args!("Serialization error: {}", e)),
Self::ValidationError(e) => f.write_fmt(format_args!("Validation error: {}", e)),
}
}
}
impl std::error::Error for UUriError {}
// [impl->req~uri-serialization~1]
impl From<&UUri> for String {
/// Serializes a UUri to a URI string.
///
/// # Arguments
///
/// * `uri` - The UUri to serialize.
///
/// # Returns
///
/// The output of [`UUri::to_uri`] without inlcuding the uProtocol scheme.
fn from(uri: &UUri) -> Self {
UUri::to_uri(uri, false)
}
}
impl FromStr for UUri {
type Err = UUriError;
/// Attempts to parse a `String` into a `UUri`.
///
/// As part of the parsing, the _authority_ of the URI is getting normalized. This means that all characters
/// are converted to lowercase, no bytes that are in the unreserved character set remain percent-encoded,
/// and all alphabetical characters in percent-encodings are converted to uppercase.
///
/// # Arguments
///
/// * `uri` - The `String` to be converted into a `UUri`.
///
/// # Returns
///
/// A `Result` containing either the `UUri` representation of the URI or a `SerializationError`.
///
/// # Examples
///
/// ```rust
/// use std::str::FromStr;
/// use up_rust::UUri;
///
/// let uri = UUri {
/// authority_name: "VIN.vehicles".to_string(),
/// ue_id: 0x000A_8000,
/// ue_version_major: 0x02,
/// resource_id: 0x0000_1a50,
/// ..Default::default()
/// };
///
/// let uri_from = UUri::from_str("//VIN.vehicles/A8000/2/1A50").unwrap();
/// assert_eq!(uri, uri_from);
/// ````
// [impl->dsn~uri-authority-name-length~1]
// [impl->dsn~uri-scheme~1]
// [impl->dsn~uri-host-only~2]
// [impl->dsn~uri-authority-mapping~1]
// [impl->dsn~uri-path-mapping~1]
// [impl->req~uri-serialization~1]
fn from_str(uri: &str) -> Result<Self, Self::Err> {
if uri.is_empty() {
return Err(UUriError::serialization_error("URI is empty"));
}
let parsed_uri = URIReference::try_from(uri)
.map_err(|e| UUriError::serialization_error(e.to_string()))?;
if let Some(scheme) = parsed_uri.scheme() {
if scheme.ne("up") {
return Err(UUriError::serialization_error(
"uProtocol URI must use 'up' scheme",
));
}
}
if parsed_uri.has_query() {
return Err(UUriError::serialization_error(
"uProtocol URI must not contain query",
));
}
if parsed_uri.has_fragment() {
return Err(UUriError::serialization_error(
"uProtocol URI must not contain fragment",
));
}
let authority_name = parsed_uri
.authority()
.map_or(Ok(String::default()), |auth| {
if auth.has_port() {
Err(UUriError::serialization_error(
"uProtocol URI's authority must not contain port",
))
} else if auth.has_username() || auth.has_password() {
Err(UUriError::serialization_error(
"uProtocol URI's authority must not contain userinfo",
))
} else {
let auth_name = auth.host().to_string();
if auth_name.len() <= 128 {
Ok(auth_name)
} else {
Err(UUriError::serialization_error(
"URI's authority name must not exceed 128 characters",
))
}
}
})?;
let path_segments = parsed_uri.path().segments();
if path_segments.len() != 3 {
return Err(UUriError::serialization_error(
"uProtocol URI must contain entity ID, entity version and resource ID",
));
}
let entity = path_segments[0].as_str();
if entity.is_empty() {
return Err(UUriError::serialization_error(
"URI must contain non-empty entity ID",
));
}
let ue_id = u32::from_str_radix(entity, 16).map_err(|e| {
UUriError::serialization_error(format!("Cannot parse entity ID: {}", e))
})?;
let version = path_segments[1].as_str();
if version.is_empty() {
return Err(UUriError::serialization_error(
"URI must contain non-empty entity version",
));
}
let ue_version_major = u8::from_str_radix(version, 16).map_err(|e| {
UUriError::serialization_error(format!("Cannot parse entity version: {}", e))
})?;
let resource = path_segments[2].as_str();
if resource.is_empty() {
return Err(UUriError::serialization_error(
"URI must contain non-empty resource ID",
));
}
let resource_id = u16::from_str_radix(resource, 16).map_err(|e| {
UUriError::serialization_error(format!("Cannot parse resource ID: {}", e))
})?;
Ok(UUri {
authority_name,
ue_id,
ue_version_major: ue_version_major as u32,
resource_id: resource_id as u32,
..Default::default()
})
}
}
// [impl->req~uri-serialization~1]
impl TryFrom<String> for UUri {
type Error = UUriError;
/// Attempts to serialize a `String` into a `UUri`.
///
/// # Arguments
///
/// * `uri` - The `String` to be converted into a `UUri`.
///
/// # Returns
///
/// A `Result` containing either the `UUri` representation of the URI or a `SerializationError`.
fn try_from(uri: String) -> Result<Self, Self::Error> {
UUri::from_str(uri.as_str())
}
}
// [impl->req~uri-serialization~1]
impl TryFrom<&str> for UUri {
type Error = UUriError;
/// Attempts to serialize a `String` into a `UUri`.
///
/// # Arguments
///
/// * `uri` - The `String` to be converted into a `UUri`.
///
/// # Returns
///
/// A `Result` containing either the `UUri` representation of the URI or a `SerializationError`.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// authority_name: "".to_string(),
/// ue_id: 0x001A_8000,
/// ue_version_major: 0x02,
/// resource_id: 0x0000_1a50,
/// ..Default::default()
/// };
///
/// let uri_from = UUri::try_from("/1A8000/2/1A50").unwrap();
/// assert_eq!(uri, uri_from);
/// ````
fn try_from(uri: &str) -> Result<Self, Self::Error> {
UUri::from_str(uri)
}
}
impl Hash for UUri {
fn hash<H: Hasher>(&self, state: &mut H) {
self.authority_name.hash(state);
self.ue_id.hash(state);
self.ue_version_major.hash(state);
self.resource_id.hash(state);
}
}
impl Eq for UUri {}
impl UUri {
/// Serializes this UUri to a URI string.
///
/// # Arguments
///
/// * `include_scheme` - Indicates whether to include the uProtocol scheme (`up`) in the URI.
///
/// # Returns
///
/// The URI as defined by the [uProtocol Specification](https://github.com/eclipse-uprotocol/up-spec).
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uuri = UUri {
/// authority_name: String::from("VIN.vehicles"),
/// ue_id: 0x0000_800A,
/// ue_version_major: 0x02,
/// resource_id: 0x0000_1a50,
/// ..Default::default()
/// };
///
/// let uri_string = uuri.to_uri(true);
/// assert_eq!(uri_string, "up://VIN.vehicles/800A/2/1A50");
/// ````
pub fn to_uri(&self, include_scheme: bool) -> String {
let mut output = String::default();
if include_scheme {
output.push_str("up:");
}
if !self.authority_name.is_empty() {
output.push_str("//");
output.push_str(&self.authority_name);
}
let uri = format!(
"/{:X}/{:X}/{:X}",
self.ue_id, self.ue_version_major, self.resource_id
);
output.push_str(&uri);
output
}
/// Creates a new UUri from its parts.
///
/// # Errors
///
/// Returns a [`UUriError::ValidationError`] if the authority does not comply with the UUri specification.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// assert!(UUri::try_from_parts("vin", 0x5a6b, 0x01, 0x0001).is_ok());
/// ```
// [impl->dsn~uri-authority-name-length~1]
// [impl->dsn~uri-host-only~2]
pub fn try_from_parts(
authority: &str,
entity_id: u32,
entity_version: u8,
resource_id: u16,
) -> Result<Self, UUriError> {
let auth = Authority::try_from(authority)
.map_err(|e| UUriError::validation_error(format!("invalid authority: {}", e)))
.and_then(|auth| {
if auth.has_port() {
Err(UUriError::validation_error(
"uProtocol URI's authority must not contain port",
))
} else if auth.has_username() || auth.has_password() {
Err(UUriError::validation_error(
"uProtocol URI's authority must not contain userinfo",
))
} else {
let auth_name = auth.host().to_string();
if auth_name.len() <= 128 {
Ok(auth)
} else {
Err(UUriError::validation_error(
"URI's authority name must not exceed 128 characters",
))
}
}
})?;
Ok(UUri {
authority_name: auth.host().to_string(),
ue_id: entity_id,
ue_version_major: entity_version as u32,
resource_id: resource_id as u32,
..Default::default()
})
}
/// Gets a URI that consists of wildcards only and therefore matches any URI.
pub fn any() -> Self {
UUri {
authority_name: WILDCARD_AUTHORITY.to_string(),
ue_id: WILDCARD_ENTITY_ID,
ue_version_major: WILDCARD_ENTITY_VERSION,
resource_id: WILDCARD_RESOURCE_ID,
..Default::default()
}
}
/// Checks if this URI is empty.
///
/// # Returns
///
/// 'true' if this URI is equal to `UUri::default()`, `false` otherwise.
pub fn is_empty(&self) -> bool {
self.eq(&UUri::default())
}
/// Check if an `UUri` is remote, by comparing authority fields.
///
/// # Returns
///
/// 'true' if other_uri has a different authority than `Self`, `false` otherwise.
///
/// # Examples
///
/// ```rust
/// use std::str::FromStr;
/// use up_rust::UUri;
///
/// let authority_a = UUri::from_str("up://Authority.A/100A/1/0").unwrap();
/// let authority_b = UUri::from_str("up://Authority.B/200B/2/20").unwrap();
/// assert!(authority_a.is_remote_authority(&authority_b));
/// ````
pub fn is_remote_authority(&self, other_uri: &UUri) -> bool {
self.authority_name != WILDCARD_AUTHORITY
&& other_uri.authority_name != WILDCARD_AUTHORITY
&& self.authority_name != other_uri.authority_name
}
/// Checks if this UUri has an empty authority name.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uuri = UUri::try_from_parts("", 0x9b3a, 0x01, 0x145b).unwrap();
/// assert!(uuri.has_empty_authority());
/// ```
pub fn has_empty_authority(&self) -> bool {
self.authority_name.is_empty()
}
/// Checks if this UUri has a wildcard authority name.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uuri = UUri::try_from_parts("*", 0x9b3a, 0x01, 0x145b).unwrap();
/// assert!(uuri.has_wildcard_authority());
/// ```
pub fn has_wildcard_authority(&self) -> bool {
self.authority_name == WILDCARD_AUTHORITY
}
/// Checks if this UUri has a wildcard entity identifier.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uuri = UUri::try_from_parts("vin", 0xFFFF, 0x01, 0x145b).unwrap();
/// assert!(uuri.has_wildcard_entity_id());
/// ```
pub fn has_wildcard_entity_id(&self) -> bool {
self.ue_id & WILDCARD_ENTITY_ID == WILDCARD_ENTITY_ID
}
/// Checks if this UUri has a wildcard major version.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uuri = UUri::try_from_parts("vin", 0x9b3a, 0xFF, 0x145b).unwrap();
/// assert!(uuri.has_wildcard_version());
/// ```
pub fn has_wildcard_version(&self) -> bool {
self.ue_version_major == WILDCARD_ENTITY_VERSION
}
/// Checks if this UUri has a wildcard entity identifier.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uuri = UUri::try_from_parts("vin", 0x9b3a, 0x01, 0xFFFF).unwrap();
/// assert!(uuri.has_wildcard_resource_id());
/// ```
pub fn has_wildcard_resource_id(&self) -> bool {
self.resource_id == WILDCARD_RESOURCE_ID
}
/// Verifies that this UUri does not contain any wildcards.
///
/// # Errors
///
/// Returns an error if any of this UUri's properties contain a wildcard value.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// authority_name: String::from("VIN.vehicles"),
/// ue_id: 0x0000_2310,
/// ue_version_major: 0x03,
/// resource_id: 0xa000,
/// ..Default::default()
/// };
/// assert!(uri.verify_no_wildcards().is_ok());
/// ```
pub fn verify_no_wildcards(&self) -> Result<(), UUriError> {
if self.has_wildcard_authority() {
Err(UUriError::validation_error(format!(
"Authority must not contain wildcard character [{}]",
WILDCARD_AUTHORITY
)))
} else if self.has_wildcard_entity_id() {
Err(UUriError::validation_error(format!(
"Entity ID must not be set to wildcard value [{:#X}]",
WILDCARD_ENTITY_ID
)))
} else if self.has_wildcard_version() {
Err(UUriError::validation_error(format!(
"Entity version must not be set to wildcard value [{:#X}]",
WILDCARD_ENTITY_VERSION
)))
} else if self.has_wildcard_resource_id() {
Err(UUriError::validation_error(format!(
"Resource ID must not be set to wildcard value [{:#X}]",
WILDCARD_RESOURCE_ID
)))
} else {
Ok(())
}
}
/// Checks if this UUri refers to a service method.
///
/// Returns `true` if 0 < resource ID < 0x8000.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// resource_id: 0x7FFF,
/// ..Default::default()
/// };
/// assert!(uri.is_rpc_method());
/// ```
pub fn is_rpc_method(&self) -> bool {
self.resource_id > RESOURCE_ID_RESPONSE && self.resource_id < RESOURCE_ID_MIN_EVENT
}
/// Verifies that this UUri refers to a service method.
///
/// # Errors
///
/// Returns an error if [`Self::is_rpc_method`] fails or
/// the UUri [contains any wildcards](Self::verify_no_wildcards).
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// resource_id: 0x8000,
/// ..Default::default()
/// };
/// assert!(uri.verify_rpc_method().is_err());
///
/// let uri = UUri {
/// resource_id: 0x0,
/// ..Default::default()
/// };
/// assert!(uri.verify_rpc_method().is_err());
/// ```
pub fn verify_rpc_method(&self) -> Result<(), UUriError> {
if !self.is_rpc_method() {
Err(UUriError::validation_error(format!(
"Resource ID must be a value from ]{:#X}, {:#X}[",
RESOURCE_ID_RESPONSE, RESOURCE_ID_MIN_EVENT
)))
} else {
self.verify_no_wildcards()
}
}
/// Checks if this UUri represents a destination for a Notification.
///
/// Returns `true` if resource ID is 0.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// resource_id: 0,
/// ..Default::default()
/// };
/// assert!(uri.is_notification_destination());
/// ```
pub fn is_notification_destination(&self) -> bool {
self.resource_id == RESOURCE_ID_RESPONSE
}
/// Checks if this UUri represents an RPC response address.
///
/// Returns `true` if resource ID is 0.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// resource_id: 0,
/// ..Default::default()
/// };
/// assert!(uri.is_rpc_response());
/// ```
pub fn is_rpc_response(&self) -> bool {
self.resource_id == RESOURCE_ID_RESPONSE
}
/// Verifies that this UUri represents an RPC response address.
///
/// # Errors
///
/// Returns an error if [`Self::is_rpc_response`] fails or
/// the UUri [contains any wildcards](Self::verify_no_wildcards).
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// resource_id: 0x4001,
/// ..Default::default()
/// };
/// assert!(uri.verify_rpc_response().is_err());
/// ```
pub fn verify_rpc_response(&self) -> Result<(), UUriError> {
if !self.is_rpc_response() {
Err(UUriError::validation_error(format!(
"Resource ID must be {:#X}",
RESOURCE_ID_RESPONSE
)))
} else {
self.verify_no_wildcards()
}
}
/// Checks if this UUri can be used as the source of an event.
///
/// Returns `true` if resource ID >= 0x8000.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// resource_id: 0x8000,
/// ..Default::default()
/// };
/// assert!(uri.is_event());
/// ```
pub fn is_event(&self) -> bool {
self.resource_id >= RESOURCE_ID_MIN_EVENT
}
/// Verifies that this UUri can be used as the source of an event.
///
/// # Errors
///
/// Returns an error if [`Self::is_event`] fails or
/// the UUri [contains any wildcards](Self::verify_no_wildcards).
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let uri = UUri {
/// resource_id: 0x7FFF,
/// ..Default::default()
/// };
/// assert!(uri.verify_event().is_err());
/// ```
pub fn verify_event(&self) -> Result<(), UUriError> {
if !self.is_event() {
Err(UUriError::validation_error(format!(
"Resource ID must be >= {:#X}",
RESOURCE_ID_MIN_EVENT
)))
} else {
self.verify_no_wildcards()
}
}
fn matches_authority(&self, candidate: &UUri) -> bool {
self.authority_name == WILDCARD_AUTHORITY || self.authority_name == candidate.authority_name
}
fn matches_entity_type(&self, candidate: &UUri) -> bool {
self.ue_id & WILDCARD_ENTITY_ID == WILDCARD_ENTITY_ID
|| self.ue_id & WILDCARD_ENTITY_ID == candidate.ue_id & WILDCARD_ENTITY_ID
}
fn matches_entity_instance(&self, candidate: &UUri) -> bool {
self.ue_id & 0xFFFF_0000 == 0x0000_0000
|| self.ue_id & 0xFFFF_0000 == candidate.ue_id & 0xFFFF_0000
}
fn matches_entity_version(&self, candidate: &UUri) -> bool {
self.ue_version_major == WILDCARD_ENTITY_VERSION
|| self.ue_version_major == candidate.ue_version_major
}
fn matches_entity(&self, candidate: &UUri) -> bool {
self.matches_entity_type(candidate)
&& self.matches_entity_instance(candidate)
&& self.matches_entity_version(candidate)
}
fn matches_resource(&self, candidate: &UUri) -> bool {
self.resource_id == WILDCARD_RESOURCE_ID || self.resource_id == candidate.resource_id
}
/// Checks if a given candidate URI matches a pattern.
///
/// # Returns
///
/// `true` if the candiadate matches the pattern represented by this UUri.
///
/// # Examples
///
/// ```rust
/// use up_rust::UUri;
///
/// let pattern = UUri::try_from("//VIN/A14F/3/FFFF").unwrap();
/// let candidate = UUri::try_from("//VIN/A14F/3/B1D4").unwrap();
/// assert!(pattern.matches(&candidate));
/// ```
// [impl->dsn~uri-pattern-matching~1]
pub fn matches(&self, candidate: &UUri) -> bool {
self.matches_authority(candidate)
&& self.matches_entity(candidate)
&& self.matches_resource(candidate)
}
}
#[cfg(test)]
mod tests {
use super::*;
use protobuf::Message;
use test_case::test_case;
// [utest->req~uri-serialization~1]
// [utest->dsn~uri-scheme~1]
// [utest->dsn~uri-host-only~2]
// [utest->dsn~uri-authority-mapping~1]
// [utest->dsn~uri-path-mapping~1]
#[test_case(""; "for empty string")]
#[test_case("/"; "for single slash")]
#[test_case("up:/"; "for scheme and single slash")]
#[test_case("//"; "for double slash")]
#[test_case("up://"; "for scheme and double slash")]
#[test_case("custom://my-vehicle/8000/2/1"; "for unsupported scheme")]
#[test_case("////2/1"; "for missing authority and entity")]
#[test_case("/////1"; "for missing authority, entity and version")]
#[test_case("up://MYVIN/1A23/1/a13?foo=bar"; "for URI with query")]
#[test_case("up://MYVIN/1A23/1/a13#foobar"; "for URI with fragement")]
#[test_case("up://MYVIN:1000/1A23/1/A13"; "for authority with port")]
#[test_case("up://user:pwd@MYVIN/1A23/1/A13"; "for authority with userinfo")]
fn test_from_string_fails(string: &str) {
let parsing_result = UUri::from_str(string);
assert!(parsing_result.is_err());
}
// [utest->req~uri-serialization~1]
// [utest->dsn~uri-scheme~1]
// [utest->dsn~uri-host-only~2]
// [utest->dsn~uri-authority-mapping~1]
// [utest->dsn~uri-path-mapping~1]
#[test_case("UP:/8000/1/2",
UUri {
authority_name: String::default(),
ue_id: 0x0000_8000,
ue_version_major: 0x01,
resource_id: 0x0002,
..Default::default()
};
"for local service with version and resource")]
#[test_case("/108000/1/2",
UUri {
authority_name: String::default(),
ue_id: 0x0010_8000,
ue_version_major: 0x01,
resource_id: 0x0002,
..Default::default()
};
"for local service instance with version and resource")]
#[test_case("/8000/1/0",
UUri {
authority_name: String::default(),
ue_id: 0x0000_8000,
ue_version_major: 0x01,
resource_id: 0x0000,
..Default::default()
};
"for local rpc service response")]
#[test_case("up://VCU.MY_CAR_VIN/108000/1/2",
UUri {
authority_name: "VCU.MY_CAR_VIN".to_string(),
ue_id: 0x0010_8000,
ue_version_major: 0x01,
resource_id: 0x0002,
..Default::default()
};
"for remote uri")]
#[test_case("//*/FFFF/FF/FFFF",
UUri {
authority_name: "*".to_string(),
ue_id: 0x0000_FFFF,
ue_version_major: 0xFF,
resource_id: 0xFFFF,
..Default::default()
};
"for remote uri with wildcards")]
fn test_from_string_succeeds(uri: &str, expected_uuri: UUri) {
let parsing_result = UUri::from_str(uri);
if parsing_result.is_err() {
println!("error: {}", parsing_result.as_ref().unwrap_err());
}
assert!(parsing_result.is_ok());
let parsed_uuri = parsing_result.unwrap();
assert_eq!(expected_uuri, parsed_uuri);
}
#[test_case("//*/A100/1/1"; "for any authority")]
#[test_case("//VIN/FFFF/1/1"; "for any entity")]
#[test_case("//VIN/A100/FF/1"; "for any version")]
#[test_case("//VIN/A100/1/FFFF"; "for any resource")]
fn test_verify_no_wildcards_fails(uri: &str) {
let uuri = UUri::try_from(uri).expect("should have been able to deserialize URI");
assert!(uuri.verify_no_wildcards().is_err());
}
// [utest->req~uri-data-model-proto~1]
#[test]
fn test_protobuf_serialization() {
let uri = UUri {
authority_name: "MYVIN".to_string(),
ue_id: 0x0000_1a4f,
ue_version_major: 0x10,
resource_id: 0xb392,
..Default::default()
};
let pb = uri.write_to_bytes().unwrap();
let deserialized_uri = UUri::parse_from_bytes(pb.as_slice()).unwrap();
assert_eq!(uri, deserialized_uri);
}
// [utest->dsn~uri-authority-name-length~1]
#[test]
fn test_from_str_fails_for_authority_exceeding_max_length() {
let host_name = ['a'; 129];
let uri = format!("//{}/A100/1/6501", host_name.iter().collect::<String>());
assert!(UUri::from_str(&uri).is_err());
let host_name = ['a'; 126];
// add single percent encoded character
// this should result in a 129 character host
let uri = format!("//{}%42/A100/1/6501", host_name.iter().collect::<String>());
assert!(UUri::from_str(&uri).is_err());
}
// [utest->dsn~uri-authority-name-length~1]
#[test]
fn test_try_from_parts_fails_for_authority_exceeding_max_length() {
let authority = ['a'; 129].iter().collect::<String>();
assert!(UUri::try_from_parts(&authority, 0xa100, 0x01, 0x6501).is_err());
let mut authority = ['a'; 126].iter().collect::<String>();
// add single percent encoded character
// this should result in a 129 character host
authority.push_str("%42");
assert!(UUri::try_from_parts(&authority, 0xa100, 0x01, 0x6501).is_err());
}
// [utest->dsn~uri-host-only~2]
#[test_case("MYVIN:1000"; "for authority with port")]
#[test_case("user:pwd@MYVIN"; "for authority with userinfo")]
fn test_try_from_parts_fails(authority: &str) {
assert!(UUri::try_from_parts(authority, 0xa100, 0x01, 0x6501).is_err());
}
// [utest->dsn~uri-pattern-matching~1]
#[test_case("//authority/A410/3/1003", "//authority/A410/3/1003"; "for identical URIs")]
#[test_case("//*/A410/3/1003", "//authority/A410/3/1003"; "for pattern with wildcard authority")]
#[test_case("//*/A410/3/1003", "/A410/3/1003"; "for pattern with wildcard authority and local candidate URI")]
#[test_case("//authority/FFFF/3/1003", "//authority/A410/3/1003"; "for pattern with wildcard entity ID")]
#[test_case("//authority/A410/3/1003", "//authority/2A410/3/1003"; "for pattern with wildcard entity instance")]
#[test_case("//authority/A410/FF/1003", "//authority/A410/3/1003"; "for pattern with wildcard entity version")]
#[test_case("//authority/A410/3/FFFF", "//authority/A410/3/1003"; "for pattern with wildcard resource")]
fn test_matches_succeeds(pattern: &str, candidate: &str) {
let pattern_uri =
UUri::try_from(pattern).expect("should have been able to create pattern UUri");
let candidate_uri =
UUri::try_from(candidate).expect("should have been able to create candidate UUri");
assert!(pattern_uri.matches(&candidate_uri));
}
// [utest->dsn~uri-pattern-matching~1]
#[test_case("//Authority/A410/3/1003", "//authority/A410/3/1003"; "for pattern with upper case authority")]
#[test_case("/A410/3/1003", "//authority/A410/3/1003"; "for local pattern and candidate URI with authority")]
#[test_case("//other/A410/3/1003", "//authority/A410/3/1003"; "for pattern with different authority")]
#[test_case("//authority/45/3/1003", "//authority/A410/3/1003"; "for pattern with different entity ID")]
#[test_case("//authority/30A410/3/1003", "//authority/2A410/3/1003"; "for pattern with different entity instance")]
#[test_case("//authority/A410/1/1003", "//authority/A410/3/1003"; "for pattern with different entity version")]
#[test_case("//authority/A410/3/ABCD", "//authority/A410/3/1003"; "for pattern with different resource")]
fn test_matches_fails(pattern: &str, candidate: &str) {
let pattern_uri =
UUri::try_from(pattern).expect("should have been able to create pattern UUri");
let candidate_uri =
UUri::try_from(candidate).expect("should have been able to create candidate UUri");
assert!(!pattern_uri.matches(&candidate_uri));
}
}