Loading tools/aconfig/aconfig/src/commands.rs +131 −0 Original line number Diff line number Diff line Loading @@ -69,6 +69,7 @@ pub fn parse_flags( declarations: Vec<Input>, values: Vec<Input>, default_permission: ProtoFlagPermission, allow_read_write: bool, ) -> Result<Vec<u8>> { let mut parsed_flags = ProtoParsedFlags::new(); Loading Loading @@ -195,6 +196,16 @@ pub fn parse_flags( } } if !allow_read_write { if let Some(pf) = parsed_flags .parsed_flag .iter() .find(|pf| pf.permission() == ProtoFlagPermission::READ_WRITE) { bail!("flag {} has permission READ_WRITE, but allow_read_write is false", pf.name()); } } // Create a sorted parsed_flags aconfig_protos::parsed_flags::sort_parsed_flags(&mut parsed_flags); aconfig_protos::parsed_flags::verify_fields(&parsed_flags)?; Loading Loading @@ -576,6 +587,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_ONLY, true, ) .unwrap(); let parsed_flags = Loading Loading @@ -609,6 +621,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_WRITE, true, ) .unwrap_err(); assert_eq!( Loading Loading @@ -640,6 +653,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_WRITE, true, ) .unwrap_err(); assert_eq!( Loading @@ -647,6 +661,121 @@ mod tests { "failed to parse memory: expected container argument.container, got declaration.container" ); } #[test] fn test_parse_flags_no_allow_read_write_default_error() { let first_flag = r#" package: "com.first" container: "com.first.container" flag { name: "first" namespace: "first_ns" description: "This is the description of the first flag." bug: "123" } "#; let declaration = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }]; let error = crate::commands::parse_flags( "com.first", Some("com.first.container"), declaration, vec![], ProtoFlagPermission::READ_WRITE, false, ) .unwrap_err(); assert_eq!( format!("{:?}", error), "flag first has permission READ_WRITE, but allow_read_write is false" ); } #[test] fn test_parse_flags_no_allow_read_write_value_error() { let first_flag = r#" package: "com.first" container: "com.first.container" flag { name: "first" namespace: "first_ns" description: "This is the description of the first flag." bug: "123" } "#; let declaration = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }]; let first_flag_value = r#" flag_value { package: "com.first" name: "first" state: DISABLED permission: READ_WRITE } "#; let value = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag_value.as_bytes()), }]; let error = crate::commands::parse_flags( "com.first", Some("com.first.container"), declaration, value, ProtoFlagPermission::READ_ONLY, false, ) .unwrap_err(); assert_eq!( format!("{:?}", error), "flag first has permission READ_WRITE, but allow_read_write is false" ); } #[test] fn test_parse_flags_no_allow_read_write_success() { let first_flag = r#" package: "com.first" container: "com.first.container" flag { name: "first" namespace: "first_ns" description: "This is the description of the first flag." bug: "123" } "#; let declaration = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }]; let first_flag_value = r#" flag_value { package: "com.first" name: "first" state: DISABLED permission: READ_ONLY } "#; let value = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag_value.as_bytes()), }]; let flags_bytes = crate::commands::parse_flags( "com.first", Some("com.first.container"), declaration, value, ProtoFlagPermission::READ_ONLY, false, ) .unwrap(); let parsed_flags = aconfig_protos::parsed_flags::try_from_binary_proto(&flags_bytes).unwrap(); assert_eq!(1, parsed_flags.parsed_flag.len()); let parsed_flag = parsed_flags.parsed_flag.first().unwrap(); assert_eq!(ProtoFlagState::DISABLED, parsed_flag.state()); assert_eq!(ProtoFlagPermission::READ_ONLY, parsed_flag.permission()); } #[test] fn test_parse_flags_override_fixed_read_only() { Loading Loading @@ -682,6 +811,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_WRITE, true, ) .unwrap_err(); assert_eq!( Loading Loading @@ -716,6 +846,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_ONLY, true, ) .unwrap(); let parsed_flags = Loading tools/aconfig/aconfig/src/main.rs +9 −0 Original line number Diff line number Diff line Loading @@ -62,6 +62,12 @@ fn cli() -> Command { &commands::DEFAULT_FLAG_PERMISSION, )), ) .arg( Arg::new("allow-read-write") .long("allow-read-write") .value_parser(clap::value_parser!(bool)) .default_value("true"), ) .arg(Arg::new("cache").long("cache").required(true)), ) .subcommand( Loading Loading @@ -242,12 +248,15 @@ fn main() -> Result<()> { sub_matches, "default-permission", )?; let allow_read_write = get_optional_arg::<bool>(sub_matches, "allow-read-write") .expect("failed to parse allow-read-write"); let output = commands::parse_flags( package, container, declarations, values, *default_permission, *allow_read_write, ) .context("failed to create cache")?; let path = get_required_arg::<String>(sub_matches, "cache")?; Loading tools/aconfig/aconfig/src/storage/mod.rs +1 −0 Original line number Diff line number Diff line Loading @@ -163,6 +163,7 @@ mod tests { reader: Box::new(value_content), }], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading tools/aconfig/aconfig/src/test.rs +3 −0 Original line number Diff line number Diff line Loading @@ -266,6 +266,7 @@ parsed_flag { reader: Box::new(include_bytes!("../tests/read_only_test.values").as_slice()), }], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading @@ -290,6 +291,7 @@ parsed_flag { }, ], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading @@ -308,6 +310,7 @@ parsed_flag { reader: Box::new(include_bytes!("../tests/third.values").as_slice()), }], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading Loading
tools/aconfig/aconfig/src/commands.rs +131 −0 Original line number Diff line number Diff line Loading @@ -69,6 +69,7 @@ pub fn parse_flags( declarations: Vec<Input>, values: Vec<Input>, default_permission: ProtoFlagPermission, allow_read_write: bool, ) -> Result<Vec<u8>> { let mut parsed_flags = ProtoParsedFlags::new(); Loading Loading @@ -195,6 +196,16 @@ pub fn parse_flags( } } if !allow_read_write { if let Some(pf) = parsed_flags .parsed_flag .iter() .find(|pf| pf.permission() == ProtoFlagPermission::READ_WRITE) { bail!("flag {} has permission READ_WRITE, but allow_read_write is false", pf.name()); } } // Create a sorted parsed_flags aconfig_protos::parsed_flags::sort_parsed_flags(&mut parsed_flags); aconfig_protos::parsed_flags::verify_fields(&parsed_flags)?; Loading Loading @@ -576,6 +587,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_ONLY, true, ) .unwrap(); let parsed_flags = Loading Loading @@ -609,6 +621,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_WRITE, true, ) .unwrap_err(); assert_eq!( Loading Loading @@ -640,6 +653,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_WRITE, true, ) .unwrap_err(); assert_eq!( Loading @@ -647,6 +661,121 @@ mod tests { "failed to parse memory: expected container argument.container, got declaration.container" ); } #[test] fn test_parse_flags_no_allow_read_write_default_error() { let first_flag = r#" package: "com.first" container: "com.first.container" flag { name: "first" namespace: "first_ns" description: "This is the description of the first flag." bug: "123" } "#; let declaration = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }]; let error = crate::commands::parse_flags( "com.first", Some("com.first.container"), declaration, vec![], ProtoFlagPermission::READ_WRITE, false, ) .unwrap_err(); assert_eq!( format!("{:?}", error), "flag first has permission READ_WRITE, but allow_read_write is false" ); } #[test] fn test_parse_flags_no_allow_read_write_value_error() { let first_flag = r#" package: "com.first" container: "com.first.container" flag { name: "first" namespace: "first_ns" description: "This is the description of the first flag." bug: "123" } "#; let declaration = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }]; let first_flag_value = r#" flag_value { package: "com.first" name: "first" state: DISABLED permission: READ_WRITE } "#; let value = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag_value.as_bytes()), }]; let error = crate::commands::parse_flags( "com.first", Some("com.first.container"), declaration, value, ProtoFlagPermission::READ_ONLY, false, ) .unwrap_err(); assert_eq!( format!("{:?}", error), "flag first has permission READ_WRITE, but allow_read_write is false" ); } #[test] fn test_parse_flags_no_allow_read_write_success() { let first_flag = r#" package: "com.first" container: "com.first.container" flag { name: "first" namespace: "first_ns" description: "This is the description of the first flag." bug: "123" } "#; let declaration = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }]; let first_flag_value = r#" flag_value { package: "com.first" name: "first" state: DISABLED permission: READ_ONLY } "#; let value = vec![Input { source: "memory".to_string(), reader: Box::new(first_flag_value.as_bytes()), }]; let flags_bytes = crate::commands::parse_flags( "com.first", Some("com.first.container"), declaration, value, ProtoFlagPermission::READ_ONLY, false, ) .unwrap(); let parsed_flags = aconfig_protos::parsed_flags::try_from_binary_proto(&flags_bytes).unwrap(); assert_eq!(1, parsed_flags.parsed_flag.len()); let parsed_flag = parsed_flags.parsed_flag.first().unwrap(); assert_eq!(ProtoFlagState::DISABLED, parsed_flag.state()); assert_eq!(ProtoFlagPermission::READ_ONLY, parsed_flag.permission()); } #[test] fn test_parse_flags_override_fixed_read_only() { Loading Loading @@ -682,6 +811,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_WRITE, true, ) .unwrap_err(); assert_eq!( Loading Loading @@ -716,6 +846,7 @@ mod tests { declaration, value, ProtoFlagPermission::READ_ONLY, true, ) .unwrap(); let parsed_flags = Loading
tools/aconfig/aconfig/src/main.rs +9 −0 Original line number Diff line number Diff line Loading @@ -62,6 +62,12 @@ fn cli() -> Command { &commands::DEFAULT_FLAG_PERMISSION, )), ) .arg( Arg::new("allow-read-write") .long("allow-read-write") .value_parser(clap::value_parser!(bool)) .default_value("true"), ) .arg(Arg::new("cache").long("cache").required(true)), ) .subcommand( Loading Loading @@ -242,12 +248,15 @@ fn main() -> Result<()> { sub_matches, "default-permission", )?; let allow_read_write = get_optional_arg::<bool>(sub_matches, "allow-read-write") .expect("failed to parse allow-read-write"); let output = commands::parse_flags( package, container, declarations, values, *default_permission, *allow_read_write, ) .context("failed to create cache")?; let path = get_required_arg::<String>(sub_matches, "cache")?; Loading
tools/aconfig/aconfig/src/storage/mod.rs +1 −0 Original line number Diff line number Diff line Loading @@ -163,6 +163,7 @@ mod tests { reader: Box::new(value_content), }], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading
tools/aconfig/aconfig/src/test.rs +3 −0 Original line number Diff line number Diff line Loading @@ -266,6 +266,7 @@ parsed_flag { reader: Box::new(include_bytes!("../tests/read_only_test.values").as_slice()), }], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading @@ -290,6 +291,7 @@ parsed_flag { }, ], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading @@ -308,6 +310,7 @@ parsed_flag { reader: Box::new(include_bytes!("../tests/third.values").as_slice()), }], crate::commands::DEFAULT_FLAG_PERMISSION, true, ) .unwrap(); aconfig_protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() Loading