View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.sftp.dataintegrity;
8   
9   import org.mule.api.transport.DispatchException;
10  import org.mule.module.client.MuleClient;
11  import org.mule.transport.sftp.SftpClient;
12  
13  import java.io.IOException;
14  
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertTrue;
20  
21  /**
22   * Verify that the original file is not lost if the outbound directory doesn't exist
23   */
24  public class SftpNoWriteAccessToOutboundDirectoryTestCase extends AbstractSftpDataIntegrityTestCase
25  {
26      private static final String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
27      private static final String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "dataintegrity/sftp-dataintegrity-common-config.xml";
33      }
34  
35      @Override
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39  
40          // Delete the in & outbound directories
41          initEndpointDirectory(INBOUND_ENDPOINT_NAME);
42          initEndpointDirectory(OUTBOUND_ENDPOINT_NAME);
43      }
44  
45      /** No write access on the outbound directory. The source file should still exist */
46      @Test
47      public void testNoWriteAccessToOutboundDirectory() throws Exception
48      {
49          MuleClient muleClient = new MuleClient(muleContext);
50  
51          SftpClient sftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
52  
53          try
54          {
55              // change the chmod to "dr-x------" on the outbound-directory
56              remoteChmod(muleClient, sftpClient, OUTBOUND_ENDPOINT_NAME, 00500);
57  
58              // Send an file to the SFTP server, which the inbound-outboundEndpoint
59              // then can pick up
60              Exception exception = dispatchAndWaitForException(new DispatchParameters(INBOUND_ENDPOINT_NAME,
61                  OUTBOUND_ENDPOINT_NAME), "sftp", "service");
62              assertNotNull(exception);
63              assertTrue("expected DispatchException, but got : " + exception.getClass().toString(),
64                  exception instanceof DispatchException);
65              assertTrue("expected IOException, but got : " + exception.getCause().getClass().toString(),
66                  exception.getCause() instanceof IOException);
67              assertEquals("Permission denied", exception.getCause().getMessage());
68  
69              verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME, true, false);
70          }
71          finally
72          {
73              sftpClient.disconnect();
74          }
75      }
76  
77  }