Created
September 4, 2017 15:33
-
-
Save barryor/9e571a063ee13f07b9c9290a5c2c21bc to your computer and use it in GitHub Desktop.
Test for unpinning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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