aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/com/cyanogenmod/explorer/commands/shell/ListCommandTest.java
blob: a94555db47b15a889f6d535dbf63b873440fa1a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2012 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cyanogenmod.explorer.commands.shell;

import java.util.List;

import com.cyanogenmod.explorer.commands.ListExecutable.LIST_MODE;
import com.cyanogenmod.explorer.model.BlockDevice;
import com.cyanogenmod.explorer.model.CharacterDevice;
import com.cyanogenmod.explorer.model.Directory;
import com.cyanogenmod.explorer.model.DomainSocket;
import com.cyanogenmod.explorer.model.FileSystemObject;
import com.cyanogenmod.explorer.model.NamedPipe;
import com.cyanogenmod.explorer.model.RegularFile;
import com.cyanogenmod.explorer.model.Symlink;
import com.cyanogenmod.explorer.util.CommandHelper;

/**
 * A class for testing list command.
 *
 * @see ListCommand
 */
public class ListCommandTest extends AbstractConsoleTest {

    private static final String LS_PATH = "/"; //$NON-NLS-1$
    private static final String LS_INFOFILE = "/boot.txt"; //$NON-NLS-1$
    private static final String LS_INFOFILE_NAME = "boot.txt"; //$NON-NLS-1$

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isRootConsoleNeeded() {
        return false;
    }

    /**
     * Method that performs a test over list command setting the
     * directory to list.
     *
     * @throws Exception If test failed
     */
    public void testList() throws Exception {
        List<FileSystemObject> files = CommandHelper.listFiles(getContext(), LS_PATH, getConsole());
        assertNotNull("files==null", files); //$NON-NLS-1$
        assertTrue("no objects returned", files.size() > 0); //$NON-NLS-1$
    }

    /**
     * Method that performs a test over list command setting the
     * directory to list.
     *
     * @throws Exception If test failed
     */
    public void testListInfo() throws Exception {
        FileSystemObject file = CommandHelper.getFileInfo(getContext(), LS_INFOFILE, getConsole());
        assertNotNull("file==null", file); //$NON-NLS-1$
        assertTrue(
                String.format("file!=%s", LS_INFOFILE_NAME), //$NON-NLS-1$
                file.getName().compareTo(LS_INFOFILE_NAME) == 0);
    }

