diff --git a/ffi_utils/status.rs b/ffi_utils/status.rs index 73be81a..59d2ef2 100644 --- a/ffi_utils/status.rs +++ b/ffi_utils/status.rs @@ -76,24 +76,23 @@ impl Debug for ffi::FfiStatus { #[derive(Debug, PartialEq, Eq, Clone, Copy)] #[non_exhaustive] #[repr(i32)] -#[allow(non_camel_case_types)] pub enum StatusErrorCode { - CANCELLED = 1, - UNKNOWN = 2, - INVALID_ARGUMENT = 3, - DEADLINE_EXCEEDED = 4, - NOT_FOUND = 5, - ALREADY_EXISTS = 6, - PERMISSION_DENIED = 7, - RESOURCE_EXHAUSTED = 8, - FAILED_PRECONDITION = 9, - ABORTED = 10, - OUT_OF_RANGE = 11, - UNIMPLEMENTED = 12, - INTERNAL = 13, - UNAVAILABLE = 14, - DATA_LOSS = 15, - UNAUTHENTICATED = 16, + Cancelled = 1, + Unknown = 2, + InvalidArgument = 3, + DeadlineExceeded = 4, + NotFound = 5, + AlreadyExists = 6, + PermissionDenied = 7, + ResourceExhausted = 8, + FailedPrecondition = 9, + Aborted = 10, + OutOfRange = 11, + Unimplemented = 12, + Internal = 13, + Unavailable = 14, + DataLoss = 15, + Unauthenticated = 16, } /// Holds a wrapped non-OK absl::Status. @@ -137,7 +136,7 @@ impl StatusError { /// Returns the canonical error code of this status. pub fn code(&self) -> StatusErrorCode { - ffi::ffi_status_code(&self.ffi_status).try_into().unwrap_or(StatusErrorCode::UNKNOWN) + ffi::ffi_status_code(&self.ffi_status).try_into().unwrap_or(StatusErrorCode::Unknown) } /// Returns the error message associated with this error code. @@ -188,22 +187,22 @@ impl StatusErrorCode { pub fn as_str(self) -> &'static str { // Same as `absl::StatusCodeToString` match self { - StatusErrorCode::CANCELLED => "CANCELLED", - StatusErrorCode::UNKNOWN => "UNKNOWN", - StatusErrorCode::INVALID_ARGUMENT => "INVALID_ARGUMENT", - StatusErrorCode::DEADLINE_EXCEEDED => "DEADLINE_EXCEEDED", - StatusErrorCode::NOT_FOUND => "NOT_FOUND", - StatusErrorCode::ALREADY_EXISTS => "ALREADY_EXISTS", - StatusErrorCode::PERMISSION_DENIED => "PERMISSION_DENIED", - StatusErrorCode::RESOURCE_EXHAUSTED => "RESOURCE_EXHAUSTED", - StatusErrorCode::FAILED_PRECONDITION => "FAILED_PRECONDITION", - StatusErrorCode::ABORTED => "ABORTED", - StatusErrorCode::OUT_OF_RANGE => "OUT_OF_RANGE", - StatusErrorCode::UNIMPLEMENTED => "UNIMPLEMENTED", - StatusErrorCode::INTERNAL => "INTERNAL", - StatusErrorCode::UNAVAILABLE => "UNAVAILABLE", - StatusErrorCode::DATA_LOSS => "DATA_LOSS", - StatusErrorCode::UNAUTHENTICATED => "UNAUTHENTICATED", + StatusErrorCode::Cancelled => "CANCELLED", + StatusErrorCode::Unknown => "UNKNOWN", + StatusErrorCode::InvalidArgument => "INVALID_ARGUMENT", + StatusErrorCode::DeadlineExceeded => "DEADLINE_EXCEEDED", + StatusErrorCode::NotFound => "NOT_FOUND", + StatusErrorCode::AlreadyExists => "ALREADY_EXISTS", + StatusErrorCode::PermissionDenied => "PERMISSION_DENIED", + StatusErrorCode::ResourceExhausted => "RESOURCE_EXHAUSTED", + StatusErrorCode::FailedPrecondition => "FAILED_PRECONDITION", + StatusErrorCode::Aborted => "ABORTED", + StatusErrorCode::OutOfRange => "OUT_OF_RANGE", + StatusErrorCode::Unimplemented => "UNIMPLEMENTED", + StatusErrorCode::Internal => "INTERNAL", + StatusErrorCode::Unavailable => "UNAVAILABLE", + StatusErrorCode::DataLoss => "DATA_LOSS", + StatusErrorCode::Unauthenticated => "UNAUTHENTICATED", } } } @@ -213,22 +212,22 @@ impl TryFrom for StatusErrorCode { fn try_from(value: i32) -> Result { Ok(match value { - 1 => StatusErrorCode::CANCELLED, - 2 => StatusErrorCode::UNKNOWN, - 3 => StatusErrorCode::INVALID_ARGUMENT, - 4 => StatusErrorCode::DEADLINE_EXCEEDED, - 5 => StatusErrorCode::NOT_FOUND, - 6 => StatusErrorCode::ALREADY_EXISTS, - 7 => StatusErrorCode::PERMISSION_DENIED, - 8 => StatusErrorCode::RESOURCE_EXHAUSTED, - 9 => StatusErrorCode::FAILED_PRECONDITION, - 10 => StatusErrorCode::ABORTED, - 11 => StatusErrorCode::OUT_OF_RANGE, - 12 => StatusErrorCode::UNIMPLEMENTED, - 13 => StatusErrorCode::INTERNAL, - 14 => StatusErrorCode::UNAVAILABLE, - 15 => StatusErrorCode::DATA_LOSS, - 16 => StatusErrorCode::UNAUTHENTICATED, + 1 => StatusErrorCode::Cancelled, + 2 => StatusErrorCode::Unknown, + 3 => StatusErrorCode::InvalidArgument, + 4 => StatusErrorCode::DeadlineExceeded, + 5 => StatusErrorCode::NotFound, + 6 => StatusErrorCode::AlreadyExists, + 7 => StatusErrorCode::PermissionDenied, + 8 => StatusErrorCode::ResourceExhausted, + 9 => StatusErrorCode::FailedPrecondition, + 10 => StatusErrorCode::Aborted, + 11 => StatusErrorCode::OutOfRange, + 12 => StatusErrorCode::Unimplemented, + 13 => StatusErrorCode::Internal, + 14 => StatusErrorCode::Unavailable, + 15 => StatusErrorCode::DataLoss, + 16 => StatusErrorCode::Unauthenticated, _ => return Err(StatusErrorCodeTryFromError(())), }) } @@ -273,8 +272,8 @@ impl std::fmt::Display for StatusErrorCodeTryFromError { write!( f, "error status code out of range: must be between {} and {}", - StatusErrorCode::CANCELLED as i32, - StatusErrorCode::UNAUTHENTICATED as i32 + StatusErrorCode::Cancelled as i32, + StatusErrorCode::Unauthenticated as i32 ) } } @@ -305,18 +304,18 @@ pub fn rust_status_from_cpp(status: ffi::FfiStatus) -> Status { #[track_caller] pub fn cancelled(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::CANCELLED, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::Cancelled, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn unknown(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::UNKNOWN, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::Unknown, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn invalid_argument(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::INVALID_ARGUMENT, + StatusErrorCode::InvalidArgument, msg.as_bytes(), core::panic::Location::caller(), ) @@ -325,7 +324,7 @@ pub fn invalid_argument(msg: &str) -> StatusError { #[track_caller] pub fn deadline_exceeded(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::DEADLINE_EXCEEDED, + StatusErrorCode::DeadlineExceeded, msg.as_bytes(), core::panic::Location::caller(), ) @@ -333,13 +332,13 @@ pub fn deadline_exceeded(msg: &str) -> StatusError { #[track_caller] pub fn not_found(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::NOT_FOUND, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::NotFound, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn already_exists(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::ALREADY_EXISTS, + StatusErrorCode::AlreadyExists, msg.as_bytes(), core::panic::Location::caller(), ) @@ -348,7 +347,7 @@ pub fn already_exists(msg: &str) -> StatusError { #[track_caller] pub fn permission_denied(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::PERMISSION_DENIED, + StatusErrorCode::PermissionDenied, msg.as_bytes(), core::panic::Location::caller(), ) @@ -357,7 +356,7 @@ pub fn permission_denied(msg: &str) -> StatusError { #[track_caller] pub fn resource_exhausted(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::RESOURCE_EXHAUSTED, + StatusErrorCode::ResourceExhausted, msg.as_bytes(), core::panic::Location::caller(), ) @@ -366,7 +365,7 @@ pub fn resource_exhausted(msg: &str) -> StatusError { #[track_caller] pub fn failed_precondition(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::FAILED_PRECONDITION, + StatusErrorCode::FailedPrecondition, msg.as_bytes(), core::panic::Location::caller(), ) @@ -374,18 +373,18 @@ pub fn failed_precondition(msg: &str) -> StatusError { #[track_caller] pub fn aborted(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::ABORTED, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::Aborted, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn out_of_range(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::OUT_OF_RANGE, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::OutOfRange, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn unimplemented(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::UNIMPLEMENTED, + StatusErrorCode::Unimplemented, msg.as_bytes(), core::panic::Location::caller(), ) @@ -393,23 +392,23 @@ pub fn unimplemented(msg: &str) -> StatusError { #[track_caller] pub fn internal(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::INTERNAL, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::Internal, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn unavailable(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::UNAVAILABLE, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::Unavailable, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn data_loss(msg: &str) -> StatusError { - StatusError::new(StatusErrorCode::DATA_LOSS, msg.as_bytes(), core::panic::Location::caller()) + StatusError::new(StatusErrorCode::DataLoss, msg.as_bytes(), core::panic::Location::caller()) } #[track_caller] pub fn unauthenticated(msg: &str) -> StatusError { StatusError::new( - StatusErrorCode::UNAUTHENTICATED, + StatusErrorCode::Unauthenticated, msg.as_bytes(), core::panic::Location::caller(), ) @@ -442,7 +441,7 @@ mod tests { #[gtest] fn test() -> Result<()> { let status = fail_whale(); - if status.is_err() && status.as_ref().err().unwrap().code() == StatusErrorCode::CANCELLED { + if status.is_err() && status.as_ref().err().unwrap().code() == StatusErrorCode::Cancelled { Ok(()) } else { fail!("unexpected status: {:?}", status) @@ -481,7 +480,7 @@ mod tests { #[gtest] fn test_ffi_status_from_status_error() { - let error = StatusError::new_untracked(StatusErrorCode::CANCELLED, b"test"); + let error = StatusError::new_untracked(StatusErrorCode::Cancelled, b"test"); let ffi_status: ffi::FfiStatus = error.into(); expect_eq!(ffi_status_code(&ffi_status), 1); expect_eq!(ffi_status_message(&ffi_status), b"test"); @@ -492,7 +491,7 @@ mod tests { let ffi_status = ffi::make_ffi_status(1, b"test"); let rust_status = rust_status_from_cpp(ffi_status); assert!(rust_status.is_err()); - expect_eq!(&rust_status.as_ref().err().unwrap().code(), &StatusErrorCode::CANCELLED); + expect_eq!(&rust_status.as_ref().err().unwrap().code(), &StatusErrorCode::Cancelled); expect_eq!(&rust_status.as_ref().err().unwrap().message_bytes(), &b"test"); } @@ -506,9 +505,9 @@ mod tests { #[gtest] fn test_ffi_status_from_non_ok_status() { - let rust_status = Err(StatusError::new_untracked(StatusErrorCode::CANCELLED, b"test")); + let rust_status = Err(StatusError::new_untracked(StatusErrorCode::Cancelled, b"test")); let ffi_status: ffi::FfiStatus = rust_status.into(); - expect_eq!(ffi_status_code(&ffi_status), StatusErrorCode::CANCELLED as i32); + expect_eq!(ffi_status_code(&ffi_status), StatusErrorCode::Cancelled as i32); expect_eq!(ffi_status_message(&ffi_status), b"test"); } } diff --git a/ffi_utils/status_matchers.rs b/ffi_utils/status_matchers.rs index 25f3ed1..5df8778 100644 --- a/ffi_utils/status_matchers.rs +++ b/ffi_utils/status_matchers.rs @@ -23,11 +23,11 @@ use std::result::Result; /// /// ``` /// verify_that!(cancelled("cancelled"), -/// status_is(StatusErrorCode::CANCELLED))?; // Passes +/// status_is(StatusErrorCode::Cancelled))?; // Passes /// verify_that!(internal("out of bound"), -/// status_is(StatusErrorCode::INVALID_ARGUMENT))?; // Fails +/// status_is(StatusErrorCode::InvalidArgument))?; // Fails /// verify_that!(Ok("Some value"), -/// status_is(StatusErrorCode::CANCELLED))?; // Fails +/// status_is(StatusErrorCode::Cancelled))?; // Fails /// ``` /// /// If you are looking for the Rust correspondent to `testing::status::IsOk()` @@ -158,38 +158,38 @@ mod tests { #[gtest] fn matches_status() -> Result<()> { - verify_that!(cancelled("cancelled"), status_is(StatusErrorCode::CANCELLED)) + verify_that!(cancelled("cancelled"), status_is(StatusErrorCode::Cancelled)) } #[gtest] fn matches_status_with_message() -> Result<()> { verify_that!( cancelled("cancelled"), - status_is(StatusErrorCode::CANCELLED).with_message(eq("cancelled")) + status_is(StatusErrorCode::Cancelled).with_message(eq("cancelled")) ) } #[gtest] fn matches_status_error() -> Result<()> { verify_that!( - StatusError::new_with_current_location(StatusErrorCode::CANCELLED, "cancelled",), - status_is(StatusErrorCode::CANCELLED) + StatusError::new_with_current_location(StatusErrorCode::Cancelled, "cancelled",), + status_is(StatusErrorCode::Cancelled) ) } #[gtest] fn matches_status_error_with_message() -> Result<()> { verify_that!( - StatusError::new_with_current_location(StatusErrorCode::CANCELLED, "cancelled",), - status_is(StatusErrorCode::CANCELLED).with_message(eq("cancelled")) + StatusError::new_with_current_location(StatusErrorCode::Cancelled, "cancelled",), + status_is(StatusErrorCode::Cancelled).with_message(eq("cancelled")) ) } #[gtest] fn wrong_code_failure_message() -> Result<()> { let result = verify_that!( - StatusError::new_untracked(StatusErrorCode::CANCELLED, "cancelled"), - status_is(StatusErrorCode::INVALID_ARGUMENT) + StatusError::new_untracked(StatusErrorCode::Cancelled, "cancelled"), + status_is(StatusErrorCode::InvalidArgument) ); verify_that!( @@ -198,7 +198,7 @@ mod tests { r#" Expected: is a StatusError with code INVALID_ARGUMENT Actual: StatusError { - code: CANCELLED, + code: Cancelled, message: "cancelled", loc: None, }, diff --git a/shell_wrapper/kahe_test.rs b/shell_wrapper/kahe_test.rs index 2657154..a6eb31a 100644 --- a/shell_wrapper/kahe_test.rs +++ b/shell_wrapper/kahe_test.rs @@ -157,7 +157,7 @@ fn encrypt_decrypt_long() -> Result<()> { (0..num_values_too_long).map(|_| rand::thread_rng().gen_range(0..input_domain)).collect(); let plaintext_too_long = HashMap::from([(DEFAULT_ID, input_values_too_long.as_slice())]); match encrypt(&plaintext_too_long, &packed_vector_configs, &secret_key, ¶ms, &mut prng) { - Err(e) => expect_that!(e, status_is(StatusErrorCode::INVALID_ARGUMENT)), + Err(e) => expect_that!(e, status_is(StatusErrorCode::InvalidArgument)), Ok(_) => fail!("Expected call to fail")?, } diff --git a/willow/crypto/shell_ahe.rs b/willow/crypto/shell_ahe.rs index dcad347..fcd2da3 100644 --- a/willow/crypto/shell_ahe.rs +++ b/willow/crypto/shell_ahe.rs @@ -940,7 +940,7 @@ mod test { let pt_1 = generate_random_signed_vector(NUM_VALUES_1, MAX_ABSOLUTE_VALUE); let mut pt_2 = generate_random_signed_vector(NUM_VALUES_2, MAX_ABSOLUTE_VALUE); let res = ahe.add_plaintexts_in_place(&pt_1, &mut pt_2); - verify_that!(res, status_is(StatusErrorCode::INVALID_ARGUMENT))?; + verify_that!(res, status_is(StatusErrorCode::InvalidArgument))?; // Create new plaintexts. let pt_1 = generate_random_signed_vector(NUM_VALUES_1, MAX_ABSOLUTE_VALUE); @@ -951,7 +951,7 @@ mod test { let (ct_1, _) = ahe.encrypt(&pt_1, &pk, &mut prng)?; let (mut ct_2, _) = ahe.encrypt(&pt_2, &pk, &mut prng)?; let res = ahe.add_ciphertexts_in_place(&ct_1, &mut ct_2); - verify_that!(res, status_is(StatusErrorCode::INVALID_ARGUMENT))?; + verify_that!(res, status_is(StatusErrorCode::InvalidArgument))?; // Create new ciphertexts. let (ct_1, _) = ahe.encrypt(&pt_1, &pk, &mut prng)?; @@ -974,7 +974,7 @@ mod test { // Recovery, check that we can't combine different lengths. let res = ahe.recover(&partial_decryption_1, &ct_2.component_b, None); - verify_that!(res, status_is(StatusErrorCode::INVALID_ARGUMENT))?; + verify_that!(res, status_is(StatusErrorCode::InvalidArgument))?; Ok(()) } diff --git a/willow/crypto/zk_linear_ip.rs b/willow/crypto/zk_linear_ip.rs index eef8129..3fc23c5 100644 --- a/willow/crypto/zk_linear_ip.rs +++ b/willow/crypto/zk_linear_ip.rs @@ -350,7 +350,7 @@ mod tests { let mut transcript = MerlinTranscript::new(b"linear_ip_zkp_test"); verify_that!( verifier.verify(&statement, &proof, &mut transcript), - status_is(status::StatusErrorCode::PERMISSION_DENIED) + status_is(status::StatusErrorCode::PermissionDenied) ) } } diff --git a/willow/protocol/willow_v1_verifier.rs b/willow/protocol/willow_v1_verifier.rs index 9217aea..e213ac6 100644 --- a/willow/protocol/willow_v1_verifier.rs +++ b/willow/protocol/willow_v1_verifier.rs @@ -366,7 +366,7 @@ mod tests { setup .verifier .verify_and_include(setup.decryption_request_contribution, &mut verifier_state,), - status_is(status::StatusErrorCode::FAILED_PRECONDITION) + status_is(status::StatusErrorCode::FailedPrecondition) .with_message(contains_substring("already processed.")) ) } @@ -388,7 +388,7 @@ mod tests { setup .verifier .verify_and_include(setup.decryption_request_contribution, &mut verifier_state,), - status_is(status::StatusErrorCode::INVALID_ARGUMENT).with_message(eq( + status_is(status::StatusErrorCode::InvalidArgument).with_message(eq( "`nonce_bounds.0` must be less than or equal to `nonce_bounds.1`" )) ) @@ -410,7 +410,7 @@ mod tests { // Try to merge the states, should fail. verify_that!( setup.verifier.merge_states(verifier_state_1, verifier_state_2), - err(status_is(status::StatusErrorCode::INVALID_ARGUMENT).with_message(eq( + err(status_is(status::StatusErrorCode::InvalidArgument).with_message(eq( "`nonce_bounds.0` must be less than or equal to `nonce_bounds.1`" ))) ) @@ -462,7 +462,7 @@ mod tests { verify_that!( &setup.verifier.create_partial_decryption_request(verifier_state), - err(status_is(status::StatusErrorCode::FAILED_PRECONDITION) + err(status_is(status::StatusErrorCode::FailedPrecondition) .with_message(contains_substring("at least one client message "))) ) } diff --git a/willow/tests/willow_v1_shell.rs b/willow/tests/willow_v1_shell.rs index 6c03e1e..6142ee3 100644 --- a/willow/tests/willow_v1_shell.rs +++ b/willow/tests/willow_v1_shell.rs @@ -488,7 +488,7 @@ fn encrypt_decrypt_multiple_clients_including_invalid_proofs() -> googletest::Re server.split_client_message(client_message).unwrap(); verify_that!( verifier.verify_and_include(decryption_request_contribution, &mut verifier_state), - status_is(StatusErrorCode::PERMISSION_DENIED) + status_is(StatusErrorCode::PermissionDenied) )?; }