1 use cfg_if::cfg_if;
2 use openssl::error::Error;
3
4 openssl_errors::openssl_errors! {
5 library Test("test library") {
6 functions {
7 FOO("function foo");
8 BAR("function bar");
9 }
10
11 reasons {
12 NO_MILK("out of milk");
13 NO_BACON("out of bacon");
14 }
15 }
16 }
17
18 #[test]
basic()19 fn basic() {
20 openssl_errors::put_error!(Test::FOO, Test::NO_MILK);
21
22 let error = Error::get().unwrap();
23 assert_eq!(error.library().unwrap(), "test library");
24 assert_eq!(error.function().unwrap(), "function foo");
25 assert_eq!(error.reason().unwrap(), "out of milk");
26 // Replace Windows `\` separators with `/`
27 assert_eq!(
28 error.file().replace('\\', "/"),
29 "openssl-errors/tests/test.rs"
30 );
31 assert_eq!(error.line(), line!() - 11);
32 cfg_if! {
33 if #[cfg(ossl300)] {
34 // https://github.com/openssl/openssl/issues/12530
35 assert!(error.data().is_none() || error.data() == Some(""));
36 } else {
37 assert_eq!(error.data(), None);
38 }
39 }
40 }
41
42 #[test]
static_data()43 fn static_data() {
44 openssl_errors::put_error!(Test::BAR, Test::NO_BACON, "foobar {{}}");
45
46 let error = Error::get().unwrap();
47 assert_eq!(error.library().unwrap(), "test library");
48 assert_eq!(error.function().unwrap(), "function bar");
49 assert_eq!(error.reason().unwrap(), "out of bacon");
50 // Replace Windows `\` separators with `/`
51 assert_eq!(
52 error.file().replace('\\', "/"),
53 "openssl-errors/tests/test.rs"
54 );
55 assert_eq!(error.line(), line!() - 11);
56 assert_eq!(error.data(), Some("foobar {}"));
57 }
58
59 #[test]
dynamic_data()60 fn dynamic_data() {
61 openssl_errors::put_error!(Test::BAR, Test::NO_MILK, "hello {}", "world");
62
63 let error = Error::get().unwrap();
64 assert_eq!(error.library().unwrap(), "test library");
65 assert_eq!(error.function().unwrap(), "function bar");
66 assert_eq!(error.reason().unwrap(), "out of milk");
67 // Replace Windows `\` separators with `/`
68 assert_eq!(
69 error.file().replace('\\', "/"),
70 "openssl-errors/tests/test.rs"
71 );
72 assert_eq!(error.line(), line!() - 11);
73 assert_eq!(error.data(), Some("hello world"));
74 }
75
76 #[test]
deferred_error_render()77 fn deferred_error_render() {
78 openssl_errors::put_error!(Test::BAR, Test::NO_MILK);
79
80 let error = Error::get().unwrap();
81
82 for _ in 0..100 {
83 openssl_errors::put_error!(Test::FOO, Test::NO_BACON);
84 }
85
86 assert_eq!(error.function().unwrap(), "function bar");
87 // Replace Windows `\` separators with `/`
88 assert_eq!(
89 error.file().replace('\\', "/"),
90 "openssl-errors/tests/test.rs"
91 );
92
93 // clear out the stack for other tests on the same thread
94 while Error::get().is_some() {}
95 }
96