use of android.net.LocalServerSocket in project android_frameworks_base by DirtyUnicorns.
the class LocalSocketTest method testBasic.
@SmallTest
public void testBasic() throws Exception {
LocalServerSocket ss;
LocalSocket ls;
LocalSocket ls1;
ss = new LocalServerSocket("android.net.LocalSocketTest");
ls = new LocalSocket();
ls.connect(new LocalSocketAddress("android.net.LocalSocketTest"));
ls1 = ss.accept();
// Test trivial read and write
ls.getOutputStream().write(42);
assertEquals(42, ls1.getInputStream().read());
// Test getting credentials
Credentials c = ls1.getPeerCredentials();
MoreAsserts.assertNotEqual(0, c.getPid());
// Test sending and receiving file descriptors
ls.setFileDescriptorsForSend(new FileDescriptor[] { FileDescriptor.in });
ls.getOutputStream().write(42);
assertEquals(42, ls1.getInputStream().read());
FileDescriptor[] out = ls1.getAncillaryFileDescriptors();
assertEquals(1, out.length);
// Test multible byte write and available()
ls1.getOutputStream().write(new byte[] { 0, 1, 2, 3, 4, 5 }, 1, 5);
assertEquals(1, ls.getInputStream().read());
assertEquals(4, ls.getInputStream().available());
byte[] buffer = new byte[16];
int countRead;
countRead = ls.getInputStream().read(buffer, 1, 15);
assertEquals(4, countRead);
assertEquals(2, buffer[1]);
assertEquals(3, buffer[2]);
assertEquals(4, buffer[3]);
assertEquals(5, buffer[4]);
// Try various array-out-of-bound cases
try {
ls.getInputStream().read(buffer, 1, 16);
fail("expected exception");
} catch (ArrayIndexOutOfBoundsException ex) {
// excpected
}
try {
ls.getOutputStream().write(buffer, 1, 16);
fail("expected exception");
} catch (ArrayIndexOutOfBoundsException ex) {
// excpected
}
try {
ls.getOutputStream().write(buffer, -1, 15);
fail("expected exception");
} catch (ArrayIndexOutOfBoundsException ex) {
// excpected
}
try {
ls.getOutputStream().write(buffer, 0, -1);
fail("expected exception");
} catch (ArrayIndexOutOfBoundsException ex) {
// excpected
}
try {
ls.getInputStream().read(buffer, -1, 15);
fail("expected exception");
} catch (ArrayIndexOutOfBoundsException ex) {
// excpected
}
try {
ls.getInputStream().read(buffer, 0, -1);
fail("expected exception");
} catch (ArrayIndexOutOfBoundsException ex) {
// excpected
}
// Try read of length 0
ls.getOutputStream().write(42);
countRead = ls1.getInputStream().read(buffer, 0, 0);
assertEquals(0, countRead);
assertEquals(42, ls1.getInputStream().read());
ss.close();
ls.close();
try {
ls.getOutputStream().write(42);
fail("expected exception");
} catch (IOException ex) {
// Expected
}
try {
ls.getInputStream().read();
fail("expected exception");
} catch (IOException ex) {
// Expected
}
try {
ls1.getOutputStream().write(42);
fail("expected exception");
} catch (IOException ex) {
// Expected
}
// Try read on socket whose peer has closed
assertEquals(-1, ls1.getInputStream().read());
ls1.close();
}
use of android.net.LocalServerSocket in project android_frameworks_base by DirtyUnicorns.
the class NetworkTest method testBindSocketOfLocalSocketThrows.
@SmallTest
public void testBindSocketOfLocalSocketThrows() throws Exception {
final LocalSocket mLocalClient = new LocalSocket();
mLocalClient.bind(new LocalSocketAddress("testClient"));
assertTrue(mLocalClient.getFileDescriptor().valid());
try {
mNetwork.bindSocket(mLocalClient.getFileDescriptor());
fail("SocketException not thrown");
} catch (SocketException expected) {
}
final LocalServerSocket mLocalServer = new LocalServerSocket("testServer");
mLocalClient.connect(mLocalServer.getLocalSocketAddress());
assertTrue(mLocalClient.isConnected());
try {
mNetwork.bindSocket(mLocalClient.getFileDescriptor());
fail("SocketException not thrown");
} catch (SocketException expected) {
}
}
use of android.net.LocalServerSocket in project android_frameworks_base by ResurrectionRemix.
the class NetworkManagementServiceTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
// TODO: make this unnecessary. runtest might already make it unnecessary.
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
// Set up a sheltered test environment.
BroadcastInterceptingContext context = new BroadcastInterceptingContext(getContext());
mServerSocket = new LocalServerSocket(SOCKET_NAME);
// Start the service and wait until it connects to our socket.
mNMService = NetworkManagementService.create(context, SOCKET_NAME);
mSocket = mServerSocket.accept();
mOutputStream = mSocket.getOutputStream();
}
use of android.net.LocalServerSocket in project android_frameworks_base by ResurrectionRemix.
the class ZygoteInit method registerZygoteSocket.
/**
* Registers a server socket for zygote command connections
*
* @throws RuntimeException when open fails
*/
private static void registerZygoteSocket(String socketName) {
if (sServerSocket == null) {
int fileDesc;
final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName;
try {
String env = System.getenv(fullSocketName);
fileDesc = Integer.parseInt(env);
} catch (RuntimeException ex) {
throw new RuntimeException(fullSocketName + " unset or invalid", ex);
}
try {
FileDescriptor fd = new FileDescriptor();
fd.setInt$(fileDesc);
sServerSocket = new LocalServerSocket(fd);
} catch (IOException ex) {
throw new RuntimeException("Error binding to local socket '" + fileDesc + "'", ex);
}
}
}
use of android.net.LocalServerSocket in project android_frameworks_base by DirtyUnicorns.
the class ZygoteInit method registerZygoteSocket.
/**
* Registers a server socket for zygote command connections
*
* @throws RuntimeException when open fails
*/
private static void registerZygoteSocket(String socketName) {
if (sServerSocket == null) {
int fileDesc;
final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName;
try {
String env = System.getenv(fullSocketName);
fileDesc = Integer.parseInt(env);
} catch (RuntimeException ex) {
throw new RuntimeException(fullSocketName + " unset or invalid", ex);
}
try {
FileDescriptor fd = new FileDescriptor();
fd.setInt$(fileDesc);
sServerSocket = new LocalServerSocket(fd);
} catch (IOException ex) {
throw new RuntimeException("Error binding to local socket '" + fileDesc + "'", ex);
}
}
}
Aggregations