Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Fixed zmodem protocol tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Sep 30, 2023
1 parent d750f21 commit d2872ea
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 178 deletions.
5 changes: 2 additions & 3 deletions src/protocol/xymodem/sy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl Sy {
errors: 0,
bytes_send: 0,
block_number: match configuration.variant {
XYModemVariant::YModem | XYModemVariant::YModemG => 1,
_ => 0,
XYModemVariant::YModem | XYModemVariant::YModemG => 0,
_ => 1,
},
cur_file: 0,
transfer_stopped: false,
Expand All @@ -82,7 +82,6 @@ impl Sy {
transfer_info.check_size = self.configuration.get_check_and_size();
transfer_info.update_bps();
}
println!("send state: {:?} {:?}", self.send_state, self.configuration.variant);

match self.send_state {
SendState::None => {}
Expand Down
40 changes: 23 additions & 17 deletions src/protocol/xymodem/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
use crate::protocol::Protocol;

#[cfg(test)]
pub fn test_sender(test_connection: &mut dyn crate::ui::connect::DataConnection, files: Vec<crate::protocol::FileDescriptor>, send: &mut dyn Protocol) {
pub fn test_sender(test_connection: &mut crate::ui::connect::TestConnection, files: Vec<crate::protocol::FileDescriptor>, send: &mut dyn Protocol) {
use crate::protocol::{TestStorageHandler, TransferState};
use std::sync::{Arc, Mutex};

test_connection.is_sender = true;
let send_transfer_state = Arc::new(Mutex::new(TransferState::default()));
send.initiate_send(test_connection, files, &mut send_transfer_state.lock().unwrap())
.expect("error.");
Expand All @@ -17,10 +17,11 @@ pub fn test_sender(test_connection: &mut dyn crate::ui::connect::DataConnection,
}

#[cfg(test)]
pub fn test_receiver(test_connection: &mut dyn crate::ui::connect::DataConnection, receiver: &mut dyn Protocol) -> crate::protocol::TestStorageHandler {
pub fn test_receiver(test_connection: &mut crate::ui::connect::TestConnection, receiver: &mut dyn Protocol) -> crate::protocol::TestStorageHandler {
use crate::protocol::{TestStorageHandler, TransferState};
use std::sync::{Arc, Mutex};

test_connection.is_sender = false;
let send_transfer_state = Arc::new(Mutex::new(TransferState::default()));
receiver
.initiate_recv(test_connection, &mut send_transfer_state.lock().unwrap())
Expand Down Expand Up @@ -58,7 +59,7 @@ mod xy_modem_tests {

#[test]
fn test_xmodem_128block_sender() {
let mut test_connection: TestConnection = TestConnection::new();
let mut test_connection = TestConnection::new(false);

let mut send = crate::protocol::XYmodem::new(XYModemVariant::XModem);

Expand All @@ -80,12 +81,12 @@ mod xy_modem_tests {
result.push(0xAA); // CHECKSUM
result.push(EOT);

assert_eq!(result, test_connection.read_buffer());
assert_eq!(result, test_connection.read_receive_buffer());
}

#[test]
fn test_xmodem_128block_case2_sender() {
let mut test_connection: TestConnection = TestConnection::new();
let mut test_connection = TestConnection::new(false);
let mut send = crate::protocol::XYmodem::new(XYModemVariant::XModem);
let mut data = vec![1u8, 2, 5, 10];
let files = vec![FileDescriptor::create_test("foo.bar".to_string(), data.clone())];
Expand All @@ -102,12 +103,12 @@ mod xy_modem_tests {
result.push(207); // CHECKSUM
result.push(EOT);

assert_eq!(result, test_connection.read_buffer());
assert_eq!(result, test_connection.read_receive_buffer());
}

#[test]
fn test_xmodem_1kblock_sender() {
let mut test_connection: TestConnection = TestConnection::new();
let mut test_connection = TestConnection::new(false);
let mut send = crate::protocol::XYmodem::new(XYModemVariant::XModem1k);
let mut data = vec![5; 900];
let files = vec![FileDescriptor::create_test("foo.bar".to_string(), data.clone())];
Expand All @@ -128,12 +129,12 @@ mod xy_modem_tests {
result.push(85); // CHECKSUM
result.push(EOT);

assert_eq!(result, test_connection.read_buffer());
assert_eq!(result, test_connection.read_receive_buffer());
}

#[test]
fn test_xmodem_128block_receiver() {
let mut test_connection: TestConnection = TestConnection::new();
let mut test_connection = TestConnection::new(true);
let mut recv = crate::protocol::XYmodem::new(XYModemVariant::XModem);
let data = vec![1u8, 2, 5, 10];

Expand All @@ -154,7 +155,7 @@ mod xy_modem_tests {

#[test]
fn test_xmodem_1kblock_receiver() {
let mut test_connection: TestConnection = TestConnection::new();
let mut test_connection = TestConnection::new(true);
let mut recv = crate::protocol::XYmodem::new(XYModemVariant::XModem1k);
let data = vec![5; 900];

Expand All @@ -176,7 +177,7 @@ mod xy_modem_tests {

#[test]
fn test_ymodem_sender() {
let mut test_connection: TestConnection = TestConnection::new();
let mut test_connection = TestConnection::new(false);

let mut send = crate::protocol::XYmodem::new(XYModemVariant::YModem);

Expand Down Expand Up @@ -205,29 +206,34 @@ mod xy_modem_tests {
result.extend_from_slice(&[150, 207]); // CHECKSUM
result.push(EOT);

assert_eq!(result, test_connection.read_buffer());
assert_eq!(result, test_connection.read_receive_buffer());
}

#[test]
fn test_ymodem_receiver() {
let mut test_connection: TestConnection = TestConnection::new();
let mut test_connection = TestConnection::new(true);
let mut recv = crate::protocol::XYmodem::new(XYModemVariant::YModem);
let data = vec![1u8, 2, 5, 10];
let mut result = vec![SOH, 0x00, 0xFF];

result.extend_from_slice(b"foo.bar");
result.extend_from_slice(&[0, b'4']); // length

result.extend_from_slice(vec![0; 128 - "foo.bar".len() - 2].as_slice());
result.extend_from_slice(&[108, 107]); // CHECKSUM

result.extend_from_slice(&[SOH, 0x01, 0xFE]);

let mut cloned_data = data.clone();
cloned_data.resize(128, 0x1A);
result.extend_from_slice(&cloned_data);
result.extend_from_slice(&[150, 207]); // CHECKSUM
result.push(EOT);
result.push(EOT); // -> NACK
result.push(EOT); // -> ACK

// No next file:
result.extend_from_slice(&[SOH, 0x00, 0xFF]);
result.extend_from_slice(vec![0; 128].as_slice());
result.extend_from_slice(&[0, 0]);

test_connection.send(result).unwrap();

let storage_handler = test_receiver(&mut test_connection, &mut recv);
Expand Down
14 changes: 13 additions & 1 deletion src/protocol/zmodem/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,30 @@ impl Header {
pub fn f0(&self) -> u8 {
self.data[3]
}
pub fn p3(&self) -> u8 {
self.data[3]
}

pub fn f1(&self) -> u8 {
self.data[2]
}
pub fn p2(&self) -> u8 {
self.data[2]
}

pub fn f2(&self) -> u8 {
self.data[1]
}
pub fn p1(&self) -> u8 {
self.data[1]
}

pub fn f3(&self) -> u8 {
self.data[0]
}
pub fn p0(&self) -> u8 {
self.data[0]
}

pub fn number(&self) -> u32 {
u32::from_le_bytes(self.data)
Expand Down Expand Up @@ -178,7 +190,7 @@ impl Header {
}

pub fn write(&self, com: &mut dyn DataConnection, header_type: HeaderType, escape_ctrl_chars: bool) -> TerminalResult<usize> {
// println!("send header:{:?} - {:?}", header_type, self);
println!("send header:{:?} - {:?}", header_type, self);
com.send(self.build(header_type, escape_ctrl_chars))?;
Ok(12)
}
Expand Down
19 changes: 13 additions & 6 deletions src/protocol/zmodem/sz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct Sz {
retries: usize,
can_count: usize,
receiver_capabilities: u8,

nonstop: bool,
}

impl Sz {
Expand All @@ -50,6 +52,7 @@ impl Sz {
receiver_capabilities: 0,
can_count: 0,
package_len: block_length,
nonstop: true,
}
}

Expand Down Expand Up @@ -125,7 +128,7 @@ impl Sz {
transfer_info.check_size = format!("Crc32/{}", self.package_len);
transfer_info.update_bps();
}
// println!("sz state: {:?}", self.state );
println!("sz state: {:?}", self.state);
match self.state {
SendState::Await => {
self.read_next_header(com)?;
Expand Down Expand Up @@ -156,15 +159,14 @@ impl Sz {
}
let old_pos = self.cur_file_pos;
let end_pos = min(self.data.len(), self.cur_file_pos + self.package_len);
let nonstop = !self.can_receive_data_during_io();

println!("nonstop:{}", self.nonstop);
let crc_byte = if self.cur_file_pos + self.package_len < self.data.len() {
if nonstop {
if self.nonstop {
ZCRCG
} else {
ZCRCQ
}
} else if nonstop {
} else if self.nonstop {
ZCRCE
} else {
ZCRCW
Expand All @@ -180,7 +182,7 @@ impl Sz {
self.state = SendState::Await;
}
com.send(p)?;
if !nonstop {
if !self.nonstop {
let ack = Header::read(com, &mut self.can_count)?;
if let Some(header) = ack {
// println!("got header after data package: {header}",);
Expand Down Expand Up @@ -249,6 +251,11 @@ impl Sz {
}
self.cur_file_pos = 0;
self.receiver_capabilities = res.f0();
let block_size = res.p0() as usize + ((res.p1() as usize) << 8);
self.nonstop = block_size == 0;
if block_size != 0 {
self.package_len = block_size;
}
/*
if self._can_decrypt() {
println!("receiver can decrypt");
Expand Down
Loading

0 comments on commit d2872ea

Please sign in to comment.