annotate libpam-sys/libpam-sys-test/build.rs @ 125:2b255c92417b

Introduce base PAM functions; use the real X/SSO PAM header for tests.
author Paul Fisher <paul@pfish.zone>
date Mon, 30 Jun 2025 17:47:32 -0400
parents f469b8d9ad78
children c77846f3a979
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
2 use libpam_sys_impls::__pam_impl_enum__;
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
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
8 // We're using the macro directly so we can match exhaustively.
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
9 __pam_impl_enum__!();
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
10
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 fn main() {
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
12 let config = match PamImpl::CURRENT {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
13 PamImpl::LinuxPam => TestConfig {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
14 headers: vec![
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
15 "<security/_pam_types.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
16 "<security/pam_appl.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
17 "<security/pam_ext.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
18 "<security/pam_modules.h>",
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
19 ],
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
20 ignore_consts: vec![
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
21 "__LINUX_PAM__",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
22 "__LINUX_PAM_MINOR__",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
23 "PAM_AUTHTOK_RECOVER_ERR",
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
24 ],
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
25 ..Default::default()
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
26 },
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
27 PamImpl::OpenPam => TestConfig {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
28 headers: vec![
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
29 "<security/pam_types.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
30 "<security/openpam.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
31 "<security/pam_appl.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
32 "<security/pam_constants.h>",
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
33 ],
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
34 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
35 ..Default::default()
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
36 },
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
37 PamImpl::Sun => TestConfig {
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
38 headers: vec!["<security/pam_appl.h>", "<security/pam_modules.h>"],
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
39 block_headers: vec!["sys/.*"],
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 },
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
42 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
43 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
44 ignore_consts: vec!["PAM_CRED_PRELIM_CHECK"],
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
45 ..Default::default()
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
46 },
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
47 };
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
48 generate_const_test(&config);
110
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
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
51 fn generate_const_test(config: &TestConfig) {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
52 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
53 .header_contents("_.h", &config.header_contents())
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 .merge_extern_blocks(true)
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 .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
56 .blocklist_type(".*")
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 .blocklist_function(".*")
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 .allowlist_var(".*")
114
93d423b65555 Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
59 .default_macro_constant_type(MacroTypeVariation::Signed);
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
60 for hdr in config.block_headers.iter() {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
61 builder = builder.blocklist_file(".*?/".to_owned() + hdr)
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
62 }
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 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
65 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
66 let mut tests = vec![];
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 tests.push("{".into());
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 tests.extend(
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 file.items
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 .iter()
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 .filter_map(|item| {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 if let Item::Const(item) = item {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 Some(item)
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 } else {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
75 None
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 }
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 .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
79 .cloned()
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
80 .map(|mut item| {
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
81 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
82 qself: None,
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
83 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
84 }));
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 format!(
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 "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
87 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
88 name = item.ident
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 }),
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 tests.push("}".into());
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 fs::write(
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 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
95 tests.join("\n"),
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 .unwrap();
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
98 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
99
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
100 #[derive(Default)]
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
101 struct TestConfig {
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
102 headers: Vec<&'static str>,
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
103 block_headers: Vec<&'static str>,
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
104 ignore_consts: Vec<&'static str>,
110
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
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
107 impl TestConfig {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
108 fn header_contents(&self) -> String {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
109 let vec: Vec<_> = self
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
110 .headers
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
111 .iter()
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
112 .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
113 .collect();
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
114 vec.join("")
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
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
117 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
118 !self
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
119 .ignore_consts
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
120 .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
121 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
122 }