sys-admin 1.5.6 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/sys-admin.gemspec CHANGED
@@ -4,7 +4,7 @@ require 'rubygems'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'sys-admin'
7
- spec.version = '1.5.6'
7
+ spec.version = '1.6.0'
8
8
  spec.author = 'Daniel J. Berger'
9
9
  spec.license = 'Artistic 2.0'
10
10
  spec.email = 'djberg96@gmail.com'
@@ -16,7 +16,8 @@ Gem::Specification.new do |spec|
16
16
  spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
17
17
  spec.rubyforge_project = 'sysutils'
18
18
 
19
- spec.add_development_dependency('test-unit', '>= 2.1.1')
19
+ spec.add_dependency('ffi', '>= 1.1.0')
20
+ spec.add_development_dependency('test-unit', '>= 2.5.0')
20
21
 
21
22
  spec.description = <<-EOF
22
23
  The sys-admin library is a unified, cross platform replacement for the
@@ -4,9 +4,7 @@
4
4
  # This exists mostly for the sake of the gemspec, so that it calls the right
5
5
  # test suite based on the platform.
6
6
  ###############################################################################
7
- require 'rubygems'
8
- gem 'test-unit'
9
- require 'test/unit'
7
+ require 'test-unit'
10
8
 
11
9
  if File::ALT_SEPARATOR
12
10
  require 'test_sys_admin_windows'
@@ -15,7 +13,7 @@ else
15
13
  end
16
14
 
17
15
  class TC_Sys_Admin_All < Test::Unit::TestCase
18
- def test_version
19
- assert_equal('1.5.6', Sys::Admin::VERSION)
16
+ test "version is set to expected value" do
17
+ assert_equal('1.6.0', Sys::Admin::VERSION)
20
18
  end
21
19
  end
@@ -4,7 +4,7 @@
4
4
  # Test suite for the Unix version of sys-admin. This test should be run
5
5
  # via the 'rake test' task.
6
6
  ###############################################################################
7
- require 'test/unit'
7
+ require 'test-unit'
8
8
  require 'sys/admin'
9
9
  include Sys
10
10
 
@@ -18,224 +18,239 @@ class TC_Sys_Admin_Unix < Test::Unit::TestCase
18
18
 
19
19
  ## Admin singleton methods
20
20
 
21
- def test_get_login
21
+ test "get_login basic functionality" do
22
22
  assert_respond_to(Admin, :get_login)
23
23
  assert_nothing_raised{ Admin.get_login }
24
+ end
25
+
26
+ test "get_login returns a string" do
24
27
  assert_kind_of(String, Admin.get_login)
28
+ assert_true(Admin.get_login.length > 0)
25
29
  end
26
30
 
27
- def test_get_user_basic
31
+ test "get_user basic functionality" do
28
32
  assert_respond_to(Admin, :get_user)
29
33
  assert_nothing_raised{ Admin.get_user(@user) }
30
34
  assert_nothing_raised{ Admin.get_user(@user_id) }
31
35
  end
32
36
 
33
- def test_get_user_by_name
34
- assert_kind_of(User, Admin.get_user(@user))
37
+ test "get_user with a string argument works as expected" do
38
+ assert_kind_of(Admin::User, Admin.get_user(@user))
35
39
  end
36
40
 
37
- def test_get_user_by_id
38
- assert_kind_of(User, Admin.get_user(@user_id))
41
+ test "get_user with an integer argument works as expected" do
42
+ assert_kind_of(Admin::User, Admin.get_user(@user_id))
39
43
  end
40
44
 
41
- def test_get_user_expected_errors
45
+ test "get_user requires one argument only" do
42
46
  assert_raise(ArgumentError){ Admin.get_user }
47
+ assert_raise(ArgumentError){ Admin.get_user(@user, @user) }
48
+ end
49
+
50
+ test "get_user requires a string or integer argument" do
43
51
  assert_raise(TypeError){ Admin.get_user([]) }
52
+ end
53
+
54
+ test "get_user raises an Error if the user cannot be found" do
44
55
  assert_raise(Admin::Error){ Admin.get_user('foofoofoo') }
