Search in sources :

Example 11 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by ResurrectionRemix.

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) {
    }
}
Also used : SocketException(java.net.SocketException) LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 12 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by ResurrectionRemix.

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();
}
Also used : LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) Credentials(android.net.Credentials) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 13 with LocalSocketAddress

use of android.net.LocalSocketAddress in project Android-IMSI-Catcher-Detector by CellularPrivacy.

the class SamsungMulticlientRilExecutor method detect.

@Override
public DetectResult detect() {
    String gsmVerRilImpl = "";
    try {
        Class clazz;
        clazz = Class.forName("android.os.SystemProperties");
        Method method = clazz.getDeclaredMethod("get", String.class, String.class);
        gsmVerRilImpl = (String) method.invoke(null, "gsm.version.ril-impl", "");
    } catch (Exception ignore) {
        log.debug("ignore this exception?", ignore);
    }
    // WARNING may have bad consequences...
    if (!gsmVerRilImpl.matches("Samsung\\s+RIL\\(IPC\\).*")) {
        return DetectResult.Unavailable("gsm.version.ril-impl = " + gsmVerRilImpl);
    }
    LocalSocket s = new LocalSocket();
    try {
        s.connect(new LocalSocketAddress(MULTICLIENT_SOCKET));
    } catch (IOException e) {
        log.warn(e.getMessage());
        return DetectResult.Unavailable("Multiclient socket is not available\n" + "gsm.version.ril-impl = " + gsmVerRilImpl);
    } finally {
        try {
            s.close();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
    return DetectResult.AVAILABLE;
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) Method(java.lang.reflect.Method) IOException(java.io.IOException) IOException(java.io.IOException)

Example 14 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by crdroidandroid.

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) {
    }
}
Also used : SocketException(java.net.SocketException) LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 15 with LocalSocketAddress

use of android.net.LocalSocketAddress in project libstreaming by fyhertz.

the class MediaStream method createSockets.

protected void createSockets() throws IOException {
    if (sPipeApi == PIPE_API_LS) {
        final String LOCAL_ADDR = "net.majorkernelpanic.streaming-";
        for (int i = 0; i < 10; i++) {
            try {
                mSocketId = new Random().nextInt();
                mLss = new LocalServerSocket(LOCAL_ADDR + mSocketId);
                break;
            } catch (IOException e1) {
            }
        }
        mReceiver = new LocalSocket();
        mReceiver.connect(new LocalSocketAddress(LOCAL_ADDR + mSocketId));
        mReceiver.setReceiveBufferSize(500000);
        mReceiver.setSoTimeout(3000);
        mSender = mLss.accept();
        mSender.setSendBufferSize(500000);
    } else {
        Log.e(TAG, "parcelFileDescriptors createPipe version = Lollipop");
        mParcelFileDescriptors = ParcelFileDescriptor.createPipe();
        mParcelRead = new ParcelFileDescriptor(mParcelFileDescriptors[0]);
        mParcelWrite = new ParcelFileDescriptor(mParcelFileDescriptors[1]);
    }
}
Also used : Random(java.util.Random) LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) ParcelFileDescriptor(android.os.ParcelFileDescriptor) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint)

Aggregations

LocalSocket (android.net.LocalSocket)32 LocalSocketAddress (android.net.LocalSocketAddress)32 IOException (java.io.IOException)27 LocalServerSocket (android.net.LocalServerSocket)12 SmallTest (android.test.suitebuilder.annotation.SmallTest)11 FileDescriptor (java.io.FileDescriptor)11 InputStream (java.io.InputStream)7 Credentials (android.net.Credentials)6 Message (android.os.Message)6 SocketException (java.net.SocketException)5 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 SuppressLint (android.annotation.SuppressLint)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 Method (java.lang.reflect.Method)1 Random (java.util.Random)1