-
Notifications
You must be signed in to change notification settings - Fork 8
/
matching.rs
1370 lines (1299 loc) · 59.1 KB
/
matching.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
//! Functions for matching Protobuf messages
use std::collections::BTreeMap;
use std::fmt::{Debug, Display, Formatter};
use anyhow::anyhow;
use bytes::{Bytes, BytesMut};
use itertools::{Either, Itertools};
use maplit::hashmap;
use pact_matching::{BodyMatchResult, CoreMatchingContext, DiffConfig, MatchingContext, Mismatch};
use pact_matching::json::compare_json;
use pact_matching::matchers::{match_values, Matches};
use pact_matching::matchingrules::{compare_lists_with_matchingrule, compare_maps_with_matchingrule};
use pact_matching::Mismatch::BodyMismatch;
use pact_models::content_types::ContentType;
use pact_models::matchingrules::{Category, MatchingRule, RuleList, RuleLogic};
use pact_models::path_exp::DocPath;
use pact_models::prelude::MatchingRuleCategory;
use prost_types::{DescriptorProto, FieldDescriptorProto, FileDescriptorSet};
use prost_types::field_descriptor_proto::Type;
use tracing::{debug, instrument, trace, warn};
use crate::message_decoder::{decode_message, ProtobufField, ProtobufFieldData};
use crate::utils::{display_bytes, enum_name, field_data_to_json, find_message_field_by_name, find_message_type_by_name, find_service_descriptor, is_map_field, is_repeated_field, last_name};
/// Match a single Protobuf message
pub fn match_message(
message_name: &str,
descriptors: &FileDescriptorSet,
expected_request: &mut Bytes,
actual_request: &mut Bytes,
matching_rules: &MatchingRuleCategory,
allow_unexpected_keys: bool
) -> anyhow::Result<BodyMatchResult> {
debug!("Looking for message '{}'", message_name);
let (message_descriptor, _) = find_message_type_by_name(message_name, descriptors)?;
let expected_message = decode_message(expected_request, &message_descriptor, descriptors)?;
debug!("expected message = {:?}", expected_message);
let actual_message = decode_message(actual_request, &message_descriptor, descriptors)?;
debug!("actual message = {:?}", actual_message);
let plugin_config = hashmap!{};
let diff_config = if allow_unexpected_keys {
DiffConfig::AllowUnexpectedKeys
} else {
DiffConfig::NoUnexpectedKeys
};
let context = CoreMatchingContext::new(diff_config, matching_rules, &plugin_config);
compare(&message_descriptor, &expected_message, &actual_message, &context,
expected_request, descriptors)
}
/// Match a Protobuf service call, which has an input and output message
pub fn match_service(
service_name: &str,
method_name: &str,
descriptors: &FileDescriptorSet,
expected_request: &mut Bytes,
actual_request: &mut Bytes,
rules: &MatchingRuleCategory,
allow_unexpected_keys: bool,
content_type: &ContentType
) -> anyhow::Result<BodyMatchResult> {
debug!("Looking for service '{}'", service_name);
let (_, service_descriptor) = find_service_descriptor(descriptors, service_name)?;
trace!("Found service descriptor with name {:?}", service_descriptor.name);
let (method_name, service_part) = if method_name.contains(':') {
method_name.split_once(':').unwrap_or((method_name, ""))
} else {
(method_name, "")
};
let method_descriptor = service_descriptor.method.iter().find(|method_desc| {
method_desc.name.clone().unwrap_or_default() == method_name
}).ok_or_else(|| anyhow!("Did not find the method {} in the Protobuf file descriptor for service '{}'", method_name, service_name))?;
trace!("Found method descriptor with name {:?}", method_descriptor.name);
let expected_message_type = content_type.attributes.get("message");
let message_type = if let Some(message_type) = expected_message_type {
let input_type = method_descriptor.input_type.clone().unwrap_or_default();
if last_name(input_type.as_str()) == message_type.as_str() {
input_type
} else {
method_descriptor.output_type.clone().unwrap_or_default()
}
} else if service_part == "request" {
method_descriptor.input_type.clone().unwrap_or_default()
} else {
method_descriptor.output_type.clone().unwrap_or_default()
};
trace!("Message type = {}", message_type);
match_message(last_name(message_type.as_str()), descriptors,
expected_request, actual_request,
rules, allow_unexpected_keys)
}
/// Compare the expected message to the actual one
#[tracing::instrument(ret, skip_all)]
pub(crate) fn compare(
message_descriptor: &DescriptorProto,
expected_message: &[ProtobufField],
actual_message: &[ProtobufField],
matching_context: &(dyn MatchingContext + Send + Sync),
expected_message_bytes: &Bytes,
descriptors: &FileDescriptorSet
) -> anyhow::Result<BodyMatchResult> {
let actual_fields = populate_default_values(actual_message, message_descriptor, descriptors);
if expected_message.is_empty() {
Ok(BodyMatchResult::Ok)
} else if actual_fields.is_empty() {
Ok(BodyMatchResult::BodyMismatches(hashmap!{
"$".to_string() => vec![Mismatch::BodyMismatch {
path: "$".to_string(),
expected: Some(expected_message_bytes.clone()),
actual: None,
mismatch: format!("Expected message '{}' but was missing or empty", message_descriptor.name.clone().unwrap_or_default())
}]
}))
} else {
compare_message(DocPath::root(), expected_message, actual_fields.as_slice(), matching_context, message_descriptor, descriptors)
}
}
fn populate_default_values(
fields: &[ProtobufField],
message_descriptor: &DescriptorProto,
fds: &FileDescriptorSet
) -> Vec<ProtobufField> {
let mut field_vec = Vec::from(fields);
for field in &message_descriptor.field {
if let Some(field_num) = field.number {
let entry = field_vec.iter()
.find(|i| i.field_num == field_num as u32);
if entry.is_none() && should_use_default(field) {
if let Some(def) = ProtobufField::default_field(field, message_descriptor, fds) {
field_vec.push(def)
}
}
}
}
field_vec
}
fn should_use_default(descriptor: &FieldDescriptorProto) -> bool {
// For strings, the default value is the empty string.
// For bytes, the default value is empty bytes.
// For bools, the default value is false.
// For numeric types, the default value is zero. For float and double types, -0.0 and 0.0 are treated as equivalent, and will round-trip.
// For enums, the default value is the first defined enum value, which must be 0.
match descriptor.r#type() {
Type::Double | Type::Float | Type::Int64 | Type::Uint64 | Type::Int32 | Type::Fixed64 |
Type::Fixed32 | Type::Bool | Type::String | Type::Bytes | Type::Uint32 | Type::Enum |
Type::Sfixed32 | Type::Sfixed64 | Type::Sint32 | Type::Sint64 => !is_repeated_field(descriptor),
_ => false
}
}
/// Compare the fields of the expected and actual messages
#[tracing::instrument(ret,
skip_all,
fields(%path, expected_message_fields, actual_message_fields)
)]
pub fn compare_message(
path: DocPath,
expected_message_fields: &[ProtobufField],
actual_message_fields: &[ProtobufField],
matching_context: &(dyn MatchingContext + Send + Sync),
message_descriptor: &DescriptorProto,
descriptors: &FileDescriptorSet,
) -> anyhow::Result<BodyMatchResult> {
let mut results = hashmap!{};
let fields = message_descriptor.field.iter()
.filter_map(|field| {
field.number.map(|no| {
let expected_field_values = expected_message_fields.iter().filter(|value| value.field_num == no as u32)
.collect_vec();
let actual_field_values = actual_message_fields.iter().filter(|value| value.field_num == no as u32)
.collect_vec();
(no as u32, (field, expected_field_values, actual_field_values))
})
});
for (field_no, (field_descriptor, expected, actual)) in fields {
let field_name = field_descriptor.name
.clone()
.unwrap_or_else(|| {
warn!("Field number {} does not have a field name in the descriptor, will use the number", field_no);
field_no.to_string()
});
let field_path = path.join(&field_name);
trace!(%field_name, field_no, "Comparing message field {:?} => {:?}", expected, actual);
if is_map_field(message_descriptor, field_descriptor) {
trace!(%field_name, field_no, "field is a map field");
let map_comparison = compare_map_field(&field_path, field_descriptor, expected, actual, matching_context, descriptors);
if !map_comparison.is_empty() {
results.insert(field_path.to_string(), map_comparison);
}
} else if is_repeated_field(field_descriptor) {
trace!(%field_name, field_no, "field is a repeated field");
let e = expected.iter().map(|f| (*f).clone()).collect_vec();
let a = actual.iter().map(|f| (*f).clone()).collect_vec();
let repeated_comparison = compare_repeated_field(&field_path, field_descriptor, &e, &a, matching_context, descriptors);
if !repeated_comparison.is_empty() {
results.insert(field_path.to_string(), repeated_comparison);
}
} else if let Some(expected_value) = expected.first() {
let actual_value = actual.first().map(|v| (*v).clone()).unwrap_or_else(|| {
// Need to compare against the default values, as gRPC lib may have skipped sending the field if it was a default
expected_value.default_field_value(field_descriptor)
});
if matching_context.values_matcher_defined(&path) {
debug!("compare_message: Values Matcher defined for path '{}'", path);
let rules = matching_context.select_best_matcher(&path);
let associated_rules = rules.rules.iter().find_map(|rule| match rule {
MatchingRule::EachValue(associated) => if !rules.cascaded {
debug!("Matching {} with EachValue", path);
Some(associated.rules.iter().filter_map(|rule| {
match rule {
Either::Left(rule) => Some(rule.clone()),
Either::Right(reference) => {
results.insert(field_path.to_string(), vec![BodyMismatch {
path: path.to_string(),
expected: Some(format!("{:?}", expected).into()),
actual: Some(format!("{:?}", actual).into()),
mismatch: format!("Found an un-resolved reference {}", reference.name)
}]);
None
}
}
}).collect::<Vec<_>>())
} else {
None
},
_ => None
});
let context = if let Some(associated_rules) = associated_rules {
if associated_rules.is_empty() {
matching_context.clone_with(matching_context.matchers())
} else {
let rules = MatchingRuleCategory {
name: Category::BODY,
rules: hashmap! {
path.join("*") => RuleList {
rules: associated_rules,
rule_logic: RuleLogic::And,
cascaded: false
}
}
};
matching_context.clone_with(&rules)
}
} else {
matching_context.clone_with(matching_context.matchers())
};
let comparison = compare_field(&field_path, expected_value, field_descriptor, &actual_value, context.as_ref(), descriptors);
if !comparison.is_empty() {
results.insert(field_path.to_string(), comparison);
}
} else {
let comparison = compare_field(&field_path, expected_value, field_descriptor, &actual_value, matching_context, descriptors);
if !comparison.is_empty() {
results.insert(field_path.to_string(), comparison);
}
}
} else if !actual.is_empty() && matching_context.config() == DiffConfig::NoUnexpectedKeys {
trace!(field_name = field_name.as_str(), field_no, "actual field list is not empty");
if !actual[0].is_default_value() {
results.insert(field_path.to_string(), vec![
BodyMismatch {
path: field_path.to_string(),
expected: None,
actual: actual.first().map(|field_data| Bytes::from(field_data.data.as_bytes())),
mismatch: format!("Expected field '{}' to be missing, but received a value for it", field_name)
}
]);
}
}
}
if results.is_empty() {
Ok(BodyMatchResult::Ok)
} else {
Ok(BodyMatchResult::BodyMismatches(results))
}
}
/// Compare a simple field (non-map and non-repeated)
#[tracing::instrument(ret,
skip_all,
fields(%path, %field, %actual)
)]
fn compare_field(
path: &DocPath,
field: &ProtobufField,
descriptor: &FieldDescriptorProto,
actual: &ProtobufField,
matching_context: &(dyn MatchingContext + Send + Sync),
descriptors: &FileDescriptorSet
) -> Vec<Mismatch> {
match (&field.data, &actual.data) {
(ProtobufFieldData::String(s1), ProtobufFieldData::String(s2)) => {
trace!("Comparing string values");
let s1 = s1.clone();
let s2 = s2.clone();
compare_value(path, field, &s1, &s2, s1.as_str(), s2.as_str(), matching_context)
},
(ProtobufFieldData::Boolean(b1), ProtobufFieldData::Boolean(b2)) => {
trace!("Comparing boolean values");
compare_value(path, field, *b1, *b2, b1.to_string().as_str(), b2.to_string().as_str(), matching_context)
},
(ProtobufFieldData::UInteger32(n1), ProtobufFieldData::UInteger32(n2)) => {
trace!("Comparing UInteger32 values");
compare_value(path, field, *n1 as u64, *n2 as u64, n1.to_string().as_str(), n2.to_string().as_str(), matching_context)
},
(ProtobufFieldData::Integer32(n1), ProtobufFieldData::Integer32(n2)) => {
trace!("Comparing Integer32 values");
compare_value(path, field, *n1, *n2, n1.to_string().as_str(), n2.to_string().as_str(), matching_context)
},
(ProtobufFieldData::UInteger64(n1), ProtobufFieldData::UInteger64(n2)) => {
trace!("Comparing UInteger64 values");
compare_value(path, field, *n1, *n2, n1.to_string().as_str(), n2.to_string().as_str(), matching_context)
},
(ProtobufFieldData::Integer64(n1), ProtobufFieldData::Integer64(n2)) => {
trace!("Comparing Integer64 values");
compare_value(path, field, *n1, *n2, n1.to_string().as_str(), n2.to_string().as_str(), matching_context)
},
(ProtobufFieldData::Float(n1), ProtobufFieldData::Float(n2)) => {
trace!("Comparing Float values");
compare_value(path, field, *n1 as f64, *n2 as f64, n1.to_string().as_str(), n2.to_string().as_str(), matching_context)
},
(ProtobufFieldData::Double(n1), ProtobufFieldData::Double(n2)) => {
trace!("Comparing Double values");
compare_value(path, field, *n1, *n2, n1.to_string().as_str(), n2.to_string().as_str(), matching_context)
},
(ProtobufFieldData::Bytes(b1), ProtobufFieldData::Bytes(b2)) => {
trace!("Comparing byte arrays");
let b1_str = display_bytes(b1);
let b2_str = display_bytes(b2);
compare_value(path, field, b1.as_slice(), b2.as_slice(), b1_str.as_str(), b2_str.as_str(), matching_context)
},
(ProtobufFieldData::Enum(b1, descriptor), ProtobufFieldData::Enum(b2, _)) => {
trace!("Comparing Enum values");
let enum_1 = enum_name(*b1, descriptor);
let enum_2 = enum_name(*b2, descriptor);
compare_value(path, field, &enum_1, &enum_2, enum_1.as_str(), enum_2.as_str(), matching_context)
},
(ProtobufFieldData::Message(b1, message_descriptor), ProtobufFieldData::Message(b2, _)) => {
trace!("Comparing embedded messages");
let mut expected_bytes = BytesMut::from(b1.as_slice());
let expected_message = match decode_message(&mut expected_bytes, message_descriptor, descriptors) {
Ok(message) => message,
Err(err) => {
return vec![
BodyMismatch {
path: path.to_string(),
expected: Some(field.data.to_string().into()),
actual: Some(actual.data.to_string().into()),
mismatch: format!("Could not decode expected message field {} - {}", descriptor.name.clone().unwrap_or_else(|| "unknown".to_string()), err)
}
];
}
};
let mut actual_bytes = BytesMut::from(b2.as_slice());
let actual_message = match decode_message(&mut actual_bytes, message_descriptor, descriptors) {
Ok(message) => message,
Err(err) => {
return vec![
BodyMismatch {
path: path.to_string(),
expected: Some(field.data.to_string().into()),
actual: Some(actual.data.to_string().into()),
mismatch: format!("Could not decode actual message field {} - {}", descriptor.name.clone().unwrap_or_else(|| "unknown".to_string()), err)
}
];
}
};
match &descriptor.type_name {
Some(name) => match name.as_str() {
".google.protobuf.BytesValue" => {
debug!("Field is a Protobuf BytesValue");
let expected_field_data = find_message_field_by_name(message_descriptor, expected_message, "value");
let actual_field_data = find_message_field_by_name(message_descriptor, actual_message, "value");
let b1 = expected_field_data.map(|f| match f.data {
ProtobufFieldData::Bytes(b) => b,
_ => vec![]
}).unwrap_or_default();
let b1_str = display_bytes(&b1);
let b2 = actual_field_data.map(|f| match f.data {
ProtobufFieldData::Bytes(b) => b,
_ => vec![]
}).unwrap_or_default();
let b2_str = display_bytes(&b2);
compare_value(path, field, b1, b2, b1_str.as_str(), b2_str.as_str(), matching_context)
}
".google.protobuf.Struct" => {
debug!("Field is a Protobuf Struct, will compare it as JSON");
let expected_json = match field_data_to_json(expected_message, message_descriptor, descriptors) {
Ok(j) => j,
Err(err) => {
return vec![
BodyMismatch {
path: path.to_string(),
expected: Some(field.data.to_string().into()),
actual: Some(actual.data.to_string().into()),
mismatch: format!("Could not decode expected message field {} - {}", descriptor.name.clone().unwrap_or_else(|| "unknown".to_string()), err)
}
];
}
};
let actual_json = match field_data_to_json(actual_message, message_descriptor, descriptors) {
Ok(j) => j,
Err(err) => {
return vec![
BodyMismatch {
path: path.to_string(),
expected: Some(field.data.to_string().into()),
actual: Some(actual.data.to_string().into()),
mismatch: format!("Could not decode actual message field {} - {}", descriptor.name.clone().unwrap_or_else(|| "unknown".to_string()), err)
}
];
}
};
match compare_json(path, &expected_json, &actual_json, matching_context) {
Ok(_) => vec![],
Err(err) => err
}
}
_ => {
debug!("Field is a normal message");
match compare_message(path.clone(), &expected_message, &actual_message, matching_context, message_descriptor, descriptors) {
Ok(result) => match result {
BodyMatchResult::Ok => vec![],
BodyMatchResult::BodyTypeMismatch { message, .. } => vec![
BodyMismatch {
path: path.to_string(),
expected: Some(name.clone().into()),
actual: Some(name.clone().into()),
mismatch: message
}
],
BodyMatchResult::BodyMismatches(mismatches) => mismatches.values().flatten().cloned().collect()
}
Err(err) => vec![
BodyMismatch {
path: path.to_string(),
expected: Some(name.clone().into()),
actual: Some(name.clone().into()),
mismatch: err.to_string()
}
]
}
}
}
None => vec![
BodyMismatch {
path: path.to_string(),
expected: Some(field.data.to_string().into()),
actual: Some(actual.data.to_string().into()),
mismatch: format!("Message field {} type name is not set, can not compare it", descriptor.name.clone().unwrap_or_else(|| "unknown".to_string()))
}
]
}
}
_ => vec![
BodyMismatch {
path: path.to_string(),
expected: Some(field.data.to_string().into()),
actual: Some(actual.data.to_string().into()),
mismatch: format!("Expected and actual field have different types: {} and {}",
field, actual)
}
]
}
}
/// Compares the actual value to the expected one.
fn compare_value<T>(
path: &DocPath,
field: &ProtobufField,
expected: T,
actual: T,
expected_str: &str,
actual_str: &str,
matching_context: &dyn MatchingContext
) -> Vec<Mismatch> where T: Clone + Debug + Matches<T> {
trace!("compare_value({}, {:?}, {}, {})", path, field, expected_str, actual_str);
if matching_context.matcher_is_defined(path) {
debug!("compare_value: Matcher defined for path '{}' and values {:?} -> {:?}", path, expected, actual);
match match_values(path, &matching_context.select_best_matcher(path), expected, actual) {
Ok(_) => vec![],
Err(mismatches) => mismatches.iter().map(|m| BodyMismatch {
path: path.to_string(),
expected: Some(expected_str.as_bytes().to_vec().into()),
actual: Some(actual_str.as_bytes().to_vec().into()),
mismatch: m.clone()
}).collect()
}
} else {
debug!("compare_value: No matcher defined for path '{}', using equality", path);
match expected.matches_with(actual, &MatchingRule::Equality, false) {
Ok(_) => vec![],
Err(err) => vec![BodyMismatch {
path: path.to_string(),
expected: Some(expected_str.as_bytes().to_vec().into()),
actual: Some(actual_str.as_bytes().to_vec().into()),
mismatch: err.to_string()
}]
}
}
}
/// Compare a repeated field
#[tracing::instrument(ret, skip_all, fields(%path))]
fn compare_repeated_field(
path: &DocPath,
descriptor: &FieldDescriptorProto,
expected_fields: &[ProtobufField],
actual_fields: &[ProtobufField],
matching_context: &(dyn MatchingContext + Send + Sync),
descriptors: &FileDescriptorSet
) -> Vec<Mismatch> {
trace!(">>> compare_repeated_field({}, {:?}, {:?})", path, expected_fields, actual_fields);
let mut result = vec![];
if matching_context.matcher_is_defined(path) {
debug!("compare_repeated_field: Matcher defined for path '{}'", path);
let rules = matching_context.select_best_matcher(path);
for matcher in &rules.rules {
if let Err(comparison) = compare_lists_with_matchingrule(matcher, path,
expected_fields, actual_fields, matching_context, rules.cascaded, &mut |field_path, expected, actual, context| {
let comparison = compare_field(field_path, expected, descriptor, actual, context, descriptors);
if comparison.is_empty() {
Ok(())
} else {
Err(comparison)
}
}) {
result.extend(comparison);
}
}
} else if expected_fields.is_empty() && !actual_fields.is_empty() {
debug!("Expected an empty list, but actual has {} fields", actual_fields.len());
result.push(Mismatch::BodyMismatch {
path: path.to_string(),
expected: None,
actual: None,
mismatch: format!("Expected repeated field '{}' to be empty but received {} values",
descriptor.name.clone().unwrap_or_else(|| descriptor.number.unwrap_or_default().to_string()),
actual_fields.len()
)
})
} else {
trace!("Comparing repeated fields as a list");
result.extend(compare_list_content(path, descriptor, expected_fields, actual_fields, matching_context, descriptors));
if expected_fields.len() != actual_fields.len() {
result.push(Mismatch::BodyMismatch {
path: path.to_string(),
expected: None,
actual: None,
mismatch: format!("Expected repeated field '{}' to have {} values but received {} values",
descriptor.name.clone().unwrap_or_else(|| descriptor.number.unwrap_or_default().to_string()),
expected_fields.len(),
actual_fields.len()
)
})
}
}
result
}
/// Compare a map field
#[instrument(ret, skip_all, fields(%path))]
fn compare_map_field(
path: &DocPath,
descriptor: &FieldDescriptorProto,
expected_fields: Vec<&ProtobufField>,
actual_fields: Vec<&ProtobufField>,
matching_context: &(dyn MatchingContext + Send + Sync),
descriptors: &FileDescriptorSet
) -> Vec<Mismatch> {
trace!(">> compare_map_field('{}', {:?}, {:?})", path, expected_fields, actual_fields);
let mut result = vec![];
let expected_map = expected_fields.iter()
.filter_map(|f| {
match &f.data {
ProtobufFieldData::Message(d, descriptor) => decode_message_map_entry(descriptor, d, descriptors).ok(),
_ => None
}
})
.collect::<BTreeMap<String, MapEntry>>();
let actual_map = actual_fields.iter()
.filter_map(|f| {
match &f.data {
ProtobufFieldData::Message(d, descriptor) => decode_message_map_entry(descriptor, d, descriptors).ok(),
_ => None
}
})
.collect::<BTreeMap<String, MapEntry>>();
if matching_context.matcher_is_defined(path) {
debug!("compare_map_field: matcher defined for path '{}'", path);
let rules = matching_context.select_best_matcher(path);
for matcher in &rules.rules {
trace!("compare_map_field: matcher = {:?}", matcher);
if let Err(comparison) = compare_maps_with_matchingrule(matcher, rules.cascaded, path,
&expected_map, &actual_map, matching_context, &mut |field_path, expected, actual, context| {
let comparison = compare_field(field_path, &expected.value, &expected.field_descriptor, &actual.value, context, descriptors);
if comparison.is_empty() {
Ok(())
} else {
Err(comparison)
}
}) {
result.extend(comparison);
}
}
} else {
debug!("compare_map_field: no matcher defined for path '{}'", path);
debug!(" expected keys {:?}", expected_map.keys());
debug!(" actual keys {:?}", actual_map.keys());
if expected_fields.is_empty() && !actual_fields.is_empty() && matching_context.config() == DiffConfig::NoUnexpectedKeys {
result.push(BodyMismatch {
path: path.to_string(),
expected: None,
actual: None,
mismatch: format!("Expected repeated field '{}' to be empty but received {} values",
descriptor.name.clone().unwrap_or_else(|| descriptor.number.unwrap_or_default().to_string()),
actual_fields.len()
)
});
} else {
let expected_keys = expected_map.keys().cloned().collect();
let actual_keys = actual_map.keys().cloned().collect();
if let Err(mismatches) = matching_context.match_keys(path, &expected_keys, &actual_keys) {
result.extend(mismatches);
}
for (key, value) in &expected_map {
let entry_path = path.join(key);
if let Some(actual) = actual_map.get(key.as_str()) {
result.extend(compare_field(&entry_path, &value.value, &value.field_descriptor, &actual.value, matching_context, descriptors));
} else {
result.push(BodyMismatch {
path: path.to_string(),
expected: None,
actual: None,
mismatch: format!("Expected map field '{}' to have entry '{}', but was missing",
descriptor.name.clone().unwrap_or_else(|| descriptor.number.unwrap_or_default().to_string()),
key
)
});
}
}
}
}
result
}
/// Struct to represent a protobuf map entry
#[derive(Clone, Debug, PartialEq)]
struct MapEntry {
pub field_descriptor: FieldDescriptorProto,
pub value: ProtobufField
}
impl Display for MapEntry {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}
/// Decodes an embedded entry message into a key and value part
fn decode_message_map_entry(
descriptor: &DescriptorProto,
data: &[u8],
descriptors: &FileDescriptorSet
) -> anyhow::Result<(String, MapEntry)> {
let message = decode_message(&mut BytesMut::from(data), descriptor, descriptors)?;
let key = message.iter().find(|field| field.field_num == 1)
.ok_or_else(|| anyhow!("Did not find the key value when decoding map entry {}", descriptor.name.clone().unwrap_or_else(|| "unknown".to_string())))?;
let value = message.iter().find(|field| field.field_num == 2)
.ok_or_else(|| anyhow!("Did not find the value value when decoding map entry {}", descriptor.name.clone().unwrap_or_else(|| "unknown".to_string())))?;
let value_descriptor = find_field_descriptor(value, descriptor)
.ok_or_else(|| anyhow!("Did not find the field descriptor for the value field of the map entry"))?;
let key_str = match &key.data {
ProtobufFieldData::String(s) => s.clone(),
_ => key.data.to_string()
};
Ok((key_str, MapEntry { field_descriptor: value_descriptor, value: value.clone() } ))
}
/// Compares the items in the actual list against the expected
#[tracing::instrument(ret, skip_all, fields(%path))]
fn compare_list_content(
path: &DocPath,
descriptor: &FieldDescriptorProto,
expected: &[ProtobufField],
actual: &[ProtobufField],
matching_context: &(dyn MatchingContext + Send + Sync),
descriptors: &FileDescriptorSet
) -> Vec<Mismatch> {
let mut result = vec![];
for (index, value) in expected.iter().enumerate() {
let ps = index.to_string();
debug!("Comparing list item {} with value '{:?}' to '{:?}'", index, actual.get(index), value);
let p = path.join(ps);
if index < actual.len() {
result.extend(compare_field(&p, value, descriptor, actual.get(index).unwrap(), matching_context, descriptors));
} else if !matching_context.matcher_is_defined(&p) {
result.push(Mismatch::BodyMismatch {
path: path.to_string(),
expected: Some(Bytes::from(value.data.to_string())),
actual: None,
mismatch: format!("Expected field {}({})={} but was missing", descriptor.name.clone().unwrap_or_default(), value.field_num, value.data)
});
}
}
result
}
/// Find the field descriptor in the message descriptor for the given field value
fn find_field_descriptor(field: &ProtobufField, descriptor: &DescriptorProto) -> Option<FieldDescriptorProto> {
descriptor.field.iter()
.find(|field_desc| field_desc.number.unwrap_or_default() == field.field_num as i32)
.cloned()
}
#[cfg(test)]
mod tests {
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use expectest::prelude::*;
use pact_models::matchingrules::expressions::{MatchingRuleDefinition, ValueType};
use pact_models::{matchingrules, matchingrules_list};
use prost::encoding::WireType;
use prost::Message;
use prost_types::{DescriptorProto, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorSet, MessageOptions};
use prost_types::field_descriptor_proto::Label;
use prost_types::field_descriptor_proto::Label::{Optional, Repeated};
use prost_types::field_descriptor_proto::Type::{Enum, String};
use crate::message_decoder::ProtobufField;
use crate::utils::find_enum_by_name;
use super::*;
const DESCRIPTORS: &'static str = "CuIFChxnb29nbGUvcHJvdG9idWYvc3RydWN0LnByb3RvEg9nb29\
nbGUucHJvdG9idWYimAEKBlN0cnVjdBI7CgZmaWVsZHMYASADKAsyIy5nb29nbGUucHJvdG9idWYuU3RydWN0LkZpZWxkc\
0VudHJ5UgZmaWVsZHMaUQoLRmllbGRzRW50cnkSEAoDa2V5GAEgASgJUgNrZXkSLAoFdmFsdWUYAiABKAsyFi5nb29nbGU\
ucHJvdG9idWYuVmFsdWVSBXZhbHVlOgI4ASKyAgoFVmFsdWUSOwoKbnVsbF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ucG\
x1Z2luLkNvbXBhcmVDb250ZW50c1Jlc3BvbnNlEnEKFENvbmZpZ3VyZUludGVyYWN0aW9uEisuaW8ucGFjdC5wbHVnaW4uQ\
29uZmlndXJlSW50ZXJhY3Rpb25SZXF1ZXN0GiwuaW8ucGFjdC5wbHVnaW4uQ29uZmlndXJlSW50ZXJhY3Rpb25SZXNwb25z\
ZRJiCg9HZW5lcmF0ZUNvbnRlbnQSJi5pby5wYWN0LnBsdWdpbi5HZW5lcmF0ZUNvbnRlbnRSZXF1ZXN0GicuaW8ucGFjdC5\
wbHVnaW4uR2VuZXJhdGVDb250ZW50UmVzcG9uc2VCEFoOaW8ucGFjdC5wbHVnaW5iBnByb3RvMw==";
#[test_log::test]
fn compare_message_where_the_actual_field_is_missing_due_it_being_the_default_enum_value() {
let bytes = BASE64.decode(DESCRIPTORS).unwrap();
let bytes1 = Bytes::copy_from_slice(bytes.as_slice());
let fds = FileDescriptorSet::decode(bytes1).unwrap();
let (message_descriptor, _) = find_message_type_by_name("InitPluginResponse", &fds).unwrap();
let path = DocPath::new("$").unwrap();
let context = CoreMatchingContext::new(DiffConfig::AllowUnexpectedKeys, &matchingrules_list! {
"body";
"$.catalogue.type" => [ MatchingRule::Regex("CONTENT_MATCHER|CONTENT_GENERATOR".into()) ],
"$.catalogue.key" => [ MatchingRule::NotEmpty ],
"$.catalogue.*" => [ MatchingRule::Type ],
"$.catalogue" => [ MatchingRule::Values ]
}, &hashmap!{});
let descriptor = DescriptorProto {
name: Some("CatalogueEntry".to_string()),
field: [
FieldDescriptorProto {
name: Some("type".to_string()),
number: Some(1),
label: Some(Optional as i32),
r#type: Some(Enum as i32),
type_name: Some(".io.pact.plugin.CatalogueEntry.EntryType".to_string()),
extendee: None,
default_value: None,
oneof_index: None,
json_name: Some("type".to_string()),
options: None,
proto3_optional: None
},
FieldDescriptorProto {
name: Some("key".to_string()),
number: Some(2),
label: Some(Optional as i32),
r#type: Some(String as i32),
type_name: None,
extendee: None,
default_value: None,
oneof_index: None,
json_name: Some("key".to_string()),
options: None,
proto3_optional: None
},
FieldDescriptorProto {
name: Some("values".to_string()),
number: Some(3),
label: Some(Repeated as i32),
r#type: Some(prost_types::field_descriptor_proto::Type::Message as i32),
type_name: Some(".io.pact.plugin.CatalogueEntry.ValuesEntry".to_string()),
extendee: None,
default_value: None,
oneof_index: None,
json_name: Some("values".to_string()),
options: None,
proto3_optional: None
}
].to_vec(),
extension: Default::default(),
nested_type: [
DescriptorProto {
name: Some("ValuesEntry".to_string()),
field: [
FieldDescriptorProto {
name: Some("key".to_string()),
number: Some(1),
label: Some(Optional as i32),
r#type: Some(String as i32),
type_name: None,
extendee: None,
default_value: None,
oneof_index: None,
json_name: Some("key".to_string()),
options: None,
proto3_optional: None
},
FieldDescriptorProto {
name: Some("value".to_string()),
number: Some(2),
label: Some(Optional as i32),
r#type: Some(String as i32),
type_name: None,
extendee: None,
default_value: None,
oneof_index: None,
json_name: Some("value".to_string()),
options: None,
proto3_optional: None
}
].to_vec(),
extension: Default::default(),
nested_type: Default::default(),
enum_type: Default::default(),
extension_range: Default::default(),
oneof_decl: Default::default(),
options: Some(MessageOptions {
message_set_wire_format: None,
no_standard_descriptor_accessor: None,
deprecated: None,
map_entry: Some(true),
uninterpreted_option: Default::default()
}),
reserved_range: Default::default(),
reserved_name: Default::default()
}
].to_vec(),
enum_type: [
EnumDescriptorProto {
name: Some("EntryType".to_string()),
value: [
EnumValueDescriptorProto {
name: Some("CONTENT_MATCHER".to_string()),
number: Some(0),
options: None
},
EnumValueDescriptorProto {
name: Some("CONTENT_GENERATOR".to_string()),
number: Some(1),
options: None
},
EnumValueDescriptorProto {
name: Some("TRANSPORT".to_string()),
number: Some(2),
options: None
},
EnumValueDescriptorProto {
name: Some("MATCHER".to_string()),
number: Some(3),
options: None
},
EnumValueDescriptorProto {
name: Some("INTERACTION".to_string()),
number: Some(4),
options: None
}
].to_vec(),
options: None,
reserved_range: Default::default(),
reserved_name: Default::default()
}
].to_vec(),
extension_range: Default::default(),
oneof_decl: Default::default(),
options: None,
reserved_range: Default::default(),
reserved_name: Default::default()
};
let expected = vec![
ProtobufField {
field_num: 1,
field_name: "catalogue".to_string(),
wire_type: WireType::LengthDelimited,
data: ProtobufFieldData::Message(vec![8, 0, 18, 4, 116, 101, 115, 116], descriptor.clone())
}