|
| 1 | +use http::StatusCode; |
| 2 | +use ic_asset_certification::{Asset, AssetConfig, AssetEncoding, AssetRouter, ASSET_CHUNK_SIZE}; |
| 3 | +use ic_certification_testing::{CertificateBuilder, CertificateData}; |
| 4 | +use ic_http_certification::{ |
| 5 | + DefaultCelBuilder, DefaultResponseCertification, HeaderField, HttpRequest, HttpResponse, |
| 6 | +}; |
| 7 | +use ic_response_verification::verify_request_response_pair; |
| 8 | +use ic_response_verification_test_utils::{create_canister_id, get_current_timestamp}; |
| 9 | +use once_cell::sync::OnceCell; |
| 10 | +use rstest::*; |
| 11 | + |
| 12 | +mod common; |
| 13 | +use common::*; |
| 14 | + |
| 15 | +const ASSET_ONE_NAME: &str = "asset_one"; |
| 16 | +const ASSET_ONE_SIZE: usize = ASSET_CHUNK_SIZE + 1; |
| 17 | + |
| 18 | +const MAX_CERT_TIME_OFFSET_NS: u128 = 300_000_000_000; |
| 19 | +const MIN_REQUESTED_VERIFICATION_VERSION: u8 = 2; |
| 20 | + |
| 21 | +#[rstest] |
| 22 | +fn should_certify_long_asset_chunkwise( |
| 23 | + asset_one_body: &'static [u8], |
| 24 | + asset_one_chunk_one: &'static [u8], |
| 25 | + asset_one_chunk_two: &'static [u8], |
| 26 | +) { |
| 27 | + let current_time = get_current_timestamp(); |
| 28 | + let canister_id = create_canister_id("rdmx6-jaaaa-aaaaa-aaadq-cai"); |
| 29 | + let req_url = format!("/{}", ASSET_ONE_NAME); |
| 30 | + |
| 31 | + let mut asset_router = AssetRouter::default(); |
| 32 | + let assets = [Asset::new(ASSET_ONE_NAME, asset_one_body)]; |
| 33 | + let asset_configs = [asset_config(ASSET_ONE_NAME.to_string(), vec![])]; |
| 34 | + asset_router.certify_assets(assets, asset_configs).unwrap(); |
| 35 | + |
| 36 | + let certified_data = asset_router.root_hash(); |
| 37 | + let CertificateData { |
| 38 | + cbor_encoded_certificate, |
| 39 | + certificate: _, |
| 40 | + root_key, |
| 41 | + } = CertificateBuilder::new(&canister_id.to_string(), &certified_data) |
| 42 | + .expect("Failed to create CertificateBuilder") |
| 43 | + .with_time(current_time) |
| 44 | + .build() |
| 45 | + .expect("Failed to create CertificateData from CertificateBuilder"); |
| 46 | + |
| 47 | + let mut expected_headers = common_asset_headers(); |
| 48 | + expected_headers.extend(vec![ |
| 49 | + ("content-type".to_string(), "text/html".to_string()), |
| 50 | + ("content-length".to_string(), ASSET_CHUNK_SIZE.to_string()), |
| 51 | + ( |
| 52 | + "content-range".to_string(), |
| 53 | + format!("bytes 0-{}/{}", ASSET_CHUNK_SIZE - 1, ASSET_ONE_SIZE), |
| 54 | + ), |
| 55 | + ]); |
| 56 | + let expected_chunk_one_res = HttpResponse::builder() |
| 57 | + .with_status_code(StatusCode::PARTIAL_CONTENT) |
| 58 | + .with_headers(expected_headers) |
| 59 | + .with_body(asset_one_chunk_one) |
| 60 | + .build(); |
| 61 | + |
| 62 | + let chunk_one_req = HttpRequest::get(&req_url).build(); |
| 63 | + let chunk_one_res = asset_router |
| 64 | + .serve_asset(&cbor_encoded_certificate, &chunk_one_req) |
| 65 | + .unwrap(); |
| 66 | + assert_response_eq!(chunk_one_res, expected_chunk_one_res); |
| 67 | + |
| 68 | + let chunk_one_verification = verify_request_response_pair( |
| 69 | + chunk_one_req, |
| 70 | + chunk_one_res, |
| 71 | + canister_id.as_ref(), |
| 72 | + current_time, |
| 73 | + MAX_CERT_TIME_OFFSET_NS, |
| 74 | + &root_key, |
| 75 | + MIN_REQUESTED_VERIFICATION_VERSION, |
| 76 | + ) |
| 77 | + .unwrap(); |
| 78 | + assert_eq!(chunk_one_verification.verification_version, 2); |
| 79 | + assert_verified_response_eq!( |
| 80 | + chunk_one_verification.response.unwrap(), |
| 81 | + expected_chunk_one_res |
| 82 | + ); |
| 83 | + |
| 84 | + let mut expected_headers = common_asset_headers(); |
| 85 | + expected_headers.extend(vec![ |
| 86 | + ("content-type".to_string(), "text/html".to_string()), |
| 87 | + ( |
| 88 | + "content-length".to_string(), |
| 89 | + (ASSET_ONE_SIZE - ASSET_CHUNK_SIZE).to_string(), |
| 90 | + ), |
| 91 | + ( |
| 92 | + "content-range".to_string(), |
| 93 | + format!( |
| 94 | + "bytes {}-{}/{}", |
| 95 | + ASSET_CHUNK_SIZE, |
| 96 | + ASSET_ONE_SIZE - 1, |
| 97 | + ASSET_ONE_SIZE |
| 98 | + ), |
| 99 | + ), |
| 100 | + ]); |
| 101 | + let expected_chunk_two_res = HttpResponse::builder() |
| 102 | + .with_status_code(StatusCode::PARTIAL_CONTENT) |
| 103 | + .with_headers(expected_headers) |
| 104 | + .with_body(asset_one_chunk_two) |
| 105 | + .build(); |
| 106 | + |
| 107 | + let chunk_two_req = HttpRequest::get(&req_url) |
| 108 | + .with_headers(vec![( |
| 109 | + "range".to_string(), |
| 110 | + format!("bytes={}-", ASSET_CHUNK_SIZE), |
| 111 | + )]) |
| 112 | + .build(); |
| 113 | + let chunk_two_res = asset_router |
| 114 | + .serve_asset(&cbor_encoded_certificate, &chunk_two_req) |
| 115 | + .unwrap(); |
| 116 | + assert_response_eq!(chunk_two_res, expected_chunk_two_res); |
| 117 | + |
| 118 | + let chunk_two_verification = verify_request_response_pair( |
| 119 | + chunk_two_req, |
| 120 | + chunk_two_res, |
| 121 | + canister_id.as_ref(), |
| 122 | + current_time, |
| 123 | + MAX_CERT_TIME_OFFSET_NS, |
| 124 | + &root_key, |
| 125 | + MIN_REQUESTED_VERIFICATION_VERSION, |
| 126 | + ) |
| 127 | + .unwrap(); |
| 128 | + assert_eq!(chunk_two_verification.verification_version, 2); |
| 129 | + assert_verified_response_eq!( |
| 130 | + chunk_two_verification.response.unwrap(), |
| 131 | + expected_chunk_two_res |
| 132 | + ); |
| 133 | +} |
| 134 | + |
| 135 | +#[fixture] |
| 136 | +fn asset_cel_expr() -> String { |
| 137 | + DefaultCelBuilder::full_certification() |
| 138 | + .with_response_certification(DefaultResponseCertification::response_header_exclusions( |
| 139 | + vec![], |
| 140 | + )) |
| 141 | + .build() |
| 142 | + .to_string() |
| 143 | +} |
| 144 | + |
| 145 | +#[fixture] |
| 146 | +fn asset_range_cel_expr() -> String { |
| 147 | + DefaultCelBuilder::full_certification() |
| 148 | + .with_request_headers(vec!["range"]) |
| 149 | + .with_response_certification(DefaultResponseCertification::response_header_exclusions( |
| 150 | + vec![], |
| 151 | + )) |
| 152 | + .build() |
| 153 | + .to_string() |
| 154 | +} |
| 155 | + |
| 156 | +#[fixture] |
| 157 | +fn asset_one_body() -> &'static [u8] { |
| 158 | + static ASSET_ONE_BODY: OnceCell<Vec<u8>> = OnceCell::new(); |
| 159 | + |
| 160 | + ASSET_ONE_BODY.get_or_init(|| asset_body(ASSET_ONE_NAME, ASSET_ONE_SIZE)) |
| 161 | +} |
| 162 | + |
| 163 | +#[fixture] |
| 164 | +fn asset_one_chunk_one(asset_one_body: &'static [u8]) -> &'static [u8] { |
| 165 | + static ASSET_ONE_CHUNK: OnceCell<&[u8]> = OnceCell::new(); |
| 166 | + |
| 167 | + ASSET_ONE_CHUNK.get_or_init(|| asset_chunk(asset_one_body, 0)) |
| 168 | +} |
| 169 | + |
| 170 | +#[fixture] |
| 171 | +fn asset_one_chunk_two(asset_one_body: &'static [u8]) -> &'static [u8] { |
| 172 | + static ASSET_ONE_CHUNK: OnceCell<&[u8]> = OnceCell::new(); |
| 173 | + |
| 174 | + ASSET_ONE_CHUNK.get_or_init(|| asset_chunk(asset_one_body, 1)) |
| 175 | +} |
| 176 | + |
| 177 | +fn asset_config(path: String, encodings: Vec<(AssetEncoding, String)>) -> AssetConfig { |
| 178 | + AssetConfig::File { |
| 179 | + path, |
| 180 | + content_type: Some("text/html".to_string()), |
| 181 | + headers: common_asset_headers(), |
| 182 | + fallback_for: vec![], |
| 183 | + aliased_by: vec![], |
| 184 | + encodings, |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +fn common_asset_headers() -> Vec<HeaderField> { |
| 189 | + vec![( |
| 190 | + "cache-control".to_string(), |
| 191 | + "public, no-cache, no-store".to_string(), |
| 192 | + )] |
| 193 | +} |
0 commit comments