Search in sources :

Example 1 with AppPort

use of distributed_match_engine.Appcommon.AppPort in project edge-cloud-sampleapps by mobiledgex.

the class EngineCallTest method testRegisterAndFindCloudlet_001.

@Test
public void testRegisterAndFindCloudlet_001() {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    MatchingEngine me = new MatchingEngine(context);
    me.setUseWifiOnly(useWifiOnly);
    me.setMatchingEngineLocationAllowed(true);
    me.setAllowSwitchIfNoSubscriberInfo(true);
    AppConnectionManager appConnectionManager = me.getAppConnectionManager();
    AppClient.RegisterClientReply registerClientReply = null;
    String carrierName = "TDG";
    String organizationName = "MobiledgeX";
    String appName = "HttpEcho";
    String appVersion = "20191204";
    Socket socket = null;
    try {
        Location location = getTestLocation(47.6062, 122.3321);
        Future<AppClient.FindCloudletReply> findCloudletReplyFuture = me.registerAndFindCloudlet(context, hostOverride, portOverride, organizationName, appName, appVersion, location, "", 0, null, null, // FIXME: These parameters should be overloaded or optional.
        null);
        // Just wait:
        AppClient.FindCloudletReply findCloudletReply = findCloudletReplyFuture.get();
        HashMap<Integer, AppPort> appTcpPortMap = appConnectionManager.getTCPMap(findCloudletReply);
        AppPort appPort = appTcpPortMap.get(3001);
        // There should be at least one for a connection to be made.
        assertTrue(appPort != null);
        Future<Socket> socketFuture = me.getAppConnectionManager().getTcpSocket(findCloudletReply, appPort, appPort.getPublicPort(), (int) GRPC_TIMEOUT_MS);
        socket = socketFuture.get();
        assertTrue("FindCloudletReply failed!", findCloudletReply != null);
    } catch (ExecutionException ee) {
        Log.i(TAG, Log.getStackTraceString(ee));
        assertFalse("testRegisterAndFindCloudlet_001: ExecutionException! " + ee.getLocalizedMessage(), true);
    } catch (StatusRuntimeException sre) {
        Log.i(TAG, Log.getStackTraceString(sre));
        assertFalse("testRegisterAndFindCloudlet_001: StatusRuntimeException!", true);
    } catch (InterruptedException ie) {
        Log.i(TAG, Log.getStackTraceString(ie));
        assertFalse("testRegisterAndFindCloudlet_001: InterruptedException!", true);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        enableMockLocation(context, false);
    }
}
Also used : Context(android.content.Context) AppPort(distributed_match_engine.Appcommon.AppPort) IOException(java.io.IOException) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) StatusRuntimeException(io.grpc.StatusRuntimeException) AppClient(distributed_match_engine.AppClient) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) AppConnectionManager(com.mobiledgex.matchingengine.AppConnectionManager) Location(android.location.Location) Test(org.junit.Test)

Example 2 with AppPort

use of distributed_match_engine.Appcommon.AppPort in project edge-cloud-sampleapps by mobiledgex.

the class EngineCallTest method appConnectionTestTcp002.

/**
 * Tests the MatchingEngine SDK supplied HTTP connection to the edge cloudlet. FIXME: TLS Test with certs.
 */
