mil.dod.th.ose.remote.integration/src/mil/dod/th/ose/remote/integration/encryption/TestEncryptedSystem.java
@Test
    public void testNoInfiniteLoop() throws IOException, InterruptedException, InvalidKeyException, 
        IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, 
        NoSuchProviderException, NoSuchPaddingException
    {
        //Create the message to alter encryption level property.
        TerraHarvestMessage message = EncryptionUtil.createSetEncryptionLevelMessage(EncryptionMode.AES_ECDH_ECDSA);
        
        //Send message
        message.writeDelimitedTo(m_Socket.getOutputStream());
        
        //wait for response
        final MessageListener listener = new MessageListener(m_Socket);
        listener.waitForMessage(Namespace.ConfigAdmin, ConfigAdminMessageType.SetPropertyResponse, 800);
        
        EncryptionInfoErrorResponseData encryptErrorResponse = EncryptionInfoErrorResponseData.newBuilder().setError(
                EncryptionErrorCode.INVALID_ENCRYPTION_LEVEL).setErrorDescription("Test error!").setType(
                        EncryptType.NONE).build();
        TerraHarvestMessage encryptErrorMessage = EncryptionUtil.createEncryptionInfoMessage(
                EncryptionInfoMessageType.EncryptionInfoErrorResponse, encryptErrorResponse);
        
        //Send message
        encryptErrorMessage.writeDelimitedTo(m_Socket.getOutputStream());
        
        //Verify that the error message was accepted and no other error message was returned. If an error message is
        //returned then there may be an infinite loop condition present.
        listener.waitForMessages(1200, new BasicMessageMatcher(Namespace.EncryptionInfo, 
                EncryptionInfoMessageType.EncryptionInfoErrorResponse, MatchCount.times(0)),
                new BasicMessageMatcher(Namespace.Base, BaseMessageType.GenericErrorResponse,  MatchCount.times(0)));
        
        //Create message to return encryption type to none.
        message = EncryptionUtil.createSetEncryptionLevelMessage(EncryptionMode.NONE);
        TerraHarvestMessage encryptedMessage = EncryptionUtil.encryptMessageWithECDH(message);
        
        //Send message
        encryptedMessage.writeDelimitedTo(m_Socket.getOutputStream());
        
        //wait for response
        listener.waitForMessage(Namespace.ConfigAdmin, ConfigAdminMessageType.SetPropertyResponse, 1000);
        
        //Create message
        message = EncryptionUtil.createEncryptionInfoMessage(
                EncryptionInfoMessageType.GetEncryptionTypeRequest, null);
        
        //Send request
        message.writeDelimitedTo(m_Socket.getOutputStream());
        
        //wait for response
        final EncryptionInfoNamespace namespace = (EncryptionInfoNamespace)listener.waitForMessage(
                Namespace.EncryptionInfo, EncryptionInfoMessageType.GetEncryptionTypeResponse, 500);
        
        //Verify response
        final GetEncryptionTypeResponseData data = GetEncryptionTypeResponseData.parseFrom(namespace.getData());
        
        assertThat(data.getType(), is(EncryptType.NONE));
    }
/**
     * Verify that the encrypted system accepts an unencrypted encryption info error response and sends no error 
     * response. This ensures that an infinite loop condition won't happen by sending back and forth encrypted 
     * generic error responses.
     */