45
56
  end
46
57
 
47
- def test_users_basic
58
+ test "users basic functionality" do
48
59
  assert_respond_to(Admin, :users)
49
60
  assert_nothing_raised{ Admin.users }
50
61
  end
51
62
 
52
- def test_users
53
- assert_kind_of(Array, Admin.users)
54
- assert_kind_of(User, Admin.users.first)
55
- end
56
-
57
- def test_users_block_form
58
- array = []
59
- assert_nothing_raised{ Admin.users{ |g| array << g } }
60
- assert_equal(true, Admin.users.length == array.length)
61
- assert_nothing_raised{ Admin.users{ |g| break } }
63
+ test "users returns an array of User objects" do
64
+ users = Admin.users
65
+ assert_kind_of(Array, users)
66
+ assert_kind_of(Admin::User, users.first)
62
67
  end
63
68
 
64
- def test_users_expected_errors
69
+ test "users does not accept any arguments" do
65
70
  assert_raise(ArgumentError){ Admin.users(@user_id) }
66
71
  end
67
72
 
68
- def test_get_group_basic
73
+ test "get_group basic functionality" do
69
74
  assert_respond_to(Admin, :get_group)
70
75
  assert_nothing_raised{ Admin.get_group(@group) }
71
76
  assert_nothing_raised{ Admin.get_group(@group_id) }
72
77
  end
73
78
 
74
- def test_get_group_by_name
75
- assert_kind_of(Group, Admin.get_group(@group))
79
+ test "get_group accepts a string argument" do
80
+ assert_kind_of(Admin::Group, Admin.get_group(@group))
76
81
  end
77
82
 
78
- def test_get_group_by_id
79
- assert_kind_of(Group, Admin.get_group(@group_id))
83
+ test "get_group accepts an integer argument" do
84
+ assert_kind_of(Admin::Group, Admin.get_group(@group_id))
80
85
  end
81
86
 
82
- def test_get_group_expected_errors
87
+ test "get_group requires one argument only" do
83
88
  assert_raise(ArgumentError){ Admin.get_group }
89
+ assert_raise(ArgumentError){ Admin.get_group(@group_id, @group_id) }
90
+ end
91
+
92
+ test "get_group raises a TypeError if an invalid type is passed" do
84
93
  assert_raise(TypeError){ Admin.get_group([]) }
94
+ end
95
+
96
+ test "get_group raises an Error if the group cannot be found" do
85
97
  assert_raise(Admin::Error){ Admin.get_group('foofoofoo') }
86
98
  end
87
99
 
88
- def test_groups_basic
100
+ test "groups basic functionality" do
89
101
  assert_respond_to(Admin, :groups)
90
102
  assert_nothing_raised{ Admin.groups }
91
103
  end
92
104
 
93
- def test_groups
94
- assert_kind_of(Array, Admin.groups)
95
- assert_kind_of(Group, Admin.groups.first)
105
+ test "groups returns an array of Group objects" do
106
+ groups = Admin.groups
107
+ assert_kind_of(Array, groups)
108
+ assert_kind_of(Admin::Group, groups.first)
96
109
  end
97
110
 
98
- def test_groups_expected_errors
111
+ test "groups method does not accept any arguments" do
99
112
  assert_raise(ArgumentError){ Admin.groups(@group_id) }
100
113
  end
101
114
 
102
- def test_groups_block_form
103
- array = []
104
- assert_nothing_raised{ Admin.groups{ |g| array << g } }
105
- assert_equal(true, Admin.groups.length == array.length)
106
- assert_nothing_raised{ Admin.groups{ |g| break } }
107
- end
108
-
109
115
  ## User Tests
110
116
 
111
- def test_user_name
112
- @user = Admin.users.first
117
+ test "user.name behaves as expected" do
118
+ @user = Admin.get_user(@user_id)
113
119
  assert_respond_to(@user, :name)
114
120
  assert_kind_of(String, @user.name)
115
121
  end
116
122
 
