From ba98168b71919df8a48338f89537e8a89a11e021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <bkchr@users.noreply.github.com> Date: Thu, 26 Sep 2019 23:02:30 +0200 Subject: [PATCH] Make `TestOffchainExt` panic on unknown requests (#3710) * Make `TestOffchainExt` panic on unknown requests * Fix test --- substrate/core/executor/src/wasm_executor.rs | 2 +- substrate/core/offchain/src/testing.rs | 22 +++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/substrate/core/executor/src/wasm_executor.rs b/substrate/core/executor/src/wasm_executor.rs index bfd33f0f16d..afdbe576402 100644 --- a/substrate/core/executor/src/wasm_executor.rs +++ b/substrate/core/executor/src/wasm_executor.rs @@ -1842,7 +1842,7 @@ mod tests { body: vec![1, 2, 3, 4], headers: vec![("X-Auth".to_owned(), "test".to_owned())], sent: true, - response: vec![1, 2, 3], + response: Some(vec![1, 2, 3]), response_headers: vec![("X-Auth".to_owned(), "hello".to_owned())], ..Default::default() }, diff --git a/substrate/core/offchain/src/testing.rs b/substrate/core/offchain/src/testing.rs index 496feb4b63b..e1cc7f71a38 100644 --- a/substrate/core/offchain/src/testing.rs +++ b/substrate/core/offchain/src/testing.rs @@ -48,7 +48,7 @@ pub struct PendingRequest { /// Has the request been sent already. pub sent: bool, /// Response body - pub response: Vec<u8>, + pub response: Option<Vec<u8>>, /// Number of bytes already read from the response body. pub read: usize, /// Response headers @@ -89,7 +89,7 @@ impl State { *req, expected, ); - req.response = response.into(); + req.response = Some(response.into()); req.response_headers = response_headers.into_iter().collect(); } } @@ -97,7 +97,7 @@ impl State { fn fulfill_expected(&mut self, id: u16) { if let Some(mut req) = self.expected_requests.remove(&RequestId(id)) { - let response = std::mem::replace(&mut req.response, vec![]); + let response = req.response.take().expect("Response checked while added."); let headers = std::mem::replace(&mut req.response_headers, vec![]); self.fulfill_pending_request(id, req, response, headers); } @@ -110,6 +110,9 @@ impl State { /// Expected request has to be fulfilled before this struct is dropped, /// the `response` and `response_headers` fields will be used to return results to the callers. pub fn expect_request(&mut self, id: u16, expected: PendingRequest) { + if expected.response.is_none() { + panic!("Expected request needs to have a response."); + } self.expected_requests.insert(RequestId(id), expected); } } @@ -254,7 +257,8 @@ impl offchain::Externalities for TestOffchainExt { let state = self.0.read(); ids.iter().map(|id| match state.requests.get(id) { - Some(req) if req.response.is_empty() => RequestStatus::DeadlineReached, + Some(req) if req.response.is_none() => + panic!("No `response` provided for request with id: {:?}", id), None => RequestStatus::Invalid, _ => RequestStatus::Finished(200), }).collect() @@ -281,13 +285,17 @@ impl offchain::Externalities for TestOffchainExt { ) -> Result<usize, HttpError> { let mut state = self.0.write(); if let Some(req) = state.requests.get_mut(&request_id) { - if req.read >= req.response.len() { + let response = req.response + .as_mut() + .expect(&format!("No response provided for request: {:?}", request_id)); + + if req.read >= response.len() { // Remove the pending request as per spec. state.requests.remove(&request_id); Ok(0) } else { - let read = std::cmp::min(buffer.len(), req.response[req.read..].len()); - buffer[0..read].copy_from_slice(&req.response[req.read..read]); + let read = std::cmp::min(buffer.len(), response[req.read..].len()); + buffer[0..read].copy_from_slice(&response[req.read..read]); req.read += read; Ok(read) } -- GitLab