File: test_file_stat.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (328 lines) | stat: -rw-r--r-- 7,134 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
require 'stat'
require 'file_info_testcase.rb'

class TestFileStat < FileInfoTest

  def setup
    super
    @s1 = File.stat(@file1)
    @s2 = File.stat(@file2)
  end

  # compares modified times
  def test_CMP # '<=>'
    assert_equal(0,  @s1 <=> @s1)
    assert_equal(0,  @s2 <=> @s2)
    assert_equal(+1, @s1 <=> @s2)
    assert_equal(-1, @s2 <=> @s1)
  end

  Windows.dont do     # not on FAT filesystems
    def test_atime
      assert_equal(@aTime1, @s1.atime)
      assert_equal(@aTime2, @s2.atime)
    end
  end

  def test_blksize
    blksize = checkstat(".").split[8].to_i
    if $? != 0 || blksize == -1
      skipping("Couldn't find block size")
    else
      assert_equal(blksize, File.stat('.').blksize)
    end
  end

  def try(sym, file, expected)
    if File.exist?(file)
      s = File.stat(file)
      assert_equal(expected, s.send(sym), "File: #{file}")
    else
      skipping("#{sym}: #{file} not found")
    end
  end

  def test_blockdev?
    try(:blockdev?, ".",        false)
    Unix.or_variant do
      try(:blockdev?, "/dev/tty", false)
      Linux.only do
	try(:blockdev?, "/dev/fd0", true)
      end
    end
  end

  MsWin32.dont do
    def test_blocks
      file = "_test/_size"
      File.open(file, "w") { |f| }
      assert_equal(0, File.stat(file).blocks)
      File.open(file, "w") { |f| f.syswrite 'a'}
      assert(File.stat(file).blocks > 0)
      assert(File.stat(file).blocks < 16)
    end
  end

  def test_chardev?
    try(:chardev?, ".",        false)
    Unix.only do
      try(:chardev?, "/dev/tty", true)
      Linux.only do
	try(:chardev?, "/dev/fd0", false)
      end
    end
  end

  def test_ctime
    cTime1 = Time.at(RubiconStat::ctime(@file1))
    assert_equal(cTime1, @s1.ctime)
  end

  def test_dev
#    fail("untested")
  end

  def test_directory?
    try(:directory?, "/dev/tty", false)
    try(:directory?, ".",        true)
    try(:directory?, "/dev/fd0", false)
  end

  def test_executable?
    try(:executable?, "/dev/tty", false)
    try(:executable?, "/bin/echo",true)
    try(:executable?, "/dev/fd0", false)
  end

  def test_executable_real?
