use feature_verify_proto::Verify;
use googletest::prelude::*;
use no_features_proto2_proto::NoFeaturesProto2;
use no_features_proto3_proto::NoFeaturesProto3;
use protobuf::{ParseError, ProtoStr};
const NON_UTF8_BYTES: &[u8] = b"\x80";
fn make_non_utf8_proto_str() -> &'static ProtoStr {
unsafe {
ProtoStr::from_utf8_unchecked(NON_UTF8_BYTES)
}
}
#[test]
fn test_proto2() {
let non_utf8_str = make_non_utf8_proto_str();
let mut msg = NoFeaturesProto2::new();
msg.set_my_field(non_utf8_str);
assert_that!(msg.my_field().as_bytes(), eq(NON_UTF8_BYTES));
let serialized_nonutf8 = msg.serialize();
let parsed_result = NoFeaturesProto2::parse(&serialized_nonutf8);
assert_that!(parsed_result, ok(anything()));
}
#[test]
fn test_proto3() {
let non_utf8_str = make_non_utf8_proto_str();
let mut msg = NoFeaturesProto3::new();
msg.set_my_field(non_utf8_str);
assert_that!(msg.my_field().as_bytes(), eq(NON_UTF8_BYTES));
let serialized_nonutf8 = msg.serialize();
let parsed_result = NoFeaturesProto3::parse(&serialized_nonutf8);
assert_that!(parsed_result, err(matches_pattern!(&ParseError)));
}
#[test]
fn test_verify() {
let non_utf8_str = make_non_utf8_proto_str();
let mut msg = Verify::new();
msg.set_my_field(non_utf8_str);
assert_that!(msg.my_field().as_bytes(), eq(NON_UTF8_BYTES));
let serialized_nonutf8 = msg.serialize();
let parsed_result = Verify::parse(&serialized_nonutf8);
assert_that!(parsed_result, err(matches_pattern!(&ParseError)));
}