Mercurial > crates > nonstick
annotate libpam-sys/libpam-sys-test/build.rs @ 121:397743cb70e2
Make libpam-sys-tests work on Illumos!
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Mon, 30 Jun 2025 03:43:48 -0400 |
parents | 39760dfc9b3b |
children | 9e05e44050d0 |
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; |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
2 use libpam_sys::PamImpl; |
113
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
3 use quote::{format_ident, ToTokens}; |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
4 use std::path::PathBuf; |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 use std::{env, fs}; |
114
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
6 use syn::{Item, ItemConst, Type, TypePath}; |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 fn main() { |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
9 let config = match PamImpl::CURRENT { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
10 PamImpl::LinuxPam => TestConfig { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
11 headers: vec![ |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
12 "security/_pam_types.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
13 "security/pam_appl.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
14 "security/pam_ext.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
15 "security/pam_modules.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
16 ], |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
17 ignore_consts: vec![ |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
18 "__LINUX_PAM__".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
19 "__LINUX_PAM_MINOR__".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
20 "PAM_AUTHTOK_RECOVER_ERR".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
21 ], |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
22 ..Default::default() |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
23 }, |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
24 PamImpl::OpenPam => TestConfig { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
25 headers: vec![ |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
26 "security/pam_types.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
27 "security/openpam.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
28 "security/pam_appl.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
29 "security/pam_constants.h".into(), |
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 ignore_consts: vec![ |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
32 "OPENPAM_VERSION".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
33 "OPENPAM_RELEASE".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
34 "PAM_SOEXT".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
35 ], |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
36 ..Default::default() |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
37 }, |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
38 PamImpl::Sun => TestConfig { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
39 headers: vec![ |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
40 "security/pam_appl.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
41 "security/pam_modules.h".into(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
42 ], |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
43 block_headers: vec!["sys/types.h".into()], |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
44 ..Default::default() |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
45 }, |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
46 PamImpl::XSso => Default::default(), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
47 other => panic!("Unknown PAM implementation {other:?}"), |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
48 }; |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
49 generate_const_test(&config); |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
52 fn generate_const_test(config: &TestConfig) { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
53 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
|
54 .header_contents("_.h", &config.header_contents()) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 .merge_extern_blocks(true) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 .blocklist_type(".*") |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 .blocklist_function(".*") |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
59 .allowlist_var(".*") |
114
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
60 .default_macro_constant_type(MacroTypeVariation::Signed); |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
61 for hdr in config.block_headers.iter() { |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
62 builder = builder.blocklist_file(".*?/".to_owned() + hdr) |
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
63 } |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 let generated = builder.generate().unwrap().to_string(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 let file = syn::parse_file(&generated).unwrap(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 let mut tests = vec![]; |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 tests.push("{".into()); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 tests.extend( |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 file.items |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
71 .iter() |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
72 .filter_map(|item| { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
73 if let Item::Const(item) = item { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 Some(item) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 } else { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 None |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
77 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
78 }) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
79 .filter(|item| config.should_check_const(item)) |
113
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
80 .cloned() |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
81 .map(|mut item| { |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
82 item.ty = Box::new(Type::Path(TypePath { |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
83 qself: None, |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
84 path: format_ident!("i32").into(), |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
85 })); |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
86 format!( |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
87 "assert_eq!({tokens}, libpam_sys::{name});", |
113
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
88 tokens = item.expr.to_token_stream(), |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
89 name = item.ident |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
90 ) |
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 ); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
93 tests.push("}".into()); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
94 fs::write( |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 PathBuf::from(env::var("OUT_DIR").unwrap()).join("constant_test.rs"), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 tests.join("\n"), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 ) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 .unwrap(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
101 #[derive(Default)] |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
102 struct TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
103 headers: Vec<String>, |
121
397743cb70e2
Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents:
118
diff
changeset
|
104 block_headers: Vec<String>, |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
105 ignore_consts: Vec<String>, |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
106 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
107 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 impl TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 fn header_contents(&self) -> String { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
110 let vec: Vec<_> = self |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
111 .headers |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 .iter() |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
113 .map(|h| format!("#include <{h}>\n")) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
114 .collect(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
115 vec.join("") |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
116 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
117 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
118 fn should_check_const(&self, item: &ItemConst) -> bool { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
119 !self.ignore_consts.contains(&item.ident.to_string()) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
120 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
121 } |