117
- def test_user_passwd
118
- @user = Admin.users.first
123
+ test "user.passwd behaves as expected" do
124
+ @user = Admin.get_user(@user_id)
119
125
  assert_respond_to(@user, :passwd)
120
126
  assert_kind_of(String, @user.passwd)
121
127
  end
122
128
 
123
- def test_user_uid
124
- @user = Admin.users.first
129
+ test "user.uid behaves as expected" do
130
+ @user = Admin.get_user(@user_id)
125
131
  assert_respond_to(@user, :uid)
126
132
  assert_kind_of(Fixnum, @user.uid)
127
133
  end
128
134
 
129
- def test_user_gid
130
- @user = Admin.users.first
135
+ test "user.gid behaves as expected" do
136
+ @user = Admin.get_user(@user_id)
131
137
  assert_respond_to(@user, :gid)
132
138
  assert_kind_of(Fixnum, @user.gid)
133
139
  end
134
140
 
135
- def test_user_dir
136
- @user = Admin.users.first
141
+ test "user.dir behaves as expected" do
142
+ @user = Admin.get_user(@user_id)
137
143
  assert_respond_to(@user, :dir)
138
144
  assert_kind_of(String, @user.dir)
139
145
  end
140
146
 
141
- def test_user_shell
142
- @user = Admin.users.first
147
+ test "user.shell behaves as expected" do
148
+ @user = Admin.get_user(@user_id)
143
149
  assert_respond_to(@user, :shell)
144
150
  assert_kind_of(String, @user.shell)
145
151
  end
146
152
 
147
- def test_user_gecos
148
- @user = Admin.users.first
153
+ test "user.gecos behaves as expected" do
154
+ @user = Admin.get_user(@user_id)
149
155
  assert_respond_to(@user, :gecos)
150
- assert_kind_of(String, @user.gecos)
156
+ assert_kind_of([String, NilClass], @user.gecos)
151
157
  end
152
158
 
153
- def test_user_quota
154
- @user = Admin.users.first
159
+ test "user.quota behaves as expected" do
160
+ @user = Admin.get_user(@user_id)
155
161
  assert_respond_to(@user, :quota)
156
162
  assert_true([Fixnum, NilClass].include?(@user.quota.class))
157
163
  end
158
164
 
159
- def test_user_age
160
- @user = Admin.users.first
165
+ test "user.age behaves as expected" do
166
+ @user = Admin.get_user(@user_id)
161
167
  assert_respond_to(@user, :age)
162
168
  assert_true([Fixnum, NilClass].include?(@user.age.class))
163
169
  end
164
170
 
165
- def test_user_access_class
166
- @user = Admin.users.first
171
+ test "user.access behaves as expected" do
172
+ @user = Admin.get_user(@user_id)
167
173
  assert_respond_to(@user, :access_class)
168
174
  assert_true([String, NilClass].include?(@user.access_class.class))
169
175
  end
170
176
 
171
- def test_user_comment
172
- @user = Admin.users.first
177
+ test "user.comment behaves as expected" do
178
+ @user = Admin.get_user(@user_id)
173
179
  assert_respond_to(@user, :comment)
174
180
  assert_true([String, NilClass].include?(@user.comment.class))
175
181
  end
176
182
 
177
- def test_user_expire
178
- @user = Admin.users.first
183
+ test "user.expire behaves as expected" do
184
+ @user = Admin.get_user(@user_id)
179
185
  assert_respond_to(@user, :expire)
180
186
  assert_true([Time, NilClass].include?(@user.expire.class))
181
187
  end
182
188
 
183
- def test_user_change
184
- @user = Admin.users.first
189
+ test "user.change behaves as expected" do
190
+ @user = Admin.get_user(@user_id)
185
191
  assert_respond_to(@user, :change)
186
192
  assert_true([Time, NilClass].include?(@user.change.class))
187
193
  end
188
194
 
189
- def test_user_login_time
190
- @user = Admin.users.first
195
+ test "user.login_time behaves as expected" do
196
+ @user = Admin.get_user(@user_id)
191
197
  assert_respond_to(@user, :login_time)
192
198
  assert_true([Time, NilClass].include?(@user.login_time.class))