    /**
     * Method that performs a test over a known parse result.
     *
     * @throws Exception If test failed
     * {@link ListCommand#parse(String, String)}
     */
    public void testParse() throws Exception {
        ListCommand cmd = new ListCommand(LS_PATH, LIST_MODE.DIRECTORY, getConsole());
        String in =
            "drwxr-xr-x root     root              2012-05-04 01:51 acct\n" + //$NON-NLS-1$
            "-rw-r--r-- root     root2         229 2012-05-04 01:51 boot.txt\n" + //$NON-NLS-1$
            "lrwxrwxrwx root     root              2012-05-04 01:51 d -> " //$NON-NLS-1$
            + "/sys/kernel/debug\n" + //$NON-NLS-1$
            "prw-r--r-- root     root            0 2012-05-04 01:51 pipe\n" + //$NON-NLS-1$
            "srw-r--r-- root     root            0 2012-05-04 01:51 socket\n" + //$NON-NLS-1$
            "brw------- root     root       7,   0 2012-05-04 01:51 loop0\n" + //$NON-NLS-1$
            "crw------- root     root       4,  64 2012-05-04 01:51 ttyS0\n" + //$NON-NLS-1$
            "-rwsr-sr-t root     root          229 2012-05-04 01:51 permission1\n" + //$NON-NLS-1$
            "-rwSr-Sr-T root     root          229 2012-05-04 01:51 permission2"; //$NON-NLS-1$
        String err = ""; //$NON-NLS-1$
        cmd.parse(in, err);
        List<FileSystemObject> files = cmd.getResult();
        assertNotNull("files==null", files); //$NON-NLS-1$
        assertTrue("length!=9", files.size() == 9); //$NON-NLS-1$
        assertTrue(
                "files(0) is not a directory", //$NON-NLS-1$
                files.get(0) instanceof Directory);
        assertTrue(
                "files(1) is not a file", //$NON-NLS-1$
                files.get(1) instanceof RegularFile);
        assertTrue(
                "files(2) is not a symlink", //$NON-NLS-1$
                files.get(2) instanceof Symlink);
        assertTrue(
                "files(3) is not a named pipe", //$NON-NLS-1$
                files.get(3) instanceof NamedPipe);
        assertTrue(
                "files(4) is not a domain socket", //$NON-NLS-1$
                files.get(4) instanceof DomainSocket);
        assertTrue(
                "files(5) is not a block device", //$NON-NLS-1$
                files.get(5) instanceof BlockDevice);
        assertTrue(
                "files(6) is not a character device", //$NON-NLS-1$
                files.get(6) instanceof CharacterDevice);
        assertTrue(
                "files(0) != name", //$NON-NLS-1$
                files.get(0).getName().compareTo("acct") == 0); //$NON-NLS-1$
        assertTrue(
                "files(2) != name", //$NON-NLS-1$
                files.get(2).getName().compareTo("d") == 0); //$NON-NLS-1$
        assertTrue(
                "files(2) != link", //$NON-NLS-1$
                ((Symlink)files.get(2)).getLink().compareTo(
                        "/sys/kernel/debug") == 0); //$NON-NLS-1$
        assertTrue(
                "files(1) != user", //$NON-NLS-1$
                files.get(1).getUser().getName().compareTo("root") == 0); //$NON-NLS-1$
        assertTrue(
                "files(1) != group", //$NON-NLS-1$
                files.get(1).getGroup().getName().compareTo("root2") == 0); //$NON-NLS-1$
        assertTrue(
                "files(1) != size", //$NON-NLS-1$
                files.get(1).getSize() == 229);
        assertTrue(
                "files(1) != permissions",  //$NON-NLS-1$
                files.get(1).getPermissions()
                    .toRawString().compareTo("rw-r--r--") == 0); //$NON-NLS-1$
        assertTrue(
                "files(7) != setuid", //$NON-NLS-1$
                files.get(7).getPermissions().getUser().isSetUID());
        assertTrue(
                "files(7) != setgid", //$NON-NLS-1$
                files.get(7).getPermissions().getGroup().isSetGID());
        assertTrue(
                "files(7) != stickybit", //$NON-NLS-1$
                files.get(7).getPermissions().getOthers().isStickybit());
        assertTrue(
                "files(7) != setuid+execute", //$NON-NLS-1$
                files.get(7).getPermissions().getUser().isExecute());
        assertTrue(
                "files(7) != setgid+execute", //$NON-NLS-1$
                files.get(7).getPermissions().getGroup().isExecute());
        assertTrue(
                "files(7) != stickybit+execute", //$NON-NLS-1$
                files.get(7).getPermissions().getOthers().isExecute());
        assertTrue(
                "files(8) != setuid", //$NON-NLS-1$
                files.get(8).getPermissions().getUser().isSetUID());
        assertTrue(
                "files(8) != setgid", //$NON-NLS-1$
                files.get(8).getPermissions().getGroup().isSetGID());
        assertTrue(
                "files(8) != stickybit", //$NON-NLS-1$
                files.get(8).getPermissions().getOthers().isStickybit());
        assertTrue(
                "files(8) != setuid+execute", //$NON-NLS-1$
                !files.get(8).getPermissions().getUser().isExecute());
        assertTrue(
                "files(8) != setgid+execute", //$NON-NLS-1$
                !files.get(8).getPermissions().getGroup().isExecute());
        assertTrue(
                "files(8) != stickybit+execute", //$NON-NLS-1$
                !files.get(8).getPermissions().getOthers().isExecute());
    }

}