Merge "Add addPublicKeyPins test" am: d18610d9b1

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2497539

Change-Id: I311a7011dd1729af2e659de680113c5b9dcfa8eb
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Stefano Duo
2023-03-21 14:55:30 +00:00
committed by Automerger Merge Worker

View File

@@ -26,6 +26,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import android.content.Context;
@@ -49,6 +50,11 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import java.time.Instant;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Set;
@RunWith(AndroidJUnit4.class)
public class HttpEngineTest {
private static final String HOST = "source.android.com";
@@ -182,6 +188,38 @@ public class HttpEngineTest {
// server.
}
private byte[] generateSha256() {
byte[] sha256 = new byte[32];
Arrays.fill(sha256, (byte) 58);
return sha256;
}
private Instant instantInFuture(int secondsIntoFuture) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, secondsIntoFuture);
return cal.getTime().toInstant();
}
@Test
public void testHttpEngine_AddPublicKeyPins() {
// CtsTestServer, when set in SslMode.NO_CLIENT_AUTH (required to trigger
// certificate verification, needed by this test), uses a certificate that
// doesn't match the hostname. For this reason, CtsTestServer cannot be used
// by this test.
Instant expirationInstant = instantInFuture(/* secondsIntoFuture */ 100);
boolean includeSubdomains = true;
Set<byte[]> pinsSha256 = Set.of(generateSha256());
mEngine = mEngineBuilder.addPublicKeyPins(
HOST, pinsSha256, includeSubdomains, expirationInstant).build();
UrlRequest.Builder builder =
mEngine.newUrlRequestBuilder(URL, mCallback.getExecutor(), mCallback);
mRequest = builder.build();
mRequest.start();
mCallback.expectCallback(ResponseStep.ON_FAILED);
assertNotNull("Expected an error", mCallback.mError);
}
@Test
public void testHttpEngine_EnableQuic() throws Exception {
mEngine = mEngineBuilder.setEnableQuic(true).addQuicHint(HOST, 443, 443).build();