Compare commits
2 commits
c609f51c6a
...
51cf212bb6
| Author | SHA1 | Date | |
|---|---|---|---|
| 51cf212bb6 | |||
| 65f1e5d083 |
6 changed files with 197 additions and 5 deletions
|
|
@ -76,7 +76,7 @@ mod tests {
|
|||
);
|
||||
check_trait(format!("{chunk:?}"), repr_expect, "Debug", "Chunk");
|
||||
|
||||
assert_eq!(chunk.get_file_id().to_string(), "fid");
|
||||
assert_eq!(chunk.get_file_id().to_string(), "");
|
||||
assert_eq!(chunk.get_offset(), mock_offset);
|
||||
assert_eq!(chunk.get_data(), data);
|
||||
assert_eq!(chunk.get_length(), len);
|
||||
|
|
|
|||
|
|
@ -192,8 +192,8 @@ mod tests {
|
|||
#[test]
|
||||
fn test_pub_endpoints() {
|
||||
let uri = Uri::default();
|
||||
let share_id = ShareID::default();
|
||||
let file_id = FileID::default();
|
||||
let share_id = ShareID::from("sid".to_string());
|
||||
let file_id = FileID::new_test("fid".to_string());
|
||||
|
||||
assert_eq!("/api/v2/alias/upload/new", uri.share_create());
|
||||
assert_eq!("/api/v2/alias/mail/notify/sid", uri.share_notify(&share_id));
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ const DATA: [&[u8]; 8] = [
|
|||
0x3C, 0xA7, 0x5D, 0xE1, 0x4F, 0x99, 0x00, 0x20, 0x7F, 0xB3, 0xCD, 0x8A, 0x10, 0x55, 0xAA,
|
||||
0xFF, 0x5E, 0xA3, 0x1F, 0xC8, 0x72, 0x4D, 0x99, 0x00, 0xB7, 0x3C, 0x8E, 0xAD, 0x26, 0xF1,
|
||||
],
|
||||
// long run of identical bytes (1 kib of ascii 'a')
|
||||
// long run of identical bytes (1 KiB of ascii 'A')
|
||||
&[b'A'; 1024],
|
||||
// very large slice (10 mib of zeroes)
|
||||
// very large slice (10 MiB of zeroes)
|
||||
&[0u8; 10 * 1024 * 1024],
|
||||
];
|
||||
|
||||
|
|
|
|||
57
src/test_util/mock_client.rs
Normal file
57
src/test_util/mock_client.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use crate::{
|
||||
file,
|
||||
sharry::{self, AliasID, FileID, ShareID, Uri},
|
||||
test_util::mock_ids,
|
||||
};
|
||||
|
||||
pub struct MockClient;
|
||||
|
||||
impl sharry::Client for MockClient {
|
||||
fn share_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
_: sharry::json::NewShareRequest,
|
||||
) -> crate::Result<ShareID> {
|
||||
mock_ids::check_alias(uri, alias_id)?;
|
||||
|
||||
Ok(true.into())
|
||||
}
|
||||
|
||||
fn share_notify(&self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID) -> crate::Result<()> {
|
||||
mock_ids::check_alias(uri, alias_id)?;
|
||||
mock_ids::check_share(share_id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn file_create(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
share_id: &ShareID,
|
||||
_: &file::Checked,
|
||||
) -> crate::Result<FileID> {
|
||||
mock_ids::check_alias(uri, alias_id)?;
|
||||
mock_ids::check_share(share_id)?;
|
||||
|
||||
Ok(true.into())
|
||||
}
|
||||
|
||||
fn file_patch(
|
||||
&self,
|
||||
uri: &Uri,
|
||||
alias_id: &AliasID,
|
||||
share_id: &ShareID,
|
||||
chunk: &file::Chunk,
|
||||
) -> crate::Result<()> {
|
||||
mock_ids::check_alias(uri, alias_id)?;
|
||||
mock_ids::check_file(share_id, chunk.get_file_id())?;
|
||||
|
||||
// TODO: `chunk` must align to a full MiB
|
||||
|
||||
// Ok(())
|
||||
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
133
src/test_util/mock_ids.rs
Normal file
133
src/test_util/mock_ids.rs
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
use crate::sharry::{AliasID, FileID, ShareID, Uri};
|
||||
|
||||
const VALID_URI: &'static str = "scheme://valid.uri";
|
||||
const VALID_ALIAS: &'static str = "valid-alias";
|
||||
const VALID_SHARE: &'static str = "valid-share";
|
||||
const VALID_FILE: &'static str = "valid-file";
|
||||
|
||||
pub fn check_uri(uri: &Uri) -> crate::Result<&Uri> {
|
||||
if uri.to_string() != VALID_URI {
|
||||
Err(uri.into())
|
||||
} else {
|
||||
Ok(uri)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_alias<'t>(uri: &Uri, alias_id: &'t AliasID) -> crate::Result<&'t AliasID> {
|
||||
check_uri(uri)?;
|
||||
|
||||
if alias_id.as_ref() != VALID_ALIAS {
|
||||
Err(alias_id.into())
|
||||
} else {
|
||||
Ok(alias_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_share(share_id: &ShareID) -> crate::Result<&ShareID> {
|
||||
if share_id.to_string() != VALID_SHARE {
|
||||
Err(share_id.into())
|
||||
} else {
|
||||
Ok(share_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_file<'t>(share_id: &ShareID, file_id: &'t FileID) -> crate::Result<&'t FileID> {
|
||||
check_share(share_id)?;
|
||||
|
||||
if file_id.to_string() != VALID_FILE {
|
||||
Err(file_id.into())
|
||||
} else {
|
||||
Ok(file_id)
|
||||
}
|
||||
}
|
||||
|
||||
fn make_invalid(valid: &str) -> String {
|
||||
let invalid = valid.replace("valid", "invalid");
|
||||
assert_ne!(valid, invalid);
|
||||
|
||||
invalid
|
||||
}
|
||||
|
||||
impl From<bool> for Uri {
|
||||
fn from(value: bool) -> Self {
|
||||
match value {
|
||||
true => Self::from(VALID_URI.to_string()),
|
||||
false => Self::from(make_invalid(VALID_URI)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for AliasID {
|
||||
fn from(value: bool) -> Self {
|
||||
match value {
|
||||
true => Self::from(VALID_ALIAS.to_string()),
|
||||
false => Self::from(make_invalid(VALID_ALIAS)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for ShareID {
|
||||
fn from(value: bool) -> Self {
|
||||
match value {
|
||||
true => Self::from(VALID_SHARE.to_string()),
|
||||
false => Self::from(make_invalid(VALID_SHARE)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for FileID {
|
||||
fn from(value: bool) -> Self {
|
||||
match value {
|
||||
true => Self::new_test(VALID_FILE),
|
||||
false => Self::new_test(make_invalid(VALID_FILE)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Parameter;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn true_makes_valids() {
|
||||
assert!(check_uri(&true.into()).is_ok());
|
||||
assert!(check_alias(&true.into(), &true.into()).is_ok());
|
||||
assert!(check_share(&true.into()).is_ok());
|
||||
assert!(check_file(&true.into(), &true.into()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn false_makes_invalids() {
|
||||
// invalid Uri
|
||||
let check = check_uri(&false.into()).expect_err("should be invalid");
|
||||
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||
assert!(matches!(p, Parameter::Uri(_)));
|
||||
|
||||
// invalid Uri, valid alias
|
||||
let check = check_alias(&false.into(), &true.into()).expect_err("should be invalid");
|
||||
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||
assert!(matches!(p, Parameter::Uri(_)));
|
||||
|
||||
// valid Uri, invalid alias
|
||||
let check = check_alias(&true.into(), &false.into()).expect_err("should be invalid");
|
||||
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||
assert!(matches!(p, Parameter::AliasID(_)));
|
||||
|
||||
// invalid share
|
||||
let check = check_share(&false.into()).expect_err("should be invalid");
|
||||
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||
assert!(matches!(p, Parameter::ShareID(_)));
|
||||
|
||||
// invalid share, valid file
|
||||
let check = check_file(&false.into(), &true.into()).expect_err("should be invalid");
|
||||
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||
assert!(matches!(p, Parameter::ShareID(_)));
|
||||
|
||||
// valid share, invalid file
|
||||
let check = check_file(&true.into(), &false.into()).expect_err("should be invalid");
|
||||
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||
assert!(matches!(p, Parameter::FileID(_)));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
#![cfg(test)]
|
||||
|
||||
pub mod data;
|
||||
mod mock_client;
|
||||
mod mock_ids;
|
||||
|
||||
use std::{fmt, io::Write};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue