Skip to content

Commit

Permalink
fix: update project to not use external sources
Browse files Browse the repository at this point in the history
  • Loading branch information
aaneto committed Jul 13, 2024
1 parent fc61158 commit 014ad45
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 75 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ To run all tests a few dependencies are needed, you need to run the docker image

### Building the image
```bash
cd sample-server
docker build . -t ftp-server
docker build . -t ftp-server -f sample-server/Dockerfile
```

### Running the image on the background

```bash
docker run -d -p 20:20 -p 21:21 -p 2558:2558 -p 2559:2559 ftp-server
docker run -p 20:20 -p 21:21 -p 2558:2558 -p 2559:2559 ftp-server
```

After that, you can run ```cargo test``` as you normally would.
1 change: 1 addition & 0 deletions res/pub/example/data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions res/pub/example/file1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions res/pub/example/file2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions res/pub/example/file3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
Empty file added res/pub/sample.txt
Empty file.
6 changes: 3 additions & 3 deletions sample-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ FROM python:3.8
WORKDIR /app
RUN mkdir res

COPY cat.png res/cat.png
COPY . .
COPY ./res res/
COPY ./sample-server .

RUN pip install --no-cache-dir -r requirements.txt

Expand All @@ -13,4 +13,4 @@ EXPOSE 2558 2559

EXPOSE 20 21

CMD [ "python", "server.py" ]
CMD [ "python", "server.py" ]
2 changes: 1 addition & 1 deletion sample-server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
handler.passive_ports = range(2558, 2560)

server = FTPServer(("0.0.0.0", 21), handler)
server.serve_forever()
server.serve_forever()
20 changes: 16 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,14 @@ impl Client {
///
/// The help command can also be used with an argument to see detailed
/// information about a single command, this behaviour is not implemented.
pub async fn help(&mut self) -> Result<(), crate::error::Error> {
pub async fn help(&mut self, command: Option<String>) -> Result<(), crate::error::Error> {
let command = match command {
Some(command_string) => format!(" {command_string}"),
None => "".to_string(),
};

self.write_command_expecting(
"HELP",
&format!("HELP{command}"),
vec![StatusCodeKind::SystemStatus, StatusCodeKind::HelpMessage],
)
.await?;
Expand Down Expand Up @@ -430,10 +435,17 @@ impl Client {
/// the HELP SITE command.
///
/// Extracted from RFC959.
pub async fn site_parameters(&mut self) -> Result<String, crate::error::Error> {
pub async fn site_parameters(
&mut self,
argument: Option<String>,
) -> Result<String, crate::error::Error> {
let argument = match argument {
Some(argument_string) => format!(" {argument_string}"),
None => "".to_string(),
};
let response = self
.write_command_expecting(
"SITE",
&format!("SITE{argument}"),
vec![StatusCodeKind::Ok, StatusCodeKind::FeatureNotImplemented],
)
.await?;
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//! use ftp_client::{error::Error, sync::Client};
//!
//! fn main() -> Result<(), Error> {
//! let mut client = Client::connect("test.rebex.net", "demo", "password")?;
//! let server_name = std::env::var("SERVER_HOSTNAME").expect("SERVER_HOSTNAME is not set.");
//! let mut client = Client::connect(&server_name, "user", "user")?;
//! let names = client.list_names("/")?;
//! println!("Listing names: ");
//! for name in names {
Expand Down
12 changes: 8 additions & 4 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl Client {
///
/// The help command can also be used with an argument to see detailed
/// information about a single command, this behaviour is not implemented.
pub fn help(&mut self) -> Result<(), crate::error::Error> {
self.runtime.block_on(self.inner_client.help())
pub fn help(&mut self, command: Option<String>) -> Result<(), crate::error::Error> {
self.runtime.block_on(self.inner_client.help(command))
}

/// This command should not do anything other than receiving
Expand Down Expand Up @@ -181,8 +181,12 @@ impl Client {
/// the HELP SITE command.
///
/// Extracted from RFC959.
pub fn site_parameters(&mut self) -> Result<String, crate::error::Error> {
self.runtime.block_on(self.inner_client.site_parameters())
pub fn site_parameters(
&mut self,
argument: Option<String>,
) -> Result<String, crate::error::Error> {
self.runtime
.block_on(self.inner_client.site_parameters(argument))
}

/// Get the type of operating system on the server.
Expand Down
97 changes: 38 additions & 59 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests for the FTP client crate, all the tests
//! are made with real sample FTP servers.
//!
//! Tests that start with external_ are run with
//! Tests that start with test_ are run with
//! external FTP servers, the others are run
//! with a local dockerize server that you should start.
use ftp_client::error::Error as FtpError;
Expand All @@ -11,19 +11,18 @@ use std::io::Read;
use std::sync::Mutex;

#[test]
fn external_name_listing() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;

fn test_name_listing() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
assert_eq!(
vec!["/pub".to_string(), "/readme.txt".to_string()],
client.list_names("/")?
vec!["example".to_string(), "sample.txt".to_string(),],
client.list_names("/pub/")?
);
Ok(())
}

#[test]
fn external_pwd() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
fn test_pwd() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
client.cwd("/pub")?;
let dir = client.pwd()?;
assert!(dir.contains("/pub"));
Expand All @@ -32,39 +31,40 @@ fn external_pwd() -> Result<(), FtpError> {
}

#[test]
fn external_site() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
client.site_parameters()?;
fn test_site() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
client.site_parameters(Some("HELP".to_string()))?;

Ok(())
}

#[test]
fn external_file_retrieval() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
let readme_file = client.retrieve_file("/readme.txt")?;
fn test_file_retrieval() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
let cat_file = client.retrieve_file("/cat.png")?;
// Taken previously and unlikely to change
let file_size = 403;
let file_size = 29712;

assert_eq!(readme_file.len(), file_size);
assert_eq!(cat_file.len(), file_size);
Ok(())
}

#[test]
fn external_cwd() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
fn test_cwd() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
client.cwd("/pub/example")?;

// The /pub/example dir has many files
let names = client.list_names("")?;
dbg!(names.clone());
assert!(names.len() > 3);

Ok(())
}

#[test]
fn external_cdup() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
fn test_cdup() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
let initial_names = client.list_names("")?;
client.cwd("/pub/example")?;

Expand All @@ -79,74 +79,53 @@ fn external_cdup() -> Result<(), FtpError> {
}

#[test]
fn external_logout() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
fn test_logout() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
client.logout()
}

#[test]
fn external_noop() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
fn test_noop() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
client.noop()
}

#[test]
fn external_help() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
client.help()
fn test_help() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
client.help(Some("LIST".to_string()))
}

#[test]
fn external_store() -> Result<(), FtpError> {
let mut client = SyncClient::connect(
"speedtest4.tele2.net",
"anonymous",
"anonymous@anonymous.com",
)?;
fn test_store() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
let file_data = b"Some data for you";
let file_name = "/upload/readyou.txt";
let file_name = "/pub/example/readyou.txt";

client.store(file_name, file_data)
}

#[test]
fn external_store_unique() -> Result<(), FtpError> {
let mut client = SyncClient::connect(
"speedtest4.tele2.net",
"anonymous",
"anonymous@anonymous.com",
)?;
client.cwd("/upload/")?;
fn test_store_unique() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;

client.cwd("/pub/example/")?;
let file_data = b"Some data for you";
client.store_unique(file_data)?;

Ok(())
}

#[test]
fn external_system() -> Result<(), FtpError> {
let mut client = SyncClient::connect("test.rebex.net", "demo", "password")?;
fn test_system() -> Result<(), FtpError> {
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
// Should be Windows_NT but we don't need to check that..
// since we don't want to break tests if the server changes OS
let _system_name = client.system()?;

Ok(())
}

#[test]
#[ignore]
fn external_ipv6() -> Result<(), FtpError> {
let mut client = SyncClient::connect(
"speedtest6.tele2.net",
"anonymous",
"anonymous@anonymous.com",
)?;

let data = b"DATA";
let file_path = "/upload/readyou.txt";
client.store(file_path, data)
}

#[test]
fn append() -> Result<(), FtpError> {
lock_server();
Expand All @@ -163,9 +142,9 @@ fn rename_file() -> Result<(), FtpError> {
lock_server();
let mut client = SyncClient::connect(&get_local_server_hostname(), "user", "user")?;
if !client.list_names("/")?.contains(&"testfile".to_string()) {
client.store("testfile", b"DATA")?;
client.store("/testfile", b"DATA")?;
}
client.rename_file("testfile", "testfile.txt")?;
client.rename_file("/testfile", "/testfile.txt")?;

Ok(())
}
Expand Down

0 comments on commit 014ad45

Please sign in to comment.