Mercurial > crates > nonstick
annotate libpam-sys/libpam-sys-test/build.rs @ 171:e27c5c667a5a
Create full new types for return code and flags, separate end to end.
This plumbs the ReturnCode and RawFlags types through the places where
we call into or are called from PAM.
Also adds Sun documentation to the project.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Fri, 25 Jul 2025 20:52:14 -0400 |
parents | 3a7cf05d2b5f |
children | 0730f5f2ee2a |
rev | line source |
---|---|
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
1 use bindgen::MacroTypeVariation; |
148
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
2 use libpam_sys_consts::pam_impl::PamImpl; |
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
3 use libpam_sys_consts::{pam_impl, pam_impl_name}; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
127
diff
changeset
|
4 use proc_macro2::{Group, Ident, TokenStream, TokenTree}; |
113
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
5 use quote::{format_ident, ToTokens}; |
127 | 6 use std::path::Path; |
7 use std::process::Command; | |
8 use std::str::FromStr; | |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
9 use std::{env, fs}; |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
10 use syn::{Item, ItemConst}; |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
12 const REDIR_FD: &str = "pam_modutil_redirect_fd"; |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
13 |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
14 fn main() { |
136
efbc235f01d3
Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
134
diff
changeset
|
15 pam_impl::enable_pam_impl_cfg(); |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
16 let config = match PamImpl::CURRENT { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
17 PamImpl::LinuxPam => TestConfig { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
18 headers: vec![ |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
19 "<security/_pam_types.h>", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
20 "<security/pam_appl.h>", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
21 "<security/pam_ext.h>", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
22 "<security/pam_modules.h>", |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
23 "<security/pam_modutil.h>", |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
24 ], |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
25 allow_types: vec![REDIR_FD], |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
26 ignore_consts: vec![ |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
27 "__LINUX_PAM__", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
28 "__LINUX_PAM_MINOR__", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
29 "PAM_AUTHTOK_RECOVER_ERR", |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
30 ], |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
31 }, |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
32 PamImpl::OpenPam => TestConfig { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
33 headers: vec![ |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
34 "<security/pam_types.h>", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
35 "<security/openpam.h>", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
36 "<security/pam_appl.h>", |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
37 "<security/pam_constants.h>", |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
38 ], |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
39 ignore_consts: vec!["OPENPAM_VERSION", "OPENPAM_RELEASE", "PAM_SOEXT"], |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
40 ..Default::default() |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
41 }, |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
42 PamImpl::Sun => TestConfig { |
137
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
43 headers: vec![ |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
44 "<security/pam_appl.h>", |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
45 "<security/pam_modules.h>", |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
46 "\"illumos_pam_impl.h\"", |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
47 ], |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
48 ..Default::default() |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
49 }, |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
50 PamImpl::XSso => TestConfig { |
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
124
diff
changeset
|
51 headers: vec!["\"xsso_pam_appl.h\""], |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
52 ..Default::default() |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
53 }, |
134
6c1e1bdb4164
Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents:
132
diff
changeset
|
54 other => panic!("PAM implementation {other:?} is not yet tested"), |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
55 }; |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
56 generate_const_test(&config); |
127 | 57 generate_ctest(&config); |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
59 |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
60 fn generate_const_test(config: &TestConfig) { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
61 let mut builder = bindgen::Builder::default() |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 .header_contents("_.h", &config.header_contents()) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 .merge_extern_blocks(true) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
65 .allowlist_var("(OPEN)?PAM_.*") |
114
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
66 .default_macro_constant_type(MacroTypeVariation::Signed); |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
67 |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
68 for &typ in config.allow_types.iter() { |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
69 builder = builder.allowlist_type(typ); |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
70 } |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
71 |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
72 let generated = builder.generate().unwrap(); |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
73 generated.write_to_file(test_file("bindgen.rs")).unwrap(); |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
74 let file = syn::parse_file(&generated.to_string()).unwrap(); |
137
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
75 let mut tests = vec![ |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
76 "#[allow(deprecated, overflowing_literals)]".into(), |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
77 "fn main() {".into(), |
127 | 78 format!( |
136
efbc235f01d3
Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
134
diff
changeset
|
79 "assert_eq!(libpam_sys::pam_impl::PamImpl::CURRENT, libpam_sys::pam_impl::PamImpl::{:?});", |
127 | 80 PamImpl::CURRENT |
81 ), | |
82 ]; | |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
83 tests.extend( |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
84 file.items |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
85 .iter() |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
86 .filter_map(|item| { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
87 if let Item::Const(item) = item { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
88 Some(item) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
89 } else { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
90 None |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
91 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
92 }) |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
93 .filter(|&item| config.should_check_const(item)) |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
94 .map(|item| { |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
95 let name = item.ident.to_string(); |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
96 if let Some(stripped) = name.strip_prefix(&format!("{REDIR_FD}_")) { |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
138
diff
changeset
|
97 format!("\ |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
138
diff
changeset
|
98 assert_eq!(generated::{name} as i32, libpam_sys::{REDIR_FD}::{stripped}.into());\ |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
138
diff
changeset
|
99 assert_eq!((generated::{name} as i32).try_into(), Ok(libpam_sys::{REDIR_FD}::{stripped}));\ |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
138
diff
changeset
|
100 ") |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
101 } else { |
132
0b6a17f8c894
Get constant test working again with OpenPAM.
Paul Fisher <paul@pfish.zone>
parents:
131
diff
changeset
|
102 format!("assert_eq!(generated::{name} as i32, libpam_sys::{name});") |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
103 } |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
104 }), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
105 ); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
106 tests.push("}".into()); |
127 | 107 let const_test = test_file("constant_test.rs"); |
108 fs::write(&const_test, tests.join("\n")).unwrap(); | |
109 rustfmt(&const_test); | |
110 } | |
111 | |
112 fn generate_ctest(config: &TestConfig) { | |
113 let mut test = ctest::TestGenerator::new(); | |
136
efbc235f01d3
Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
134
diff
changeset
|
114 test.cfg("pam_impl", Some(pam_impl_name!())); |
127 | 115 |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
116 for &header in config.headers.iter() { |
127 | 117 if header.starts_with('"') { |
118 test.include(env::var("CARGO_MANIFEST_DIR").unwrap()); | |
119 } | |
120 test.header(&header[1..header.len() - 1]); | |
121 } | |
122 // These are opaque structs. | |
123 test.skip_struct(|name| matches!(name, "pam_handle" | "AppData")); | |
124 test.skip_type(|name| matches!(name, "ConversationCallback" | "CleanupCallback")); | |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
125 test.type_name(|name, is_struct, is_union| { |
127 | 126 assert!(!is_union); // we scabbin' |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
127 match (name, is_struct) { |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
128 ("AppData", _) => "void".into(), |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
129 (REDIR_FD, _) => format!("enum {REDIR_FD}"), |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
130 ("passwd", _) => "struct passwd".into(), |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
131 ("group", _) => "struct group".into(), |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
132 ("spwd", _) => "struct spwd".into(), |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
133 (name, true) => format!("struct {name}"), |
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
134 (other, false) => other.into(), |
127 | 135 } |
136 }); | |
137
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
137 test.field_name(|_, name| { |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
138 match name { |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
139 "type_" => "type", |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
140 other => other, |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
141 } |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
142 .into() |
88627c057709
Add Sun PAM functions from Illumos headers.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
143 }); |
127 | 144 |
145 // | |
146 // Welcome to THE HACK ZONE. | |
147 // | |
148 | |
149 // Define away constness because the various PAM implementations | |
150 // have different const annotations and this will surely drive you crazy. | |
151 test.define("const", Some("")); | |
152 | |
153 // Also replace all the `const`s with `mut`s in the ffi.rs file. | |
136
efbc235f01d3
Separate libpam-sys-helpers from libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
134
diff
changeset
|
154 let file_contents = include_str!("../src/lib.rs"); |
127 | 155 let deconsted_file = test_file("ffi.rs"); |
156 remove_consts(file_contents, &deconsted_file); | |
157 | |
158 test.generate(&deconsted_file, "ctest.rs"); | |
159 } | |
160 | |
161 fn remove_consts(file_contents: &str, out_file: impl AsRef<Path>) { | |
151
3a7cf05d2b5f
Fix libpam-sys-test from panicking over `mod aliases`.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
162 let lines: Vec<_> = file_contents |
3a7cf05d2b5f
Fix libpam-sys-test from panicking over `mod aliases`.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
163 .lines() |
3a7cf05d2b5f
Fix libpam-sys-test from panicking over `mod aliases`.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
164 .filter(|&l| !l.starts_with("pub mod")) |
3a7cf05d2b5f
Fix libpam-sys-test from panicking over `mod aliases`.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
165 .collect(); |
3a7cf05d2b5f
Fix libpam-sys-test from panicking over `mod aliases`.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
166 let file_contents = lines.join("\n"); |
127 | 167 let deconstified = deconstify( |
151
3a7cf05d2b5f
Fix libpam-sys-test from panicking over `mod aliases`.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
168 TokenStream::from_str(&file_contents).unwrap(), |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
127
diff
changeset
|
169 &format_ident!("mut"), |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
170 ) |
127 | 171 .to_string(); |
172 let out_file = out_file.as_ref(); | |
173 fs::write(out_file, deconstified).unwrap(); | |
174 rustfmt(out_file) | |
175 } | |
176 | |
177 fn rustfmt(file: impl AsRef<Path>) { | |
178 let status = Command::new(env!("CARGO")) | |
179 .args(["fmt", "--", file.as_ref().to_str().unwrap()]) | |
180 .status() | |
181 .unwrap(); | |
182 assert!(status.success(), "rustfmt exited with code {status}"); | |
183 } | |
184 | |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
127
diff
changeset
|
185 fn deconstify(stream: TokenStream, mut_token: &Ident) -> TokenStream { |
127 | 186 TokenStream::from_iter(stream.into_iter().map(|token| { |
187 match token { | |
188 TokenTree::Group(g) => { | |
189 TokenTree::Group(Group::new(g.delimiter(), deconstify(g.stream(), mut_token))) | |
190 .into_token_stream() | |
191 } | |
148
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
192 // Remove all 'consts' from the file and replace them with 'mut'. |
127 | 193 TokenTree::Ident(id) if id == "const" => mut_token.into_token_stream(), |
194 other => other.into_token_stream(), | |
195 } | |
196 })) | |
197 } | |
198 | |
199 fn test_file(name: impl AsRef<str>) -> String { | |
200 format!("{}/{}", env::var("OUT_DIR").unwrap(), name.as_ref()) | |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
201 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
202 |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
203 #[derive(Default)] |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
204 struct TestConfig { |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
205 headers: Vec<&'static str>, |
131
a632a8874131
Get all the Linux-PAM functions into libpam-sys, and get tests right.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
206 allow_types: Vec<&'static str>, |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
207 ignore_consts: Vec<&'static str>, |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
208 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
209 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
210 impl TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
211 fn header_contents(&self) -> String { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
212 let vec: Vec<_> = self |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
213 .headers |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
214 .iter() |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
215 .map(|h| format!("#include {h}\n")) |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
216 .collect(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
217 vec.join("") |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
218 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
219 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
220 fn should_check_const(&self, item: &ItemConst) -> bool { |
124
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
221 !self |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
222 .ignore_consts |
f469b8d9ad78
Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents:
122
diff
changeset
|
223 .contains(&item.ident.to_string().as_ref()) |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
224 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
225 } |