2015-09-05 17:04:19 +00:00
|
|
|
require 'helper'
|
2019-05-25 08:33:26 +00:00
|
|
|
require 'inspec/resource'
|
|
|
|
require 'inspec/resources/passwd'
|
2015-09-05 17:04:19 +00:00
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
describe 'Inspec::Resources::Passwd' do
|
2016-02-17 16:49:44 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
_(child.to_s).must_equal '/etc/passwd with uid == 0'
|
2016-02-17 16:49:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves singular elements instead of arrays when filter has only one entry' do
|
|
|
|
_(child.users).must_equal ['root']
|
2018-02-08 10:05:21 +00:00
|
|
|
_(child.entries.length).must_equal 1
|
2016-02-17 16:49:44 +00:00
|
|
|
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']
|
2018-02-08 10:05:21 +00:00
|
|
|
_(child.entries.length).must_equal 1
|
2016-02-17 16:49:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'prints a nice to_s string' do
|
2016-03-30 23:51:43 +00:00
|
|
|
_(child.to_s).must_equal '/etc/passwd with user == /^www/'
|
2016-02-17 16:49:44 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
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
|
2016-03-30 23:51:43 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-05 17:04:19 +00:00
|
|
|
end
|