Search in sources :

Example 6 with HostNum

use of com.ichi2.libanki.sync.HostNum in project Anki-Android by ankidroid.

the class Connection method doInBackgroundLogin.

private Payload doInBackgroundLogin(Payload data) {
    String username = (String) data.data[0];
    String password = (String) data.data[1];
    HostNum hostNum = (HostNum) data.data[2];
    RemoteServer server = new RemoteServer(this, null, hostNum);
    Response ret;
    try {
        ret = server.hostKey(username, password);
    } catch (UnknownHttpResponseException e) {
        Timber.w(e);
        data.success = false;
        data.resultType = ERROR;
        data.result = new Object[] { e.getResponseCode(), e.getMessage() };
        return data;
    } catch (CustomSyncServerUrlException e2) {
        Timber.w(e2);
        data.success = false;
        data.resultType = CUSTOM_SYNC_SERVER_URL;
        data.result = new Object[] { e2 };
        return data;
    } catch (Exception e2) {
        Timber.w(e2);
        // Ask user to report all bugs which aren't timeout errors
        if (!timeoutOccurred(e2)) {
            AnkiDroidApp.sendExceptionReport(e2, "doInBackgroundLogin");
        }
        data.success = false;
        data.resultType = CONNECTION_ERROR;
        data.result = new Object[] { e2 };
        return data;
    }
    String hostkey = null;
    boolean valid = false;
    if (ret != null) {
        data.returnType = ret.code();
        Timber.d("doInBackgroundLogin - response from server: %d, (%s)", data.returnType, ret.message());
        if (data.returnType == 200) {
            try {
                JSONObject response = new JSONObject(ret.body().string());
                hostkey = response.getString("key");
                valid = (hostkey != null) && (hostkey.length() > 0);
            } catch (JSONException e) {
                Timber.w(e);
                valid = false;
            } catch (IllegalStateException | IOException | NullPointerException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        Timber.e("doInBackgroundLogin - empty response from server");
    }
    if (valid) {
        data.success = true;
        data.data = new String[] { username, hostkey };
    } else {
        data.success = false;
    }
    return data;
}
Also used : JSONException(com.ichi2.utils.JSONException) HostNum(com.ichi2.libanki.sync.HostNum) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) CustomSyncServerUrlException(com.ichi2.libanki.sync.CustomSyncServerUrlException) JSONException(com.ichi2.utils.JSONException) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) CustomSyncServerUrlException(com.ichi2.libanki.sync.CustomSyncServerUrlException) JSONObject(com.ichi2.utils.JSONObject) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

Example 7 with HostNum

use of com.ichi2.libanki.sync.HostNum in project Anki-Android by ankidroid.

the class HttpTest method testLogin.

@Test
// #7108: AsyncTask
@SuppressWarnings("deprecation")
public void testLogin() {
    String username = "AnkiDroidInstrumentedTestUser";
    String password = "AnkiDroidInstrumentedTestInvalidPass";
    Connection.Payload invalidPayload = new Connection.Payload(new Object[] { username, password, new HostNum(null) });
    TestTaskListener testListener = new TestTaskListener(invalidPayload);
    // We have to carefully run things on the main thread here or the threading protections in BaseAsyncTask throw
    // The first one is just to run the static initializer, really
    Runnable onlineRunnable = () -> {
        try {
            Class.forName("com.ichi2.async.Connection");
        } catch (Exception e) {
            Assert.fail("Unable to load Connection class: " + e.getMessage());
        }
    };
    InstrumentationRegistry.getInstrumentation().runOnMainSync(onlineRunnable);
    // TODO simulate offline programmatically - currently exercised by manually toggling an emulator offline pre-test
    if (!Connection.isOnline()) {
        Connection.login(testListener, invalidPayload);
        Assert.assertFalse("Successful login despite being offline", testListener.getPayload().success);
        Assert.assertTrue("onDisconnected not called despite being offline", testListener.mDisconnectedCalled);
        return;
    }
    Runnable r = () -> {
        Connection conn = Connection.login(testListener, invalidPayload);
        try {
            // This forces us to synchronously wait for the AsyncTask to do it's work
            conn.get();
        } catch (Exception e) {
            Assert.fail("Caught exception while trying to login: " + e.getMessage());
        }
    };
    InstrumentationRegistry.getInstrumentation().runOnMainSync(r);
    Assert.assertFalse("Successful login despite invalid credentials", testListener.getPayload().success);
}
Also used : Connection(com.ichi2.async.Connection) HostNum(com.ichi2.libanki.sync.HostNum) Test(org.junit.Test)

Aggregations

HostNum (com.ichi2.libanki.sync.HostNum)7 MediaSyncException (com.ichi2.anki.exception.MediaSyncException)5 UnknownHttpResponseException (com.ichi2.anki.exception.UnknownHttpResponseException)5 RemoteServer (com.ichi2.libanki.sync.RemoteServer)5 JSONException (com.ichi2.utils.JSONException)5 JSONObject (com.ichi2.utils.JSONObject)5 IOException (java.io.IOException)5 HttpSyncer (com.ichi2.libanki.sync.HttpSyncer)4 NoEnoughServerSpaceException (com.ichi2.anki.exception.NoEnoughServerSpaceException)3 Response (okhttp3.Response)3 Connection (com.ichi2.async.Connection)2 Collection (com.ichi2.libanki.Collection)2 CustomSyncServerUrlException (com.ichi2.libanki.sync.CustomSyncServerUrlException)2 FullSyncer (com.ichi2.libanki.sync.FullSyncer)2 MediaSyncer (com.ichi2.libanki.sync.MediaSyncer)2 RemoteMediaServer (com.ichi2.libanki.sync.RemoteMediaServer)2 Syncer (com.ichi2.libanki.sync.Syncer)2 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 Pair (android.util.Pair)1