feat: fix tests

This commit is contained in:
Codetrauma
2025-07-07 10:39:28 -07:00
parent 5b2c7cfc51
commit 090e2b8de6
5 changed files with 58 additions and 33 deletions

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,5 @@
use k256::pkcs8::DecodePublicKey;
use k256::pkcs8::EncodePublicKey;
use neon::prelude::*;
use tlsn_core::{
presentation::{Presentation, PresentationOutput},
@@ -28,11 +29,9 @@ fn verify_presentation(
presentation_cx: String,
notary_key_cx: String,
) -> Result<(String, String, u64), String> {
// Deserialize the presentation
let bytes: Vec<u8> = hex::decode(presentation_cx.as_str()).map_err(|e| e.to_string())?;
let presentation: Presentation = bincode::deserialize(&bytes).map_err(|e| e.to_string())?;
// Verify the session proof against the Notary's public key
let VerifyingKey {
alg: _,
data: key_data,
@@ -47,7 +46,6 @@ fn verify_presentation(
Err("The verifying key does not match the notary key")?;
}
// Verify the presentation.
let provider = CryptoProvider::default();
let PresentationOutput {
connection_info,
@@ -59,7 +57,6 @@ fn verify_presentation(
let (sent, recv) = transcript
.map(|mut partial_transcript| {
// Set the unauthenticated bytes so they are distinguishable.
partial_transcript.set_unauthed(b'X');
let sent = String::from_utf8_lossy(partial_transcript.sent_unsafe()).to_string();
let recv = String::from_utf8_lossy(partial_transcript.received_unsafe()).to_string();
@@ -87,47 +84,76 @@ mod tests {
data: String,
}
/// get example presentation from example.com for testing
fn example_presentation() -> String {
let example = include_str!("example.json");
let presentation: Presentation = serde_json::from_str(&example).unwrap();
assert_eq!("0.1.0-alpha.7", presentation.version);
assert_eq!("0.1.0-alpha.12", presentation.version);
presentation.data
}
#[test]
fn test_verify() {
let notary_key_cx = String::from(
"-----BEGIN PUBLIC KEY-----
MDYwEAYHKoZIzj0CAQYFK4EEAAoDIgADe0jxnBObaIj7Xjg6TXLCM1GG/VhY5650
OrS/jgcbBuc=
-----END PUBLIC KEY-----",
);
fn extract_verifying_key_as_pem(presentation_data: String) -> Result<String, String> {
let bytes: Vec<u8> = hex::decode(presentation_data.as_str()).map_err(|e| e.to_string())?;
let presentation: tlsn_core::presentation::Presentation = bincode::deserialize(&bytes).map_err(|e| e.to_string())?;
let (sent, recv, time) =
verify_presentation(example_presentation(), notary_key_cx).unwrap();
let VerifyingKey {
alg: _,
data: key_data,
} = presentation.verifying_key();
assert_eq!(1728416631, time);
assert!(sent.contains("host: example.com"));
assert!(sent.contains("XXXXXXXXXXXXXXXXXX"));
assert!(recv.contains("<title>Example Domain</title>"));
assert!(recv.contains("Date: Tue, 08 Oct 2024"));
let verifying_key = k256::PublicKey::from_sec1_bytes(&key_data)
.map_err(|x| format!("Invalid verifying key: {}", x))?;
let pem = verifying_key.to_public_key_pem(k256::pkcs8::LineEnding::LF)
.map_err(|x| format!("Failed to convert to PEM: {}", x))?;
Ok(pem)
}
#[test]
fn test_verify_wrong_key() {
let notary_key_cx = String::from(
"-----BEGIN PUBLIC KEY-----
MDYwEAYHKoZIzj0CAQYFK4EEAAoDIgADZT9nJiwhGESLjwQNnZ2MsZ1xwjGzvmhF
xFi8Vjzanlg=
-----END PUBLIC KEY-----",
fn test_verify_with_correct_key() {
let correct_key = extract_verifying_key_as_pem(example_presentation())
.expect("Failed to extract verifying key from presentation");
let (sent, recv, time) =
verify_presentation(example_presentation(), correct_key).unwrap();
assert_eq!(1748415894, time);
assert!(sent.contains("host: raw.githubusercontent.com"));
assert!(sent.contains("XXXXXXXXXXXXXXXXXX"));
assert!(recv.contains("HTTP/1.1 200 OK"));
assert!(recv.contains("Content-Type: text/plain"));
}
#[test]
fn test_verify_with_your_current_key() {
let your_notary_key = String::from(
"-----BEGIN PUBLIC KEY-----
MDYwEAYHKoZIzj0CAQYFK4EEAAoDIgACWq2qrz9HJbTB32D4WowdXQfnCaBS5eas
rPwHd4svpUo=
-----END PUBLIC KEY-----"
);
let res = verify_presentation(example_presentation(), notary_key_cx);
let res = verify_presentation(example_presentation(), your_notary_key);
assert_eq!(
res,
Err("The verifying key does not match the notary key".to_string())
);
}
#[test]
fn test_verify_wrong_key() {
let invalid_key = String::from(
"-----BEGIN PUBLIC KEY-----
MDYwEAYHKoZIzj0CAQYFK4EEAAoDIgABm3AS+GGr3gEwbDOWNJTR7oWF/xJ6LBf+
z9KxqnGiW9o=
-----END PUBLIC KEY-----"
);
let res = verify_presentation(example_presentation(), invalid_key);
assert!(res.is_err());
let error_msg = res.unwrap_err();
assert!(error_msg.contains("Invalid notary key") || error_msg.contains("does not match"));
}
}

View File

@@ -12,7 +12,7 @@ import { convertNotaryWsToHttp, fetchPublicKeyFromNotary } from './util/index';
import { assignPoapToUser } from './util/index';
const app = express();
const port = 3030;
const port = 3031;
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');

View File

@@ -12,10 +12,10 @@ const MatomoTracking = () => {
useEffect(() => {
if (typeof window !== 'undefined') {
var _paq = (window._paq = window._paq || []);
const _paq = (window._paq = window._paq || []);
_paq.push(['setTrackerUrl', 'https://psedev.matomo.cloud/matomo.php']);
_paq.push(['setSiteId', '16']);
let script = document.createElement('script');
const script = document.createElement('script');
script.async = true;
script.src = 'https://cdn.matomo.cloud/psedev.matomo.cloud/matomo.js';
document.head.appendChild(script);
@@ -23,7 +23,6 @@ const MatomoTracking = () => {
}, []);
useEffect(() => {
if (typeof window !== 'undefined' && window._paq) {
window._paq.push(['setCustomUrl', window.location.pathname]);
window._paq.push(['trackPageView']);