-
Notifications
You must be signed in to change notification settings - Fork 91
/
FileStatTest.java
145 lines (122 loc) · 4.35 KB
/
FileStatTest.java
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
package jnr.posix;
import java.io.FileOutputStream;
import java.io.IOException;
import jnr.posix.util.FieldAccess;
import jnr.posix.util.Platform;
import org.junit.*;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import static org.junit.Assert.*;
public class FileStatTest {
public FileStatTest() {
}
private void addNBytes(File file, int amount) {
FileOutputStream fis = null;
try {
fis = new FileOutputStream(file);
byte[] buf = new byte[amount];
fis.write(buf);
} catch (IOException e) {
if (fis != null) { try { fis.close(); } catch (IOException e2) {} }
}
}
private static POSIX posix;
@BeforeClass
public static void setUpClass() throws Exception {
posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true);
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test public void filestat() throws Throwable {
File f = File.createTempFile("stat", null);
int size = 1567;
//Thread.sleep(2000);
addNBytes(f, size);
try {
FileStat stat = posix.stat(f.getAbsolutePath());
assertNotNull("posix.stat failed", stat);
assertEquals(size, stat.st_size());
//assertNotEquals(stat.mtime(), stat.ctime());
stat = posix.allocateStat();
int result = posix.stat(f.getAbsolutePath(), stat);
assertNotNull("posix.stat failed", stat);
assertEquals(0, result);
assertEquals(size, stat.st_size());
} finally {
f.delete();
}
}
@Test
public void filestatDescriptor() throws Throwable {
File f = File.createTempFile("stat", null);
try {
FileInputStream fis = new FileInputStream(f);
FileStat stat = posix.allocateStat();
int result = posix.fstat(fis.getFD(), stat);
assertEquals(0, result);
assertEquals(0, stat.st_size());
} finally {
f.delete();
}
}
@Test
public void filestatInt() throws Throwable {
// Windows does not store fd in FileDescriptor so this test wll not work
if (jnr.ffi.Platform.getNativePlatform().isUnix()) {
File f = File.createTempFile("stat", null);
try {
FileInputStream fis = new FileInputStream(f);
FileDescriptor desc = fis.getFD();
int fd = -1;
try {
Field fdField = FieldAccess.getProtectedField(FileDescriptor.class, "fd");
fd = fdField.getInt(desc);
} catch (SecurityException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
FileStat stat = posix.allocateStat();
int result = posix.fstat(fd, stat);
assertTrue(fd > 2); // should not be stdin, stdout, stderr
assertEquals(0, result);
} finally {
f.delete();
}
} else {
FileStat stat = posix.fstat(0);
assertTrue(stat != null);
}
}
@Test public void structStatSize() throws Throwable {
if (Platform.IS_SOLARIS) {
jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getSystemRuntime();
if (Platform.IS_32_BIT) {
assertEquals("struct size is wrong", 144, new SolarisFileStat32.Layout(runtime).size());
} else {
assertEquals("struct size is wrong", 128, new SolarisFileStat64.Layout(runtime).size());
}
}
if (Platform.IS_SOLARIS) {
File f = File.createTempFile("stat", null);
try {
FileStat st = posix.stat(f.getAbsolutePath());
if (Platform.IS_32_BIT) {
assertSame("incorrect stat instance returned", SolarisFileStat32.class, st.getClass());
} else {
assertSame("incorrect stat instance returned", SolarisFileStat64.class, st.getClass());
}
} finally {
f.delete();
}
}
}
}