ConfigurableSSLSocketFactoryTest.java

/*
 * Copyright (c) 2026 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.http.javanet;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ConfigurableSSLSocketFactoryTest {

  @Test
  public void testCreateSocketDelegatesAndConfigures() throws IOException {
    SSLSocketFactory delegate = (SSLSocketFactory) SSLSocketFactory.getDefault();
    final AtomicInteger callbackCount = new AtomicInteger(0);
    SslSocketConfigurator configurator =
        new SslSocketConfigurator() {
          @Override
          public void configure(SSLSocket socket) {
            callbackCount.incrementAndGet();
          }
        };

    ConfigurableSSLSocketFactory factory = new ConfigurableSSLSocketFactory(delegate, configurator);

    Socket result = factory.createSocket();
    assertTrue(result instanceof SSLSocket);
    assertEquals(1, callbackCount.get());
  }

  @Test
  public void testGetCipherSuitesDelegates() {
    SSLSocketFactory delegate = (SSLSocketFactory) SSLSocketFactory.getDefault();
    ConfigurableSSLSocketFactory factory = new ConfigurableSSLSocketFactory(delegate, null);

    assertEquals(delegate.getDefaultCipherSuites().length, factory.getDefaultCipherSuites().length);
    assertEquals(
        delegate.getSupportedCipherSuites().length, factory.getSupportedCipherSuites().length);
  }
}