1
1
// RUN: %target-run-simple-swift %t
2
2
// REQUIRES: executable_test
3
3
4
- // Android Bionic does not provide a working implementation of
5
- // <semaphore.h>.
6
- // XFAIL: OS=linux-androideabi
7
-
8
4
import StdlibUnittest
9
- #if os(Linux)
5
+ #if os(Linux) || os(Android)
10
6
import Glibc
11
7
#else
12
8
import Darwin
@@ -17,21 +13,55 @@ chdir(CommandLine.arguments[1])
17
13
var POSIXTests = TestSuite ( " POSIXTests " )
18
14
19
15
let semaphoreName = " TestSem "
16
+ #if os(Android)
17
+ // In Android, the cwd is the root directory, which is not writable.
18
+ let fn : String = {
19
+ let capacity = Int ( PATH_MAX)
20
+ let resolvedPath = UnsafeMutablePointer< Int8> . allocate( capacity: capacity)
21
+ resolvedPath. initialize ( repeating: 0 , count: capacity)
22
+ defer {
23
+ resolvedPath. deinitialize ( count: capacity)
24
+ resolvedPath. deallocate ( )
25
+ }
26
+ guard let _ = realpath ( " /proc/self/exe " , resolvedPath) else {
27
+ fatalError ( " Couldn't obtain executable path " )
28
+ }
29
+
30
+ let length = strlen ( resolvedPath)
31
+ precondition ( length != 0 , " Couldn't obtain valid executable path " )
32
+
33
+ // Search backwards for the last /, and turn it into a null byte.
34
+ for idx in stride ( from: length- 1 , through: 0 , by: - 1 ) {
35
+ if Unicode . Scalar ( UInt8 ( resolvedPath [ idx] ) ) == Unicode . Scalar ( " / " ) {
36
+ resolvedPath [ idx] = 0
37
+ break
38
+ }
39
+
40
+ precondition ( idx != 0 , " Couldn't obtain valid executable directory " )
41
+ }
42
+
43
+ return String ( cString: resolvedPath) + " /test.txt "
44
+ } ( )
45
+ #else
20
46
let fn = " test.txt "
47
+ #endif
21
48
22
49
POSIXTests . setUp {
23
50
sem_unlink ( semaphoreName)
24
51
unlink ( fn)
25
52
}
26
53
27
54
// Failed semaphore creation.
55
+ #if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
28
56
POSIXTests . test ( " sem_open fail " ) {
29
57
let sem = sem_open ( semaphoreName, 0 )
30
58
expectEqual ( SEM_FAILED, sem)
31
59
expectEqual ( ENOENT, errno)
32
60
}
61
+ #endif
33
62
34
63
// Successful semaphore creation.
64
+ #if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
35
65
POSIXTests . test ( " sem_open success " ) {
36
66
let sem = sem_open ( semaphoreName, O_CREAT, 0o777 , 1 )
37
67
expectNotEqual ( SEM_FAILED, sem)
@@ -42,8 +72,10 @@ POSIXTests.test("sem_open success") {
42
72
let res2 = sem_unlink ( semaphoreName)
43
73
expectEqual ( 0 , res2)
44
74
}
75
+ #endif
45
76
46
77
// Successful semaphore creation with O_EXCL.
78
+ #if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
47
79
POSIXTests . test ( " sem_open O_EXCL success " ) {
48
80
let sem = sem_open ( semaphoreName, O_CREAT | O_EXCL, 0o777 , 1 )
49
81
expectNotEqual ( SEM_FAILED, sem)
@@ -54,8 +86,10 @@ POSIXTests.test("sem_open O_EXCL success") {
54
86
let res2 = sem_unlink ( semaphoreName)
55
87
expectEqual ( 0 , res2)
56
88
}
89
+ #endif
57
90
58
91
// Successful creation and re-obtaining of existing semaphore.
92
+ #if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
59
93
POSIXTests . test ( " sem_open existing " ) {
60
94
let sem = sem_open ( semaphoreName, O_CREAT, 0o777 , 1 )
61
95
expectNotEqual ( SEM_FAILED, sem)
@@ -71,8 +105,10 @@ POSIXTests.test("sem_open existing") {
71
105
let res2 = sem_unlink ( semaphoreName)
72
106
expectEqual ( 0 , res2)
73
107
}
108
+ #endif
74
109
75
110
// Fail because the semaphore already exists.
111
+ #if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
76
112
POSIXTests . test ( " sem_open existing O_EXCL fail " ) {
77
113
let sem = sem_open ( semaphoreName, O_CREAT, 0o777 , 1 )
78
114
expectNotEqual ( SEM_FAILED, sem)
@@ -87,6 +123,7 @@ POSIXTests.test("sem_open existing O_EXCL fail") {
87
123
let res2 = sem_unlink ( semaphoreName)
88
124
expectEqual ( 0 , res2)
89
125
}
126
+ #endif
90
127
91
128
// Fail because the file descriptor is invalid.
92
129
POSIXTests . test ( " ioctl(CInt, UInt, CInt): fail " ) {
@@ -99,7 +136,7 @@ POSIXTests.test("ioctl(CInt, UInt, CInt): fail") {
99
136
expectEqual ( EBADF, errno)
100
137
}
101
138
102
- #if os(Linux)
139
+ #if os(Linux) || os(Android)
103
140
// Successful creation of a socket and listing interfaces
104
141
POSIXTests . test ( " ioctl(CInt, UInt, UnsafeMutableRawPointer): listing interfaces success " ) {
105
142
// Create a socket
@@ -204,7 +241,13 @@ POSIXTests.test("fcntl(CInt, CInt, UnsafeMutableRawPointer): locking and unlocki
204
241
// Lock for reading...
205
242
var flck = flock ( )
206
243
flck. l_type = Int16 ( F_RDLCK)
244
+ #if os(Android)
245
+ // In Android l_len is __kernel_off_t which is not the same size as off_t in
246
+ // 64 bits.
247
+ flck. l_len = __kernel_off_t ( data. utf8. count)
248
+ #else
207
249
flck. l_len = off_t ( data. utf8. count)
250
+ #endif
208
251
rc = fcntl ( fd, F_SETLK, & flck)
209
252
expectEqual ( 0 , rc)
210
253
@@ -223,4 +266,3 @@ POSIXTests.test("fcntl(CInt, CInt, UnsafeMutableRawPointer): locking and unlocki
223
266
}
224
267
225
268
runAllTests ( )
226
-
0 commit comments