Debian Codegraph @

third_party/rust/neqo-crypto/src/result.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use crate::{
8 err::{nspr, Error, PR_ErrorToName, PR_ErrorToString, PR_GetError, Res, PR_LANGUAGE_I_DEFAULT},
9 ssl,
10 };
11
12 use std::ffi::CStr;
13
14 pub fn result(rv: ssl::SECStatus) -> Res<()> {
15 _ = result_helper(rv, false)?;
16 Ok(())
17 }
18
19 pub fn result_or_blocked(rv: ssl::SECStatus) -> Res<bool> {
20 result_helper(rv, true)
21 }
22
23 fn wrap_str_fn<F>(f: F, dflt: &str) -> String
24 where
25 F: FnOnce() -> *const i8,
26 {
27 unsafe {
28 let p = f();
29 if p.is_null() {
30 return dflt.to_string();
31 }
32 CStr::from_ptr(p).to_string_lossy().into_owned()
33 }
34 }
35
36 fn result_helper(rv: ssl::SECStatus, allow_blocked: bool) -> Res<bool> {
37 if rv == ssl::_SECStatus_SECSuccess {
38 return Ok(false);
39 }
40
41 let code = unsafe { PR_GetError() };
42 if allow_blocked && code == nspr::PR_WOULD_BLOCK_ERROR {
43 return Ok(true);
44 }
45
46 let name = wrap_str_fn(|| unsafe { PR_ErrorToName(code) }, "UNKNOWN_ERROR");
47 let desc = wrap_str_fn(
48 || unsafe { PR_ErrorToString(code, PR_LANGUAGE_I_DEFAULT) },
49 "...",
50 );
51 Err(Error::NssError { name, code, desc })
52 }
53
54 #[cfg(test)]
55 mod tests {
56 use super::{result, result_or_blocked};
57 use crate::{
58 err::{self, nspr, Error, PRErrorCode, PR_SetError},
59 ssl,
60 };
61 use test_fixture::fixture_init;
62
63 fn set_error_code(code: PRErrorCode) {
64 unsafe { PR_SetError(code, 0) };
65 }
66
67 #[test]
68 fn is_ok() {
69 assert!(result(ssl::SECSuccess).is_ok());
70 }
71
72 #[test]
73 fn is_err() {
74 // This code doesn't work without initializing NSS first.
75 fixture_init();
76
77 set_error_code(err::ssl::SSL_ERROR_BAD_MAC_READ);
78 let r = result(ssl::SECFailure);
79 assert!(r.is_err());
80 match r.unwrap_err() {
81 Error::NssError { name, code, desc } => {
82 assert_eq!(name, "SSL_ERROR_BAD_MAC_READ");
83 assert_eq!(code, -12273);
84 assert_eq!(
85 desc,
86 "SSL received a record with an incorrect Message Authentication Code"
87 );
88 }
89 _ => unreachable!(),
90 }
91 }
92
93 #[test]
94 fn is_err_zero_code() {
95 // This code doesn't work without initializing NSS first.
96 fixture_init();
97
98 set_error_code(0);
99 let r = result(ssl::SECFailure);
100 assert!(r.is_err());
101 match r.unwrap_err() {
102 Error::NssError { name, code, .. } => {
103 assert_eq!(name, "UNKNOWN_ERROR");
104 assert_eq!(code, 0);
105 // Note that we don't test |desc| here because that comes from
106 // strerror(0), which is platform-dependent.
107 }
108 _ => unreachable!(),
109 }
110 }
111
112 #[test]
113 fn blocked_as_error() {
114 // This code doesn't work without initializing NSS first.
115 fixture_init();
116
117 set_error_code(nspr::PR_WOULD_BLOCK_ERROR);
118 let r = result(ssl::SECFailure);
119 assert!(r.is_err());
120 match r.unwrap_err() {
121 Error::NssError { name, code, desc } => {
122 assert_eq!(name, "PR_WOULD_BLOCK_ERROR");
123 assert_eq!(code, -5998);
124 assert_eq!(desc, "The operation would have blocked");
125 }
126 _ => panic!("bad error type"),
127 }
128 }
129
130 #[test]
131 fn is_blocked() {
132 set_error_code(nspr::PR_WOULD_BLOCK_ERROR);
133 assert!(result_or_blocked(ssl::SECFailure).unwrap());
134 }
135 }