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   * Tests that files are not deleted if the temp directory can't be created
23   */
24  public class SftpCantCreateTempDirectoryTestCase extends AbstractSftpDataIntegrityTestCase
25  {
26  
27      private static String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
28      private static String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
29  
30      @Override
31      protected String getConfigResources()
32      {
33          return "dataintegrity/sftp-dataintegrity-common-with-tempdir-config.xml";
34      }
35  
36      @Override
37      protected void doSetUp() throws Exception
38      {
39          super.doSetUp();
40  
41          // Delete the in & outbound directories
42          initEndpointDirectory(INBOUND_ENDPOINT_NAME);
43          initEndpointDirectory(OUTBOUND_ENDPOINT_NAME);
44      }
45  
46      /**
47       * No write access on the outbound directory and thus the TEMP directory cant be
48       * created. The source file should still exist
49       * 
50       * @throws Exception If an error occurred
51       */
52      @Test
53      public void testCantCreateTempDirectory() throws Exception
54      {
55          MuleClient muleClient = new MuleClient(muleContext);
56  
57          SftpClient sftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
58  
59          try
60          {
61              // change the chmod to "dr-x------" on the outbound-directory
62              // --> the temp directory should not be able to be created
63              remoteChmod(muleClient, sftpClient, OUTBOUND_ENDPOINT_NAME, 00500);
64  
65              // Send an file to the SFTP server, which the inbound-outboundEndpoint
66              // then can pick up
67              // Expect an error, permission denied
68              Exception exception = dispatchAndWaitForException(new DispatchParameters(INBOUND_ENDPOINT_NAME,
69                  OUTBOUND_ENDPOINT_NAME), "sftp", "service");
70              assertNotNull(exception);
71              assertTrue("expected DispatchException, but got : " + exception.getClass().toString(),
72                  exception instanceof DispatchException);
73              assertTrue("expected IOException, but got : " + exception.getCause().getClass().toString(),
74                  exception.getCause() instanceof IOException);
75              assertEquals("Could not create the directory 'uploading', caused by: Permission denied",
76                  exception.getCause().getMessage());
77  
78              verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME, true, false);
79          }
80          finally
81          {
82              sftpClient.disconnect();
83          }
84      }
85  
86      /**
87       * The same test as above, but with the difference that this time it should be
88       * okay to create the directory, and the file should be gone from the inbound
89       * directory.
90       */
91      // Works, but this is more or less the same test as SftpTempDirFunctionalTestCase
92      // so don't use this
93      // @Test
94      //public void testCanCreateTempDirectory() throws Exception
95      // {
96      // MuleClient muleClient = new MuleClient();
97      //
98      // dispatchAndWaitForDelivery(new DispatchParameters(INBOUND_ENDPOINT_NAME,
99      // OUTBOUND_ENDPOINT_NAME));
100     //	
101     // verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME,
102     // false, true);
103     // }
104 
105 }