193
199
  end
194
200
 
195
- def test_user_login_device
196
- @user = Admin.users.first
201
+ test "user.login_device behaves as expected" do
202
+ @user = Admin.get_user(@user_id)
197
203
  assert_respond_to(@user, :login_device)
198
204
  assert_true([String, NilClass].include?(@user.login_device.class))
199
205
  end
200
206
 
201
- def test_user_login_host
202
- @user = Admin.users.first
207
+ test "user.login_host behaves as expected" do
208
+ @user = Admin.get_user(@user_id)
203
209
  assert_respond_to(@user, :login_host)
204
210
  assert_true([String, NilClass].include?(@user.login_host.class))
205
211
  end
206
212
 
207
- def test_user_groups
208
- @user = Admin.users.first
213
+ test "user.groups behaves as expected" do
214
+ @user = Admin.get_user(@user_id)
209
215
  assert_respond_to(@user, :groups)
210
216
  assert_kind_of(Array, @user.groups)
211
217
  end
212
218
 
213
219
  ## Group Tests
214
220
 
215
- def test_group_name
216
- @group = Admin.groups.first
221
+ test "group.name behaves as expected" do
222
+ @group = Admin.get_group(@group_id)
217
223
  assert_respond_to(@group, :name)
218
224
  assert_kind_of(String, @group.name)
219
225
  end
220
226
 
221
- def test_group_gid
222
- @group = Admin.groups.first
227
+ test "group.gid behaves as expected" do
228
+ @group = Admin.get_group(@group_id)
223
229
  assert_respond_to(@group, :gid)
224
230
  assert_kind_of(Fixnum, @group.gid)
225
231
  end
226
232
 
227
- def test_group_members
228
- @group = Admin.groups.first
233
+ test "group.members behaves as expected" do
234
+ @group = Admin.get_group(@group_id)
229
235
  assert_respond_to(@group, :members)
230
236
  assert_kind_of(Array, @group.members)
231
237
  end
232
238
 
233
- def test_group_passwd
234
- @group = Admin.groups.first
239
+ test "group.passwd behaves as expected" do
240
+ @group = Admin.get_group(@group_id)
235
241
  assert_respond_to(@group, :passwd)
236
242
  assert_kind_of(String, @group.passwd)
237
243
  end
238
244
 
245
+ ## FFI
246
+
247
+ test "ffi functions are private" do
248
+ methods = Admin.methods(false).map{ |e| e.to_s }
249
+ assert_false(methods.include?('getlogin'))
250
+ assert_false(methods.include?('getlogin_r'))
251
+ assert_false(methods.include?('strerror'))
252
+ end
253
+
239
254
  def teardown
240
255
  @user = nil
241
256
  @user_id = nil
@@ -7,20 +7,23 @@
7
7
  #
8
8
  # It is assumed that this test will be run via the 'rake test' task.
9
9
  ###############################################################################
10
- require 'rubygems'
11
- gem 'test-unit'
12
-
13
- require 'test/unit'
10
+ require 'test-unit'
14
11
  require 'sys/admin'
15
12
  require 'win32/security'
16
13
  require 'socket'
17
- require 'etc'
18
14
  include Sys
19
15
 
20
16
  class TC_Sys_Admin_Win32 < Test::Unit::TestCase
21
17
  def self.startup
22
18
  @@host = Socket.gethostname
23
- @@elev = Win32::Security.elevated_security?
19
+ @@version = `ver`.scan(/\d/).first.to_i
20
+
21
+ # No elevated_security? method for XP yet.
22
+ if @@version < 6
23
+ @@elevated = true
24
+ else
25
+ @@elevated = Win32::Security.elevated_security?
26
+ end
24
27
  end
25
28
 
26
29
  def setup
@@ -35,7 +38,7 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
35
38
  # Admin singleton methods
36
39
 
37
40
  def test_01_add_user
38
- omit_unless(@@elev, "insufficient privileges - skipping")
41
+ omit_unless(@@elevated)
39
42
  assert_respond_to(Admin, :add_user)
