use super::*;
fn policy_file() -> FileDescriptorProto {
let modes_entry = DescriptorProto {
name: Some("ModesEntry".to_string()),
field: vec![
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
FieldDescriptorProto {
type_name: Some(".policy.test.Mode".to_string()),
..make_field("value", 2, Label::LABEL_OPTIONAL, Type::TYPE_ENUM)
},
],
options: MessageOptions {
map_entry: Some(true),
..Default::default()
}
.into(),
..Default::default()
};
let holder = DescriptorProto {
name: Some("Holder".to_string()),
field: vec![
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_BYTES),
make_field("name", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
FieldDescriptorProto {
type_name: Some(".policy.test.Holder.ModesEntry".to_string()),
..make_field("modes", 3, Label::LABEL_REPEATED, Type::TYPE_MESSAGE)
},
make_field("plain", 4, Label::LABEL_OPTIONAL, Type::TYPE_BYTES),
make_field("embedding", 5, Label::LABEL_REPEATED, Type::TYPE_FLOAT),
make_field("ordinary", 6, Label::LABEL_REPEATED, Type::TYPE_FLOAT),
],
nested_type: vec![modes_entry],
..Default::default()
};
let mut file = proto3_file("policy.proto");
file.package = Some("policy.test".to_string());
file.message_type = vec![holder];
file.enum_type = vec![EnumDescriptorProto {
name: Some("Mode".to_string()),
value: vec![enum_value("MODE_UNSPECIFIED", 0)],
..Default::default()
}];
file
}
fn policy_config() -> CodeGenConfig {
let mut config = CodeGenConfig {
generate_json: true,
..Default::default()
};
config.bytes_fields = vec![
(
".policy.test.Holder.key".to_string(),
BytesRepr::Custom("crate::Secret".to_string()),
),
(
".policy.test.Holder.plain".to_string(),
BytesRepr::Custom("crate::Plain".to_string()),
),
];
config.string_fields = vec![(
".policy.test.Holder.name".to_string(),
StringRepr::Custom("crate::Text".to_string()),
)];
config
}
fn generate_policy(config: &CodeGenConfig) -> Result<(String, Vec<CodeGenWarning>), CodeGenError> {
generate_with_diagnostics(&[policy_file()], &["policy.proto".to_string()], config)
.map(|(files, warnings)| (joined(&files), warnings))
}
#[test]
fn listed_types_use_their_policy_and_unlisted_types_are_unchanged() {
let mut config = policy_config();
config.json_policy_types = vec!["crate :: Secret".to_string(), "crate::Text".to_string()];
let (code, warnings) = generate_policy(&config).unwrap();
assert!(warnings.is_empty(), "{warnings:?}");
assert!(code.contains("json_helpers::bytes_policy"), "{code}");
assert!(code.contains("json_helpers::string_policy"), "{code}");
assert!(code.contains("BytesPolicyJson"), "{code}");
assert!(code.contains("StringPolicyJson"), "{code}");
assert!(code.contains("json_helpers::bytes\""), "{code}");
let (unlisted, _) = generate_policy(&policy_config()).unwrap();
assert!(!unlisted.contains("Policy"), "{unlisted}");
}
#[test]
fn a_listed_type_no_rule_selects_is_an_error() {
let mut config = policy_config();
config.json_policy_types = vec!["crate::Secret".into(), "crate::Missing".into()];
assert!(
matches!(generate_policy(&config),Err(CodeGenError::JsonPolicyTypeMatchedNothing { rust_type }) if rust_type=="crate::Missing")
);
}
#[test]
fn a_shadowed_representation_does_not_count_as_a_selected_policy_type() {
let mut config = policy_config();
config.json_policy_types = vec!["crate::Secret".into()];
config.bytes_fields.push((
".policy.test.Holder.key".into(),
BytesRepr::Custom("crate::Plain".into()),
));
assert!(matches!(
generate_policy(&config),
Err(CodeGenError::JsonPolicyTypeMatchedNothing { .. })
));
}
#[test]
fn a_second_rule_with_a_near_miss_is_an_error() {
let mut config = policy_config();
config.json_policy_types = vec!["crate::Secret".into()];
config.bytes_fields.push((
".policy.test.Holder.plain".into(),
BytesRepr::Custom("::crate::Secret".into()),
));
assert!(
matches!(generate_policy(&config),Err(CodeGenError::JsonPolicyTypeNearMiss {selected,listed}) if selected=="::crate::Secret" && listed=="crate::Secret")
);
}
#[test]
fn global_policy_bindings_do_not_require_fields_in_each_directory() {
let mut config = policy_config();
config.string_fields.push((
".other_directory.Secret.name".into(),
StringRepr::Custom("crate::OtherText".into()),
));
config.json_policy_types = vec!["crate::Secret".into(), "crate::OtherText".into()];
let (_, warnings) = generate_policy(&config).unwrap();
assert!(warnings
.iter()
.any(|warning| matches!(warning, CodeGenWarning::FieldTypeRuleMatchedNothing { .. })));
config.json_policy_types.push("crate::Misspelled".into());
assert!(matches!(
generate_policy(&config),
Err(CodeGenError::JsonPolicyTypeMatchedNothing { .. })
));
}
#[test]
fn deliberately_distinct_same_name_types_must_each_be_listed() {
let mut config = policy_config();
config.bytes_fields.push((
".policy.test.Holder.plain".into(),
BytesRepr::Custom("other::Secret".into()),
));
config.json_policy_types = vec!["crate::Secret".into(), "other::Secret".into()];
assert!(generate_policy(&config).is_ok());
}
#[test]
fn policy_types_without_json_warn_and_change_nothing() {
let mut config = policy_config();
config.generate_json = false;
config.json_policy_types = vec!["crate::Secret".to_string()];
let (code, warnings) = generate_policy(&config).unwrap();
assert_eq!(warnings, [CodeGenWarning::JsonPolicyTypesRequireJson]);
assert!(!code.contains("Policy"));
}
#[test]
fn custom_repeated_scalars_decode_directly_into_their_collection() {
let mut config = policy_config();
config.repeated_fields.push((
".policy.test.Holder.embedding".to_string(),
RepeatedRepr::Custom("crate::SecretList<*>".to_string()),
));
let (code, warnings) = generate_policy(&config).unwrap();
assert!(warnings.is_empty(), "{warnings:?}");
assert!(code.contains("json_helpers::proto_list\""), "{code}");
assert!(code.contains("json_helpers::proto_seq\""), "{code}");
}
#[test]
fn a_policy_string_key_over_enum_values_is_rejected() {
let mut config = policy_config();
config.string_fields.push((
".policy.test.Holder.modes".to_string(),
StringRepr::Custom("crate::Text".to_string()),
));
config.json_policy_types = vec!["crate::Text".to_string()];
let err = generate_policy(&config).unwrap_err();
assert!(
matches!(&err, CodeGenError::JsonPolicyMapKey(path) if path == ".policy.test.Holder.modes"),
"{err:?}"
);
}
fn map_entry(
name: &str,
key: FieldDescriptorProto,
value: FieldDescriptorProto,
) -> DescriptorProto {
DescriptorProto {
name: Some(name.to_string()),
field: vec![key, value],
options: MessageOptions {
map_entry: Some(true),
..Default::default()
}
.into(),
..Default::default()
}
}
fn map_field(
package: &str,
message: &str,
name: &str,
number: i32,
entry: &str,
) -> FieldDescriptorProto {
FieldDescriptorProto {
type_name: Some(format!(".{package}.{message}.{entry}")),
..make_field(name, number, Label::LABEL_REPEATED, Type::TYPE_MESSAGE)
}
}
fn containers_file(package: &str) -> FileDescriptorProto {
let string_key = || make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING);
let value = |ty| make_field("value", 2, Label::LABEL_OPTIONAL, ty);
let message = DescriptorProto {
name: Some("Box".to_string()),
field: vec![
make_field("chain", 1, Label::LABEL_REPEATED, Type::TYPE_BYTES),
make_field("labels", 2, Label::LABEL_REPEATED, Type::TYPE_STRING),
map_field(package, "Box", "by_name", 3, "ByNameEntry"),
map_field(package, "Box", "texts", 4, "TextsEntry"),
map_field(package, "Box", "pairs", 5, "PairsEntry"),
map_field(package, "Box", "keyed", 6, "KeyedEntry"),
map_field(package, "Box", "tagged", 7, "TaggedEntry"),
],
nested_type: vec![
map_entry("ByNameEntry", string_key(), value(Type::TYPE_BYTES)),
map_entry(
"TextsEntry",
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_INT32),
value(Type::TYPE_STRING),
),
map_entry("PairsEntry", string_key(), value(Type::TYPE_STRING)),
map_entry("KeyedEntry", string_key(), value(Type::TYPE_BYTES)),
map_entry("TaggedEntry", string_key(), value(Type::TYPE_BYTES)),
],
..Default::default()
};
let mut file = proto3_file(&format!("{package}.proto"));
file.package = Some(package.to_string());
file.message_type = vec![message];
file
}
fn containers_config(package: &str) -> CodeGenConfig {
let mut config = CodeGenConfig {
generate_json: true,
..Default::default()
};
let secret = || BytesRepr::Custom("crate::Secret".to_string());
let text = || StringRepr::Custom("crate::Text".to_string());
config.bytes_fields = vec![(format!(".{package}.Box"), secret())];
config.string_fields = vec![
(format!(".{package}.Box.labels"), text()),
(format!(".{package}.Box.texts"), text()),
(format!(".{package}.Box.pairs"), text()),
(format!(".{package}.Box.keyed"), text()),
(
format!(".{package}.Box.tagged"),
StringRepr::Custom("crate::Tag".to_string()),
),
];
config.json_policy_types = vec!["crate::Secret".to_string(), "crate::Text".to_string()];
config
}
fn generate_containers(files: &[FileDescriptorProto], package: &str) -> String {
let (generated, warnings) = generate_with_diagnostics(
files,
&[format!("{package}.proto")],
&containers_config(package),
)
.unwrap();
assert!(
warnings
.iter()
.all(|w| matches!(w, CodeGenWarning::FieldTypeRuleMatchedNothing { .. })),
"{warnings:?}"
);
joined(&generated)
}
fn with_module_of(code: &str, field: &str) -> String {
let compact: String = code.split_whitespace().collect();
let anchor = format!("pub{field}:");
let at = compact
.find(&anchor)
.unwrap_or_else(|| panic!("no field {field} in {code}"));
let start = compact[..at]
.rfind("pub")
.or_else(|| compact[..at].rfind('{'))
.unwrap_or(0);
let attrs = &compact[start..at];
let with = attrs
.find("with=\"")
.unwrap_or_else(|| panic!("no with module on {field}: {attrs}"));
let rest = &attrs[with + "with=\"".len()..];
rest[..rest.find('"').unwrap()].to_string()
}
#[test]
fn policy_containers_use_buffa_policy_modules_and_emit_no_element_impl() {
let code = generate_containers(&[containers_file("pa")], "pa");
for (field, module) in [
("chain", "::buffa::json_helpers::repeated_bytes_policy"),
("labels", "::buffa::json_helpers::repeated_string_policy"),
("by_name", "::buffa::json_helpers::map_bytes_policy"),
("texts", "::buffa::json_helpers::map_string_policy"),
(
"pairs",
"::buffa::json_helpers::map_string_policy_policy_key",
),
(
"keyed",
"::buffa::json_helpers::map_bytes_policy_policy_key",
),
(
"tagged",
"::buffa::json_helpers::map_bytes_policy_serde_key",
),
] {
assert_eq!(with_module_of(&code, field), module, "field {field}");
}
assert!(!code.contains("ProtoElemJson for crate::"), "{code}");
assert!(!code.contains("proto_seq"), "{code}");
assert!(!code.contains("proto_map"), "{code}");
assert!(!code.contains("proto_str_key_map"), "{code}");
assert!(!code.contains("policy_str_key_map"), "{code}");
}
#[test]
fn separate_runs_sharing_a_policy_type_in_containers_emit_no_duplicate_impls() {
let files = [containers_file("pa"), containers_file("pb")];
let first = generate_containers(&files, "pa");
let second = generate_containers(&files, "pb");
let both = format!("{first}\n{second}");
assert!(!both.contains("ProtoElemJson for crate::"), "{both}");
let mut unlisted = containers_config("pa");
unlisted.json_policy_types.clear();
let (generated, _) =
generate_with_diagnostics(&files, &["pa.proto".to_string()], &unlisted).unwrap();
assert!(joined(&generated).contains("ProtoElemJson for crate::Secret"));
}
#[test]
fn a_policy_value_under_a_byte_key_keeps_its_policy() {
use crate::generated::descriptor::{
feature_set::Utf8Validation, Edition, FeatureSet, FieldOptions,
};
let key = FieldDescriptorProto {
options: FieldOptions {
features: FeatureSet {
utf8_validation: Some(Utf8Validation::NONE),
..Default::default()
}
.into(),
..Default::default()
}
.into(),
..make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING)
};
let file = FileDescriptorProto {
name: Some("raw.proto".to_string()),
package: Some("raw".to_string()),
syntax: Some("editions".to_string()),
edition: Some(Edition::EDITION_2023),
message_type: vec![DescriptorProto {
name: Some("Raw".to_string()),
field: vec![map_field("raw", "Raw", "by_key", 1, "ByKeyEntry")],
nested_type: vec![map_entry(
"ByKeyEntry",
key,
make_field("value", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
)],
..Default::default()
}],
..Default::default()
};
let mut config = CodeGenConfig {
generate_json: true,
strict_utf8_mapping: true,
..Default::default()
};
config.string_fields = vec![(
".raw.Raw.by_key".to_string(),
StringRepr::Custom("crate::Text".to_string()),
)];
config.json_policy_types = vec!["crate::Text".to_string()];
let (generated, _) =
generate_with_diagnostics(&[file], &["raw.proto".to_string()], &config).unwrap();
let code = joined(&generated);
assert_eq!(
with_module_of(&code, "by_key"),
"::buffa::json_helpers::map_string_policy_bytes_key"
);
assert!(!code.contains("bytes_key_map"), "{code}");
assert!(!code.contains("ProtoElemJson for crate::"), "{code}");
}
#[test]
fn custom_and_guarded_policy_collections_receive_elements_directly() {
let mut config = containers_config("pa");
for field in ["chain", "labels"] {
config.repeated_fields.push((
format!(".pa.Box.{field}"),
RepeatedRepr::Custom("crate::SecretList<*>".to_string()),
));
}
let (generated, _) =
generate_with_diagnostics(&[containers_file("pa")], &["pa.proto".to_string()], &config)
.unwrap();
let code = joined(&generated);
assert_eq!(
with_module_of(&code, "chain"),
"::buffa::json_helpers::list_bytes_policy"
);
assert_eq!(
with_module_of(&code, "labels"),
"::buffa::json_helpers::list_string_policy"
);
let mut config = containers_config("pa");
config
.string_fields
.retain(|(path, _)| !path.ends_with(".tagged"));
for field in ["tagged", "by_name"] {
config.string_fields.push((
format!(".pa.Box.{field}"),
StringRepr::Custom("crate::Text".to_string()),
));
}
config.zeroize_messages = vec![".pa".to_string()];
config.preserve_unknown_fields = false;
let (generated, _) =
generate_with_diagnostics(&[containers_file("pa")], &["pa.proto".to_string()], &config)
.unwrap();
let code = joined(&generated);
for (field, module) in [
("chain", "::buffa::json_helpers::list_bytes_policy"),
("labels", "::buffa::json_helpers::list_string_policy"),
(
"by_name",
"::buffa::json_helpers::map_bytes_policy_policy_key",
),
("texts", "::buffa::json_helpers::map_string_policy"),
(
"pairs",
"::buffa::json_helpers::map_string_policy_policy_key",
),
(
"keyed",
"::buffa::json_helpers::map_bytes_policy_policy_key",
),
(
"tagged",
"::buffa::json_helpers::map_bytes_policy_policy_key",
),
] {
assert_eq!(with_module_of(&code, field), module, "field {field}");
}
assert!(
code.contains("::buffa::sensitive::List<crate::Secret>"),
"{code}"
);
assert!(!code.contains("ProtoElemJson for crate::"), "{code}");
assert!(!code.contains("guarded_map"), "{code}");
assert!(!code.contains("proto_list"), "{code}");
}
#[test]
fn reflection_with_policy_types_warns_that_reflective_json_bypasses_them() {
let mut config = policy_config();
config.json_policy_types = vec!["crate::Secret".to_string()];
config.generate_reflection = true;
let (_, warnings) = generate_policy(&config).unwrap();
assert_eq!(
warnings,
[CodeGenWarning::JsonPolicyReflectionBypass {
policy_types: vec!["crate :: Secret".into()],
}]
);
let text = warnings[0].to_string();
assert!(text.contains("DynamicMessage"), "{text}");
assert!(text.contains("refuses export"), "{text}");
assert!(text.contains("import checks"), "{text}");
assert!(text.contains("try_from_message"), "{text}");
assert!(text.contains("not a confidentiality boundary"), "{text}");
config.generate_reflection_vtable = true;
assert_eq!(
generate_policy(&config).unwrap().1,
[CodeGenWarning::JsonPolicyReflectionBypass {
policy_types: vec!["crate :: Secret".into()],
}]
);
config.generate_reflection_vtable = false;
config.generate_reflection = false;
assert!(generate_policy(&config).unwrap().1.is_empty());
let mut unlisted = policy_config();
unlisted.generate_reflection = true;
assert!(generate_policy(&unlisted).unwrap().1.is_empty());
config.generate_reflection = true;
config.generate_json = false;
assert_eq!(
generate_policy(&config).unwrap().1,
[CodeGenWarning::JsonPolicyTypesRequireJson]
);
}
#[test]
fn equivalent_custom_type_spellings_emit_one_impl_per_trait() {
for policy in [false, true] {
let mut config = containers_config("pa");
config.generate_reflection = true;
config.generate_reflection_vtable = true;
config.bytes_fields.push((
".pa.Box.chain".into(),
BytesRepr::Custom("crate :: Secret".into()),
));
config.string_fields.push((
".pa.Box.pairs".into(),
StringRepr::Custom("crate :: Text".into()),
));
config.bytes_fields.push((
".pa.Box.by_name".into(),
BytesRepr::Custom("((crate::r#Secret))".into()),
));
config.string_fields.push((
".pa.Box.keyed".into(),
StringRepr::Custom("(crate::Text)".into()),
));
if !policy {
config.json_policy_types.clear();
}
let generated = generate(&[containers_file("pa")], &["pa.proto".into()], &config).unwrap();
fn count_impls(items: &[syn::Item], trait_name: &str, type_key: &str) -> usize {
items
.iter()
.map(|item| match item {
syn::Item::Mod(module) => module
.content
.as_ref()
.map_or(0, |(_, items)| count_impls(items, trait_name, type_key)),
syn::Item::Impl(implementation) => {
let matches_trait = implementation
.trait_
.as_ref()
.and_then(|(path, _)| path.segments.last())
.is_some_and(|segment| segment.ident == trait_name);
let ty = &implementation.self_ty;
usize::from(
matches_trait
&& crate::context::rust_type_key("e::quote!(#ty).to_string())
== type_key,
)
}
_ => 0,
})
.sum()
}
let parsed = syn::parse_file(&joined(&generated)).unwrap();
for (trait_name, ty, expected) in [
("ProtoElemJson", "crate::Secret", usize::from(!policy)),
("ReflectElement", "crate::Secret", 1),
("ReflectElement", "crate::Text", 1),
("ReflectMapKey", "crate::Text", 1),
("ReflectMapKey", "crate::Tag", 1),
] {
assert_eq!(
count_impls(
&parsed.items,
trait_name,
&crate::context::rust_type_key(ty)
),
expected,
"{trait_name} for {ty}"
);
}
}
}
#[test]
fn unused_policy_rules_do_not_warn_about_reflection() {
let mut config = policy_config();
config.generate_reflection = true;
config.bytes_fields = vec![(
".another.Directory".into(),
BytesRepr::Custom("crate::Secret".into()),
)];
config.json_policy_types = vec!["crate::Secret".into()];
let (_, warnings) = generate_policy(&config).unwrap();
assert!(!warnings
.iter()
.any(|warning| matches!(warning, CodeGenWarning::JsonPolicyReflectionBypass { .. })));
}
#[test]
fn reflection_warning_names_only_selected_policy_types_once() {
let mut config = policy_config();
config.generate_reflection = true;
config.generate_views = false;
config.json_policy_types = vec!["crate::Text".into(), "((crate::Secret))".into()];
let (_, warnings) = generate_policy(&config).unwrap();
assert_eq!(
warnings,
[CodeGenWarning::JsonPolicyReflectionBypass {
policy_types: vec!["crate :: Secret".into(), "crate :: Text".into()],
}]
);
let mut file = policy_file();
file.message_type[0]
.oneof_decl
.push(crate::generated::descriptor::OneofDescriptorProto {
name: Some("choice".into()),
..Default::default()
});
file.message_type[0].field[0].oneof_index = Some(0);
config.json_policy_types = vec!["crate::Secret".into()];
let (_, warnings) =
generate_with_diagnostics(&[file], &["policy.proto".into()], &config).unwrap();
assert_eq!(
warnings,
[CodeGenWarning::JsonPolicyReflectionBypass {
policy_types: vec!["crate :: Secret".into()],
}]
);
}