Skip to content

Commit 85a2cc4

Browse files
authored
Merge pull request #20019 from drodriguez/android-fix-posix-test
[android] Adapt POSIX test to Android
2 parents 0bf440c + a17f45d commit 85a2cc4

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

test/stdlib/POSIX.swift

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// RUN: %target-run-simple-swift %t
22
// REQUIRES: executable_test
33

4-
// Android Bionic does not provide a working implementation of
5-
// <semaphore.h>.
6-
// XFAIL: OS=linux-androideabi
7-
84
import StdlibUnittest
9-
#if os(Linux)
5+
#if os(Linux) || os(Android)
106
import Glibc
117
#else
128
import Darwin
@@ -17,21 +13,55 @@ chdir(CommandLine.arguments[1])
1713
var POSIXTests = TestSuite("POSIXTests")
1814

1915
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
2046
let fn = "test.txt"
47+
#endif
2148

2249
POSIXTests.setUp {
2350
sem_unlink(semaphoreName)
2451
unlink(fn)
2552
}
2653

2754
// Failed semaphore creation.
55+
#if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
2856
POSIXTests.test("sem_open fail") {
2957
let sem = sem_open(semaphoreName, 0)
3058
expectEqual(SEM_FAILED, sem)
3159
expectEqual(ENOENT, errno)
3260
}
61+
#endif
3362

3463
// Successful semaphore creation.
64+
#if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
3565
POSIXTests.test("sem_open success") {
3666
let sem = sem_open(semaphoreName, O_CREAT, 0o777, 1)
3767
expectNotEqual(SEM_FAILED, sem)
@@ -42,8 +72,10 @@ POSIXTests.test("sem_open success") {
4272
let res2 = sem_unlink(semaphoreName)
4373
expectEqual(0, res2)
4474
}
75+
#endif
4576

4677
// Successful semaphore creation with O_EXCL.
78+
#if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
4779
POSIXTests.test("sem_open O_EXCL success") {
4880
let sem = sem_open(semaphoreName, O_CREAT | O_EXCL, 0o777, 1)
4981
expectNotEqual(SEM_FAILED, sem)
@@ -54,8 +86,10 @@ POSIXTests.test("sem_open O_EXCL success") {
5486
let res2 = sem_unlink(semaphoreName)
5587
expectEqual(0, res2)
5688
}
89+
#endif
5790

5891
// Successful creation and re-obtaining of existing semaphore.
92+
#if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
5993
POSIXTests.test("sem_open existing") {
6094
let sem = sem_open(semaphoreName, O_CREAT, 0o777, 1)
6195
expectNotEqual(SEM_FAILED, sem)
@@ -71,8 +105,10 @@ POSIXTests.test("sem_open existing") {
71105
let res2 = sem_unlink(semaphoreName)
72106
expectEqual(0, res2)
73107
}
108+
#endif
74109

75110
// Fail because the semaphore already exists.
111+
#if !os(Android) // Android doesn’t implement sem_open and always return ENOSYS
76112
POSIXTests.test("sem_open existing O_EXCL fail") {
77113
let sem = sem_open(semaphoreName, O_CREAT, 0o777, 1)
78114
expectNotEqual(SEM_FAILED, sem)
@@ -87,6 +123,7 @@ POSIXTests.test("sem_open existing O_EXCL fail") {
87123
let res2 = sem_unlink(semaphoreName)
88124
expectEqual(0, res2)
89125
}
126+
#endif
90127

91128
// Fail because the file descriptor is invalid.
92129
POSIXTests.test("ioctl(CInt, UInt, CInt): fail") {
@@ -99,7 +136,7 @@ POSIXTests.test("ioctl(CInt, UInt, CInt): fail") {
99136
expectEqual(EBADF, errno)
100137
}
101138

102-
#if os(Linux)
139+
#if os(Linux) || os(Android)
103140
// Successful creation of a socket and listing interfaces
104141
POSIXTests.test("ioctl(CInt, UInt, UnsafeMutableRawPointer): listing interfaces success") {
105142
// Create a socket
@@ -204,7 +241,13 @@ POSIXTests.test("fcntl(CInt, CInt, UnsafeMutableRawPointer): locking and unlocki
204241
// Lock for reading...
205242
var flck = flock()
206243
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
207249
flck.l_len = off_t(data.utf8.count)
250+
#endif
208251
rc = fcntl(fd, F_SETLK, &flck)
209252
expectEqual(0, rc)
210253

@@ -223,4 +266,3 @@ POSIXTests.test("fcntl(CInt, CInt, UnsafeMutableRawPointer): locking and unlocki
223266
}
224267

225268
runAllTests()
226-

0 commit comments

Comments
 (0)