inspec/test/unit/resources/passwd_test.rb

97 lines
2.9 KiB
Ruby
Raw Normal View History

2015-10-06 16:55:44 +00:00
# author: Christoph Hartmann
# author: Dominik Richter
2015-09-05 17:04:19 +00:00
require 'helper'
2015-10-26 03:04:18 +00:00
require 'inspec/resource'
2015-09-05 17:04:19 +00:00
2015-10-26 03:04:18 +00:00
describe 'Inspec::Resources::Passwd' do
let(:passwd) { load_resource('passwd') }
it 'retrieve users via field' do
_(passwd.users).must_equal %w{root www-data}
end
it 'retrieve uids via field' do
_(passwd.uids).must_equal %w{0 33}
end
it 'retrieve gids via field' do
_(passwd.gids).must_equal %w{0 133}
end
it 'retrieve passwords via field' do
_(passwd.passwords).must_equal %w{x x}
end
it 'retrieve login-shells via field' do
_(passwd.shells).must_equal %w{/bin/bash /bin/sh}
end
it 'access all lines of the file' do
_(passwd.lines).must_equal %w{root:x:0:0:root:/root:/bin/bash www-data:x:33:133:www-data:/var/www:/bin/sh}
end
it 'access all params of the file' do
_(passwd.params[1]).must_equal({"user"=>"www-data", "password"=>"x", "uid"=>"33", "gid"=>"133", "desc"=>"www-data", "home"=>"/var/www", "shell"=>"/bin/sh"})
end
describe 'filter by uid == 0' do
let(:child) { passwd.uids(0) }
it 'creates a new passwd instance' do
_(child.content).must_equal 'root:x:0:0:root:/root:/bin/bash'
end
it 'prints a nice to_s string' do
_(child.to_s).must_equal '/etc/passwd with uid == 0'
end
it 'retrieves singular elements instead of arrays when filter has only one entry' do
_(child.users).must_equal ['root']
Remove deprecations for InSpec 2.0 (#2506) * Add `release-2.0` target branch to AppVeyor/Travis (#2510) Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * simpleconfig: Remove deprecated config keys Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * cli (exec): Remove `--cache` command line argument Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * platform: Remove lowercase os name protection Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `contain_legacy_plus` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `contain_match` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `with_version` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `belong_to_group` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `belong_to_primary_group` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `contain` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * passwd: Remove deprecated properties This removes: - `passwd.count` - `passwd.username` - `passwd.usernames` - `passwd.uid` Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * auditd_rules: Remove in favor of `auditd` resource Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * cli: Remove `login_automate` command Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * Remove `resource_skipped` message method Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
2018-02-08 10:05:21 +00:00
_(child.entries.length).must_equal 1
end
end
describe 'filter via name =~ /^www/' do
let(:child) { passwd.users(/^www/) }
it 'filters by user via name (regex)' do
_(child.users).must_equal ['www-data']
Remove deprecations for InSpec 2.0 (#2506) * Add `release-2.0` target branch to AppVeyor/Travis (#2510) Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * simpleconfig: Remove deprecated config keys Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * cli (exec): Remove `--cache` command line argument Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * platform: Remove lowercase os name protection Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `contain_legacy_plus` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `contain_match` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `with_version` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `belong_to_group` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `belong_to_primary_group` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * matcher: Remove `contain` matcher Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * passwd: Remove deprecated properties This removes: - `passwd.count` - `passwd.username` - `passwd.usernames` - `passwd.uid` Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * auditd_rules: Remove in favor of `auditd` resource Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * cli: Remove `login_automate` command Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com> * Remove `resource_skipped` message method Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
2018-02-08 10:05:21 +00:00
_(child.entries.length).must_equal 1
end
it 'prints a nice to_s string' do
_(child.to_s).must_equal '/etc/passwd with user == /^www/'
end
2015-09-05 17:04:19 +00:00
end
2016-02-18 11:00:34 +00:00
2016-04-26 12:27:21 +00:00
describe 'where clause' do
it 'retrieves username via uids < x' do
2016-04-26 12:27:21 +00:00
_(passwd.where { uid.to_i < 33 }.entries.length).must_equal 1
_(passwd.where { uid.to_i < 34 }.entries.length).must_equal 2
end
it 'retrieves username via uids <= x' do
2016-04-26 12:27:21 +00:00
_(passwd.where { uid.to_i <= 32 }.entries.length).must_equal 1
_(passwd.where { uid.to_i <= 33 }.entries.length).must_equal 2
end
it 'retrieves username via uids > x' do
2016-04-26 12:27:21 +00:00
_(passwd.where { uid.to_i > 0 }.entries.length).must_equal 1
_(passwd.where { uid.to_i > -1 }.entries.length).must_equal 2
end
it 'retrieves username via uids >= x' do
2016-04-26 12:27:21 +00:00
_(passwd.where { uid.to_i >= 1 }.entries.length).must_equal 1
_(passwd.where { uid.to_i >= 0 }.entries.length).must_equal 2
end
it 'retrieves username via uids == x' do
2016-04-26 12:27:21 +00:00
_(passwd.where { uid.to_i == 0 }.entries.length).must_equal 1
_(passwd.where { uid.to_i == 1 }.entries.length).must_equal 0
end
it 'retrieves username via uids != x' do
2016-04-26 12:27:21 +00:00
_(passwd.where { uid.to_i != 0 }.entries.length).must_equal 1
_(passwd.where { uid.to_i != 1 }.entries.length).must_equal 2
end
end
2015-09-05 17:04:19 +00:00
end