Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test for unpinning
/**
* Logs if HttpsURLConnection.setHostnameVerifier() is working or not.
* Also returns boolean to that effect.
*/
static boolean testHostnameVerifier() {
boolean isWorking = false;
HttpsURLConnection connection = null;
try {
URL aUrl = new URL("https://firebase.google.com");
// Get our HTTPS connection object
connection = (HttpsURLConnection) aUrl.openConnection();
// Register a hostname verifier which always fails
connection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
Log.i("testHostnameVerifier", "CALLED");
return false;
}
});
connection.connect();
// Should never get here with the HostnameVerifier we just set.
Log.i("testHostnameVerifier", "HostnameVerifer NOT WORKING");
} catch (SSLPeerUnverifiedException ex) {
// This exception happens with hostname verifier returns false
Log.i("testHostnameVerifier", "HostnameVerifer IS WORKING");
isWorking = true;
} catch(Exception ex) {
Log.e("testHostnameVerifier", ex.getMessage());
}
if(connection != null)
connection.disconnect();
return isWorking;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment