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;
}
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);
}
Aggregations