Skip to content

Commit

Permalink
Update code to use borsh v1 APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
r-n-o committed Jun 27, 2024
1 parent 89e3535 commit d290dfb
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 53 deletions.
95 changes: 86 additions & 9 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/integration/src/bin/pivot_remote_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
sync::Arc,
};

use borsh::{BorshDeserialize, BorshSerialize};
use borsh::BorshDeserialize;
use integration::PivotRemoteTlsMsg;
use qos_core::{
io::{SocketAddress, TimeVal},
Expand Down Expand Up @@ -86,10 +86,9 @@ impl RequestProcessor for Processor {

let fetched_content =
std::str::from_utf8(&response_bytes).unwrap();
PivotRemoteTlsMsg::RemoteTlsResponse(format!(
borsh::to_vec(&PivotRemoteTlsMsg::RemoteTlsResponse(format!(
"Content fetched successfully: {fetched_content}"
))
.try_to_vec()
)))
.expect("RemoteTlsResponse is valid borsh")
}
PivotRemoteTlsMsg::RemoteTlsResponse(_) => {
Expand Down
72 changes: 40 additions & 32 deletions src/integration/tests/borsh_serialize.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
#[cfg(test)]
mod tests {
use borsh::{BorshSerialize, BorshDeserialize};
use borsh::{BorshDeserialize, BorshSerialize};

#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq)]
struct TestSerializable {
a: u32,
b: String,
c: Vec<u8>,
}
#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq)]
struct TestSerializable {
a: u32,
b: String,
c: Vec<u8>,
}

#[test]
fn test_serializable_to_vec() {
let inst = TestSerializable {
a: 42,
b: "Hello, world!".to_string(),
c: vec![1, 2, 3, 4, 5],
};
#[test]
fn test_serializable_to_vec() {
let inst = TestSerializable {
a: 42,
b: "Hello, world!".to_string(),
c: vec![1, 2, 3, 4, 5],
};

// Expected serialized output
let expected_serialized: Vec<u8> = vec![
42, 0, 0, 0, // a: u32 (little-endian)
13, 0, 0, 0, // Length of the string b (13)
72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, // "Hello, world!" as bytes
5, 0, 0, 0, // Length of the vector c (5)
1, 2, 3, 4, 5 // c: Vec<u8>
];
// Expected serialized output
let expected_serialized: Vec<u8> = vec![
42, 0, 0, 0, // a: u32 (little-endian)
13, 0, 0, 0, // Length of the string b (13)
72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100,
33, // "Hello, world!" as bytes
5, 0, 0, 0, // Length of the vector c (5)
1, 2, 3, 4, 5, // c: Vec<u8>
];

// Serialize the instance
let serialized = borsh::to_vec(&inst).expect("Serialization failed");
// Serialize the instance
let serialized = borsh::to_vec(&inst).expect("Serialization failed");

// Assert that the serialized output matches the expected value
assert_eq!(serialized, expected_serialized, "Serialized bytes differ from the expected value");
// Assert that the serialized output matches the expected value
assert_eq!(
serialized, expected_serialized,
"Serialized bytes differ from the expected value"
);

// Deserialize the serialized data back to a new instance
let deserialized_inst: TestSerializable = borsh::BorshDeserialize::try_from_slice(&serialized)
.expect("Deserialization failed");
// Deserialize the serialized data back to a new instance
let deserialized_inst: TestSerializable =
borsh::BorshDeserialize::try_from_slice(&serialized)
.expect("Deserialization failed");

// Assert that the deserialized instance matches the original instance
assert_eq!(deserialized_inst, inst, "Deserialized instance differs from the original");
}
// Assert that the deserialized instance matches the original instance
assert_eq!(
deserialized_inst, inst,
"Deserialized instance differs from the original"
);
}
}
11 changes: 4 additions & 7 deletions src/integration/tests/remote_tls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{process::Command, str};

use borsh::BorshSerialize;
use integration::{PivotRemoteTlsMsg, PIVOT_REMOTE_TLS_PATH, QOS_NET_PATH};
use qos_core::{
client::Client,
Expand Down Expand Up @@ -34,11 +33,10 @@ fn fetch_remote_tls_content() {
TimeVal::seconds(ENCLAVE_APP_SOCKET_CLIENT_TIMEOUT_SECS),
);

let app_request = PivotRemoteTlsMsg::RemoteTlsRequest {
let app_request = borsh::to_vec(&PivotRemoteTlsMsg::RemoteTlsRequest {
host: "api.turnkey.com".to_string(),
path: "/health".to_string(),
}
.try_to_vec()
})
.unwrap();

let response = enclave_client.send(&app_request).unwrap();
Expand All @@ -48,11 +46,10 @@ fn fetch_remote_tls_content() {
assert!(response_text.contains("HTTP/1.1 200 OK"));
assert!(response_text.contains("currentTime"));

let app_request = PivotRemoteTlsMsg::RemoteTlsRequest {
let app_request = borsh::to_vec(&PivotRemoteTlsMsg::RemoteTlsRequest {
host: "www.googleapis.com".to_string(),
path: "/oauth2/v3/certs".to_string(),
}
.try_to_vec()
})
.unwrap();

let response = enclave_client.send(&app_request).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/qos_core/src/io/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ mod test {

// Send "PONG" if "PING" was sent
if from_utf8(&buf).unwrap() == "PING" {
stream.write(b"PONG").unwrap();
let _ = stream.write(b"PONG").unwrap();
}

// Then shutdown the server
Expand Down

0 comments on commit d290dfb

Please sign in to comment.