Mercurial > crates > nonstick
annotate libpam-sys/libpam-sys-test/build.rs @ 118:39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Also formats and assorted other cleanup.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 29 Jun 2025 20:13:03 -0400 |
parents | 93d423b65555 |
children | 397743cb70e2 |
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; |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
2 use libpam_sys_impls::cfg_pam_impl; |
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() { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
9 generate_const_test(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 #[cfg_pam_impl("LinuxPam")] |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 fn test_config() -> TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
14 TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 headers: vec![ |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
16 "security/_pam_types.h".into(), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
17 "security/pam_appl.h".into(), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
18 "security/pam_ext.h".into(), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 "security/pam_modules.h".into(), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 ], |
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
114
diff
changeset
|
21 ignore_consts: vec![ |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
114
diff
changeset
|
22 "__LINUX_PAM__".into(), |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
114
diff
changeset
|
23 "__LINUX_PAM_MINOR__".into(), |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
114
diff
changeset
|
24 "PAM_AUTHTOK_RECOVER_ERR".into(), |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
114
diff
changeset
|
25 ], |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
28 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
29 #[cfg_pam_impl("OpenPam")] |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 fn test_config() -> TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 headers: vec![ |
111
04105e9a7de8
Fix constants in libpam-sys-test.
Paul Fisher <paul@pfish.zone>
parents:
110
diff
changeset
|
33 "security/pam_types.h".into(), |
04105e9a7de8
Fix constants in libpam-sys-test.
Paul Fisher <paul@pfish.zone>
parents:
110
diff
changeset
|
34 "security/openpam.h".into(), |
04105e9a7de8
Fix constants in libpam-sys-test.
Paul Fisher <paul@pfish.zone>
parents:
110
diff
changeset
|
35 "security/pam_appl.h".into(), |
04105e9a7de8
Fix constants in libpam-sys-test.
Paul Fisher <paul@pfish.zone>
parents:
110
diff
changeset
|
36 "security/pam_constants.h".into(), |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
37 ], |
114
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
38 ignore_consts: vec![ |
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
39 "OPENPAM_VERSION".into(), |
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
40 "OPENPAM_RELEASE".into(), |
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
41 "PAM_SOEXT".into(), |
93d423b65555
Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
42 ], |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
43 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
44 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 #[cfg_pam_impl(not(any("LinuxPam", "OpenPam")))] |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
47 fn test_config() -> TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 panic!("This PAM implementation is not yet tested.") |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
49 } |
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 fn generate_const_test() { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
52 let config = test_config(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 let builder = bindgen::Builder::default() |
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); |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 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
|
63 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
|
64 let mut tests = vec![]; |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 tests.push("{".into()); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 tests.extend( |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 file.items |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 .iter() |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 .filter_map(|item| { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 if let Item::Const(item) = item { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
71 Some(item) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
72 } else { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
73 None |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 }) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 .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
|
77 .cloned() |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
78 .map(|mut item| { |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
79 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
|
80 qself: None, |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
111
diff
changeset
|
81 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
|
82 })); |
110
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
83 format!( |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
84 "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
|
85 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
|
86 name = item.ident |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
87 ) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
88 }), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
89 ); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
90 tests.push("}".into()); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
91 fs::write( |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
92 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
|
93 tests.join("\n"), |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
94 ) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 .unwrap(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 } |
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 struct TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 headers: Vec<String>, |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 ignore_consts: Vec<String>, |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
101 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
102 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
103 impl TestConfig { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
104 fn header_contents(&self) -> String { |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
105 let vec: Vec<_> = self |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
106 .headers |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
107 .iter() |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 .map(|h| format!("#include <{h}>\n")) |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 .collect(); |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
110 vec.join("") |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
111 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
113 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
|
114 !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
|
115 } |
2346fd501b7a
Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
116 } |