summaryrefslogtreecommitdiffstats
path: root/vendor/gix-credentials/tests/helper/context.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/gix-credentials/tests/helper/context.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gix-credentials/tests/helper/context.rs')
-rw-r--r--vendor/gix-credentials/tests/helper/context.rs123
1 files changed, 123 insertions, 0 deletions
diff --git a/vendor/gix-credentials/tests/helper/context.rs b/vendor/gix-credentials/tests/helper/context.rs
new file mode 100644
index 000000000..6566c6c2b
--- /dev/null
+++ b/vendor/gix-credentials/tests/helper/context.rs
@@ -0,0 +1,123 @@
+use gix_credentials::protocol::Context;
+
+#[test]
+fn encode_decode_roundtrip_works_only_for_serializing_fields() {
+ for ctx in [
+ Context {
+ protocol: Some("https".into()),
+ host: Some("github.com".into()),
+ path: Some("byron/gitoxide".into()),
+ username: Some("user".into()),
+ password: Some("pass".into()),
+ url: Some("https://github.com/byron/gitoxide".into()),
+ ..Default::default()
+ },
+ Context::default(),
+ ] {
+ let mut buf = Vec::<u8>::new();
+ ctx.write_to(&mut buf).unwrap();
+ let actual = Context::from_bytes(&buf).unwrap();
+ assert_eq!(actual, ctx, "ctx should encode itself losslessly");
+ }
+}
+
+mod write_to {
+ use gix_credentials::protocol::Context;
+
+ #[test]
+ fn quit_is_not_serialized_but_can_be_parsed() {
+ let mut buf = Vec::<u8>::new();
+ Context {
+ quit: Some(true),
+ ..Default::default()
+ }
+ .write_to(&mut buf)
+ .unwrap();
+ assert_eq!(Context::from_bytes(&buf).unwrap(), Context::default());
+ assert_eq!(
+ Context::from_bytes(b"quit=true\nurl=https://example.com").unwrap(),
+ Context {
+ quit: Some(true),
+ url: Some("https://example.com".into()),
+ ..Default::default()
+ }
+ );
+ }
+
+ #[test]
+ fn null_bytes_and_newlines_are_invalid() {
+ for input in [&b"foo\0"[..], b"foo\n"] {
+ let ctx = Context {
+ path: Some(input.into()),
+ ..Default::default()
+ };
+ let mut buf = Vec::<u8>::new();
+ let err = ctx.write_to(&mut buf).unwrap_err();
+ assert_eq!(err.kind(), std::io::ErrorKind::Other);
+ }
+ }
+}
+
+mod from_bytes {
+ use gix_credentials::protocol::Context;
+
+ #[test]
+ fn empty_newlines_cause_skipping_remaining_input() {
+ let input = b"protocol=https
+host=example.com\n
+password=secr3t
+username=bob";
+ assert_eq!(
+ Context::from_bytes(input).unwrap(),
+ Context {
+ protocol: Some("https".into()),
+ host: Some("example.com".into()),
+ ..Default::default()
+ }
+ )
+ }
+
+ #[test]
+ fn unknown_field_names_are_skipped() {
+ let input = b"protocol=https
+unknown=value
+username=bob";
+ assert_eq!(
+ Context::from_bytes(input).unwrap(),
+ Context {
+ protocol: Some("https".into()),
+ username: Some("bob".into()),
+ ..Default::default()
+ }
+ )
+ }
+
+ #[test]
+ fn quit_supports_git_config_boolean_values() {
+ for true_value in ["1", "42", "-42", "true", "on", "yes"] {
+ let input = format!("quit={true_value}");
+ assert_eq!(
+ Context::from_bytes(input.as_bytes()).unwrap().quit,
+ Some(true),
+ "{input}"
+ )
+ }
+ for false_value in ["0", "false", "off", "no"] {
+ let input = format!("quit={false_value}");
+ assert_eq!(
+ Context::from_bytes(input.as_bytes()).unwrap().quit,
+ Some(false),
+ "{input}"
+ )
+ }
+ }
+
+ #[test]
+ fn null_bytes_when_decoding() {
+ let err = Context::from_bytes(b"url=https://foo\0").unwrap_err();
+ assert!(matches!(
+ err,
+ gix_credentials::protocol::context::decode::Error::Encoding(_)
+ ));
+ }
+}