@Test
public void appConnectionTestTcp002() {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    MatchingEngine me = new MatchingEngine(context);
    me.setUseWifiOnly(useWifiOnly);
    AppConnectionManager appConnect = me.getAppConnectionManager();
    me.setMatchingEngineLocationAllowed(true);
    me.setAllowSwitchIfNoSubscriberInfo(true);
    OkHttpClient httpClient = null;
    // Test against Http Echo.
    String carrierName = "TDG";
    String appName = "HttpEcho";
    String orgName = "MobiledgeX";
    String appVersion = "20191204";
    try {
        String data = "{\"Data\": \"food\"}";
        AppClient.RegisterClientRequest req = me.createDefaultRegisterClientRequest(context, orgName).setCarrierName(carrierName).setAppName(appName).setAppVers(appVersion).build();
        AppClient.RegisterClientReply registerReply;
        // FIXME: Need/want a secondary cloudlet for this AppInst test.
        if (true) {
            registerReply = me.registerClient(req, hostOverride, portOverride, GRPC_TIMEOUT_MS);
        } else {
            registerReply = me.registerClient(req, GRPC_TIMEOUT_MS);
        }
        assertTrue("Register did not succeed for HttpEcho appInst", registerReply.getStatus() == AppClient.ReplyStatus.RS_SUCCESS);
        Location location = getTestLocation(47.6062, 122.3321);
        AppClient.FindCloudletRequest findCloudletRequest = me.createDefaultFindCloudletRequest(context, location).setCarrierName(carrierName).build();
        assertEquals("Session cookies don't match!", registerReply.getSessionCookie(), findCloudletRequest.getSessionCookie());
        AppClient.FindCloudletReply findCloudletReply;
        if (true) {
            findCloudletReply = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
        } else {
            findCloudletReply = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
        }
        // SSL:
        Future<OkHttpClient> httpClientFuture = null;
        httpClientFuture = appConnect.getHttpClient((int) GRPC_TIMEOUT_MS);
        // FIXME: UI Console exposes HTTP as TCP only, so test here use getTcpMap().
        String url = null;
        assertTrue("No AppPorts!", findCloudletReply.getPortsCount() > 0);
        HashMap<Integer, AppPort> portMap = appConnect.getTCPMap(findCloudletReply);
        // Choose the port that we happen to know the internal port for, 3001.
        AppPort one = portMap.get(3001);
        url = appConnect.createUrl(findCloudletReply, one, one.getPublicPort());
        assertTrue("URL for server seems very incorrect. ", url != null && url.length() > "http://:".length());
        assertFalse("Failed to get an SSL Socket!", httpClientFuture == null);
        // Interface bound TCP socket, has default timeout equal to NetworkManager.
        httpClient = httpClientFuture.get();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, data);
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = httpClient.newCall(request).execute();
        String output = response.body().string();
        boolean found = output.indexOf("food") != -1 ? true : false;
        ;
        assertTrue("Didn't find json data [" + data + "] in response!", found == true);
    } catch (PackageManager.NameNotFoundException nnfe) {
    } catch (IOException ioe) {
        Log.e(TAG, Log.getStackTraceString(ioe));
        assertFalse("appConnectionTestTcp002: IOException", true);
    } catch (DmeDnsException dde) {
        Log.e(TAG, Log.getStackTraceString(dde));
        assertFalse("appConnectionTestTcp002: DmeDnsException", true);
    } catch (ExecutionException ee) {
        Log.i(TAG, Log.getStackTraceString(ee));
        assertFalse("appConnectionTestTcp002: ExecutionException!", true);
    } catch (StatusRuntimeException sre) {
        Log.i(TAG, sre.getMessage());
        Log.i(TAG, Log.getStackTraceString(sre));
        assertFalse("appConnectionTestTcp002: StatusRuntimeException!", true);
    } catch (InterruptedException ie) {
        Log.i(TAG, Log.getStackTraceString(ie));
        assertFalse("appConnectionTestTcp002: InterruptedException!", true);
    } finally {
        enableMockLocation(context, false);
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) AppPort(distributed_match_engine.Appcommon.AppPort) PackageManager(android.content.pm.PackageManager) StatusRuntimeException(io.grpc.StatusRuntimeException) MediaType(com.squareup.okhttp.MediaType) ExecutionException(java.util.concurrent.ExecutionException) RequestBody(com.squareup.okhttp.RequestBody) Context(android.content.Context) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) Response(com.squareup.okhttp.Response) AppClient(distributed_match_engine.AppClient) AppConnectionManager(com.mobiledgex.matchingengine.AppConnectionManager) Location(android.location.Location) DmeDnsException(com.mobiledgex.matchingengine.DmeDnsException) Test(org.junit.Test)

Example 3 with AppPort

use of distributed_match_engine.Appcommon.AppPort in project edge-cloud-sampleapps by mobiledgex.

the class EngineCallTest method appConnectionTestTcp001.

/**
 * Tests the MatchingEngine SDK supplied TCP connection to the edge cloudlet.
 *
 * This is a raw stream to a test echo server, so there are no explicit message delimiters.
 */
