Skip to content

Commit 0cdd271

Browse files
committed
tests: move session joining tests to their own process
Fixes: #39
1 parent ca18653 commit 0cdd271

File tree

5 files changed

+128
-59
lines changed

5 files changed

+128
-59
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ members = ["keyutils-raw"]
1616
[dev-dependencies]
1717
lazy_static = "1"
1818
regex = "1"
19-
serial_test = "*"
20-
serial_test_derive = "*"
2119
semver = "*"
2220

2321
[dependencies]

src/tests/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ mod permitting;
4040
mod reading;
4141
mod revoke;
4242
mod search;
43-
// FIXME(#39): These tests fail when run in the same process. Something should be done about this.
44-
// mod session;
4543
mod timeout;
4644
mod unlink;
4745
mod update;

tests/join_anonymous_session.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2019, Ben Boeckel
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without modification,
5+
// are permitted provided that the following conditions are met:
6+
//
7+
// * Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
// * Neither the name of this project nor the names of its contributors
13+
// may be used to endorse or promote products derived from this software
14+
// without specific prior written permission.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
use keyutils::keytypes;
28+
use keyutils::{KeyType, Keyring, Permission, SpecialKeyring};
29+
30+
fn getuid() -> libc::uid_t {
31+
unsafe { libc::getuid() }
32+
}
33+
34+
fn getgid() -> libc::gid_t {
35+
unsafe { libc::getgid() }
36+
}
37+
38+
#[test]
39+
fn join_anonymous_session() {
40+
let session_before = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
41+
let keyring = Keyring::join_anonymous_session().unwrap();
42+
let session_after = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
43+
44+
assert_ne!(session_before, keyring);
45+
assert_eq!(session_after, keyring);
46+
47+
let desc = keyring.description().unwrap();
48+
assert_eq!(desc.type_, keytypes::Keyring::name());
49+
assert_eq!(desc.uid, getuid());
50+
assert_eq!(desc.gid, getgid());
51+
assert_eq!(
52+
desc.perms,
53+
Permission::POSSESSOR_ALL | Permission::USER_VIEW | Permission::USER_READ
54+
);
55+
assert_eq!(desc.description, "_ses");
56+
57+
keyring.invalidate().unwrap()
58+
}

src/tests/session.rs renamed to tests/join_existing_named_session.rs

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,65 +24,18 @@
2424
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2525
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

27-
use serial_test_derive::serial;
27+
use keyutils::keytypes;
28+
use keyutils::{KeyType, Keyring, Permission, SpecialKeyring};
2829

29-
use crate::keytypes;
30-
use crate::{KeyType, Keyring, Permission, SpecialKeyring};
31-
32-
use super::utils::kernel::*;
33-
34-
#[test]
35-
#[serial(join_session)]
36-
fn join_anonymous_session() {
37-
let session_before = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
38-
let keyring = Keyring::join_anonymous_session().unwrap();
39-
let session_after = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
40-
41-
assert_ne!(session_before, keyring);
42-
assert_eq!(session_after, keyring);
43-
44-
let desc = keyring.description().unwrap();
45-
assert_eq!(desc.type_, keytypes::Keyring::name());
46-
assert_eq!(desc.uid, *UID);
47-
assert_eq!(desc.gid, *GID);
48-
assert_eq!(
49-
desc.perms,
50-
Permission::POSSESSOR_ALL | Permission::USER_VIEW | Permission::USER_READ
51-
);
52-
assert_eq!(desc.description, "_ses");
53-
54-
keyring.invalidate().unwrap()
30+
fn getuid() -> libc::uid_t {
31+
unsafe { libc::getuid() }
5532
}
5633

57-
#[test]
58-
#[serial(join_session)]
59-
fn join_new_named_session() {
60-
let session_before = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
61-
let name = "join_new_named_session";
62-
let keyring = Keyring::join_session(name).unwrap();
63-
let session_after = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
64-
65-
assert_ne!(session_before, keyring);
66-
assert_eq!(session_after, keyring);
67-
68-
let desc = keyring.description().unwrap();
69-
assert_eq!(desc.type_, keytypes::Keyring::name());
70-
assert_eq!(desc.uid, *UID);
71-
assert_eq!(desc.gid, *GID);
72-
assert_eq!(
73-
desc.perms,
74-
Permission::POSSESSOR_ALL
75-
| Permission::USER_VIEW
76-
| Permission::USER_READ
77-
| Permission::USER_LINK
78-
);
79-
assert_eq!(desc.description, name);
80-
81-
keyring.invalidate().unwrap()
34+
fn getgid() -> libc::gid_t {
35+
unsafe { libc::getgid() }
8236
}
8337

8438
#[test]
85-
#[serial(join_session)]
8639
fn join_existing_named_session() {
8740
let name = "join_existing_named_session";
8841

@@ -95,8 +48,8 @@ fn join_existing_named_session() {
9548

9649
let desc = keyring.description().unwrap();
9750
assert_eq!(desc.type_, keytypes::Keyring::name());
98-
assert_eq!(desc.uid, *UID);
99-
assert_eq!(desc.gid, *GID);
51+
assert_eq!(desc.uid, getuid());
52+
assert_eq!(desc.gid, getgid());
10053
assert_eq!(
10154
desc.perms,
10255
Permission::POSSESSOR_ALL

tests/join_new_named_session.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2019, Ben Boeckel
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without modification,
5+
// are permitted provided that the following conditions are met:
6+
//
7+
// * Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
// * Neither the name of this project nor the names of its contributors
13+
// may be used to endorse or promote products derived from this software
14+
// without specific prior written permission.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
use keyutils::keytypes;
28+
use keyutils::{KeyType, Keyring, Permission, SpecialKeyring};
29+
30+
fn getuid() -> libc::uid_t {
31+
unsafe { libc::getuid() }
32+
}
33+
34+
fn getgid() -> libc::gid_t {
35+
unsafe { libc::getgid() }
36+
}
37+
38+
#[test]
39+
fn join_new_named_session() {
40+
let session_before = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
41+
let name = "join_new_named_session";
42+
let keyring = Keyring::join_session(name).unwrap();
43+
let session_after = Keyring::attach_or_create(SpecialKeyring::Session).unwrap();
44+
45+
assert_ne!(session_before, keyring);
46+
assert_eq!(session_after, keyring);
47+
48+
let desc = keyring.description().unwrap();
49+
assert_eq!(desc.type_, keytypes::Keyring::name());
50+
assert_eq!(desc.uid, getuid());
51+
assert_eq!(desc.gid, getgid());
52+
assert_eq!(
53+
desc.perms,
54+
Permission::POSSESSOR_ALL
55+
| Permission::USER_VIEW
56+
| Permission::USER_READ
57+
| Permission::USER_LINK
58+
);
59+
assert_eq!(desc.description, name);
60+
61+
keyring.invalidate().unwrap()
62+
}

0 commit comments

Comments
 (0)