Debian Codegraph @

third_party/rust/ohttp/src/nss/err.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 #![allow(
8 dead_code,
9 clippy::upper_case_acronyms,
10 clippy::module_name_repetitions
11 )]
12
13 use super::{SECStatus, SECSuccess};
14 use crate::err::Res;
15 use std::os::raw::c_char;
16
17 include!(concat!(env!("OUT_DIR"), "/nspr_error.rs"));
18 mod codes {
19 #![allow(non_snake_case)]
20 include!(concat!(env!("OUT_DIR"), "/nss_secerr.rs"));
21 }
22 pub use codes::SECErrorCodes as sec;
23 pub mod nspr {
24 include!(concat!(env!("OUT_DIR"), "/nspr_err.rs"));
25 }
26
27 #[derive(Clone, Debug, PartialEq, Eq)]
28 pub struct Error {
29 name: String,
30 code: PRErrorCode,
31 desc: String,
32 }
33
34 impl Error {
35 /// Get an internal error.
36 pub(crate) fn internal() -> Self {
37 Self::from(sec::SEC_ERROR_LIBRARY_FAILURE)
38 }
39
40 /// Get the last error, as returned by `PR_GetError()`.
41 pub(crate) fn last() -> crate::Error {
42 crate::Error::from(Self::from(unsafe { PR_GetError() }))
43 }
44 }
45
46 impl From<PRErrorCode> for Error {
47 fn from(code: PRErrorCode) -> Self {
48 let name = wrap_str_fn(|| unsafe { PR_ErrorToName(code) }, "UNKNOWN_ERROR");
49 let desc = wrap_str_fn(
50 || unsafe { PR_ErrorToString(code, PR_LANGUAGE_I_DEFAULT) },
51 "...",
52 );
53 Error { name, code, desc }
54 }
55 }
56
57 impl std::error::Error for Error {}
58
59 impl std::fmt::Display for Error {
60 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
61 write!(f, "Error {} ({}): {}", self.name, self.code, self.desc)
62 }
63 }
64
65 use std::ffi::CStr;
66
67 fn wrap_str_fn<F>(f: F, dflt: &str) -> String
68 where
69 F: FnOnce() -> *const c_char,
70 {
71 unsafe {
72 let p = f();
73 if p.is_null() {
74 return dflt.to_string();
75 }
76 CStr::from_ptr(p).to_string_lossy().into_owned()
77 }
78 }
79
80 pub fn secstatus_to_res(rv: SECStatus) -> Res<()> {
81 if rv == SECSuccess {
82 Ok(())
83 } else {
84 Err(Error::last())
85 }
86 }
87
88 #[cfg(test)]
89 mod tests {
90 use super::{
91 super::{init, SECFailure, SECSuccess},
92 secstatus_to_res, PRErrorCode, PR_SetError,
93 };
94
95 fn set_error_code(code: PRErrorCode) {
96 // This code doesn't work without initializing NSS first.
97 init();
98 unsafe {
99 PR_SetError(code, 0);
100 }
101 }
102
103 #[test]
104 fn error_code() {
105 init();
106 assert_eq!(166 - 0x2000, super::sec::SEC_ERROR_LIBPKIX_INTERNAL);
107 assert_eq!(-5998, super::nspr::PR_WOULD_BLOCK_ERROR);
108 }
109
110 #[test]
111 fn is_ok() {
112 assert!(secstatus_to_res(SECSuccess).is_ok());
113 }
114
115 #[test]
116 fn is_err() {
117 set_error_code(super::sec::SEC_ERROR_BAD_DATABASE);
118 let r = secstatus_to_res(SECFailure);
119 assert!(r.is_err());
120 if let crate::Error::Crypto(e) = r.unwrap_err() {
121 assert_eq!(e.name, "SEC_ERROR_BAD_DATABASE");
122 assert_eq!(e.code, 18 - 0x2000);
123 assert_eq!(e.desc, "security library: bad database.");
124 } else {
125 panic!();
126 }
127 }
128
129 #[test]
130 fn is_err_zero_code() {
131 set_error_code(0);
132 let r = secstatus_to_res(SECFailure);
133 assert!(r.is_err());
134 if let crate::Error::Crypto(e) = r.unwrap_err() {
135 assert_eq!(e.name, "UNKNOWN_ERROR");
136 assert_eq!(e.code, 0);
137 } else {
138 panic!();
139 }
140 }
141 }