Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed May 8, 2024
1 parent f0743fd commit 1d48e43
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
178 changes: 178 additions & 0 deletions stackslib/src/net/api/tests/getclaritymarfvalue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright (C) 2013-2020 Blockstack PBC, a public benefit corporation
// Copyright (C) 2020-2023 Stacks Open Internet Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use clarity::vm::types::{QualifiedContractIdentifier, StacksAddressExtensions};
use clarity::vm::{ClarityName, ContractName};
use stacks_common::codec::StacksMessageCodec;
use stacks_common::types::chainstate::StacksAddress;
use stacks_common::types::net::PeerHost;
use stacks_common::types::Address;

use super::test_rpc;
use crate::net::api::*;
use crate::net::connection::ConnectionOptions;
use crate::net::httpcore::{
HttpPreambleExtensions, HttpRequestContentsExtensions, RPCRequestHandler, StacksHttp,
StacksHttpRequest,
};
use crate::net::{ProtocolFamily, TipRequest};

#[test]
fn test_try_parse_request() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 33333);
let mut http = StacksHttp::new(addr.clone(), &ConnectionOptions::default());

let request = StacksHttpRequest::new_getclaritymarfvalue(
addr.into(),
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R::hello-world::bar".to_string(),
TipRequest::SpecificTip(StacksBlockId([0x22; 32])),
true,
);
assert_eq!(
request.contents().tip_request(),
TipRequest::SpecificTip(StacksBlockId([0x22; 32]))
);
assert_eq!(request.contents().get_with_proof(), true);

let bytes = request.try_serialize().unwrap();

println!("Request:\n{}\n", std::str::from_utf8(&bytes).unwrap());

let (parsed_preamble, offset) = http.read_preamble(&bytes).unwrap();
let mut handler = getclaritymarfvalue::RPCGetClarityMarfValueRequestHandler::new();
let mut parsed_request = http
.handle_try_parse_request(
&mut handler,
&parsed_preamble.expect_request(),
&bytes[offset..],
)
.unwrap();

// parsed request consumes headers that would not be in a constructed reqeuest
parsed_request.clear_headers();
let (preamble, contents) = parsed_request.destruct();

// consumed path args
assert_eq!(
handler.clarity_marf_key,
Some("vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R::hello-world::bar".to_string())
);

assert_eq!(&preamble, request.preamble());

handler.restart();
assert!(handler.clarity_marf_key.is_none());
}

#[test]
fn test_try_make_response() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 33333);

let mut requests = vec![];

// query existing
let request = StacksHttpRequest::new_getclaritymarfvalue(
addr.into(),
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::bar".to_string(),
TipRequest::UseLatestAnchoredTip,
true,
);
requests.push(request);

// query existing unconfirmed
let request = StacksHttpRequest::new_getclaritymarfvalue(
addr.into(),
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world-unconfirmed::1::bar-unconfirmed"
.to_string(),
TipRequest::UseLatestUnconfirmedTip,
true,
);
requests.push(request);

// query non-existant var
let request = StacksHttpRequest::new_getclaritymarfvalue(
addr.into(),
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::does-not-exist".to_string(),
TipRequest::UseLatestAnchoredTip,
true,
);
requests.push(request);

// query non-existant contract
let request = StacksHttpRequest::new_getclaritymarfvalue(
addr.into(),
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.does-not-exist::1::bar".to_string(),
TipRequest::UseLatestAnchoredTip,
true,
);
requests.push(request);

let mut responses = test_rpc(function_name!(), requests);

// latest data
let response = responses.remove(0);
debug!(
"Response:\n{}\n",
std::str::from_utf8(&response.try_serialize().unwrap()).unwrap()
);

assert_eq!(
response.preamble().get_canonical_stacks_tip_height(),
Some(1)
);

let resp = response.decode_data_var_response().unwrap();
assert_eq!(resp.data, "0x0000000000000000000000000000000000");
assert!(resp.marf_proof.is_some());

// unconfirmed data
let response = responses.remove(0);
debug!(
"Response:\n{}\n",
std::str::from_utf8(&response.try_serialize().unwrap()).unwrap()
);

assert_eq!(
response.preamble().get_canonical_stacks_tip_height(),
Some(1)
);

let resp = response.decode_data_var_response().unwrap();
assert_eq!(resp.data, "0x0100000000000000000000000000000001");
assert!(resp.marf_proof.is_some());

// no such var
let response = responses.remove(0);
debug!(
"Response:\n{}\n",
std::str::from_utf8(&response.try_serialize().unwrap()).unwrap()
);

let (preamble, body) = response.destruct();
assert_eq!(preamble.status_code, 404);

// no such contract
let response = responses.remove(0);
debug!(
"Response:\n{}\n",
std::str::from_utf8(&response.try_serialize().unwrap()).unwrap()
);

let (preamble, body) = response.destruct();
assert_eq!(preamble.status_code, 404);
}
1 change: 1 addition & 0 deletions stackslib/src/net/api/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod getattachment;
mod getattachmentsinv;
mod getblock;
mod getblock_v3;
mod getclaritymarfvalue;
mod getconstantval;
mod getcontractabi;
mod getcontractsrc;
Expand Down

0 comments on commit 1d48e43

Please sign in to comment.