Skip to content

Commit

Permalink
Fix two test cases (#1129)
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n authored Jun 11, 2024
1 parent 990d704 commit 6066aca
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ wasmer = { features = ["sys"], workspace = true }
xz2 = { version = "0.1" }
reqwest = { version = "0.12", features = ["json"] }
rsa = { version = "0.9", features = ["serde", "pem"] }
pkcs1 = { version = "0.7", features = ["std", "pem"] }
pkcs8 = { version = "0.10", features = ["std", "pem"] }

# Tracing deps
opentelemetry = "0.22"
Expand Down
8 changes: 7 additions & 1 deletion crates/core/src/config/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ mod tests {

// write secrets to files
transport_keypair_file
.write_all(transport_keypair.secret().to_bytes().unwrap().as_slice())
.write_all(
transport_keypair
.secret()
.to_pkcs8_pem()
.unwrap()
.as_slice(),
)
.unwrap();
nonce_file.write_all(&nonce).unwrap();
cipher_file.write_all(&cipher).unwrap();
Expand Down
19 changes: 12 additions & 7 deletions crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,20 +1014,25 @@ impl Display for PeerId {

#[cfg(test)]
mod tests {
use std::net::Ipv4Addr;
use std::net::{Ipv4Addr, Ipv6Addr};

use super::*;

#[tokio::test]
async fn test_hostname_resolution() {
let addr = Address::Hostname("localhost".to_string());
let socket_addr = NodeConfig::parse_socket_addr(&addr).await.unwrap();
assert_eq!(
socket_addr,
SocketAddr::new(
IpAddr::V4(Ipv4Addr::LOCALHOST),
crate::config::default_http_gateway_port()
)
assert!(
socket_addr
== SocketAddr::new(
IpAddr::V4(Ipv4Addr::LOCALHOST),
crate::config::default_http_gateway_port()
)
|| socket_addr
== SocketAddr::new(
IpAddr::V6(Ipv6Addr::LOCALHOST),
crate::config::default_http_gateway_port()
)
);

let addr = Address::Hostname("google.com".to_string());
Expand Down
20 changes: 11 additions & 9 deletions crates/core/src/transport/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use pkcs1::{EncodeRsaPrivateKey, EncodeRsaPublicKey};
use rand::rngs::OsRng;
use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
use rsa::{pkcs8, Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -60,7 +59,9 @@ impl TransportPublicKey {

impl std::fmt::Display for TransportPublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let encoded = self.0.to_pkcs1_der().map_err(|_| std::fmt::Error)?;
use pkcs8::EncodePublicKey;

let encoded = self.0.to_public_key_der().map_err(|_| std::fmt::Error)?;
write!(
f,
"{}",
Expand All @@ -85,19 +86,20 @@ pub(crate) struct TransportSecretKey(RsaPrivateKey);

impl TransportSecretKey {
pub fn decrypt(&self, data: &[u8]) -> rsa::Result<Vec<u8>> {
let padding = Pkcs1v15Encrypt;
self.0.decrypt(padding, data)
self.0.decrypt(Pkcs1v15Encrypt, data)
}

pub fn to_bytes(&self) -> Result<Vec<u8>, pkcs1::Error> {
pub fn to_pkcs8_pem(&self) -> Result<Vec<u8>, pkcs8::Error> {
use pkcs8::EncodePrivateKey;

#[cfg(unix)]
let line_endings = pkcs1::LineEnding::LF;
let line_endings = pkcs8::LineEnding::LF;

#[cfg(windows)]
let line_endings = pkcs1::LineEnding::CRLF;
let line_endings = pkcs8::LineEnding::CRLF;

self.0
.to_pkcs1_pem(line_endings)
.to_pkcs8_pem(line_endings)
.map(|s| s.as_str().as_bytes().to_vec())
}
}
Expand Down

0 comments on commit 6066aca

Please sign in to comment.