Files
khadhroony-solana-project/crates/ksp-store-lib/unit_tests/store.rs
2026-08-30 16:06:20 +02:00

108 lines
5.5 KiB
Rust

// file: crates/ksp-store-lib/unit_tests/store.rs
// version: 6
fn poll_ready<T>(future: impl std::future::Future<Output = T>) -> T {
let mut future = std::boxed::Box::pin(future);
let waker = std::task::Waker::noop();
let mut context = std::task::Context::from_waker(waker);
return match std::future::Future::poll(future.as_mut(), &mut context) {
std::task::Poll::Ready(value) => value,
std::task::Poll::Pending => panic!("Store pre-I/O rejection unexpectedly became pending"),
};
}
fn valid_network() -> crate::RawNetworkId {
return match crate::RawNetworkId::new("devnet") {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => panic!("valid test network rejected: {error:?}"),
};
}
fn store_settings(connection_uri: &str) -> crate::StoreSettings {
let postgres = crate::PostgresStoreSettings::new(
connection_uri,
crate::PostgresPoolSettings::default(),
crate::PostgresTlsMode::Disabled,
crate::PostgresBootstrapSettings::default(),
);
return crate::StoreSettings::with_default_shutdown(valid_network(), crate::StoreBackendSettings::Postgres(postgres));
}
#[test]
fn invalid_settings_are_rejected_before_backend_dispatch() {
let result = poll_ready(crate::Store::open(store_settings("")));
let error = result.err();
assert_eq!(error.map(|value| return value.code()), std::option::Option::Some(crate::ERROR_CODE_SETTINGS_INVALID));
return;
}
#[cfg(feature = "postgres")]
#[test]
fn compiled_postgres_rejects_malformed_physical_configuration_before_io() {
let secret_canary = "not-a-postgresql-uri-secret-canary";
let result = poll_ready(crate::Store::open(store_settings(secret_canary)));
let error = result.err();
assert_eq!(error.as_ref().map(|value| return value.code()), std::option::Option::Some(crate::ERROR_CODE_POSTGRES_CONFIG_INVALID));
assert!(!format!("{error:?}").contains(secret_canary));
return;
}
#[cfg(not(feature = "postgres"))]
#[test]
fn known_postgres_without_feature_is_rejected_before_io() {
let result = poll_ready(crate::Store::open(store_settings("postgresql://operator-supplied-sensitive-value@localhost/ksp")));
let error = result.err();
assert_eq!(error.map(|value| return value.code()), std::option::Option::Some(crate::ERROR_CODE_BACKEND_NOT_COMPILED));
return;
}
#[test]
fn pre_008_operation_network_guard_rejects_mismatch_without_echoing_requested_network() {
let store_network = valid_network();
let hostile = match crate::RawNetworkId::new("other-network") {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => panic!("valid alternate network rejected: {error:?}"),
};
let result = super::validate_operation_network(&store_network, &hostile, crate::StoreBackendKind::Postgres);
let error = match result {
std::result::Result::Err(value) => value,
std::result::Result::Ok(()) => panic!("wrong operation network unexpectedly accepted"),
};
assert_eq!(error.code(), crate::ERROR_CODE_WRONG_NETWORK);
assert!(!std::format!("{error:?}").contains("other-network"));
return;
}
#[cfg(feature = "postgres")]
#[test]
fn pre_010_postgres_error_code_mapping_covers_every_current_backend_kind() {
let cases = [
(ksp_store_postgres_lib::PostgresBackendErrorKind::ConfigInvalid, crate::ERROR_CODE_POSTGRES_CONFIG_INVALID),
(ksp_store_postgres_lib::PostgresBackendErrorKind::ConnectFailed, crate::ERROR_CODE_POSTGRES_CONNECT_FAILED),
(ksp_store_postgres_lib::PostgresBackendErrorKind::PoolTimeout, crate::ERROR_CODE_POSTGRES_POOL_TIMEOUT),
(ksp_store_postgres_lib::PostgresBackendErrorKind::HealthFailed, crate::ERROR_CODE_POSTGRES_HEALTH_FAILED),
(ksp_store_postgres_lib::PostgresBackendErrorKind::Conflict, ksp_store_api::ERROR_CODE_RAW_CONFLICT),
(ksp_store_postgres_lib::PostgresBackendErrorKind::DataInvalid, crate::ERROR_CODE_POSTGRES_DATA_INVALID),
(ksp_store_postgres_lib::PostgresBackendErrorKind::MigrationFailed, crate::ERROR_CODE_POSTGRES_MIGRATION_FAILED),
(ksp_store_postgres_lib::PostgresBackendErrorKind::PageLimitUnsupported, crate::ERROR_CODE_POSTGRES_PAGE_LIMIT_UNSUPPORTED),
(ksp_store_postgres_lib::PostgresBackendErrorKind::MigrationMismatch, crate::ERROR_CODE_POSTGRES_MIGRATION_MISMATCH),
(ksp_store_postgres_lib::PostgresBackendErrorKind::QueryInvalid, ksp_store_api::ERROR_CODE_RAW_QUERY_INVALID),
(ksp_store_postgres_lib::PostgresBackendErrorKind::ReadFailed, crate::ERROR_CODE_POSTGRES_READ_FAILED),
(ksp_store_postgres_lib::PostgresBackendErrorKind::ReferenceNotFound, crate::ERROR_CODE_RAW_REFERENCE_NOT_FOUND),
(
ksp_store_postgres_lib::PostgresBackendErrorKind::RetentionCompactionUnsupported,
crate::ERROR_CODE_POSTGRES_RETENTION_COMPACTION_UNSUPPORTED,
),
(ksp_store_postgres_lib::PostgresBackendErrorKind::SchemaNewer, crate::ERROR_CODE_POSTGRES_SCHEMA_NEWER),
(ksp_store_postgres_lib::PostgresBackendErrorKind::ShutdownTimeout, crate::ERROR_CODE_SHUTDOWN_TIMEOUT),
(ksp_store_postgres_lib::PostgresBackendErrorKind::TlsFailed, crate::ERROR_CODE_POSTGRES_TLS_FAILED),
(ksp_store_postgres_lib::PostgresBackendErrorKind::WriteFailed, crate::ERROR_CODE_POSTGRES_WRITE_FAILED),
(ksp_store_postgres_lib::PostgresBackendErrorKind::WrongNetwork, crate::ERROR_CODE_WRONG_NETWORK),
];
assert_eq!(cases.len(), 18);
for (kind, expected) in cases {
assert_eq!(super::postgres_error_code(kind), expected);
}
return;
}