Search in sources :

Example 6 with Expectations

use of mockit.Expectations in project pulsar by yahoo.

the class AbstractIncrementalLongHashTest method testResumeLongReadOnlyByteBuffer.

@Test
public void testResumeLongReadOnlyByteBuffer() {
    final ByteBuffer input = ByteBuffer.allocate(20).asReadOnlyBuffer();
    input.position(5);
    input.limit(15);
    new Expectations(hash) {

        {
            hash.resumeUnchecked(0x4200000000L, withInstanceOf(byte[].class), 0, 10);
        }
    };
    hash.resume(0x4200000000L, input);
    assertEquals(input.limit(), input.position());
}
Also used : Expectations(mockit.Expectations) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 7 with Expectations

use of mockit.Expectations in project pulsar by yahoo.

the class AbstractIncrementalLongHashTest method testResumeLongByteBuffer.

@Test
public void testResumeLongByteBuffer() {
    final ByteBuffer input = ByteBuffer.allocate(20);
    input.position(5);
    input.limit(15);
    new Expectations(hash) {

        {
            hash.resumeUnchecked(0x4200000000L, input.array(), input.arrayOffset() + 5, 10);
        }
    };
    hash.resume(0x4200000000L, input);
    assertEquals(input.limit(), input.position());
}
Also used : Expectations(mockit.Expectations) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 8 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class TestCseApplicationListener method testCseApplicationListenerThrowException.

@Test
public void testCseApplicationListenerThrowException(@Injectable ContextRefreshedEvent event, @Injectable AbstractApplicationContext context, @Injectable BootListener listener, @Injectable ProducerProviderManager producerProviderManager) throws Exception {
    Map<String, BootListener> listeners = new HashMap<>();
    listeners.put("test", listener);
    new Expectations() {

        {
            event.getApplicationContext();
            result = context;
            context.getBeansOfType(BootListener.class);
            result = listeners;
            producerProviderManager.init();
            result = new IOException();
        }
    };
    CseApplicationListener cal = new CseApplicationListener();
    ReflectUtils.setField(cal, "producerProviderManager", producerProviderManager);
    cal.onApplicationEvent(event);
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class SSLManagerTest method testSSLManagerServerAndClient.

@Test
public void testSSLManagerServerAndClient(@Mocked final NetworkInterface nif) throws Exception {
    final InetAddress ia = Inet4Address.getByName("10.57.65.225");
    final Enumeration<NetworkInterface> interfaces = new Enumeration<NetworkInterface>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public NetworkInterface nextElement() {
            return nif;
        }
    };
    final Enumeration<InetAddress> ias = new Enumeration<InetAddress>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public InetAddress nextElement() {
            return ia;
        }
    };
    new Expectations() {

        @Mocked
        NetworkInterface nif;

        {
            NetworkInterface.getNetworkInterfaces();
            result = interfaces;
        }
    };
    new Expectations() {

        {
            nif.getInetAddresses();
            result = ias;
            ia.getHostAddress();
            result = "10.57.65.225";
        }
    };
    SSLOption option = SSLOption.build(DIR + "/server.ssl.properties");
    SSLCustom custom = new SSLCustom() {

        @Override
        public String getFullPath(String filename) {
            return DIR + "/ssl/" + filename;
        }

        @Override
        public char[] decode(char[] encrypted) {
            return encrypted;
        }
    };
    final SSLServerSocket serverSocket = SSLManager.createSSLServerSocket(option, custom);
    serverSocket.bind(new InetSocketAddress("127.0.0.1", 8886));
    String[] protos = serverSocket.getEnabledCipherSuites();
    String[] protosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(protos, protosExpected);
    String[] ciphers = serverSocket.getEnabledCipherSuites();
    String[] ciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(ciphers, ciphersExpected);
    Assert.assertEquals(serverSocket.getNeedClientAuth(), true);
    SSLOption clientoption = SSLOption.build(DIR + "/client.ssl.properties");
    SSLSocket clientsocket = SSLManager.createSSLSocket(clientoption, custom);
    String[] clientprotos = clientsocket.getEnabledCipherSuites();
    String[] clientprotosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientprotos, clientprotosExpected);
    String[] clientciphers = clientsocket.getEnabledCipherSuites();
    String[] clientciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientciphers, clientciphersExpected);
    Assert.assertEquals(clientsocket.getNeedClientAuth(), false);
    boolean validAssert = true;
    try {
        clientsocket.connect(new InetSocketAddress("127.0.0.1", 8886));
        new Thread() {

            public void run() {
                try {
                    SSLSocket s = (SSLSocket) serverSocket.accept();
                    s.addHandshakeCompletedListener(new HandshakeCompletedListener() {

                        @Override
                        public void handshakeCompleted(HandshakeCompletedEvent arg0) {
                        }
                    });
                    s.getOutputStream().write(new byte[] { 0, 1 });
                } catch (IOException e) {
                    e.printStackTrace();
                    // this should not happen, do a false assert
                    Assert.assertEquals(false, true);
                }
            }
        }.start();
        clientsocket.startHandshake();
        clientsocket.close();
        serverSocket.close();
    // socked successfully opened and closed
    } catch (Exception e) {
        e.printStackTrace();
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Expectations(mockit.Expectations) Enumeration(java.util.Enumeration) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) UnknownHostException(java.net.UnknownHostException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 10 with Expectations

use of mockit.Expectations in project java-chassis by ServiceComb.

the class CertificateUtilTest method testFindOwner.

@Test
public void testFindOwner(@Mocked X500Principal aX500Principal1, @Mocked X500Principal aX500Principal2, @Mocked MyX509Certificate myX509Certificate) {
    new Expectations() {

        {
            aX500Principal1.getName();
            result = "Huawei";
        }

        {
            aX500Principal2.getName();
            result = "Huawei";
        }

        {
            myX509Certificate.getSubjectX500Principal();
            result = aX500Principal1;
            myX509Certificate.getIssuerX500Principal();
            result = aX500Principal2;
        }
    };
    MyX509Certificate myX509Certificate1 = new MyX509Certificate();
    MyX509Certificate myX509Certificate2 = new MyX509Certificate();
    MyX509Certificate[] xxmyX509Certificate = new MyX509Certificate[2];
    xxmyX509Certificate[0] = myX509Certificate1;
    xxmyX509Certificate[1] = myX509Certificate2;
    X509Certificate aX509Certificate = CertificateUtil.findOwner(xxmyX509Certificate);
    Assert.assertNull(aX509Certificate);
}
Also used : Expectations(mockit.Expectations) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Aggregations

Expectations (mockit.Expectations)113 Test (org.junit.Test)98 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)60 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)19 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 Test (org.testng.annotations.Test)14 AmqpSendHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler)12 ByteBuffer (java.nio.ByteBuffer)12 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)10 AmqpFeedbackReceivedHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpFeedbackReceivedHandler)6 AmqpReceive (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpReceive)5 URIEndpointObject (io.servicecomb.foundation.common.net.URIEndpointObject)5 FeedbackBatch (com.microsoft.azure.sdk.iot.service.FeedbackBatch)4 Message (com.microsoft.azure.sdk.iot.service.Message)4 AmqpSend (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend)4 HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)4 WebSocketImpl (com.microsoft.azure.sdk.iot.deps.ws.impl.WebSocketImpl)3 Tools (com.microsoft.azure.sdk.iot.service.Tools)3 AmqpFileUploadNotificationReceive (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpFileUploadNotificationReceive)3 Endpoint (io.servicecomb.core.Endpoint)3