15aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair/*
25aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * Copyright 2008 the original author or authors.
35aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair *
45aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * Licensed under the Apache License, Version 2.0 (the "License");
55aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * you may not use this file except in compliance with the License.
65aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * You may obtain a copy of the License at
75aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair *
85aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair *      http://www.apache.org/licenses/LICENSE-2.0
95aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair *
105aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * Unless required by applicable law or agreed to in writing, software
115aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * distributed under the License is distributed on an "AS IS" BASIS,
125aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * See the License for the specific language governing permissions and
145aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * limitations under the License.
155aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair */
165aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismairpackage org.mockftpserver.fake.command
175aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
185aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismairimport org.mockftpserver.core.command.Command
195aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismairimport org.mockftpserver.core.command.CommandNames
205aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismairimport org.mockftpserver.core.command.ReplyCodes
21dc5be681ed2232085080c0af73613491c7a764abchrismairimport org.mockftpserver.fake.filesystem.FileEntry
22c51130cfee05b7294599cc93125db801972d0746chrismairimport org.mockftpserver.fake.filesystem.FileSystemEntry
235aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismairimport org.mockftpserver.fake.filesystem.FileSystemException
24dc5be681ed2232085080c0af73613491c7a764abchrismairimport org.mockftpserver.fake.filesystem.Permissions
25d150a70ca8f8ad6cc300cf56174c8846066a4ad4chrismair
265aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair/**
275aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * Abstract superclass for tests of Fake CommandHandlers that store a file (STOR, STOU, APPE)
285aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair *
292a0a3f946dba517a01cc26278f905156857c9c91chrismair * @version $Revision$ - $Date$
305aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair *
315aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair * @author Chris Mair
325aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair */
335303c6ae1dde5f399fe48803e677942fc4326344chrismairabstract class AbstractStoreFileCommandHandlerTestCase extends AbstractFakeCommandHandlerTestCase {
345aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
351ca7eb7f6c60913fb6edf267a79e3991d14ae3f7chrismair    protected static final DIR = "/"
361ca7eb7f6c60913fb6edf267a79e3991d14ae3f7chrismair    protected static final FILENAME = "file.txt"
371ca7eb7f6c60913fb6edf267a79e3991d14ae3f7chrismair    protected static final FILE = p(DIR, FILENAME)
381ca7eb7f6c60913fb6edf267a79e3991d14ae3f7chrismair    protected static final CONTENTS = "abc"
395aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
40dc5be681ed2232085080c0af73613491c7a764abchrismair    //-------------------------------------------------------------------------
41dc5be681ed2232085080c0af73613491c7a764abchrismair    // Tests Common to All Subclasses
42dc5be681ed2232085080c0af73613491c7a764abchrismair    //-------------------------------------------------------------------------
43c51130cfee05b7294599cc93125db801972d0746chrismair
44dc5be681ed2232085080c0af73613491c7a764abchrismair    void testHandleCommand_NoWriteAccessToExistingFile() {
45dc5be681ed2232085080c0af73613491c7a764abchrismair        fileSystem.add(new FileEntry(path: FILE))
46dc5be681ed2232085080c0af73613491c7a764abchrismair        fileSystem.getEntry(FILE).permissions = Permissions.NONE
471ca7eb7f6c60913fb6edf267a79e3991d14ae3f7chrismair        handleCommand([FILE])
48c4a22299b897279a8518308b9067da84af077281chrismair        assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ['filesystem.cannotWrite', FILE])
49dc5be681ed2232085080c0af73613491c7a764abchrismair    }
50c51130cfee05b7294599cc93125db801972d0746chrismair
51dc5be681ed2232085080c0af73613491c7a764abchrismair    void testHandleCommand_NoWriteAccessToDirectoryForNewFile() {
52dc5be681ed2232085080c0af73613491c7a764abchrismair        fileSystem.getEntry(DIR).permissions = new Permissions('r-xr-xr-x')
531ca7eb7f6c60913fb6edf267a79e3991d14ae3f7chrismair        handleCommand([FILE])
54c4a22299b897279a8518308b9067da84af077281chrismair        assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ['filesystem.cannotWrite', DIR])
55dc5be681ed2232085080c0af73613491c7a764abchrismair    }
56dc5be681ed2232085080c0af73613491c7a764abchrismair
57dc5be681ed2232085080c0af73613491c7a764abchrismair    void testHandleCommand_NoExecuteAccessToDirectory() {
58dc5be681ed2232085080c0af73613491c7a764abchrismair        fileSystem.add(new FileEntry(path: FILE))
59dc5be681ed2232085080c0af73613491c7a764abchrismair        fileSystem.getEntry(DIR).permissions = new Permissions('rw-rw-rw-')
601ca7eb7f6c60913fb6edf267a79e3991d14ae3f7chrismair        handleCommand([FILE])
61c4a22299b897279a8518308b9067da84af077281chrismair        assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ['filesystem.cannotExecute', DIR])
62c51130cfee05b7294599cc93125db801972d0746chrismair    }
63c51130cfee05b7294599cc93125db801972d0746chrismair
64c51130cfee05b7294599cc93125db801972d0746chrismair    void testHandleCommand_ThrowsFileSystemException() {
65b0a7b98e6ec500c6e292d8e5aea47d339e656f72chrismair        fileSystem.addMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY)
665aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
675aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair        handleCommand([FILE])
68777c1c842d19df00d2529ccf43e4f4c26cfd39fbchrismair        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
69c4a22299b897279a8518308b9067da84af077281chrismair        assertSessionReply(1, ReplyCodes.WRITE_FILE_ERROR, ERROR_MESSAGE_KEY)
705aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    }
715aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
725aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    //-------------------------------------------------------------------------
735aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    // Abstract Method Declarations
745aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    //-------------------------------------------------------------------------
755aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
765aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    /**
775aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair     * Verify the created output file and return its full path
785aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair     * @return the full path to the created output file; the path may be absolute or relative
795aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair     */
805aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    protected abstract String verifyOutputFile()
815aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
825aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    //-------------------------------------------------------------------------
835aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    // Helper Methods
845aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    //-------------------------------------------------------------------------
855aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
86dc5be681ed2232085080c0af73613491c7a764abchrismair    protected void testHandleCommand(List parameters, String messageKey, String contents) {
87dc5be681ed2232085080c0af73613491c7a764abchrismair        session.dataToRead = CONTENTS.bytes
88dc5be681ed2232085080c0af73613491c7a764abchrismair        handleCommand(parameters)
89dc5be681ed2232085080c0af73613491c7a764abchrismair        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
90dc5be681ed2232085080c0af73613491c7a764abchrismair        assertSessionReply(1, ReplyCodes.TRANSFER_DATA_FINAL_OK, messageKey)
91dc5be681ed2232085080c0af73613491c7a764abchrismair
92dc5be681ed2232085080c0af73613491c7a764abchrismair        def outputFile = verifyOutputFile()
93dc5be681ed2232085080c0af73613491c7a764abchrismair
9410941f2b30409f80cf92399415ffd8b3f0708d3echrismair        FileSystemEntry fileEntry = fileSystem.getEntry(outputFile)
9510941f2b30409f80cf92399415ffd8b3f0708d3echrismair        def actualContents = fileEntry.createInputStream().text
96dc5be681ed2232085080c0af73613491c7a764abchrismair        assert actualContents == contents
9710941f2b30409f80cf92399415ffd8b3f0708d3echrismair        assert fileEntry.permissions == userAccount.defaultPermissionsForNewFile
985aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    }
995aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
1005aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    Command createValidCommand() {
1015aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair        return new Command(CommandNames.APPE, [FILE])
1025aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    }
1035aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
1045aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    void setUp() {
1055aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair        super.setUp()
106d150a70ca8f8ad6cc300cf56174c8846066a4ad4chrismair        createDirectory(DIR)
1075aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair    }
1085aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair
1095aee6e3edf4d3d8decff73d2a5a26cebdd0572f5chrismair}