40
43
  assert_nothing_raised{
41
44
  Admin.add_user(:name => 'foo', :password => 'a1b2c3D4')
@@ -43,7 +46,7 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
43
46
  end
44
47
 
45
48
  def test_02_config_user
46
- omit_unless(@@elev, "insufficient privileges - skipping")
49
+ omit_unless(@@elevated)
47
50
  assert_respond_to(Admin, :configure_user)
48
51
  assert_nothing_raised{
49
52
  Admin.configure_user(
@@ -56,19 +59,19 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
56
59
  end
57
60
 
58
61
  def test_03_delete_user
59
- omit_unless(@@elev, "insufficient privileges - skipping")
62
+ omit_unless(@@elevated)
60
63
  assert_respond_to(Admin, :delete_user)
61
64
  assert_nothing_raised{ Admin.delete_user('foo') }
62
65
  end
63
66
 
64
67
  def test_01_add_group
65
- omit_unless(@@elev, "insufficient privileges - skipping")
68
+ omit_unless(@@elevated)
66
69
  assert_respond_to(Admin, :add_group)
67
70
  assert_nothing_raised{ Admin.add_group(:name => 'bar') }
68
71
  end
69
72
 
70
73
  def test_02_configure_group
71
- omit_unless(@@elev, "insufficient privileges - skipping")
74
+ omit_unless(@@elevated)
72
75
  assert_respond_to(Admin, :configure_group)
73
76
  assert_nothing_raised{
74
77
  Admin.configure_group(:name => 'bar', :description => 'delete me')
@@ -76,7 +79,7 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
76
79
  end
77
80
 
78
81
  def test_03_delete_group
79
- omit_unless(@@elev, "insufficient privileges - skipping")
82
+ omit_unless(@@elevated)
80
83
  assert_respond_to(Admin, :delete_group)
81
84
  assert_nothing_raised{ Admin.delete_group('bar') }
82
85
  end
@@ -120,11 +123,6 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
120
123
  assert_kind_of(User, Admin.get_user(@user_id, options))
121
124
  end
122
125
 
123
- def test_get_user_with_no_options
124
- assert_nothing_raised{ Admin.get_user(Etc.getlogin) }
125
- assert_kind_of(User, Admin.get_user(Etc.getlogin))
126
- end
127
-
128
126
  def test_get_user_expected_errors
129
127
  assert_raises(ArgumentError){ Admin.get_user }
130
128
  end
@@ -139,14 +137,6 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
139
137
  assert_kind_of(User, Admin.users(:localaccount => true).first)
140
138
  end
141
139
 
142
- def test_users_with_block
143
- array = []
144
- assert_nothing_raised{
145
- Admin.users(:localaccount => true){ |u| array << u }
146
- }
147
- assert_kind_of(User, array[0])
148
- end
149
-
150
140
  def test_get_group_basic
151
141
  assert_respond_to(Admin, :get_group)
152
142
  end
@@ -166,11 +156,6 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
166
156
  assert_kind_of(Group, Admin.get_group(@group_name, :localaccount => true))
167
157
  end
168
158
 
169
- def test_get_group_with_no_options
170
- assert_nothing_raised{ Admin.get_group("Administrators") }
171
- assert_kind_of(Group, Admin.get_group("Administrators"))
172
- end
173
-
174
159
  def test_get_group_expected_errors
175
160
  assert_raise(ArgumentError){ Admin.get_group }
176
161
  end
@@ -185,14 +170,6 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
185
170
  assert_kind_of(Group, Admin.groups(:localaccount => true).first)
186
171
  end
187
172
 
188
- def test_groups_with_block
189
- array = []
190
- assert_nothing_raised{
191
- Admin.groups(:localaccount => true){ |g| array << g }
192
- }
193
- assert_kind_of(Group, array[0])
194
- end
195
-
196
173
  # User class
197
174
 
198
175
  def test_user_instance_caption
@@ -348,5 +325,6 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
348
325
 
349
326
  def self.shutdown
350
327
  @@host = nil
328
+ @@elevated = nil
351
329
  end
352
330
  end