use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;
use bytes::Bytes;
use tokio::task::JoinSet;
use super::fixture::{TestBucket, foreground, user_metadata};
use super::gateway::REGION;
use super::proxy::{Fault, Proxy, Reply, Seen};
use crate::backend::FILE_ATTRIBUTE_DIRECTORY;
use crate::ntstatus;
use crate::proto::header::{Command, SMB2_HEADER_LEN};
use crate::proto::messages::{
CloseRequest, CreateRequest, CreateResponse, FileId, FlushRequest, QueryDirectoryRequest,
QueryDirectoryResponse, ReadRequest, ReadResponse, SetInfoRequest, WriteRequest, WriteResponse,
};
use crate::s3::client::S3Client;
use crate::s3::encode::uri_encode;
use crate::server::ServerState;
use crate::tests::peer::{Peer, status};
use crate::utils::FILETIME_OFFSET;
use crate::{Handle, OpenOptions, S3Backend, S3BackendBuilder, Share, ShareBackend, SmbServer};
pub(super) const READ: u32 = 0x0012_0089;
pub(super) const DELETE: u32 = 0x0001_0000;
pub(super) const WRITE: u32 = 0x0012_0116;
pub(super) const WRITE_ATTRIBUTES: u32 = 0x0000_0100;
pub(super) const FILE_OPEN: u32 = 1;
pub(super) const FILE_CREATE: u32 = 2;
pub(super) const FILE_OPEN_IF: u32 = 3;
pub(super) const FILE_OVERWRITE: u32 = 4;
pub(super) const FILE_OVERWRITE_IF: u32 = 5;
pub(super) const FILE_DIRECTORY_FILE: u32 = 0x1;
pub(super) const FILE_NON_DIRECTORY_FILE: u32 = 0x40;
pub(super) const FILE_DELETE_ON_CLOSE: u32 = 0x1000;
pub(super) fn filetime(secs: u64, nanos: u32) -> u64 {
FILETIME_OFFSET + secs * 10_000_000 + u64::from(nanos) / 100
}
pub(super) fn builder(bucket: &TestBucket, addr: SocketAddr) -> S3BackendBuilder {
S3Backend::builder(format!("http://{addr}"), REGION, &bucket.name)
.credentials(&bucket.gateway.access_key, &bucket.gateway.secret_key)
.read_only()
}
pub(super) fn writable_builder(
bucket: &TestBucket,
addr: SocketAddr,
cache: &Path,
) -> S3BackendBuilder {
S3Backend::builder(format!("http://{addr}"), REGION, &bucket.name)
.credentials(&bucket.gateway.access_key, &bucket.gateway.secret_key)
.cache_dir(cache)
.held_uploads(true)
}
pub(super) async fn backend(bucket: &TestBucket) -> S3Backend {
builder(bucket, bucket.gateway.addr).build().await.unwrap()
}
pub(super) async fn open_handle(backend: &S3Backend, name: &str) -> Box<dyn Handle> {
let (handle, _) = backend
.open(&name.parse().unwrap(), OpenOptions::default())
.await
.unwrap();
handle
}
pub(super) async fn put_with(client: &S3Client, key: &str, body: &[u8], pairs: &[(&str, &str)]) {
client
.put_object(
key,
Bytes::copy_from_slice(body),
&user_metadata(pairs),
foreground(),
)
.await
.unwrap();
}
pub(super) async fn put_all(client: &S3Client, keys: Vec<String>) {
let mut running = JoinSet::new();
for key in keys {
if running.len() >= 16 {
running.join_next().await.unwrap().unwrap();
}
let client = client.clone();
running.spawn(async move {
put_with(&client, &key, key.as_bytes(), &[]).await;
});
}
while let Some(done) = running.join_next().await {
done.unwrap();
}
}
pub(super) struct Rig {
pub(super) bucket: TestBucket,
pub(super) proxy: Proxy,
pub(super) backend: S3Backend,
pub(super) smb: Smb,
pub(super) cache: Option<tempfile::TempDir>,
}
impl Rig {
pub(super) async fn new() -> Self {
Self::with(|builder| builder).await
}
pub(super) async fn with(configure: impl FnOnce(S3BackendBuilder) -> S3BackendBuilder) -> Self {
let bucket = TestBucket::new().await;
let proxy = Proxy::start(bucket.gateway.addr).await;
let backend = configure(builder(&bucket, proxy.addr))
.build()
.await
.unwrap();
let smb = Smb::new(backend.clone()).await;
Rig {
bucket,
proxy,
backend,
smb,
cache: None,
}
}
pub(super) async fn writable() -> Self {
Self::writable_with(|builder| builder).await
}
pub(super) async fn writable_with(
configure: impl FnOnce(S3BackendBuilder) -> S3BackendBuilder,
) -> Self {
let bucket = TestBucket::new().await;
let proxy = Proxy::start(bucket.gateway.addr).await;
Rig::start(bucket, proxy, tempfile::tempdir().unwrap(), configure).await
}
pub(super) fn cache_dir(&self) -> &Path {
self.cache.as_ref().expect("a writable rig").path()
}
pub(super) async fn restart(self) -> Self {
self.restart_with(|builder| builder).await
}
pub(super) async fn restart_with(
self,
configure: impl FnOnce(S3BackendBuilder) -> S3BackendBuilder,
) -> Self {
let (bucket, proxy, cache) = self.stop().await;
Rig::start(bucket, proxy, cache, configure).await
}
pub(super) async fn start(
bucket: TestBucket,
proxy: Proxy,
cache: tempfile::TempDir,
configure: impl FnOnce(S3BackendBuilder) -> S3BackendBuilder,
) -> Self {
let backend = configure(writable_builder(&bucket, proxy.addr, cache.path()))
.build()
.await
.unwrap();
let smb = Smb::new(backend.clone()).await;
Rig {
bucket,
proxy,
backend,
smb,
cache: Some(cache),
}
}
pub(super) async fn stop(self) -> (TestBucket, Proxy, tempfile::TempDir) {
self.stop_after(|| {}).await
}
pub(super) async fn stop_after(
self,
released: impl FnOnce(),
) -> (TestBucket, Proxy, tempfile::TempDir) {
let Rig {
bucket,
proxy,
backend,
mut smb,
cache,
} = self;
smb.close_all().await;
drop(smb);
let mut exited = backend.worker_exited();
drop(backend);
released();
tokio::time::timeout(Duration::from_secs(10), exited.wait_for(|exited| *exited))
.await
.expect("the worker ends once the backend is dropped")
.expect("the worker reports its end");
(bucket, proxy, cache.expect("a writable rig"))
}
pub(super) fn store(&self) -> &S3Client {
&self.bucket.client
}
pub(super) fn hide(&self, key: &str) {
hide(&self.proxy, &self.bucket.name, key);
}
}
fn listing_xml(objects: &[(&str, u64)], prefixes: &[&str]) -> String {
let mut xml = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult>\
<Name>b</Name><MaxKeys>1000</MaxKeys><EncodingType>url</EncodingType>\
<IsTruncated>false</IsTruncated>",
);
for (key, size) in objects {
xml.push_str(&format!(
"<Contents><Key>{}</Key><LastModified>2024-01-02T03:04:05.000Z</LastModified>\
<ETag>\"e\"</ETag><Size>{size}</Size></Contents>",
uri_encode(key, true)
));
}
for prefix in prefixes {
xml.push_str(&format!(
"<CommonPrefixes><Prefix>{}</Prefix></CommonPrefixes>",
uri_encode(prefix, true)
));
}
xml.push_str("</ListBucketResult>");
xml
}
pub(super) fn listing_reply(objects: &[(&str, u64)], prefixes: &[&str]) -> Reply {
Reply::new(200, listing_xml(objects, prefixes)).header("content-type", "application/xml")
}
pub(super) fn answer_listing(proxy: &Proxy, prefix: &str, reply: Reply) {
let wanted = (!prefix.is_empty()).then(|| format!("prefix={}", uri_encode(prefix, false)));
proxy.inject(
move |seen| {
seen.method == "GET"
&& seen.has_param("delimiter")
&& match &wanted {
Some(wanted) => seen.query().split('&').any(|param| param == wanted),
None => !seen.has_param("prefix"),
}
},
Fault::Reply(reply),
usize::MAX,
);
}
pub(super) fn hide(proxy: &Proxy, bucket: &str, key: &str) {
let path = format!("/{bucket}/{}", uri_encode(key, true));
proxy.inject(
move |seen| seen.method == "HEAD" && seen.target == path,
Fault::Reply(Reply::new(404, "")),
usize::MAX,
);
let prefix = format!("prefix={}", uri_encode(&format!("{key}/"), false));
proxy.inject(
move |seen| {
seen.method == "GET"
&& seen.has_param("max-keys")
&& seen.query().split('&').any(|param| param == prefix)
},
Fault::Reply(listing_reply(&[], &[])),
usize::MAX,
);
}
pub(super) fn heads(proxy: &Proxy) -> usize {
proxy.count(|seen| seen.method == "HEAD")
}
pub(super) fn listings(proxy: &Proxy) -> usize {
proxy.count(|seen| seen.method == "GET" && seen.has_param("delimiter"))
}
pub(super) fn is_data_read(seen: &Seen) -> bool {
seen.method == "GET" && !seen.has_param("list-type")
}
pub(super) fn reads(proxy: &Proxy) -> usize {
proxy.count(is_data_read)
}
pub(super) fn requests(proxy: &Proxy) -> usize {
proxy.seen().len()
}
pub(super) async fn wait_until(what: &str, ready: impl Fn() -> bool) {
wait_until_async(what, async || ready()).await;
}
pub(super) async fn wait_until_async(what: &str, ready: impl AsyncFn() -> bool) {
for _ in 0..500 {
if ready().await {
return;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
panic!("timed out: {what}");
}
pub(super) fn run_bounded<F: std::future::Future<Output = ()>>(
what: &str,
limit: Duration,
make: impl FnOnce() -> F + Send + 'static,
) {
let (done, finished) = std::sync::mpsc::channel();
let running = std::thread::spawn(move || {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(4)
.enable_all()
.build()
.unwrap()
.block_on(make());
let _ = done.send(());
});
match finished.recv_timeout(limit) {
Ok(()) => running.join().unwrap(),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => panic!("{what}"),
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
std::panic::resume_unwind(running.join().expect_err("the scenario panicked"));
}
}
}
pub(super) async fn wait_for_requests(
proxy: &Proxy,
count: usize,
matches: impl Fn(&Seen) -> bool,
) {
wait_until(
&format!("the proxy saw fewer than {count} requests"),
|| proxy.count(&matches) >= count,
)
.await;
}
#[derive(Debug, Clone)]
pub(super) struct Opened {
pub(super) id: FileId,
pub(super) action: u32,
pub(super) size: u64,
pub(super) allocation: u64,
pub(super) attributes: u32,
pub(super) times: [u64; 4],
}
#[derive(Debug, Clone)]
pub(super) struct Listed {
pub(super) name: String,
pub(super) size: u64,
pub(super) attributes: u32,
pub(super) times: [u64; 4],
}
impl Listed {
pub(super) fn is_dir(&self) -> bool {
self.attributes & FILE_ATTRIBUTE_DIRECTORY != 0
}
}
pub(super) struct Logs {
text: Arc<Mutex<Vec<u8>>>,
_guard: tracing::subscriber::DefaultGuard,
}
impl Logs {
pub(super) fn capture() -> Logs {
#[derive(Clone)]
struct Sink(Arc<Mutex<Vec<u8>>>);
impl std::io::Write for Sink {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let text = Arc::new(Mutex::new(Vec::new()));
let sink = Sink(Arc::clone(&text));
let subscriber = tracing_subscriber::fmt()
.with_writer(move || sink.clone())
.with_ansi(false)
.finish();
Logs {
text,
_guard: tracing::subscriber::set_default(subscriber),
}
}
pub(super) fn text(&self) -> String {
let text = self.text.lock().unwrap_or_else(PoisonError::into_inner);
String::from_utf8_lossy(&text).into_owned()
}
}
pub(super) fn names(listed: &[Listed]) -> Vec<&str> {
let mut names: Vec<&str> = listed.iter().map(|e| e.name.as_str()).collect();
names.sort_unstable();
names
}
#[derive(Clone, Copy)]
pub(super) struct Spec {
pub(super) access: u32,
pub(super) disposition: u32,
pub(super) options: u32,
}
impl Spec {
pub(super) const FILE: Spec = Spec {
access: READ,
disposition: FILE_OPEN,
options: FILE_NON_DIRECTORY_FILE,
};
pub(super) const DIR: Spec = Spec {
access: READ,
disposition: FILE_OPEN,
options: FILE_DIRECTORY_FILE,
};
pub(super) const ANY: Spec = Spec {
access: READ,
disposition: FILE_OPEN,
options: 0,
};
pub(super) fn access(self, access: u32) -> Self {
Spec { access, ..self }
}
pub(super) fn disposition(self, disposition: u32) -> Self {
Spec {
disposition,
..self
}
}
pub(super) fn options(self, options: u32) -> Self {
Spec { options, ..self }
}
fn body(self, name: &str) -> Vec<u8> {
let units = crate::utils::utf16le(name);
let mut padded = units.clone();
padded.resize(padded.len().next_multiple_of(8), 0);
let mut body = Vec::new();
CreateRequest {
structure_size: 57,
security_flags: 0,
requested_oplock_level: 0,
impersonation_level: 2,
smb_create_flags: 0,
reserved: 0,
desired_access: self.access,
file_attributes: 0,
share_access: 0x7,
create_disposition: self.disposition,
create_options: self.options,
name_offset: 0x78,
name_length: units.len() as u16,
create_contexts_offset: 0,
create_contexts_length: 0,
name: padded,
create_contexts: Vec::new(),
}
.write_to(&mut body)
.expect("create");
body
}
}
pub(super) struct Smb {
peer: Peer,
state: Arc<ServerState>,
opened: Vec<FileId>,
saves: tokio::sync::watch::Receiver<usize>,
}
impl Smb {
pub(super) async fn new(backend: S3Backend) -> Self {
let saves = backend.saves();
let server = SmbServer::builder()
.listen("127.0.0.1:0".parse().unwrap())
.share(Share::new("home", backend).public())
.build()
.expect("build");
let state = server.state();
Smb {
peer: Peer::new(&state, false).await,
state,
opened: Vec::new(),
saves,
}
}
pub(super) async fn close_all(&mut self) {
for id in std::mem::take(&mut self.opened) {
self.close(id).await;
}
}
async fn send(&mut self, command: Command, body: &[u8]) -> (u32, Vec<u8>) {
let (_, reply) = self.peer.request(&self.state, command, body).await;
(status(&reply), reply[SMB2_HEADER_LEN..].to_vec())
}
pub(super) async fn create(&mut self, name: &str, spec: Spec) -> Result<Opened, u32> {
match self.send(Command::Create, &spec.body(name)).await {
(0, body) => {
let reply = CreateResponse::parse(&body).expect("create response");
self.opened.push(reply.file_id);
Ok(Opened {
id: reply.file_id,
action: reply.create_action,
size: reply.end_of_file,
allocation: reply.allocation_size,
attributes: reply.file_attributes,
times: [
reply.creation_time,
reply.last_access_time,
reply.last_write_time,
reply.change_time,
],
})
}
(status, _) => Err(status),
}
}
pub(super) async fn open(&mut self, name: &str) -> Result<Opened, u32> {
self.create(name, Spec::FILE).await
}
pub(super) async fn open_dir(&mut self, name: &str) -> Result<Opened, u32> {
self.create(name, Spec::DIR).await
}
pub(super) async fn close(&mut self, id: FileId) {
self.close_now(id).await;
self.wait_for_saves().await;
}
pub(super) async fn wait_for_saves(&mut self) {
let _ = self.saves.wait_for(|count| *count == 0).await;
}
pub(super) async fn close_now(&mut self, id: FileId) {
let mut body = Vec::new();
CloseRequest {
structure_size: 24,
flags: 0,
reserved: 0,
file_id: id,
}
.write_to(&mut body)
.expect("close");
assert_eq!(self.send(Command::Close, &body).await.0, 0);
self.opened.retain(|open| *open != id);
}
pub(super) async fn read(&mut self, id: FileId, offset: u64, len: u32) -> Result<Vec<u8>, u32> {
let mut body = Vec::new();
ReadRequest {
structure_size: 49,
padding: ReadResponse::STANDARD_DATA_OFFSET,
flags: 0,
length: len,
offset,
file_id: id,
minimum_count: 0,
channel: 0,
remaining_bytes: 0,
read_channel_info_offset: 0,
read_channel_info_length: 0,
buffer: vec![0],
}
.write_to(&mut body)
.expect("read");
match self.send(Command::Read, &body).await {
(0, body) => Ok(ReadResponse::parse(&body).expect("read response").data),
(status, _) => Err(status),
}
}
pub(super) async fn contents(&mut self, name: &str, piece: u32) -> Vec<u8> {
let file = self.open(name).await.expect("open");
let mut data = Vec::new();
loop {
match self.read(file.id, data.len() as u64, piece).await {
Ok(chunk) => data.extend_from_slice(&chunk),
Err(status) if status == ntstatus::STATUS_END_OF_FILE => break,
Err(status) => panic!("read {name}: {status:#x}"),
}
}
self.close(file.id).await;
data
}
pub(super) async fn list(&mut self, dir: FileId, pattern: &str) -> Vec<Listed> {
match self.try_list(dir, pattern).await {
Ok(listed) => listed,
Err(status) => panic!("query directory: {status:#x}"),
}
}
pub(super) async fn try_list(
&mut self,
dir: FileId,
pattern: &str,
) -> Result<Vec<Listed>, u32> {
let mut out = Vec::new();
let mut flags = QueryDirectoryRequest::FLAG_RESTART_SCANS;
loop {
let units = crate::utils::utf16le(pattern);
let mut body = Vec::new();
QueryDirectoryRequest {
structure_size: 33,
file_information_class: 0x25,
flags,
file_index: 0,
file_id: dir,
file_name_offset: 0x60,
file_name_length: units.len() as u16,
output_buffer_length: 65536,
file_name: units,
}
.write_to(&mut body)
.expect("query directory");
flags = 0;
let (code, reply) = self.send(Command::QueryDirectory, &body).await;
if code == ntstatus::STATUS_NO_MORE_FILES || code == ntstatus::STATUS_NO_SUCH_FILE {
return Ok(out);
}
if code != 0 {
return Err(code);
}
let buffer = QueryDirectoryResponse::parse(&reply)
.expect("listing")
.buffer;
let mut at = 0;
loop {
let u32_at = |at: usize| u32::from_le_bytes(buffer[at..at + 4].try_into().unwrap());
let u64_at = |at: usize| u64::from_le_bytes(buffer[at..at + 8].try_into().unwrap());
let name_len = u32_at(at + 60) as usize;
let units: Vec<u16> = buffer[at + 104..at + 104 + name_len]
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect();
let name = String::from_utf16(&units).unwrap();
if name != "." && name != ".." {
out.push(Listed {
name,
size: u64_at(at + 40),
attributes: u32_at(at + 56),
times: [
u64_at(at + 8),
u64_at(at + 16),
u64_at(at + 24),
u64_at(at + 32),
],
});
}
let next = u32_at(at) as usize;
if next == 0 {
break;
}
at += next;
}
}
}
pub(super) async fn write(&mut self, id: FileId, offset: u64, data: &[u8]) -> Result<u32, u32> {
let mut body = Vec::new();
WriteRequest {
structure_size: 49,
data_offset: WriteRequest::STANDARD_DATA_OFFSET,
length: data.len() as u32,
offset,
file_id: id,
channel: 0,
remaining_bytes: 0,
write_channel_info_offset: 0,
write_channel_info_length: 0,
flags: 0,
data: data.to_vec(),
}
.write_to(&mut body)
.expect("write");
match self.send(Command::Write, &body).await {
(0, body) => Ok(WriteResponse::parse(&body).expect("write response").count),
(status, _) => Err(status),
}
}
pub(super) async fn flush(&mut self, id: FileId) -> u32 {
let mut body = Vec::new();
FlushRequest::new(id.persistent, id.volatile)
.write_to(&mut body)
.expect("flush");
self.send(Command::Flush, &body).await.0
}
pub(super) async fn set_size(&mut self, id: FileId, len: u64) -> u32 {
self.set_info(id, 20, len.to_le_bytes().to_vec()).await
}
pub(super) async fn set_basic(&mut self, id: FileId, times: [i64; 4], attributes: u32) -> u32 {
let mut buffer = Vec::new();
for time in times {
buffer.extend_from_slice(&time.to_le_bytes());
}
buffer.extend_from_slice(&attributes.to_le_bytes());
buffer.extend_from_slice(&[0; 4]);
self.set_info(id, 4, buffer).await
}
pub(super) async fn set_write_time(&mut self, id: FileId, secs: u64) -> u32 {
let stamp = filetime(secs, 0) as i64;
self.set_basic(id, [0, 0, stamp, 0], 0).await
}
pub(super) async fn rename(&mut self, id: FileId, to: &str) -> u32 {
self.rename_with(id, to, false).await
}
pub(super) async fn rename_with(&mut self, id: FileId, to: &str, replace: bool) -> u32 {
let target = crate::utils::utf16le(to);
let mut buffer = vec![0u8; 16];
buffer[0] = u8::from(replace);
buffer.extend_from_slice(&(target.len() as u32).to_le_bytes());
buffer.extend_from_slice(&target);
self.set_info(id, 10, buffer).await
}
pub(super) async fn set_delete(&mut self, id: FileId) -> u32 {
self.set_info(id, 13, vec![1]).await
}
async fn set_info(&mut self, id: FileId, class: u8, buffer: Vec<u8>) -> u32 {
let mut body = Vec::new();
SetInfoRequest {
structure_size: 33,
info_type: 1,
file_information_class: class,
buffer_length: buffer.len() as u32,
buffer_offset: 0x60,
reserved: 0,
additional_information: 0,
file_id: id,
buffer,
}
.write_to(&mut body)
.expect("set info");
self.send(Command::SetInfo, &body).await.0
}
}