win32-file-security 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +1,33 @@
1
- #############################################################################
2
- # test_win32_file_security_ffi.rb
3
- #
4
- # Tests to ensure that the FFI functions are private
5
- #############################################################################
6
- require 'test-unit'
7
- require 'win32/file/security'
8
-
9
- class TC_Win32_File_Security_FFI < Test::Unit::TestCase
10
- def setup
11
- @singleton_methods = File.methods.map{ |m| m.to_s }
12
- @instance_methods = File.instance_methods.map{ |m| m.to_s }
13
- end
14
-
15
- test "internal ffi functions are not public as singleton methods" do
16
- assert_false(@singleton_methods.include?('AddAce'))
17
- assert_false(@singleton_methods.include?('CloseHandle'))
18
- assert_false(@singleton_methods.include?('GetFileSecurityW'))
19
- assert_false(@singleton_methods.include?('PathIsRootW'))
20
- end
21
-
22
- test "internal ffi functions are not public as instance methods" do
23
- assert_false(@instance_methods.include?('AddAce'))
24
- assert_false(@instance_methods.include?('CloseHandle'))
25
- assert_false(@instance_methods.include?('GetFileSecurityW'))
26
- assert_false(@instance_methods.include?('PathIsRootW'))
27
- end
28
-
29
- def teardown
30
- @singleton_methods = nil
31
- @instance_methods = nil
32
- end
33
- end
1
+ #############################################################################
2
+ # test_win32_file_security_ffi.rb
3
+ #
4
+ # Tests to ensure that the FFI functions are private
5
+ #############################################################################
6
+ require 'test-unit'
7
+ require 'win32/file/security'
8
+
9
+ class TC_Win32_File_Security_FFI < Test::Unit::TestCase
10
+ def setup
11
+ @singleton_methods = File.methods.map{ |m| m.to_s }
12
+ @instance_methods = File.instance_methods.map{ |m| m.to_s }
13
+ end
14
+
15
+ test "internal ffi functions are not public as singleton methods" do
16
+ assert_false(@singleton_methods.include?('AddAce'))
17
+ assert_false(@singleton_methods.include?('CloseHandle'))
18
+ assert_false(@singleton_methods.include?('GetFileSecurityW'))
19
+ assert_false(@singleton_methods.include?('PathIsRootW'))
20
+ end
21
+
22
+ test "internal ffi functions are not public as instance methods" do
23
+ assert_false(@instance_methods.include?('AddAce'))
24
+ assert_false(@instance_methods.include?('CloseHandle'))
25
+ assert_false(@instance_methods.include?('GetFileSecurityW'))
26
+ assert_false(@instance_methods.include?('PathIsRootW'))
27
+ end
28
+
29
+ def teardown
30
+ @singleton_methods = nil
31
+ @instance_methods = nil
32
+ end
33
+ end
@@ -1,174 +1,174 @@
1
- #############################################################################
2
- # test_win32_file_ownership.rb
3
- #
4
- # Test case for the file ownership related methods
5
- #############################################################################
6
- require 'etc'
7
- require 'socket'
8
- require 'sys/admin'
9
- require 'test-unit'
10
- require 'win32/security'
11
- require 'win32/file/security'
12
- require 'pathname'
13
-
14
- class TC_Win32_File_Security_Ownership < Test::Unit::TestCase
15
- extend FFI::Library
16
- ffi_lib :netapi32
17
- attach_function :NetGetDCName, [:pointer, :pointer, :buffer_out], :int
18
- attach_function :NetApiBufferFree, [:pointer], :int
19
-
20
- # Helper method to determine if we're on a domain controller
21
- def self.in_domain?
22
- bool = true
23
- buf = (0.chr * 256).encode('UTF-16LE')
24
- rv = NetGetDCName(nil, nil, buf)
25
- bool = false if rv != 0
26
- NetApiBufferFree(buf)
27
- bool
28
- end
29
-
30
- def self.startup
31
- Dir.chdir(File.dirname(File.expand_path(File.basename(__FILE__))))
32
- @@file = File.join(Dir.pwd, 'ownership_test.txt')
33
-
34
- @@host = Socket.gethostname
35
- @@temp = "Temp"
36
- @@login = Etc.getlogin
37
- @@domain = in_domain?
38
-
39
- if Win32::Security.elevated_security?
40
- Sys::Admin.add_user(:name => @@temp, :description => "Delete Me")
41
- end
42
- end
43
-
44
- def setup
45
- @elevated = Win32::Security.elevated_security?
46
- File.open(@@file, 'w'){ |fh| fh.puts "This is an ownership test." }
47
- end
48
-
49
- test "owned? method basic functionality" do
50
- assert_respond_to(File, :owned?)
51
- assert_nothing_raised{ File.owned?(@@file) }
52
- assert_boolean(File.owned?(@@file))
53
- end
54
-
55
- test "owned? method returns expected result" do
56
- if Win32::Security.elevated_security?
57
- assert_false(File.owned?(@@file))
58
- else
59
- assert_true(File.owned?(@@file))
60
- end
61
- assert_false(File.owned?("C:\\Windows\\regedit.exe"))
62
- end
63
-
64
- test "owned? requires a single argument" do
65
- assert_raise(ArgumentError){ File.owned? }
66
- assert_raise(ArgumentError){ File.owned?(@@file, @@file) }
67
- end
68
-
69
- test "owner method basic functionality" do
70
- assert_respond_to(File, :owner)
71
- assert_nothing_raised{ File.owner(@@file) }
72
- assert_kind_of(String, File.owner(@@file))
73
- end
74
-
75
- test "owner method returns the expected value" do
76
- if Win32::Security.elevated_security?
77
- expected = "BUILTIN\\Administrators"
78
- else
79
- expected = @@host + "\\" + @@login
80
- end
81
- assert_equal(expected, File.owner(@@file))
82
- end
83
-
84
- test "owner method requires a single argument" do
85
- assert_raise(ArgumentError){ File.owner }
86
- assert_raise(ArgumentError){ File.owner(@@file, @@file) }
87
- end
88
-
89
- test "owner allows a pathname object" do
90
- assert_nothing_raised{ File.owner(Pathname.new(@@file)) }
91
- end
92
-
93
- test "grpowned? method basic functionality" do
94
- assert_respond_to(File, :grpowned?)
95
- assert_nothing_raised{ File.grpowned?(@@file) }
96
- assert_boolean(File.grpowned?(@@file))
97
- end
98
-
99
- test "grpowned? method returns expected result" do
100
- if Win32::Security.elevated_security? && @@domain
101
- assert_false(File.grpowned?(@@file))
102
- else
103
- assert_true(File.grpowned?(@@file))
104
- end
105
- assert_false(File.grpowned?("C:\\Windows\\regedit.exe"))
106
- end
107
-
108
- test "grpowned? requires a single argument" do
109
- assert_raise(ArgumentError){ File.grpowned? }
110
- assert_raise(ArgumentError){ File.grpowned?(@@file, @@file) }
111
- end
112
-
113
- test "group method basic functionality" do
114
- assert_respond_to(File, :group)
115
- assert_nothing_raised{ File.group(@@file) }
116
- assert_kind_of(String, File.group(@@file))
117
- end
118
-
119
- test "group method returns the expected value" do
120
- if Win32::Security.elevated_security? && @@domain
121
- expected = "BUILTIN\\Administrators"
122
- else
123
- expected = @@host + "\\None"
124
- end
125
- assert_equal(expected, File.group(@@file))
126
- end
127
-
128
- test "group method allows a pathname object" do
129
- assert_nothing_raised{ File.group(Pathname.new(@@file)) }
130
- end
131
-
132
- test "group method requires a stringy argument" do
133
- assert_raise(TypeError){ File.group(nil) }
134
- assert_raise(TypeError){ File.group([]) }
135
- end
136
-
137
- test "chown method basic functionality" do
138
- assert_respond_to(File, :chown)
139
- end
140
-
141
- test "chown works as expected" do
142
- omit_unless(@elevated)
143
- original_owner = File.owner(@@file)
144
- expected_owner = @@host + "\\" + @@temp
145
-
146
- assert_nothing_raised{ File.chown(@@temp, nil, @@file) }
147
- assert_equal(expected_owner, File.owner(@@file))
148
- assert_nothing_raised{ File.chown(original_owner, nil, @@file) }
149
- assert_equal(original_owner, File.owner(@@file))
150
- end
151
-
152
- test "chown returns the number of files processed" do
153
- omit_unless(@elevated)
154
- assert_equal(1, File.chown(@@temp, nil, @@file))
155
- end
156
-
157
- test "chown requires at least two arguments" do
158
- assert_raise(ArgumentError){ File.chown }
159
- assert_raise(ArgumentError){ File.chown(@@temp) }
160
- end
161
-
162
- def teardown
163
- @elevated = nil
164
- File.delete(@@file) if File.exist?(@@file)
165
- end
166
-
167
- def self.shutdown
168
- Sys::Admin.delete_user(@@temp) if Win32::Security.elevated_security?
169
- @@file = nil
170
- @@login = nil
171
- @@host = nil
172
- @@domain = nil
173
- end
174
- end
1
+ #############################################################################
2
+ # test_win32_file_ownership.rb
3
+ #
4
+ # Test case for the file ownership related methods
5
+ #############################################################################
6
+ require 'etc'
7
+ require 'socket'
8
+ require 'sys/admin'
9
+ require 'test-unit'
10
+ require 'win32/security'
11
+ require 'win32/file/security'
12
+ require 'pathname'
13
+
14
+ class TC_Win32_File_Security_Ownership < Test::Unit::TestCase
15
+ extend FFI::Library
16
+ ffi_lib :netapi32
17
+ attach_function :NetGetDCName, [:pointer, :pointer, :buffer_out], :int
18
+ attach_function :NetApiBufferFree, [:pointer], :int
19
+
20
+ # Helper method to determine if we're on a domain controller
21
+ def self.in_domain?
22
+ bool = true
23
+ buf = (0.chr * 256).encode('UTF-16LE')
24
+ rv = NetGetDCName(nil, nil, buf)
25
+ bool = false if rv != 0
26
+ NetApiBufferFree(buf) if bool
27
+ bool
28
+ end
29
+
30
+ def self.startup
31
+ Dir.chdir(File.dirname(File.expand_path(File.basename(__FILE__))))
32
+ @@file = File.join(Dir.pwd, 'ownership_test.txt')
33
+
34
+ @@host = Socket.gethostname
35
+ @@temp = "Temp"
36
+ @@login = Etc.getlogin
37
+ @@domain = in_domain?
38
+
39
+ if Win32::Security.elevated_security?
40
+ Sys::Admin.add_user(:name => @@temp, :description => "Delete Me")
41
+ end
42
+ end
43
+
44
+ def setup
45
+ @elevated = Win32::Security.elevated_security?
46
+ File.open(@@file, 'w'){ |fh| fh.puts "This is an ownership test." }
47
+ end
48
+
49
+ test "owned? method basic functionality" do
50
+ assert_respond_to(File, :owned?)
51
+ assert_nothing_raised{ File.owned?(@@file) }
52
+ assert_boolean(File.owned?(@@file))
53
+ end
54
+
55
+ test "owned? method returns expected result" do
56
+ if Win32::Security.elevated_security?
57
+ assert_false(File.owned?(@@file))
58
+ else
59
+ assert_true(File.owned?(@@file))
60
+ end
61
+ assert_false(File.owned?("C:\\Windows\\regedit.exe"))
62
+ end
63
+
64
+ test "owned? requires a single argument" do
65
+ assert_raise(ArgumentError){ File.owned? }
66
+ assert_raise(ArgumentError){ File.owned?(@@file, @@file) }
67
+ end
68
+
69
+ test "owner method basic functionality" do
70
+ assert_respond_to(File, :owner)
71
+ assert_nothing_raised{ File.owner(@@file) }
72
+ assert_kind_of(String, File.owner(@@file))
73
+ end
74
+
75
+ test "owner method returns the expected value" do
76
+ if Win32::Security.elevated_security?
77
+ expected = "BUILTIN\\Administrators"
78
+ else
79
+ expected = @@host + "\\" + @@login
80
+ end
81
+ assert_equal(expected, File.owner(@@file))
82
+ end
83
+
84
+ test "owner method requires a single argument" do
85
+ assert_raise(ArgumentError){ File.owner }
86
+ assert_raise(ArgumentError){ File.owner(@@file, @@file) }
87
+ end
88
+
89
+ test "owner allows a pathname object" do
90
+ assert_nothing_raised{ File.owner(Pathname.new(@@file)) }
91
+ end
92
+
93
+ test "grpowned? method basic functionality" do
94
+ assert_respond_to(File, :grpowned?)
95
+ assert_nothing_raised{ File.grpowned?(@@file) }
96
+ assert_boolean(File.grpowned?(@@file))
97
+ end
98
+
99
+ test "grpowned? method returns expected result" do
100
+ if Win32::Security.elevated_security? && @@domain
101
+ assert_false(File.grpowned?(@@file))
102
+ else
103
+ assert_true(File.grpowned?(@@file))
104
+ end
105
+ assert_false(File.grpowned?("C:\\Windows\\regedit.exe"))
106
+ end
107
+
108
+ test "grpowned? requires a single argument" do
109
+ assert_raise(ArgumentError){ File.grpowned? }
110
+ assert_raise(ArgumentError){ File.grpowned?(@@file, @@file) }
111
+ end
112
+
113
+ test "group method basic functionality" do
114
+ assert_respond_to(File, :group)
115
+ assert_nothing_raised{ File.group(@@file) }
116
+ assert_kind_of(String, File.group(@@file))
117
+ end
118
+
119
+ test "group method returns the expected value" do
120
+ if Win32::Security.elevated_security? && @@domain
121
+ expected = "BUILTIN\\Administrators"
122
+ else
123
+ expected = @@host + "\\None"
124
+ end
125
+ assert_equal(expected.downcase, File.group(@@file).downcase)
126
+ end
127
+
128
+ test "group method allows a pathname object" do
129
+ assert_nothing_raised{ File.group(Pathname.new(@@file)) }
130
+ end
131
+
132
+ test "group method requires a stringy argument" do
133
+ assert_raise(TypeError){ File.group(nil) }
134
+ assert_raise(TypeError){ File.group([]) }
135
+ end
136
+
137
+ test "chown method basic functionality" do
138
+ assert_respond_to(File, :chown)
139
+ end
140
+
141
+ test "chown works as expected" do
142
+ omit_unless(@elevated)
143
+ original_owner = File.owner(@@file).downcase
144
+ expected_owner = (@@host + "\\" + @@temp).downcase
145
+
146
+ assert_nothing_raised{ File.chown(@@temp, nil, @@file) }
147
+ assert_equal(expected_owner, File.owner(@@file).downcase)
148
+ assert_nothing_raised{ File.chown(original_owner, nil, @@file) }
149
+ assert_equal(original_owner, File.owner(@@file).downcase)
150
+ end
151
+
152
+ test "chown returns the number of files processed" do
153
+ omit_unless(@elevated)
154
+ assert_equal(1, File.chown(@@temp, nil, @@file))
155
+ end
156
+
157
+ test "chown requires at least two arguments" do
158
+ assert_raise(ArgumentError){ File.chown }
159
+ assert_raise(ArgumentError){ File.chown(@@temp) }
160
+ end
161
+
162
+ def teardown
163
+ @elevated = nil
164
+ File.delete(@@file) if File.exist?(@@file)
165
+ end
166
+
167
+ def self.shutdown
168
+ Sys::Admin.delete_user(@@temp) if Win32::Security.elevated_security?
169
+ @@file = nil
170
+ @@login = nil
171
+ @@host = nil
172
+ @@domain = nil
173
+ end
174
+ end
@@ -1,87 +1,88 @@
1
- ##############################################################################
2
- # test_win32_file_permissions.rb
3
- #
4
- # Test case for permission related methods of win32-file-security. You should
5
- # use the 'rake test' or 'rake test:perms' task to run this.
6
- ##############################################################################
7
- require 'test-unit'
8
- require 'win32/file/security'
9
- require 'socket'
10
- require 'etc'
11
-
12
- class TC_Win32_File_Security_Permissions < Test::Unit::TestCase
13
- def self.startup
14
- @@user = Etc.getlogin
15
- @@host = Socket.gethostname
16
- @@file = File.join(Dir.pwd, 'security_test.txt')
17
-
18
- Dir.chdir(File.dirname(File.expand_path(File.basename(__FILE__))))
19
- File.open(@@file, 'w'){ |fh| fh.puts "This is a security test." }
20
- end
21
-
22
- def setup
23
- @perms = nil
24
- end
25
-
26
- test "get_permissions basic functionality" do
27
- assert_respond_to(File, :get_permissions)
28
- assert_nothing_raised{ File.get_permissions(@@file) }
29
- end
30
-
31
- test "get_permissions returns a hash" do
32
- assert_kind_of(Hash, File.get_permissions(@@file))
33
- end
34
-
35
- test "get_permissions accepts an optional hostname argument" do
36
- assert_nothing_raised{ File.get_permissions(@@file, @@host) }
37
- end
38
-
39
- test "get_permissions requires at least one argument" do
40
- assert_raise(ArgumentError){ File.get_permissions }
41
- end
42
-
43
- test "set_permissions basic functionality" do
44
- assert_respond_to(File, :set_permissions)
45
- end
46
-
47
- test "set_permissions works as expected" do
48
- assert_nothing_raised{ @perms = File.get_permissions(@@file) }
49
- assert_nothing_raised{ File.set_permissions(@@file, @perms) }
50
- assert_equal(@perms, File.get_permissions(@@file))
51
- end
52
-
53
- test "set_permissions works if host is specified" do
54
- @perms = {"#{@@host}\\#{@@user}" => File::GENERIC_ALL}
55
- assert_nothing_raised{ File.set_permissions(@@file, @perms) }
56
- assert_equal(@perms, File.get_permissions(@@file))
57
- end
58
-
59
- test "securities method basic functionality" do
60
- assert_respond_to(File, :securities)
61
- end
62
-
63
- test "securities method works as expected" do
64
- @perms = File.get_permissions(@@file)
65
-
66
- @perms.each{ |acct, mask|
67
- assert_nothing_raised{ File.securities(mask) }
68
- assert_kind_of(Array, File.securities(mask))
69
- }
70
- end
71
-
72
- test "securities method accepts a single argument only" do
73
- assert_raise(ArgumentError){ File.securities }
74
- assert_raise(ArgumentError){ File.securities({}, {}) }
75
- end
76
-
77
- def teardown
78
- @perms = nil
79
- end
80
-
81
- def self.shutdown
82
- File.delete(@@file) if File.exist?(@@file)
83
- @@file = nil
84
- @@host = nil
85
- @@user = nil
86
- end
87
- end
1
+ ##############################################################################
2
+ # test_win32_file_permissions.rb
3
+ #
4
+ # Test case for permission related methods of win32-file-security. You should
5
+ # use the 'rake test' or 'rake test:perms' task to run this.
6
+ ##############################################################################
7
+ require 'test-unit'
8
+ require 'win32/file/security'
9
+ require 'socket'
10
+ require 'etc'
11
+
12
+ class TC_Win32_File_Security_Permissions < Test::Unit::TestCase
13
+ def self.startup
14
+ @@user = Etc.getlogin
15
+ @@host = Socket.gethostname
16
+ @@file = File.join(Dir.pwd, 'security_test.txt')
17
+
18
+ Dir.chdir(File.dirname(File.expand_path(File.basename(__FILE__))))
19
+ File.open(@@file, 'w'){ |fh| fh.puts "This is a security test." }
20
+ end
21
+
22
+ def setup
23
+ @perms = nil
24
+ end
25
+
26
+ test "get_permissions basic functionality" do
27
+ assert_respond_to(File, :get_permissions)
28
+ assert_nothing_raised{ File.get_permissions(@@file) }
29
+ end
30
+
31
+ test "get_permissions returns a hash" do
32
+ assert_kind_of(Hash, File.get_permissions(@@file))
33
+ end
34
+
35
+ test "get_permissions accepts an optional hostname argument" do
36
+ assert_nothing_raised{ File.get_permissions(@@file, @@host) }
37
+ end
38
+
39
+ test "get_permissions requires at least one argument" do
40
+ assert_raise(ArgumentError){ File.get_permissions }
41
+ end
42
+
43
+ test "set_permissions basic functionality" do
44
+ assert_respond_to(File, :set_permissions)
45
+ end
46
+
47
+ test "set_permissions works as expected" do
48
+ assert_nothing_raised{ @perms = File.get_permissions(@@file) }
49
+ assert_nothing_raised{ File.set_permissions(@@file, @perms) }
50
+ assert_equal(@perms, File.get_permissions(@@file))
51
+ end
52
+
53
+ test "set_permissions works if host is specified" do
54
+ @perms = {"#{@@host}\\#{@@user}" => File::GENERIC_ALL}
55
+ assert_nothing_raised{ File.set_permissions(@@file, @perms) }
56
+ assert_equal(@perms.keys.map(&:downcase), File.get_permissions(@@file).keys.map(&:downcase))
57
+ assert_equal(@perms.values, File.get_permissions(@@file).values)
58
+ end
59
+
60
+ test "securities method basic functionality" do
61
+ assert_respond_to(File, :securities)
62
+ end
63
+
64
+ test "securities method works as expected" do
65
+ @perms = File.get_permissions(@@file)
66
+
67
+ @perms.each{ |acct, mask|
68
+ assert_nothing_raised{ File.securities(mask) }
69
+ assert_kind_of(Array, File.securities(mask))
70
+ }
71
+ end
72
+
73
+ test "securities method accepts a single argument only" do
74
+ assert_raise(ArgumentError){ File.securities }
75
+ assert_raise(ArgumentError){ File.securities({}, {}) }
76
+ end
77
+
78
+ def teardown
79
+ @perms = nil
80
+ end
81
+
82
+ def self.shutdown
83
+ File.delete(@@file) if File.exist?(@@file)
84
+ @@file = nil
85
+ @@host = nil
86
+ @@user = nil
87
+ end
88
+ end