-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathurivalidator.rs
1084 lines (983 loc) · 36.2 KB
/
urivalidator.rs
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
* 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
********************************************************************************/
use crate::uprotocol::{UAuthority, UEntity, UResource, UUri};
use crate::uri::validator::ValidationError;
/// Struct to encapsulate Uri validation logic.
pub struct UriValidator;
impl UriValidator {
/// Validates a `UUri` to ensure that it has at least a name for the uEntity.
///
/// # Arguments
/// * `uri` - The `UUri` to validate.
///
/// # Returns
/// Returns `ValidationResult` containing a success or a failure with the error message.
///
/// # Errors
///
/// Returns a `ValidationError` in the following cases:
///
/// - If the `UUri` is empty. The error message in this case will be "Uri is empty", indicating that the URI does not contain any data and is therefore considered invalid.
/// - If the `UUri` is supposed to be remote (as indicated by the presence of an authority component) but fails the `is_remote` validation check. The error message will be "Uri is remote missing uAuthority", suggesting that the URI lacks a necessary authority component for remote URIs.
/// - If the `UUri` is missing the name of the `uSoftware Entity` or the name is present but empty. The error message for this scenario will be "Uri is missing uSoftware Entity name", indicating that a critical component of the URI is absent or not properly specified.
pub fn validate(uri: &UUri) -> Result<(), ValidationError> {
if Self::is_empty(uri) {
return Err(ValidationError::new("Uri is empty"));
}
if uri.authority.is_some() && !Self::is_remote(uri) {
return Err(ValidationError::new("Uri is remote missing uAuthority"));
}
if uri
.entity
.as_ref()
.map_or(true, |entity| entity.name.trim().is_empty())
{
return Err(ValidationError::new("Uri is missing uSoftware Entity name"));
}
Ok(())
}
/// Validates a `UUri` that is meant to be used as an RPC method URI.
/// Used in Request sink values and Response source values.
///
/// # Arguments
/// * `uri` - The `UUri` to validate.
///
/// # Returns
/// Returns `ValidationResult` containing a success or a failure with the error message.
///
/// # Errors
///
/// Returns a `ValidationError` in the following cases:
///
/// - If the `UUri` fails the basic validation checks performed by `Self::validate`. The error message will detail the specific issue with the `UUri` as identified by the `Self::validate` method.
/// - If the `UUri` is not recognized as a valid RPC method URI. The error message in this case will be "Invalid RPC method uri. Uri should be the method to be called, or method from response", indicating that the `UUri` does not conform to the expected format or designation for RPC method URIs.
pub fn validate_rpc_method(uri: &UUri) -> Result<(), ValidationError> {
Self::validate(uri)?;
if !Self::is_rpc_method(uri) {
return Err(ValidationError::new("Invalid RPC method uri. Uri should be the method to be called, or method from response"));
}
Ok(())
}
/// Validates a `UUri` that is meant to be used as an RPC response URI.
/// This is used in Request source values and Response sink values.
///
/// # Arguments
///
/// * `uri` - The `UUri` instance to validate.
///
/// # Returns
///
/// Returns a `UStatus` containing either a success or a failure, along with the corresponding error message.
///
/// # Errors
///
/// Returns a `ValidationError` in the following cases:
///
/// - If the `UUri` fails the basic validation checks performed by `Self::validate`. The error message will contain details about what aspect of the `UUri` was invalid, as determined by the validation logic in `Self::validate`.
/// - If the `UUri` is not of the correct type to be used as an RPC response. In this case, the error message will be "Invalid RPC response type", indicating that the `UUri` does not meet the specific criteria for RPC response URIs.
pub fn validate_rpc_response(uri: &UUri) -> Result<(), ValidationError> {
Self::validate(uri)?;
if !Self::is_rpc_response(uri) {
return Err(ValidationError::new("Invalid RPC response type"));
}
Ok(())
}
/// Indicates whether this `UUri` is empty, meaning it does not contain authority, entity, and resource.
///
/// # Arguments
/// * `uri` - The `UUri` to check for emptiness.
///
/// # Returns
/// Returns `true` if this `UUri` is an empty container and has no valuable information for building uProtocol sinks or sources.
pub fn is_empty(uri: &UUri) -> bool {
uri.authority.is_none() && uri.entity.is_none() && uri.resource.is_none()
}
/// Checks if the URI contains both names and numeric representations of the names.
///
/// This indicates that the `UUri` can be serialized to long or micro formats.
///
/// # Arguments
/// * `uri` - The `UUri` to check if resolved.
///
/// # Returns
/// Returns `true` if the URI contains both names and numeric representations of the names,
/// meaning that this `UUri` can be serialized to long or micro formats.
pub fn is_resolved(uri: &UUri) -> bool {
!Self::is_empty(uri)
// TODO finish this
}
/// Checks if the URI is of type RPC.
///
/// # Arguments
/// * `uri` - The `UUri` to check if it is of type RPC method.
///
/// # Returns
/// Returns `true` if the URI is of type RPC.
pub fn is_rpc_method(uri: &UUri) -> bool {
if !Self::is_empty(uri) {
if let Some(resource) = uri.resource.as_ref() {
if resource.name.contains("rpc") {
let has_valid_instance = resource
.instance
.as_ref()
.map_or(false, |instance| !instance.trim().is_empty());
let has_non_zero_id = resource.id.map_or(false, |id| id != 0);
return has_valid_instance || has_non_zero_id;
}
}
}
false
}
/// Checks if the URI is of type RPC response.
///
/// # Arguments
/// * `uri` - The `UUri` to check if it is a response for an RPC method.
///
/// # Returns
/// Returns `true` if the URI is of type RPC response.
pub fn is_rpc_response(uri: &UUri) -> bool {
if Self::is_rpc_method(uri) {
if let Some(resource) = uri.resource.as_ref() {
let has_valid_instance = resource
.instance
.as_ref()
.map_or(false, |instance| instance.contains("response"));
let has_non_zero_id = resource.id.map_or(false, |id| id != 0);
return has_valid_instance || has_non_zero_id;
}
}
false
}
/// Checks if a `UAuthority` is of type remote
///
/// # Arguments
/// * `authority` - The `UAuthority` to check if.
///
/// # Returns
/// Returns `true` if the `UAuthority` is of type remote.
#[allow(clippy::missing_panics_doc)]
pub fn is_remote(uri: &UUri) -> bool {
uri.authority.is_some() && uri.authority.as_ref().unwrap().remote.is_some()
}
/// Checks if the URI contains numbers so that it can be serialized into micro format.
///
/// # Arguments
/// * `uri` - The `UUri` to check.
///
/// # Returns
/// Returns `true` if the URI contains numbers, allowing it to be serialized into micro format.
#[allow(clippy::missing_panics_doc)]
pub fn is_micro_form(uri: &UUri) -> bool {
!Self::is_empty(uri)
&& uri.entity.as_ref().map_or(false, UEntity::has_id)
&& uri.resource.as_ref().map_or(false, UResource::has_id)
&& (uri.authority.is_none()
|| UAuthority::has_ip(uri.authority.as_ref().unwrap())
|| UAuthority::has_id(uri.authority.as_ref().unwrap()))
}
/// Checks if the URI contains names so that it can be serialized into long format.
///
/// # Arguments
/// * `uri` - The `UUri` to check.
///
/// # Returns
/// Returns `true` if the URI contains names, allowing it to be serialized into long format.
pub fn is_long_form(uri: &UUri) -> bool {
if Self::is_empty(uri) {
return false;
}
let mut auth_name = String::new();
if let Some(authority) = &uri.authority {
if let Some(name) = UAuthority::get_name(authority) {
auth_name = name.to_string();
}
}
let mut ent_name = String::new();
if let Some(entity) = &uri.entity {
ent_name = entity.name.to_string();
}
let mut res_name = String::new();
if let Some(resource) = &uri.resource {
res_name = resource.name.to_string();
}
!auth_name.is_empty() && !ent_name.trim().is_empty() && !res_name.trim().is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{Error, Value};
use std::fs;
use crate::{
uprotocol::{UEntity, UResource},
uri::serializer::{LongUriSerializer, UriSerializer},
};
#[test]
fn test_validate_blank_uri() {
let uri = LongUriSerializer::deserialize("".to_string());
assert!(uri.is_err());
}
#[test]
fn test_validate_uri_with_get_entity() {
let uri = LongUriSerializer::deserialize("/hartley".to_string()).unwrap();
let status = UriValidator::validate(&uri);
assert!(status.is_ok());
}
#[test]
fn test_validate_with_malformed_uri() {
let uri = LongUriSerializer::deserialize("hartley".to_string());
assert!(uri.is_err());
}
#[test]
fn test_validate_with_blank_uentity_name_uri() {
let uri = UUri::default();
let status = UriValidator::validate(&uri);
assert!(status.is_err());
assert_eq!(status.unwrap_err().to_string(), "Uri is empty");
}
#[test]
fn test_validate_rpc_method_with_valid_uri() {
let uri = LongUriSerializer::deserialize("/hartley//rpc.echo".to_string()).unwrap();
let status = UriValidator::validate_rpc_method(&uri);
assert!(status.is_ok());
}
#[test]
fn test_validate_rpc_method_with_invalid_uri() {
let entity = UEntity {
name: "hartley".into(),
..Default::default()
};
let resource = UResource {
name: "echo".into(),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(UAuthority::default()),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_validate_rpc_method_with_malformed_uri() {
let entity = UEntity {
name: "hartley".into(),
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(UAuthority::default()),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_validate_rpc_response_with_valid_uri() {
let uri = LongUriSerializer::deserialize("/hartley//rpc.response".to_string()).unwrap();
let status = UriValidator::validate_rpc_response(&uri);
assert!(status.is_ok());
}
#[test]
fn test_validate_rpc_response_with_malformed_uri() {
let entity = UEntity {
name: "hartley".into(),
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(UAuthority::default()),
};
let status = UriValidator::validate_rpc_response(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_validate_rpc_response_with_rpc_type() {
let uri = LongUriSerializer::deserialize("/hartley//dummy.wrong".to_string()).unwrap();
let status = UriValidator::validate_rpc_response(&uri);
assert!(status.is_err());
assert_eq!(status.unwrap_err().to_string(), "Invalid RPC response type");
}
#[test]
fn test_validate_rpc_response_with_invalid_rpc_response_type() {
let uri = LongUriSerializer::deserialize("/hartley//rpc.wrong".to_string()).unwrap();
let status = UriValidator::validate_rpc_response(&uri);
assert!(status.is_err());
assert_eq!(status.unwrap_err().to_string(), "Invalid RPC response type");
}
#[test]
fn test_topic_uri_with_version_when_it_is_valid_remote() {
let uri = "//VCU.MY_CAR_VIN/body.access/1/door.front_left#Door".to_string();
let status = UriValidator::validate(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_topic_uri_no_version_when_it_is_valid_remote() {
let uri = "//VCU.MY_CAR_VIN/body.access//door.front_left#Door".to_string();
let status = UriValidator::validate(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_topic_uri_with_version_when_it_is_valid_local() {
let uri = "/body.access/1/door.front_left#Door".to_string();
let status = UriValidator::validate(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_topic_uri_no_version_when_it_is_valid_local() {
let uri = "/body.access//door.front_left#Door".to_string();
let status = UriValidator::validate(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
use crate::uprotocol::u_authority::Remote;
#[test]
fn test_topic_uri_invalid_when_uri_has_schema_only() {
let authority = UAuthority {
remote: Some(Remote::Name(":".into())),
};
let entity = UEntity {
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_topic_uri_invalid_when_uri_has_empty_use_name_local() {
let entity = UEntity {
name: "".into(),
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: None,
};
let status = UriValidator::validate(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_topic_uri_invalid_when_uri_is_remote_no_authority() {
let authority = UAuthority {
..Default::default()
};
let entity = UEntity {
name: "".into(),
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_topic_uri_invalid_when_uri_is_remote_no_authority_with_use() {
let authority = UAuthority {
..Default::default()
};
let entity = UEntity {
name: "body.access".into(),
version_major: Some(1),
..Default::default()
};
let resource = UResource {
name: "door".into(),
instance: Some("front_left".into()),
message: Some("Door".into()),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_topic_uri_invalid_when_uri_is_missing_use_remote() {
let uri = "//VCU.myvin///door.front_left#Door".to_string();
let status = UriValidator::validate(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_topic_uri_invalid_when_uri_is_missing_use_name_remote() {
let entity = UEntity {
version_major: Some(1),
..Default::default()
};
let resource = UResource {
name: "door".into(),
instance: Some("front_left".into()),
message: Some("Door".into()),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: None,
};
let status = UriValidator::validate(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_topic_uri_invalid_when_uri_is_missing_use_name_local() {
let uri = "//VCU.myvin//1".to_string();
let status = UriValidator::validate(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_rpc_topic_uri_with_version_when_it_is_valid_remote() {
let uri = "//bo.cloud/petapp/1/rpc.response".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_topic_uri_no_version_when_it_is_valid_remote() {
let uri = "//bo.cloud/petapp//rpc.response".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_topic_uri_with_version_when_it_is_valid_local() {
let uri = "/petapp/1/rpc.response".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_topic_uri_no_version_when_it_is_valid_local() {
let uri = "/petapp//rpc.response".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_topic_uri_invalid_when_uri_has_schema_only() {
let authority = UAuthority {
remote: Some(Remote::Name(":".into())),
};
let entity = UEntity {
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_rpc_topic_uri_with_version_when_it_is_not_valid_missing_rpc_response_local() {
let uri = "/petapp/1/dog".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Invalid RPC method uri. Uri should be the method to be called, or method from response"
);
}
#[test]
fn test_rpc_topic_uri_with_version_when_it_is_not_valid_missing_rpc_response_remote() {
let entity = UEntity {
name: "petapp".into(),
version_major: Some(1),
..Default::default()
};
let resource = UResource {
name: "dog".into(),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: None,
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Invalid RPC method uri. Uri should be the method to be called, or method from response"
);
}
#[test]
fn test_rpc_topic_uri_invalid_when_uri_is_remote_no_authority() {
let authority = UAuthority {
..Default::default()
};
let entity = UEntity {
name: "".into(),
..Default::default()
};
let resource = UResource {
name: "".into(),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_rpc_topic_uri_invalid_when_uri_is_remote_no_authority_with_use() {
let authority = UAuthority {
..Default::default()
};
let entity = UEntity {
name: "body.access".into(),
version_major: Some(1),
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_rpc_topic_uri_invalid_when_uri_is_missing_use() {
let uri = "//VCU.myvin".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_rpc_topic_uri_invalid_when_uri_is_missing_use_name_remote() {
let uri = "/1".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Invalid RPC method uri. Uri should be the method to be called, or method from response"
);
}
#[test]
fn test_rpc_topic_uri_invalid_when_uri_is_missing_use_name_local() {
let uri = "//VCU.myvin//1".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_rpc_method_uri_with_version_when_it_is_valid_remote() {
let uri = "//VCU.myvin/body.access/1/rpc.UpdateDoor".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_method_uri_no_version_when_it_is_valid_remote() {
let uri = "//VCU.myvin/body.access//rpc.UpdateDoor".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_method_uri_with_version_when_it_is_valid_local() {
let uri = "/body.access/1/rpc.UpdateDoor".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_method_uri_no_version_when_it_is_valid_local() {
let uri = "/body.access//rpc.UpdateDoor".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_ok());
}
#[test]
fn test_rpc_method_uri_invalid_when_uri_has_schema_only() {
let authority = UAuthority {
remote: Some(Remote::Name(":".into())),
};
let entity = UEntity {
..Default::default()
};
let resource = UResource {
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_rpc_method_uri_with_version_when_it_is_not_valid_not_rpc_method_local() {
let uri = "/body.access//UpdateDoor".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Invalid RPC method uri. Uri should be the method to be called, or method from response"
);
}
#[test]
fn test_rpc_method_uri_with_version_when_it_is_not_valid_not_rpc_method_remote() {
let authority = UAuthority {
..Default::default()
};
let entity = UEntity {
name: "body.access".into(),
version_major: Some(1),
..Default::default()
};
let resource = UResource {
name: "UpdateDoor".into(),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_rpc_method_uri_invalid_when_uri_is_remote_no_authority() {
let authority = UAuthority {
..Default::default()
};
let entity = UEntity {
name: "".into(),
..Default::default()
};
let resource = UResource {
name: "".into(),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_rpc_method_uri_invalid_when_uri_is_remote_no_authority_with_use() {
let authority = UAuthority {
..Default::default()
};
let entity = UEntity {
name: "body.access".into(),
version_major: Some(1),
..Default::default()
};
let resource = UResource {
name: "rpc".into(),
instance: Some("UpdateDoor".into()),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(authority),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_rpc_method_uri_invalid_when_uri_is_remote_missing_authority_remotecase() {
let entity = UEntity {
name: "body.access".into(),
..Default::default()
};
let resource = UResource {
name: "rpc".into(),
instance: Some("UpdateDoor".into()),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: Some(UAuthority::default()),
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is remote missing uAuthority"
);
}
#[test]
fn test_rpc_method_uri_invalid_when_uri_is_missing_use() {
let uri = "//VCU.myvin".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_rpc_method_uri_invalid_when_uri_is_missing_use_name_local() {
let entity = UEntity {
version_major: Some(1),
..Default::default()
};
let resource = UResource {
name: "rpc".into(),
instance: Some("UpdateDoor".into()),
..Default::default()
};
let uuri = UUri {
entity: Some(entity),
resource: Some(resource),
authority: None,
};
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_err());
assert_eq!(
status.unwrap_err().to_string(),
"Uri is missing uSoftware Entity name"
);
}
#[test]
fn test_rpc_method_uri_invalid_when_uri_is_missing_use_name_remote() {
let uri = "//VCU.myvin//1/rpc.UpdateDoor".to_string();
let status =
UriValidator::validate_rpc_method(&LongUriSerializer::deserialize(uri).unwrap());
assert!(status.is_err());
}
#[test]
fn test_all_valid_uris() {
let json_object = get_json_object().expect("Failed to parse JSON");
if let Some(valid_uris) = json_object.get("validUris").and_then(|v| v.as_array()) {
for uri in valid_uris {
let uri: String = uri.as_str().unwrap_or_default().to_string();
let uuri = LongUriSerializer::deserialize(uri).unwrap();
let status = UriValidator::validate(&uuri);
assert!(status.is_ok());
}
}
}
#[test]
fn test_all_invalid_uris() {
let json_object = get_json_object().expect("Failed to parse JSON");
let invalid_uris = json_object.get("invalidUris").unwrap().as_array().unwrap();
for uri_object in invalid_uris {
let uri = uri_object.get("uri").unwrap().as_str().unwrap();
if let Ok(uuri) = LongUriSerializer::deserialize(uri.into()) {
let status = UriValidator::validate(&uuri);
assert!(status.is_err());
let message = uri_object.get("status_message").unwrap().as_str().unwrap();
assert_eq!(message, status.unwrap_err().to_string());
}
}
}
#[test]
fn test_all_valid_rpc_uris() {
let json_object = get_json_object().expect("Failed to parse JSON");
let valid_rpc_uris = json_object.get("validRpcUris").unwrap().as_array().unwrap();
for uri in valid_rpc_uris {
let uuri = LongUriSerializer::deserialize(uri.to_string()).unwrap();
let status = UriValidator::validate_rpc_method(&uuri);
assert!(status.is_ok());
}