Skip to content

Instantly share code, notes, and snippets.

@barryor
Created September 4, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryor/9e571a063ee13f07b9c9290a5c2c21bc to your computer and use it in GitHub Desktop.
Save barryor/9e571a063ee13f07b9c9290a5c2c21bc to your computer and use it in GitHub Desktop.
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