Finish tests for common structures

pull/189/head
Chip Senkbeil 1 year ago
parent 62608e0718
commit 3ae6eb03ab
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

@ -11,8 +11,7 @@ use crate::utils::{deserialize_u128_option, serialize_u128_option};
pub struct Metadata {
/// Canonicalized path to the file or directory, resolving symlinks, only included if flagged
/// during the request.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub canonicalized_path: Option<PathBuf>,
/// Represents the type of the entry as a file/dir/symlink.
@ -28,40 +27,44 @@ pub struct Metadata {
/// can be optional as certain systems don't support this.
///
/// Note that this is represented as a string and not a number when serialized!
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "serialize_u128_option")]
#[serde(deserialize_with = "deserialize_u128_option")]
#[serde(default)]
#[serde(
default,
skip_serializing_if = "Option::is_none",
serialize_with = "serialize_u128_option",
deserialize_with = "deserialize_u128_option"
)]
pub accessed: Option<u128>,
/// Represents when (in milliseconds) the file/directory/symlink was created;
/// can be optional as certain systems don't support this.
///
/// Note that this is represented as a string and not a number when serialized!
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "serialize_u128_option")]
#[serde(deserialize_with = "deserialize_u128_option")]
#[serde(default)]
#[serde(
default,
skip_serializing_if = "Option::is_none",
serialize_with = "serialize_u128_option",
deserialize_with = "deserialize_u128_option"
)]
pub created: Option<u128>,
/// Represents the last time (in milliseconds) when the file/directory/symlink was modified;
/// can be optional as certain systems don't support this.
///
/// Note that this is represented as a string and not a number when serialized!
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "serialize_u128_option")]
#[serde(deserialize_with = "deserialize_u128_option")]
#[serde(default)]
#[serde(
default,
skip_serializing_if = "Option::is_none",
serialize_with = "serialize_u128_option",
deserialize_with = "deserialize_u128_option"
)]
pub modified: Option<u128>,
/// Represents metadata that is specific to a unix remote machine.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unix: Option<UnixMetadata>,
/// Represents metadata that is specific to a windows remote machine.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub windows: Option<WindowsMetadata>,
}