Verify that the encrypted system accepts an unencrypted encryption info error response and sends no error
response. This ensures that an infinite loop condition won't happen by sending back and forth encrypted
generic error responses.
Verify that the encrypted system accepts an unencrypted encryption info error response and sends no error response . This ensures that an infinite loop condition won ' t happen by sending back and forth encrypted generic error responses .
@Test
    public void testNoInfiniteLoop() throws IOException, InterruptedException, InvalidKeyException, 
        IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, 
        NoSuchProviderException, NoSuchPaddingException
    {
        TerraHarvestMessage message = EncryptionUtil.createSetEncryptionLevelMessage(EncryptionMode.AES_ECDH_ECDSA);
        message.writeDelimitedTo(m_Socket.getOutputStream());
        final MessageListener listener = new MessageListener(m_Socket);
        listener.waitForMessage(Namespace.ConfigAdmin, ConfigAdminMessageType.SetPropertyResponse, 800);
        EncryptionInfoErrorResponseData encryptErrorResponse = EncryptionInfoErrorResponseData.newBuilder().setError(
                EncryptionErrorCode.INVALID_ENCRYPTION_LEVEL).setErrorDescription("Test error!").setType(
                        EncryptType.NONE).build();
        TerraHarvestMessage encryptErrorMessage = EncryptionUtil.createEncryptionInfoMessage(
                EncryptionInfoMessageType.EncryptionInfoErrorResponse, encryptErrorResponse);
        encryptErrorMessage.writeDelimitedTo(m_Socket.getOutputStream());
        listener.waitForMessages(1200, new BasicMessageMatcher(Namespace.EncryptionInfo, 
                EncryptionInfoMessageType.EncryptionInfoErrorResponse, MatchCount.times(0)),
                new BasicMessageMatcher(Namespace.Base, BaseMessageType.GenericErrorResponse,  MatchCount.times(0)));
        message = EncryptionUtil.createSetEncryptionLevelMessage(EncryptionMode.NONE);
        TerraHarvestMessage encryptedMessage = EncryptionUtil.encryptMessageWithECDH(message);
        encryptedMessage.writeDelimitedTo(m_Socket.getOutputStream());
        listener.waitForMessage(Namespace.ConfigAdmin, ConfigAdminMessageType.SetPropertyResponse, 1000);
        message = EncryptionUtil.createEncryptionInfoMessage(
                EncryptionInfoMessageType.GetEncryptionTypeRequest, null);
        message.writeDelimitedTo(m_Socket.getOutputStream());
        final EncryptionInfoNamespace namespace = (EncryptionInfoNamespace)listener.waitForMessage(
                Namespace.EncryptionInfo, EncryptionInfoMessageType.GetEncryptionTypeResponse, 500);
        final GetEncryptionTypeResponseData data = GetEncryptionTypeResponseData.parseFrom(namespace.getData());
        assertThat(data.getType(), is(EncryptType.NONE));
    }
@ Test public void testNoInfiniteLoop ( ) throws IOException , InterruptedException , InvalidKeyException , IllegalBlockSizeException , BadPaddingException , InvalidAlgorithmParameterException , NoSuchAlgorithmException , NoSuchProviderException , NoSuchPaddingException { TerraHarvestMessage message = EncryptionUtil . createSetEncryptionLevelMessage ( EncryptionMode . AES_ECDH_ECDSA ) ; message . writeDelimitedTo ( m_Socket . getOutputStream ( ) ) ; final MessageListener listener = new MessageListener ( m_Socket ) ; listener . waitForMessage ( Namespace . ConfigAdmin , ConfigAdminMessageType . SetPropertyResponse , 800 ) ; EncryptionInfoErrorResponseData encryptErrorResponse = EncryptionInfoErrorResponseData . newBuilder ( ) . setError ( EncryptionErrorCode . INVALID_ENCRYPTION_LEVEL ) . setErrorDescription ( "Test error!" ) . setType ( EncryptType . NONE ) . build ( ) ; TerraHarvestMessage encryptErrorMessage = EncryptionUtil . createEncryptionInfoMessage ( EncryptionInfoMessageType . EncryptionInfoErrorResponse , encryptErrorResponse ) ; encryptErrorMessage . writeDelimitedTo ( m_Socket . getOutputStream ( ) ) ; listener . waitForMessages ( 1200 , new BasicMessageMatcher ( Namespace . EncryptionInfo , EncryptionInfoMessageType . EncryptionInfoErrorResponse , MatchCount . times ( 0 ) ) , new BasicMessageMatcher ( Namespace . Base , BaseMessageType . GenericErrorResponse , MatchCount . times ( 0 ) ) ) ; message = EncryptionUtil . createSetEncryptionLevelMessage ( EncryptionMode . NONE ) ; TerraHarvestMessage encryptedMessage = EncryptionUtil . encryptMessageWithECDH ( message ) ; encryptedMessage . writeDelimitedTo ( m_Socket . getOutputStream ( ) ) ; listener . waitForMessage ( Namespace . ConfigAdmin , ConfigAdminMessageType . SetPropertyResponse , 1000 ) ; message = EncryptionUtil . createEncryptionInfoMessage ( EncryptionInfoMessageType . GetEncryptionTypeRequest , null ) ; message . writeDelimitedTo ( m_Socket . getOutputStream ( ) ) ; final EncryptionInfoNamespace namespace = ( EncryptionInfoNamespace ) listener . waitForMessage ( Namespace . EncryptionInfo , EncryptionInfoMessageType . GetEncryptionTypeResponse , 500 ) ; final GetEncryptionTypeResponseData data = GetEncryptionTypeResponseData . parseFrom ( namespace . getData ( ) ) ; assertThat ( data . getType ( ) , is ( EncryptType . NONE ) ) ; }
Verify that the encrypted system accepts an unencrypted encryption info error response and sends no error
 response.
Verify that the encrypted system accepts an unencrypted encryption info error response and sends no error response .
//Create the message to alter encryption level property. //Send message //wait for response //Send message //Verify that the error message was accepted and no other error message was returned. If an error message is //returned then there may be an infinite loop condition present. //Create message to return encryption type to none. //Send message //wait for response //Create message //Send request //wait for response //Verify response