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.endpoint.ImmutableEndpoint;
10  import org.mule.api.transport.DispatchException;
11  import org.mule.module.client.MuleClient;
12  import org.mule.transport.sftp.SftpClient;
13  
14  import java.io.IOException;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  /**
24   * Tests that files are not deleted if the final destination is not writable when
25   * using a temp directory.
26   */
27  public class SftpCantWriteToFinalDestAfterTempDirectoryTestCase extends AbstractSftpDataIntegrityTestCase
28  {
29  
30      private static String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
31      private static String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "dataintegrity/sftp-dataintegrity-common-with-tempdir-config.xml";
37      }
38  
39      @Override
40      protected void doSetUp() throws Exception
41      {
42          super.doSetUp();
43  
44          // Delete the in & outbound directories
45          initEndpointDirectory(INBOUND_ENDPOINT_NAME);
46          initEndpointDirectory(OUTBOUND_ENDPOINT_NAME);
47      }
48  
49      /**
50       * No write access on the outbound directory but write access to the TEMP
51       * directory. The source file should still exist and no file should exist in the
52       * TEMP directory.
53       */
54      @Test
55      public void testCantWriteToFinalDestAfterTempDirectory() throws Exception
56      {
57          MuleClient muleClient = new MuleClient(muleContext);
58  
59          // Must create the temp directory before we change the access rights
60          createRemoteDirectory(muleClient, OUTBOUND_ENDPOINT_NAME, "uploading");
61  
62          SftpClient sftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
63  
64          try
65          {
66              // change the chmod to "dr-x------" on the outbound-directory
67              remoteChmod(muleClient, sftpClient, OUTBOUND_ENDPOINT_NAME, 00500);
68  
69              // Send an file to the SFTP server, which the inbound-outboundEndpoint
70              // then can pick up
71              // Expect an error, permission denied
72              Exception exception = dispatchAndWaitForException(new DispatchParameters(INBOUND_ENDPOINT_NAME,
73                  OUTBOUND_ENDPOINT_NAME), "sftp", "service");
74              assertNotNull(exception);
75              assertTrue("did not receive DispatchException, got : " + exception.getClass().toString(),
76                  exception instanceof DispatchException);
77              assertTrue("did not receive IOException, got : " + exception.getCause().getClass().toString(),
78                  exception.getCause() instanceof IOException);
79  
80              assertEquals("Permission denied", exception.getCause().getMessage());
81  
82              verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME, true, false);
83  
84              ImmutableEndpoint endpoint = (ImmutableEndpoint) muleClient.getProperty(OUTBOUND_ENDPOINT_NAME);
85              assertFalse("The inbound file should not be left in the TEMP-dir", super.verifyFileExists(
86                  sftpClient, endpoint.getEndpointURI().getPath() + "/" + TEMP_DIR, FILE_NAME));
87          }
88          finally
89          {
90              sftpClient.disconnect();
91          }
92      }
93  
94  }