@ -30,48 +30,39 @@ pub struct SetPermissionsOptions {
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Permissions {
/// Represents whether or not owner can read from the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner_read: Option<bool>,
/// Represents whether or not owner can write to the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner_write: Option<bool>,
/// Represents whether or not owner can execute the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner_exec: Option<bool>,
/// Represents whether or not associated group can read from the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub group_read: Option<bool>,
/// Represents whether or not associated group can write to the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub group_write: Option<bool>,
/// Represents whether or not associated group can execute the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub group_exec: Option<bool>,
/// Represents whether or not other can read from the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub other_read: Option<bool>,
/// Represents whether or not other can write to the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub other_write: Option<bool>,
/// Represents whether or not other can execute the file
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub other_exec: Option<bool>,
}

@ -107,3 +107,135 @@ impl FromStr for PtySize {
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_be_able_to_serialize_to_json() {
let size = PtySize {
rows: 10,
cols: 20,
pixel_width: 30,
pixel_height: 40,
};
let value = serde_json::to_value(size).unwrap();
assert_eq!(
value,
serde_json::json!({
"rows": 10,
"cols": 20,
"pixel_width": 30,
"pixel_height": 40,
})
);
}
#[test]
fn should_be_able_to_deserialize_minimal_size_from_json() {
let value = serde_json::json!({
"rows": 10,
"cols": 20,
});
let size: PtySize = serde_json::from_value(value).unwrap();
assert_eq!(
size,
PtySize {
rows: 10,
cols: 20,
pixel_width: 0,
pixel_height: 0,
}
);
}
#[test]
fn should_be_able_to_deserialize_full_size_from_json() {
let value = serde_json::json!({
"rows": 10,
"cols": 20,
"pixel_width": 30,
"pixel_height": 40,
});
let size: PtySize = serde_json::from_value(value).unwrap();
assert_eq!(
size,
PtySize {
rows: 10,
cols: 20,
pixel_width: 30,
pixel_height: 40,
}
);
}
#[test]
fn should_be_able_to_serialize_to_msgpack() {
let size = PtySize {
rows: 10,
cols: 20,
pixel_width: 30,
pixel_height: 40,
};
// NOTE: We don't actually check the output here because it's an implementation detail
// and could change as we change how serialization is done. This is merely to verify
// that we can serialize since there are times when serde fails to serialize at
// runtime.
let _ = rmp_serde::encode::to_vec_named(&size).unwrap();
}
#[test]
fn should_be_able_to_deserialize_minimal_size_from_msgpack() {
// NOTE: It may seem odd that we are serializing just to deserialize, but this is to
// verify that we are not corrupting or causing issues when serializing on a
// client/server and then trying to deserialize on the other side. This has happened
// enough times with minor changes that we need tests to verify.
#[derive(Serialize)]
struct PartialSize {
rows: u16,
cols: u16,
}
let buf = rmp_serde::encode::to_vec_named(&PartialSize { rows: 10, cols: 20 }).unwrap();
let size: PtySize = rmp_serde::decode::from_slice(&buf).unwrap();
assert_eq!(
size,
PtySize {
rows: 10,
cols: 20,
pixel_width: 0,
pixel_height: 0,
}
);
}
#[test]
fn should_be_able_to_deserialize_full_size_from_msgpack() {
// NOTE: It may seem odd that we are serializing just to deserialize, but this is to
// verify that we are not corrupting or causing issues when serializing on a
// client/server and then trying to deserialize on the other side. This has happened
// enough times with minor changes that we need tests to verify.
let buf = rmp_serde::encode::to_vec_named(&PtySize {
rows: 10,
cols: 20,
pixel_width: 30,
pixel_height: 40,
})
.unwrap();
let size: PtySize = rmp_serde::decode::from_slice(&buf).unwrap();
assert_eq!(
size,
PtySize {
rows: 10,
cols: 20,
pixel_width: 30,
pixel_height: 40,
}
);
}
}

File diff suppressed because it is too large Load Diff

@ -30,3 +30,113 @@ pub struct SystemInfo {
/// Default shell tied to user running the server process
pub shell: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_be_able_to_serialize_to_json() {
let info = SystemInfo {
family: String::from("family"),
os: String::from("os"),
arch: String::from("arch"),
current_dir: PathBuf::from("current-dir"),
main_separator: '/',
username: String::from("username"),
shell: String::from("shell"),
};
let value = serde_json::to_value(info).unwrap();
assert_eq!(
value,
serde_json::json!({
"family": "family",
"os": "os",
"arch": "arch",
"current_dir": "current-dir",
"main_separator": '/',
"username": "username",
"shell": "shell",
})
);
}
#[test]
fn should_be_able_to_deserialize_from_json() {
let value = serde_json::json!({
"family": "family",
"os": "os",
"arch": "arch",
"current_dir": "current-dir",
"main_separator": '/',
"username": "username",
"shell": "shell",
});
let info: SystemInfo = serde_json::from_value(value).unwrap();
assert_eq!(
info,
SystemInfo {
family: String::from("family"),
os: String::from("os"),
arch: String::from("arch"),
current_dir: PathBuf::from("current-dir"),
main_separator: '/',
username: String::from("username"),
shell: String::from("shell"),
}
);
}
#[test]
fn should_be_able_to_serialize_to_msgpack() {
let info = SystemInfo {
family: String::from("family"),
os: String::from("os"),
arch: String::from("arch"),
current_dir: PathBuf::from("current-dir"),
main_separator: '/',
username: String::from("username"),
shell: String::from("shell"),
};
// NOTE: We don't actually check the output here because it's an implementation detail
// and could change as we change how serialization is done. This is merely to verify
// that we can serialize since there are times when serde fails to serialize at
// runtime.
let _ = rmp_serde::encode::to_vec_named(&info).unwrap();
}
#[test]
fn should_be_able_to_deserialize_from_msgpack() {
// NOTE: It may seem odd that we are serializing just to deserialize, but this is to
// verify that we are not corrupting or causing issues when serializing on a
// client/server and then trying to deserialize on the other side. This has happened
// enough times with minor changes that we need tests to verify.
let buf = rmp_serde::encode::to_vec_named(&SystemInfo {
family: String::from("family"),
os: String::from("os"),
arch: String::from("arch"),
current_dir: PathBuf::from("current-dir"),
main_separator: '/',
username: String::from("username"),
shell: String::from("shell"),
})
.unwrap();
let info: SystemInfo = rmp_serde::decode::from_slice(&buf).unwrap();
assert_eq!(
info,
SystemInfo {
family: String::from("family"),
os: String::from("os"),
arch: String::from("arch"),
current_dir: PathBuf::from("current-dir"),
main_separator: '/',
username: String::from("username"),
shell: String::from("shell"),
}
);
}
}

Loading…
Cancel
Save