#    fail("untested")
  end

  def test_file?
    try(:file?, "/dev/tty", false)
    try(:file?, ".",        false)
    try(:file?, "/dev/fd0", false)
    try(:file?, @file1,     true)
  end

  def test_ftype
    Dir.chdir("_test")
    MsWin32.dont do
      File.symlink("_file1", "_file3") # may fail
    end

    tests = {
      "../_test"          => "directory",
      "_file1"            => "file",
      "/dev/tty"          => "characterSpecial",
      "/tmp/.X11-unix/X0" => "socket",
    }

    MsWin32.dont do
      tests["_file3"]     =  "file"
    end

    Linux.only do
      tests["/dev/fd0"]   = "blockSpecial"
      system("mkfifo _fifo") # may fail
      tests["_fifo"]      = "fifo" 
    end

    tests.each do |file, type|
      try(:ftype, file, type)
    end

    MsWin32.dont do
      assert_equal("link", File.lstat("_file3").ftype)
    end
  end

  Linux.only do
    def test_gid
      assert_equal(Process.gid, @s1.gid)
    end
    
    def test_grpowned?
      try(:grpowned?, @file1,        true)
      Unix.or_variant do
        try(:grpowned?, "/etc/passwd", Process.egid == 0)
      end
    end
  end

  Linux.dont do
    def test_gid
      skipping "Behavior unknown (feel free up update!)"
    end
    
    def test_grpowned?
      skipping "Behavior unknown (feel free up update!)"
    end
  end

  WindowsNative.dont do
    def test_ino
      Dir.chdir("_test")
      File.link("_file1", "_file3") # may fail
      assert(File.stat("_file1").ino > 0)
      assert(File.stat("_file2").ino > 0)
      assert_equal(File.stat("_file1").ino, File.stat("_file3").ino)
    end
  end

  def test_mode
    Dir.chdir("_test")
    begin
      File.open("_file1") do |f|
	assert_equal(0,                f.chmod(0))
	assert_equal(_fstat_map(0),    f.stat.mode & 0777)
	assert_equal(0,                f.chmod(0400))
	assert_equal(_fstat_map(0400), f.stat.mode & 0777)
	assert_equal(0,                f.chmod(0644))
	assert_equal(_fstat_map(0644), f.stat.mode & 0777)
      end
    ensure
      Dir.chdir("..")
    end
  end

  def test_mtime
    assert_equal(@mTime1, @s1.mtime)
    assert_equal(@mTime2, @s2.mtime)
 end

  def test_nlink
    Dir.chdir("_test")
    File.link("_file1", "_file3") # may fail
    try(:nlink, "_file1", 2)
    try(:nlink, "_file2", 1)
    try(:nlink, "_file3", 2)
  end

  def test_owned?
    try(:owned?, @file1,        true)
    Unix.or_variant do
      try(:owned?, "/etc/passwd", Process.euid == 0)
    end
  end

  def test_pipe?
    Unix.or_variant do
      try(:pipe?, "/dev/tty", false)
    end

    try(:pipe?, ".",        false)
    
    MsWin32.dont do
      IO.popen("-") do |p|
	assert_equal(true, (p ? p : $stdout).stat.pipe?)
      end
    end
  end

  def test_rdev
    # fail("untested")
  end

  def test_readable?
    try(:readable?, @file1, true)
    File.chmod(0222, @file1)
    if $os == MsWin32
      # The Windows documentation for chmod(2) says:
      #
      #     Note that all files are always readable;
      #     it is not possible to give write-only permission.
      #
      try(:readable?, @file1, true)
    else
      try(:readable?, @file1, false)
    end
  end

  def test_readable_real?
#    fail("untested")
  end

  Unix.or_variant do
    
    def test_setgid?
      try(:setgid?, @file1, false)
      File.chmod(02644, @file1)
      try(:setgid?, @file1, true)
    end
    
    def test_setuid?
      try(:setuid?, @file1, false)
      File.chmod(04644, @file1)
      try(:setuid?, @file1, true)
    end
  end

  def test_size
    File.open(@file1, "w") { |f| f.syswrite "wombat" }
    try(:size, @file1, 6 )
    try(:size, @file2, 0)
  end

  def test_size?
    File.open(@file1, "w") { |f| f.syswrite "wombat" }
    try(:size?, @file1, 6 )
    try(:size?, @file2, nil)
  end

  def test_socket?
    try(:socket?, "/dev/tty", false)
    try(:socket?, ".",        false)
    try(:socket?, @file1,     false)
    try(:socket?, "/tmp/.X11-unix/X0", true)
  end

  Unix.or_variant do
    def test_sticky?
      Dir.chdir("_test")
      m = File.stat(".").mode
      begin
	File.chmod(m | 01000, ".")
	try(:sticky?, ".",      true)
      ensure
	File.chmod(m, ".")
      end
      try(:sticky?, ".",        false)
      try(:sticky?, "/dev/tty", false)
      try(:sticky?, "_file2",   false)
    end
  end

  MsWin32.dont do
    def test_symlink?
      Dir.chdir("_test")
      File.symlink("_file1", "_symlink")
      try(:symlink?, ".",        false)
      try(:symlink?, "/dev/tty", false)
      try(:symlink?, "_file1",   false)
      try(:symlink?, "_symlink", false)  # try uses stat
      assert(File.lstat("_symlink").symlink?)
    end
  end

  def test_uid
    assert_equal(Process.uid, @s1.uid)
  end

  def test_writable?
    File.chmod(0444, @file1)
    try(:writable?, @file1, false)
    try(:writable?, @file2, true)
  end

  def test_writable_real?
#    fail("untested")
  end

  def test_zero?
    File.open(@file1, "w") { |f| f.puts "wombat" }
    try(:zero?, @file1, false)
    try(:zero?, @file2, true)
  end

end