@Test
public void appConnectionTestTcp001() {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    MatchingEngine me = new MatchingEngine(context);
    me.setUseWifiOnly(useWifiOnly);
    AppConnectionManager appConnect = me.getAppConnectionManager();
    enableMockLocation(context, true);
    Socket s = null;
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    try {
        // Test against Http Echo.
        String carrierName = "TDG";
        String appName = "HttpEcho";
        String orgName = "MobiledgeX";
        String appVersion = "20191204";
        // Exercise and override the default:
        // The app version will be null, but we can build from scratch for test
        AppClient.RegisterClientRequest regRequest = AppClient.RegisterClientRequest.newBuilder().setCarrierName(me.retrieveNetworkCarrierName(context)).setOrgName(orgName).setAppName(appName).setAppVers(appVersion).build();
        AppClient.RegisterClientReply registerClientReply;
        if (true) {
            registerClientReply = me.registerClient(regRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
        } else {
            registerClientReply = me.registerClient(regRequest, GRPC_TIMEOUT_MS);
        }
        assertTrue("Register did not succeed for HttpEcho appInst", registerClientReply.getStatus() == AppClient.ReplyStatus.RS_SUCCESS);
        Location location = getTestLocation(47.6062, 122.3321);
        // Defaults:
        AppClient.FindCloudletRequest findCloudletRequest = me.createDefaultFindCloudletRequest(context, location).setCarrierName(findCloudletCarrierOverride).build();
        AppClient.FindCloudletReply findCloudletReply;
        if (true) {
            findCloudletReply = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
        } else {
            findCloudletReply = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
        }
        // Just using first one. This depends entirely on the server design.
        List<AppPort> appPorts = findCloudletReply.getPortsList();
        assertTrue("AppPorts is null", appPorts != null);
        assertTrue("AppPorts is empty!", appPorts.size() > 0);
        HashMap<Integer, AppPort> portMap = appConnect.getTCPMap(findCloudletReply);
        // This internal port depends entirely the AppInst configuration/Docker image.
        AppPort one = portMap.get(3001);
        assertTrue("EndPort is expected to be 0 for this AppInst", one.getEndPort() == 0);
        // The actual mapped Public port, or one between getPublicPort() to getEndPort(), inclusive.
        Future<Socket> fs = appConnect.getTcpSocket(findCloudletReply, one, one.getPublicPort(), (int) GRPC_TIMEOUT_MS);
        // Interface bound TCP socket.
        // Nothing to do. Await value.
        s = fs.get();
        try {
            bos = new BufferedOutputStream(s.getOutputStream());
            String data = "{\"Data\": \"food\"}";
            String rawpost = "POST / HTTP/1.1\r\n" + "Host: 10.227.66.62:3000\r\n" + "User-Agent: curl/7.54.0\r\n" + "Accept: */*\r\n" + "Content-Length: " + data.length() + "\r\n" + "Content-Type: application/json\r\n" + "\r\n" + data;
            bos.write(rawpost.getBytes());
            bos.flush();
            // Some arbitrary object Monitor.
            Object aMon = new Object();
            synchronized (aMon) {
                aMon.wait(1000);
            }
            bis = new BufferedInputStream(s.getInputStream());
            int available = bis.available();
            // Probably true.
            assertTrue("No bytes available in response.", available > 0);
            byte[] b = new byte[4096];
            int numRead = bis.read(b);
            assertTrue("Didn't get response!", numRead > 0);
            String output = new String(b);
            // Not an http client, so we're just going to get the substring of something stable:
            boolean found = output.indexOf("food") != -1 ? true : false;
            ;
            assertTrue("Didn't find json data [" + data + "] in response!", found == true);
        } catch (IOException ioe) {
            assertTrue("Failed to get output stream for socket!", false);
        }
    } catch (DmeDnsException dde) {
        Log.e(TAG, Log.getStackTraceString(dde));
        assertFalse("appConnectionTestTcp001: DmeDnsException", true);
    } catch (ExecutionException ee) {
        Log.i(TAG, Log.getStackTraceString(ee));
        assertFalse("appConnectionTestTcp001: ExecutionException!", true);
    } catch (StatusRuntimeException sre) {
        Log.i(TAG, sre.getMessage());
        Log.i(TAG, Log.getStackTraceString(sre));
        assertFalse("appConnectionTestTcp001: StatusRuntimeException!", true);
    } catch (InterruptedException ie) {
        Log.i(TAG, Log.getStackTraceString(ie));
        assertFalse("appConnectionTestTcp001: InterruptedException!", true);
    } catch (PackageManager.NameNotFoundException nnfe) {
        Log.i(TAG, Log.getStackTraceString(nnfe));
        assertFalse("appConnectionTestTcp001: NameNotFoundException!", true);
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (s != null) {
                s.close();
            }
        } catch (IOException ioe) {
            assertFalse("IO Exceptions trying to close socket.", true);
        }
        me.setNetworkSwitchingEnabled(true);
    }
}
Also used : AppPort(distributed_match_engine.Appcommon.AppPort) PackageManager(android.content.pm.PackageManager) BufferedInputStream(java.io.BufferedInputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) BufferedOutputStream(java.io.BufferedOutputStream) Context(android.content.Context) IOException(java.io.IOException) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) AppClient(distributed_match_engine.AppClient) Socket(java.net.Socket) AppConnectionManager(com.mobiledgex.matchingengine.AppConnectionManager) Location(android.location.Location) DmeDnsException(com.mobiledgex.matchingengine.DmeDnsException) Test(org.junit.Test)

