etcutils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ require 'etcutils_test_helper'
2
+ require "#{$eu_user}/etc_utils"
3
+
4
+ class EtcUtilsTest < Test::Unit::TestCase
5
+ SG_MAP = Hash.new.tap do |h|
6
+ h[:pw] = { :file => PASSWD, :ext => 'd' }
7
+ h[:gr] = { :file => GROUP, :ext => 'p' }
8
+ end
9
+
10
+ def test_constants
11
+ assert_equal(EtcUtils, EU)
12
+ assert_equal('1.0.0', EU::VERSION)
13
+
14
+ # User DB Files
15
+ [:passwd, :group, :shadow, :gshadow].each do |m|
16
+ a = m.to_s.downcase
17
+ if File.exists?(f = "/etc/#{a}")
18
+ assert_equal(eval("EU::#{a.upcase}"), f)
19
+ assert EU.send("has_#{a}?"), "EtcUtils.has_#{a}? should be true."
20
+ end
21
+ end
22
+
23
+ assert_equal(EU::SHELL, '/bin/bash')
24
+ end
25
+
26
+ def test_reflective_methods
27
+ assert_not_nil(EU.me, "EU.me should not be nil")
28
+ assert_equal(EU.me.class, EtcUtils::Passwd, "EU.me should return EtcUtils::Passwd class")
29
+ assert_equal(EU.me.uid, EU.getlogin.uid)
30
+ end
31
+
32
+ # This could fail if there are less than 5 entries in either /etc/group or /etc/passwd
33
+ # This could also fail if the user cannot read from these files
34
+ def test_pw_gr_star_ent
35
+ assert_nothing_raised do
36
+ EU.setXXent
37
+ end
38
+
39
+ assert_not_nil EU.getpwent, "Should be able to read from /etc/passwd"
40
+ assert_not_nil EU.getgrent, "Should be able to read from /etc/group"
41
+ 3.times do
42
+ EU.getpwent
43
+ EU.getgrent
44
+ end
45
+
46
+ assert_not_equal(0, (EU.getpwent).uid, "EU.getpwent should iterate through /etc/passwd")
47
+ assert_not_equal(0, (EU.getgrent).gid, "EU.getgrent should iterate through /etc/group")
48
+
49
+ assert_nothing_raised do
50
+ EU.setXXent
51
+ end
52
+
53
+ assert_equal(0, (EU.getpwent).uid, "EU.setXXent should reset all user DB files (/etc/passwd)")
54
+ assert_equal(0, (EU.getgrent).gid, "EU.setXXent should reset all user DB files (/etc/group)")
55
+
56
+ assert_nothing_raised do
57
+ EU.endXXent
58
+ end
59
+ end
60
+
61
+ def test_eu_find_pwd
62
+ assert_block("EU.setpwent should reset /etc/passwd") do
63
+ 2.times do
64
+ assert_nothing_raised { setpwent }
65
+ assert_not_nil(u = find_pwd(0))
66
+ assert_equal(0, u.uid, "EU.find_pwd(0) should only return User with ID zero")
67
+ assert_nothing_raised { endpwent }
68
+ end
69
+ end
70
+ end
71
+
72
+ def test_to_entry
73
+ r = find_pwd(0).to_entry
74
+ assert_equal(r.chomp, r, "#to_entry should not have a trailing newline")
75
+ end
76
+ SG_MAP.keys.each do |xx|
77
+ define_method("test_fget#{xx}ent_cp#{SG_MAP[xx][:file].gsub('/','_')}") {
78
+ begin
79
+ tmp_fn = "/tmp/_fget#{xx}ent_test"
80
+ assert_nothing_raised do
81
+ fh = File.open(SG_MAP[xx][:file], 'r')
82
+ File.open(tmp_fn, File::RDWR|File::CREAT, 0600) { |tmp_fh|
83
+ while ( ent = EtcUtils.send("fget#{xx}ent", fh) )
84
+ ent.fputs(tmp_fh)
85
+ end
86
+ }
87
+ fh.close
88
+ assert FileUtils.compare_file(SG_MAP[xx][:file], tmp_fn) == true, "DIFF FAILED: #{SG_MAP[xx][:file]} <=> #{tmp_fn}\n" << `diff #{SG_MAP[xx][:file]} #{tmp_fn}`
89
+ end
90
+ ensure
91
+ FileUtils.remove_file(tmp_fn);
92
+ end
93
+ }
94
+
95
+ define_method("test_get#{xx}") {
96
+ assert_nothing_raised do
97
+ EtcUtils.send("set#{xx}ent")
98
+ while ( ent = EtcUtils.send("get#{xx}ent")); nil; end
99
+ end
100
+ }
101
+
102
+ m = "find_#{xx}#{SG_MAP[xx][:ext]}"
103
+ define_method("test_#{m}_typeerr") { assert_raise TypeError do; EtcUtils.send(m,{}); end }
104
+ define_method("test_#{m}_find_int") { assert_equal(EtcUtils.send(m, 0).name, "root", "EU.find_#{xx}#{SG_MAP[xx][:ent]}(0) should find root by ID") }
105
+ define_method("test_#{m}_find_str") { assert_equal(EtcUtils.send(m, 'root').name, "root", "EU.find_#{xx}#{SG_MAP[xx][:ent]}('root') should find root by name") }
106
+ end
107
+ end
@@ -0,0 +1,15 @@
1
+ if EU.can_lockfile?
2
+ require 'etcutils_test_helper'
3
+ class EULockingTest < Test::Unit::TestCase
4
+ require "#{$eu_user}/locking"
5
+
6
+ def test_methods
7
+ assert EU.respond_to?(:lock), "Should respond to lock"
8
+ assert EU.respond_to?(:lckpwdf), "Should respond to lckpwdf"
9
+
10
+ assert EU.respond_to?(:locked?), "Should respond to locked?"
11
+ assert EU.respond_to?(:unlock), "Should respond to unlock"
12
+ assert EU.respond_to?(:ulckpwdf), "Should respond to ulckpwdf"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ require 'etcutils_test_helper'
2
+
3
+ class Test::Unit::TestCase
4
+ [:uid, :gid].each do |m|
5
+ Class.new do
6
+ define_method("test_next_#{m}"){
7
+ assert(EU.send("next_#{m}"), "EU.next_#{m} should return next available #{m}")
8
+ assert_not_equal(EU.send("next_#{m}"), EU.send("next_#{m}"), "EU.next_#{m} should never return the same #{m}")
9
+ }
10
+
11
+ # #next_{u,g}id=x should always return x even if {U,G}ID x is assigned
12
+ # Weird #send bug where EU.send("next_uid=",0) != EU.next_uid=0
13
+ define_method("test_next_#{m}_equals"){
14
+ assert_equal(0, eval("EU.next_#{m}=0"), "EU.next_#{m}=x should return x")
15
+ }
16
+
17
+ define_method("test_next_#{m}_params"){
18
+ assert_not_equal(0, EU.send("next_#{m}", 0), "EU.next_#{m}(x) should return next available #{m} if x is unavailable")
19
+ assert_equal(9999, EU.send("next_#{m}", 9999), "EU.next_#{m}(x) should return #{m} if x is available")
20
+ }
21
+
22
+ define_method("test_next_#{m}_raises"){
23
+ assert_raise ArgumentError do
24
+ EU.send("next_#{m}", 65534)
25
+ end
26
+ assert_raise ArgumentError do
27
+ EU.send("next_#{m}=", -1)
28
+ end
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,91 @@
1
+ require 'etcutils_test_helper'
2
+
3
+ module EUGetPW
4
+ def self.uid
5
+ @free_uid ||= EUGetPW._free_uid
6
+ end
7
+
8
+ def self._free_uid
9
+ id = 9999
10
+ while EU.find_pwd(id) || EU.find_grp(id)
11
+ id+=1
12
+ end
13
+ return id
14
+ end
15
+ end
16
+
17
+ class EUsgetpwentTest < Test::Unit::TestCase
18
+ include EUGetPW
19
+
20
+ def setup
21
+ @uid = EUGetPW.uid
22
+ @username = "testuser#{@uid}"
23
+
24
+ @taken_uid = EU.getpwent.uid
25
+ @taken_gid = EU.getgrent.gid
26
+ end
27
+
28
+ def test_eu_known_sgetpwent
29
+ r = find_pwd(0).to_entry
30
+ assert_equal(r, sgetpwent(r).to_entry, "EU.sgetpwent(r) should equal (r = find_pwd(x).to_entry)")
31
+ end
32
+
33
+ def test_eu_changed_sgetpwent
34
+ r = find_pwd(0)
35
+ r.gecos = "Not #{r.gecos || r.name}"
36
+ r.directory = "/home/etc_utils"
37
+ r.passwd = "password"
38
+ r.shell = "/bin/false"
39
+ ent = sgetpwent(r.to_entry)
40
+ assert_not_equal(find_pwd(0).to_entry, ent.to_entry, "EU.sgetpwent should respect most attribute changes")
41
+ assert_equal(r.gecos, ent.gecos, "EU.sgetpwent should respect #gecos changes")
42
+ assert_equal(r.directory, ent.directory, "EU.sgetpwent should respect #directory changes")
43
+ assert_equal(r.passwd, ent.passwd, "EU.sgetpwent should respect #directory changes")
44
+ assert_equal(r.shell, ent.shell, "EU.sgetpwent should respect #shell changes")
45
+ end
46
+
47
+ def test_eu_new_sgetpwent
48
+ if RUBY_PLATFORM =~ /darwin/
49
+ new = "#{@username}:*:#{EUGetPW.uid}:#{EUGetPW.uid}::0:0:Test User:/home/testuser:/bin/bash"
50
+ else
51
+ new = "#{@username}:x:#{EUGetPW.uid}:#{EUGetPW.uid}:Test User:/home/testuser:/bin/bash"
52
+ end
53
+
54
+ ent = EU.sgetpwent(new)
55
+ assert_equal(@username, ent.name, "EU.sgetpwent should have the same name")
56
+ assert_equal(@uid, ent.uid, "EU.sgetpwent should return UID when available")
57
+ assert_equal(@uid, ent.gid, "EU.sgetpwent should return GID when available")
58
+
59
+ ent = EU.sgetpwent(new.gsub(/#{EUGetPW.uid}/,''))
60
+ e = EU.next_uid(9999) - 1
61
+ assert_equal(e, ent.uid, "EU.sgetpwent should return UID when available")
62
+ assert_equal(e, ent.gid, "EU.sgetpwent should return GID when available")
63
+ end
64
+
65
+ def test_eu_raise_sgetpwent
66
+ assert_raise ArgumentError do
67
+ if RUBY_PLATFORM =~ /darwin/
68
+ EU.sgetpwent(":*:1000:1000::0:0:Test User:/home/testuser:/bin/bash")
69
+ else
70
+ EU.sgetpwent(":x:1000:1000:Test User:/home/testuser:/bin/bash")
71
+ end
72
+ end
73
+
74
+ # Should raise rb_eSystemCallError if system calls fail (see README)
75
+ #
76
+ #assert_raise Errno::ENOENT do
77
+ # EU.sgetpwent("testuser:x:1000:1000:Test User:/fake/path/testuser:/bin/bash")
78
+ #end
79
+ end
80
+
81
+ def test_eu_conflict_sgetpwent
82
+ if RUBY_PLATFORM =~ /darwin/
83
+ new = "#{@username}:*:#{@taken_uid}:#{@taken_gid}::0:0:Test User:/Users/test:/bin/bash"
84
+ else
85
+ new = "#{@username}:x:#{@taken_uid}:#{@taken_gid}:Test User:/home/testuser:/bin/bash"
86
+ end
87
+ ent = EU.sgetpwent(new)
88
+ assert_not_equal(@taken_uid, ent.uid, "EU.sgetpwent should return next availabile UID when conflict")
89
+ assert_not_equal(@taken_gid, ent.gid, "EU.sgetpwent should return next availabile GID when conflict")
90
+ end
91
+ end
@@ -0,0 +1,128 @@
1
+ require 'etcutils_test_helper'
2
+
3
+ class GroupClassTest < Test::Unit::TestCase
4
+
5
+ ##
6
+ # Module Specific Methods
7
+ #
8
+
9
+ def test_set_end_ent
10
+ assert_nothing_raised do
11
+ EU.setgrent
12
+ end
13
+
14
+ assert_nothing_raised do
15
+ EU.endgrent
16
+ end
17
+ end
18
+
19
+ def test_find
20
+ assert_nil EtcUtils.find_grp("testuser"), "EU.find_grp should return nil if user does not exist"
21
+ assert_equal("root", EtcUtils.find_grp("root").name, "EU.find_grp(str) should return user if it exists")
22
+ assert_equal("root", EtcUtils.find_grp(0).name, "EU.find_grp(int) should return user if it exists")
23
+ assert_nothing_raised do
24
+ EU.setgrent
25
+ end
26
+ assert_equal(getgrnam('root').name, getgrent.name, "EU.getgrnam('root') and EU.getgrent should return the same user")
27
+ end
28
+
29
+ def test_sgetgrent
30
+ assert sgetgrent(find_grp('root').to_entry).name.eql? "root"
31
+ end
32
+
33
+ def test_getgrent_while
34
+ assert_nothing_raised do
35
+ EtcUtils.setgrent
36
+ while ( ent = EtcUtils.getgrent ); nil; end
37
+ end
38
+ end
39
+
40
+ def test_fgetgrent_and_putgrent
41
+ tmp_fn = "/tmp/_fgetsgent_test"
42
+ assert_nothing_raised do
43
+ fh = File.open('/etc/group', 'r')
44
+ File.open(tmp_fn, File::RDWR|File::CREAT, 0600) { |tmp_fh|
45
+ while ( ent = EtcUtils.fgetgrent(fh) )
46
+ EU.putgrent(ent, tmp_fh)
47
+ end
48
+ }
49
+ fh.close
50
+ end
51
+ assert File.exists?(tmp_fn), "EU.fgetgrent(fh) should write to fh"
52
+ assert FileUtils.compare_file("/etc/group", tmp_fn) == true,
53
+ "DIFF FAILED: /etc/group <=> #{tmp_fn}\n" << `diff /etc/group #{tmp_fn}`
54
+ ensure
55
+ FileUtils.remove_file(tmp_fn);
56
+ end
57
+
58
+ def test_putgrent_raises
59
+ FileUtils.touch "/tmp/_group"
60
+
61
+ assert_raise IOError do
62
+ f = File.open("/tmp/_group", 'r')
63
+ u = EU.find_grp('root')
64
+ u.fputs f
65
+ end
66
+ ensure
67
+ FileUtils.remove_file("/tmp/_group");
68
+ end
69
+
70
+ ##
71
+ # EU::Group class methods
72
+ #
73
+ def test_class_set_end_ent
74
+ assert_nothing_raised do
75
+ EU::Group.set
76
+ end
77
+
78
+ assert_nothing_raised do
79
+ EU::Group.end
80
+ end
81
+ end
82
+
83
+ def test_class_get
84
+ assert_not_nil EU::Group.get
85
+ end
86
+
87
+ def test_class_each
88
+ assert_nothing_raised do
89
+ EU::Group.each do |e|
90
+ assert e.name
91
+ end
92
+ end
93
+ end
94
+
95
+ def test_class_find
96
+ assert_equal "root", EU::Group.find('root').name
97
+ end
98
+
99
+ def test_class_parse
100
+ assert_nothing_raised do
101
+ EtcUtils::Group.parse("root:x:0:")
102
+ end
103
+ end
104
+
105
+ def test_class_parse_members
106
+ assert_nothing_raised do
107
+ assert_equal Array, EtcUtils::Group.parse("root:x:0:").members.class
108
+ end
109
+ end
110
+
111
+ ##
112
+ # EU::Group instance methods
113
+ #
114
+ def test_init
115
+ assert_equal EU::Group, EU::Group.new.class
116
+ end
117
+
118
+ def test_instance_methods
119
+ e = EU::Group.find('root')
120
+ assert_equal 'root', e.name
121
+ assert_not_nil e.passwd
122
+ assert_equal 0, e.gid
123
+ assert_equal 'root', EU::Group.parse(e.to_entry).name
124
+ assert_equal String, e.to_entry.class
125
+ assert_equal Array, e.members.class
126
+ assert e.respond_to?(:fputs)
127
+ end
128
+ end
@@ -0,0 +1,125 @@
1
+ require 'etcutils_test_helper'
2
+
3
+ class PasswdClassTest < Test::Unit::TestCase
4
+
5
+
6
+ ##
7
+ # Module Specific Methods
8
+ #
9
+
10
+ def test_set_end_ent
11
+ assert_nothing_raised do
12
+ EU.setpwent
13
+ end
14
+
15
+ assert_nothing_raised do
16
+ EU.endpwent
17
+ end
18
+ end
19
+
20
+ def test_find
21
+ assert_nil EtcUtils.find_pwd("testuser"), "EU.find_pwd should return nil if user does not exist"
22
+ assert_equal("root", EtcUtils.find_pwd("root").name, "EU.find_pwd(str) should return user if it exists")
23
+ assert_equal("root", EtcUtils.find_pwd(0).name, "EU.find_pwd(int) should return user if it exists")
24
+ assert_nothing_raised do
25
+ EU.setpwent
26
+ end
27
+ assert_equal(getpwnam('root').name, getpwent.name, "EU.getpwnam('root') and EU.getpwent should return the same user")
28
+ end
29
+
30
+ def test_sgetpwent
31
+ assert sgetpwent(find_pwd('root').to_entry).name.eql? "root"
32
+ end
33
+
34
+ def test_getpwent_while
35
+ assert_nothing_raised do
36
+ EtcUtils.setpwent
37
+ while ( ent = EtcUtils.getpwent ); nil; end
38
+ end
39
+ end
40
+
41
+ def test_fgetpwent_and_putpwent
42
+ tmp_fn = "/tmp/_fgetsgent_test"
43
+ assert_nothing_raised do
44
+ fh = File.open('/etc/passwd', 'r')
45
+ File.open(tmp_fn, File::RDWR|File::CREAT, 0600) { |tmp_fh|
46
+ while ( ent = EtcUtils.fgetpwent(fh) )
47
+ EU.putpwent(ent, tmp_fh)
48
+ end
49
+ }
50
+ fh.close
51
+ end
52
+ assert File.exists?(tmp_fn), "EU.fgetpwent(fh) should write to fh"
53
+ assert FileUtils.compare_file("/etc/passwd", tmp_fn) == true,
54
+ "DIFF FAILED: /etc/passwd <=> #{tmp_fn}\n" << `diff /etc/passwd #{tmp_fn}`
55
+ ensure
56
+ FileUtils.remove_file(tmp_fn);
57
+ end
58
+
59
+ def test_putpwent_raises
60
+ FileUtils.touch "/tmp/_passwd"
61
+
62
+ assert_raise IOError do
63
+ f = File.open("/tmp/_passwd", 'r')
64
+ u = EU.find_pwd('root')
65
+ u.fputs f
66
+ end
67
+ ensure
68
+ FileUtils.remove_file("/tmp/_passwd");
69
+ end
70
+
71
+ ##
72
+ # EU::Passwd class methods
73
+ #
74
+ def test_class_set_end_ent
75
+ assert_nothing_raised do
76
+ EU::Passwd.set
77
+ end
78
+
79
+ assert_nothing_raised do
80
+ EU::Passwd.end
81
+ end
82
+ end
83
+
84
+ def test_class_get
85
+ assert_not_nil EU::Passwd.get
86
+ end
87
+
88
+ def test_class_each
89
+ assert_nothing_raised do
90
+ EU::Passwd.each do |e|
91
+ assert e.name
92
+ end
93
+ end
94
+ end
95
+
96
+ def test_class_find
97
+ assert_equal "root", EU::Passwd.find('root').name
98
+ end
99
+
100
+ def test_class_parse
101
+ assert_nothing_raised do
102
+ EtcUtils::Passwd.parse("root:x:0:0:root:/root:/bin/bash")
103
+ end
104
+ end
105
+
106
+ ##
107
+ # EU::Passwd instance methods
108
+ #
109
+ def test_init
110
+ assert_equal EU::Passwd, EU::Passwd.new.class
111
+ end
112
+
113
+ def test_instance_methods
114
+ e = EU::Passwd.find('root')
115
+ assert_equal 'root', e.name
116
+ assert_not_nil e.passwd
117
+ assert_equal 0, e.uid
118
+ assert_equal 0, e.gid
119
+ assert_equal '/root', e.directory
120
+ assert_not_nil e.shell
121
+ assert e.respond_to?(:fputs)
122
+ assert_equal 'root', EU::Passwd.parse(e.to_entry).name
123
+ assert_equal String, e.to_entry.class
124
+ end
125
+ end