mirror of
https://github.com/tlsnotary/tlsn-js.git
synced 2026-01-06 19:23:54 -05:00
test: added Rust WASM test for the Verifier
+ Run WASM test with GitHub actions
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
rustflags = [
|
||||
"-C",
|
||||
"target-feature=+atomics,+bulk-memory,+mutable-globals",
|
||||
"-C",
|
||||
"link-arg=--max-memory=4294967296"
|
||||
]
|
||||
|
||||
[unstable]
|
||||
|
||||
3
.github/workflows/test.yaml
vendored
3
.github/workflows/test.yaml
vendored
@@ -67,6 +67,9 @@ jobs:
|
||||
- name: Build WASM
|
||||
run: npm run build:wasm
|
||||
|
||||
- name: Test WASM
|
||||
run: npm run test:wasm
|
||||
|
||||
- name: Test
|
||||
if: false
|
||||
run: npm run test
|
||||
@@ -18,6 +18,7 @@
|
||||
"build:types": "tsc --project tsconfig.compile.json",
|
||||
"build": "NODE_ENV=production concurrently npm:build:src npm:build:types",
|
||||
"update:wasm": "sh utils/check-wasm.sh -f",
|
||||
"test:wasm": "cd wasm/prover; wasm-pack test --firefox --release --headless",
|
||||
"build:wasm": "wasm-pack build --target web wasm/prover",
|
||||
"watch:dev": "webpack --config webpack.web.dev.config.js --watch",
|
||||
"predev": "sh utils/check-wasm.sh",
|
||||
|
||||
@@ -1,5 +1,2 @@
|
||||
[toolchain]
|
||||
# channel = "nightly-2022-12-12"
|
||||
# channel = "stable"
|
||||
# channel = "nightly-x86_64-apple-darwin"
|
||||
channel = "nightly"
|
||||
|
||||
4
test/assets/notary.pem
Normal file
4
test/assets/notary.pem
Normal file
@@ -0,0 +1,4 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBv36FI4ZFszJa0DQFJ3wWCXvVLFr
|
||||
cRzMG5kaTeHGoSzDu6cFqx3uEWYpFGo6C0EOUgf+mEgbktLrXocv5yHzKg==
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"serverName": "example.com",
|
||||
"time": 1700642161,
|
||||
"time": 1708595467,
|
||||
"sent": "GET / HTTP/1.1\r\nhost: example.com\r\naccept: */*\r\naccept-encoding: identity\r\nconnection: close\r\nuser-agent: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n\r\n",
|
||||
"recv": "HTTP/1.1 200 OK\r\nAge: 22659\r\nCache-Control: max-age=604800\r\nContent-Type: text/html; charset=UTF-8\r\nDate: Wed, 22 Nov 2023 08:36:02 GMT\r\nEtag: \"3147526947+ident\"\r\nExpires: Wed, 29 Nov 2023 08:36:02 GMT\r\nLast-Modified: Thu, 17 Oct 2019 07:18:26 GMT\r\nServer: ECS (dce/26AB)\r\nVary: Accept-Encoding\r\nX-Cache: HIT\r\nContent-Length: 1256\r\nConnection: close\r\n\r\n<!doctype html>\n<html>\n<head>\n <title>XXXXXXXXXXXXXX</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>XXXXXXXXXXXXXX</h1>\n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n",
|
||||
"recv": "HTTP/1.1 200 OK\r\nAge: 519895\r\nCache-Control: max-age=604800\r\nContent-Type: text/html; charset=UTF-8\r\nDate: Thu, 22 Feb 2024 09:51:08 GMT\r\nEtag: \"3147526947+ident\"\r\nExpires: Thu, 29 Feb 2024 09:51:08 GMT\r\nLast-Modified: Thu, 17 Oct 2019 07:18:26 GMT\r\nServer: ECS (dce/26A0)\r\nVary: Accept-Encoding\r\nX-Cache: HIT\r\nContent-Length: 1256\r\nConnection: close\r\n\r\n<!doctype html>\n<html>\n<head>\n <title>XXXXXXXXXXXXXX</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 2em;\n background-color: #fdfdff;\n border-radius: 0.5em;\n box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n div {\n margin: 0 auto;\n width: auto;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>XXXXXXXXXXXXXX</h1>\n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n",
|
||||
"notaryUrl": "http://localhost"
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
[package]
|
||||
authors = ["The tlsn-extension Developers"]
|
||||
description = "tlsn-js library for using TLSNotary in browsers"
|
||||
edition = "2018"
|
||||
license = "MIT OR Apache-2.0"
|
||||
name = "tlsn-extension-rs"
|
||||
rust-version = "1.56"
|
||||
version = "0.1.0"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4"
|
||||
@@ -82,7 +84,8 @@ strum_macros = "0.26.1"
|
||||
wasm-bindgen-test = "0.3.34"
|
||||
|
||||
[profile.release]
|
||||
# Tell `rustc` to optimize for small code size.
|
||||
lto = true # Enable Link Time Optimization
|
||||
opt-level = "z" # Optimize for size
|
||||
|
||||
[package.metadata.wasm-pack.profile.release]
|
||||
wasm-opt = false
|
||||
wasm-opt = true
|
||||
|
||||
@@ -17,7 +17,7 @@ use wasm_bindgen_futures::spawn_local;
|
||||
|
||||
use ws_stream_wasm::*;
|
||||
|
||||
use crate::request_opt::{RequestOptions, VerifyResult};
|
||||
pub use crate::request_opt::{RequestOptions, VerifyResult};
|
||||
use crate::requests::{ClientType, NotarizationSessionRequest, NotarizationSessionResponse};
|
||||
|
||||
pub use wasm_bindgen_rayon::init_thread_pool;
|
||||
@@ -32,7 +32,6 @@ use std::time::Duration;
|
||||
use tlsn_core::proof::{SessionProof, TlsProof};
|
||||
|
||||
use strum::EnumMessage;
|
||||
use strum_macros;
|
||||
|
||||
// A macro to provide `println!(..)`-style syntax for `console.log` logging.
|
||||
macro_rules! log {
|
||||
@@ -287,15 +286,13 @@ pub async fn prover(
|
||||
req_with_header = req_with_header.header(key.as_str(), value.as_str());
|
||||
}
|
||||
|
||||
let req_with_body;
|
||||
|
||||
if options.body.is_empty() {
|
||||
let req_with_body = if options.body.is_empty() {
|
||||
log!("empty body");
|
||||
req_with_body = req_with_header.body(Body::empty());
|
||||
req_with_header.body(Body::empty())
|
||||
} else {
|
||||
log!("added body - {}", options.body.as_str());
|
||||
req_with_body = req_with_header.body(Body::from(options.body));
|
||||
}
|
||||
req_with_header.body(Body::from(options.body))
|
||||
};
|
||||
|
||||
let unwrapped_request = req_with_body
|
||||
.map_err(|e| JsValue::from_str(&format!("Could not build request: {:?}", e)))?;
|
||||
@@ -467,7 +464,7 @@ pub async fn prover(
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub async fn verify(proof: &str, notary_pubkey_str: &str) -> Result<String, JsValue> {
|
||||
log!("!@# proof {}", proof);
|
||||
// log!("!@# proof {}", proof);
|
||||
let proof: TlsProof = serde_json::from_str(proof)
|
||||
.map_err(|e| JsValue::from_str(&format!("Could not deserialize proof: {:?}", e)))?;
|
||||
|
||||
|
||||
47
wasm/prover/tests/web.rs
Normal file
47
wasm/prover/tests/web.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
//! Test suite for the Web and headless browsers.
|
||||
|
||||
#![cfg(target_arch = "wasm32")]
|
||||
|
||||
extern crate wasm_bindgen_test;
|
||||
use serde_json::Value;
|
||||
use std::{collections::HashMap, str};
|
||||
use wasm_bindgen_test::*;
|
||||
|
||||
extern crate tlsn_extension_rs;
|
||||
use tlsn_extension_rs::*;
|
||||
|
||||
macro_rules! log {
|
||||
( $( $t:tt )* ) => {
|
||||
web_sys::console::log_1(&format!( $( $t )* ).into());
|
||||
}
|
||||
}
|
||||
|
||||
wasm_bindgen_test_configure!(run_in_browser);
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
async fn verify() {
|
||||
let pem = str::from_utf8(include_bytes!("../../../test/assets/notary.pem")).unwrap();
|
||||
let proof = str::from_utf8(include_bytes!(
|
||||
"../../../test/assets/simple_proof_redacted.json"
|
||||
))
|
||||
.unwrap();
|
||||
let m: HashMap<String, Value> = serde_json::from_str(
|
||||
&str::from_utf8(include_bytes!(
|
||||
"../../../test/assets/simple_proof_expected.json"
|
||||
))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let result = tlsn_extension_rs::verify(proof, pem).await.expect("result");
|
||||
|
||||
log!("result: {}", &result);
|
||||
|
||||
let r: VerifyResult = serde_json::from_str::<VerifyResult>(&result).unwrap();
|
||||
|
||||
assert_eq!(r.server_name, m["serverName"]);
|
||||
assert!(r.recv.contains("<title>XXXXXXXXXXXXXX</title>"));
|
||||
assert_eq!(r.time, m["time"].as_u64().unwrap());
|
||||
assert_eq!(r.sent, m["sent"].as_str().unwrap());
|
||||
assert_eq!(r.recv, m["recv"].as_str().unwrap());
|
||||
}
|
||||
20
wasm/prover/webdriver.json
Normal file
20
wasm/prover/webdriver.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"moz:firefoxOptions": {
|
||||
"prefs": {
|
||||
"media.navigator.streams.fake": true,
|
||||
"media.navigator.permission.disabled": true
|
||||
},
|
||||
"args": []
|
||||
},
|
||||
"goog:chromeOptions": {
|
||||
"args": [
|
||||
"--use-fake-device-for-media-stream",
|
||||
"--use-fake-ui-for-media-stream",
|
||||
"--headless",
|
||||
"--disable-gpu",
|
||||
"--no-sandbox",
|
||||
"--disable-dev-shm-usage",
|
||||
"--window-size=1280,800"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user