Example 4 with AppPort

use of distributed_match_engine.Appcommon.AppPort in project edge-cloud-sampleapps by mobiledgex.

the class EngineCallTest method appConnectionTestTcp_Http_001.

/**
 * NOTE: HttpEcho may only be installed on wifi.dme domain
 */
@Test
public void appConnectionTestTcp_Http_001() {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    MatchingEngine me = new MatchingEngine(context);
    me.setUseWifiOnly(useWifiOnly);
    AppConnectionManager appConnect = me.getAppConnectionManager();
    me.setMatchingEngineLocationAllowed(true);
    me.setAllowSwitchIfNoSubscriberInfo(true);
    try {
        String data = "{\"Data\": \"food\"}";
        String carrierName = "TDG";
        String orgName = "MobiledgeX";
        String appName = "HttpEcho";
        String appVersion = "20191204";
        AppClient.RegisterClientRequest req = me.createDefaultRegisterClientRequest(context, orgName).setCarrierName(carrierName).setAppName(appName).setAppVers(appVersion).build();
        AppClient.RegisterClientReply registerClientReply;
        // FIXME: Need/want a secondary cloudlet for this AppInst test.
        if (true) {
            registerClientReply = me.registerClient(req, hostOverride, portOverride, GRPC_TIMEOUT_MS);
        } else {
            registerClientReply = me.registerClient(req, GRPC_TIMEOUT_MS);
        }
        assertTrue("Register did not succeed for HttpEcho appInst", registerClientReply.getStatus() == AppClient.ReplyStatus.RS_SUCCESS);
        Location location = getTestLocation(47.6062, 122.3321);
        AppClient.FindCloudletRequest findCloudletRequest = me.createDefaultFindCloudletRequest(context, location).setCarrierName(carrierName).build();
        // TODO: Validate JWT
        AppClient.FindCloudletReply findCloudletReply;
        if (true) {
            findCloudletReply = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
        } else {
            findCloudletReply = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
        }
        // SSL:
        Future<OkHttpClient> httpClientFuture = null;
        httpClientFuture = appConnect.getHttpClient(GRPC_TIMEOUT_MS);
        assertTrue("HttpClientFuture is NULL!", httpClientFuture != null);
        // FIXME: UI Console exposes HTTP as TCP only, so the test here uses getTcpList().
        String url = null;
        HashMap<Integer, AppPort> portMap = appConnect.getTCPMap(findCloudletReply);
        // Choose the TCP port, and we happen to know our server is on one port only: 3001.
        AppPort one = portMap.get(3001);
        assertTrue("Did not find server! ", one != null);
        url = appConnect.createUrl(findCloudletReply, one, one.getPublicPort());
        assertTrue("URL for server seems very incorrect. ", url != null && url.length() > "http://:".length());
        // Interface bound TCP socket, has default timeout equal to NetworkManager.
        OkHttpClient httpClient = httpClientFuture.get();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, data);
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = httpClient.newCall(request).execute();
        String output = response.body().string();
        boolean found = output.indexOf("food") != -1 ? true : false;
        assertTrue("Didn't find json data [" + data + "] in response!", found == true);
        Request mobiledgeXSiteRequest = new Request.Builder().url("https://mobiledgex.com").build();
        Response mexSiteResponse = httpClient.newCall(mobiledgeXSiteRequest).execute();
        int httpStatus = mexSiteResponse.code();
        assertEquals("Did not reach our home site. Status: ", 200, httpStatus);
        // This certificate goes to artifactory.mobiledgex.net, it *should* fail, but "connect" with
        // HTTP Status 200 OK.
        boolean failedVerification = false;
        mobiledgeXSiteRequest = new Request.Builder().url("https://mobiledgex.net").build();
        try {
            mexSiteResponse = httpClient.newCall(mobiledgeXSiteRequest).execute();
        } catch (SSLPeerUnverifiedException e) {
            failedVerification = true;
            httpStatus = mexSiteResponse.code();
            assertEquals("Should fail SSL Host verification, but still be 200 OK. Status: ", 200, httpStatus);
        }
        assertTrue("Did not fail hostname SSL verification!", failedVerification);
    } catch (PackageManager.NameNotFoundException nnfe) {
        Log.e(TAG, nnfe.getMessage());
        Log.i(TAG, Log.getStackTraceString(nnfe));
        assertFalse("appConnectionTestTcp001: Package Info is missing!", true);
    } catch (IOException ioe) {
        Log.e(TAG, Log.getStackTraceString(ioe));
        assertFalse("appConnectionTestTcp001: IOException", true);
    } catch (DmeDnsException dde) {
        Log.e(TAG, Log.getStackTraceString(dde));
        assertFalse("appConnectionTestTcp001: DmeDnsException", true);
    } catch (ExecutionException ee) {
        Log.i(TAG, Log.getStackTraceString(ee));
        assertFalse("appConnectionTestTcp001: ExecutionException!", true);
    } catch (StatusRuntimeException sre) {
        Log.e(TAG, sre.getMessage());
        Log.i(TAG, Log.getStackTraceString(sre));
        assertFalse("appConnectionTestTcp001: StatusRuntimeException!", true);
    } catch (InterruptedException ie) {
        Log.i(TAG, Log.getStackTraceString(ie));
        assertFalse("appConnectionTestTcp001: InterruptedException!", true);
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) AppPort(distributed_match_engine.Appcommon.AppPort) PackageManager(android.content.pm.PackageManager) StatusRuntimeException(io.grpc.StatusRuntimeException) MediaType(com.squareup.okhttp.MediaType) ExecutionException(java.util.concurrent.ExecutionException) RequestBody(com.squareup.okhttp.RequestBody) Context(android.content.Context) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) MatchingEngine(com.mobiledgex.matchingengine.MatchingEngine) Response(com.squareup.okhttp.Response) AppClient(distributed_match_engine.AppClient) AppConnectionManager(com.mobiledgex.matchingengine.AppConnectionManager) Location(android.location.Location) DmeDnsException(com.mobiledgex.matchingengine.DmeDnsException) Test(org.junit.Test)

Aggregations

Context (android.content.Context)4 Location (android.location.Location)4 AppConnectionManager (com.mobiledgex.matchingengine.AppConnectionManager)4 MatchingEngine (com.mobiledgex.matchingengine.MatchingEngine)4 AppClient (distributed_match_engine.AppClient)4 AppPort (distributed_match_engine.Appcommon.AppPort)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Test (org.junit.Test)4 PackageManager (android.content.pm.PackageManager)3 DmeDnsException (com.mobiledgex.matchingengine.DmeDnsException)3 MediaType (com.squareup.okhttp.MediaType)2 OkHttpClient (com.squareup.okhttp.OkHttpClient)2 Request (com.squareup.okhttp.Request)2 RequestBody (com.squareup.okhttp.RequestBody)2 Response (com.squareup.okhttp.Response)2 Socket (java.net.Socket)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1