1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compatibility.tradefed.util;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.util.CommandResult;
25 import com.android.tradefed.util.CommandStatus;
26 import com.android.tradefed.util.TargetFileUtils;
27 import com.android.tradefed.util.TargetFileUtils.FilePermission;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import org.easymock.EasyMock;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 @RunWith(JUnit4.class)
36 public class TargetFileUtilsTest {
37     private ITestDevice mMockDevice;
38 
39     /** Test {@link TargetFileUtils#hasPermission()}. */
40     @Test
testHasPermission()41     public void testHasPermission() throws DeviceNotAvailableException {
42         for (int x = 0; x <= 7; x++) {
43             for (int y = 0; y <= 7; y++) {
44                 for (int z = 0; z <= 7; z++) {
45                     String permission = "" + x + y + z;
46                     // Determines if the permission bits grant read permission to any group.
47                     if (hasPermission(x, FilePermission.READ)
48                             || hasPermission(y, FilePermission.READ)
49                             || hasPermission(z, FilePermission.READ)) {
50                         assertTrue(TargetFileUtils.hasPermission(FilePermission.READ, permission));
51                     }
52                     // Determines if the permission bits grant write permission to any group.
53                     if (hasPermission(x, FilePermission.WRITE)
54                             || hasPermission(y, FilePermission.WRITE)
55                             || hasPermission(z, FilePermission.WRITE)) {
56                         assertTrue(TargetFileUtils.hasPermission(FilePermission.WRITE, permission));
57                     }
58                     // Determines if the permission bits grant EXECUTE permission to any group.
59                     if (hasPermission(x, FilePermission.EXECUTE)
60                             || hasPermission(y, FilePermission.EXECUTE)
61                             || hasPermission(z, FilePermission.EXECUTE)) {
62                         assertTrue(
63                                 TargetFileUtils.hasPermission(FilePermission.EXECUTE, permission));
64                     }
65                 }
66             }
67         }
68     }
69 
hasPermission(int bit, FilePermission permission)70     private boolean hasPermission(int bit, FilePermission permission) {
71         return (bit & permission.getPermissionNum()) != 0;
72     }
73 
74     /** Test {@link TargetFileUtils#findFile()} with NumberFormatException be asserted. */
75     @Test
testFindFile()76     public void testFindFile() throws DeviceNotAvailableException {
77         mMockDevice = EasyMock.createMock(ITestDevice.class);
78         CommandResult commandResult = new CommandResult(CommandStatus.SUCCESS);
79         commandResult.setStdout("path1\npath2\npath3\n");
80         commandResult.setExitCode(0);
81         EasyMock.expect(mMockDevice.executeShellV2Command("find findPath -name \"namePattern\" "
82                                 + "option1 option2"))
83                 .andReturn(commandResult);
84         EasyMock.replay(mMockDevice);
85         ArrayList<String> findPaths = new ArrayList<>();
86         findPaths.add("path1");
87         findPaths.add("path2");
88         findPaths.add("path3");
89         String[] options = {"option1", "option2"};
90         assertEquals(findPaths,
91                 TargetFileUtils.findFile(
92                         "findPath", "namePattern", Arrays.asList(options), mMockDevice));
93     }
94 
95     /** Test {@link TargetFileUtils#findFile()} with shell command failed. */
96     @Test
testFindFile_w_cmd_result_fail()97     public void testFindFile_w_cmd_result_fail() throws DeviceNotAvailableException {
98         mMockDevice = EasyMock.createMock(ITestDevice.class);
99         CommandResult commandResult = new CommandResult(CommandStatus.FAILED);
100         commandResult.setStdout("path1\npath2\npath3\n");
101         commandResult.setExitCode(0);
102         EasyMock.expect(mMockDevice.executeShellV2Command("find findPath -name \"namePattern\""))
103                 .andReturn(commandResult);
104         EasyMock.replay(mMockDevice);
105         ArrayList<String> findPaths = new ArrayList<>();
106         assertEquals(
107                 findPaths, TargetFileUtils.findFile("findPath", "namePattern", null, mMockDevice));
108     }
109 
110     /** Test {@link TargetFileUtils#findFile()} which have stdout with empty line. */
111     @Test
testFindFile_w_empty_line_stdout()112     public void testFindFile_w_empty_line_stdout() throws DeviceNotAvailableException {
113         mMockDevice = EasyMock.createMock(ITestDevice.class);
114         CommandResult commandResult = new CommandResult(CommandStatus.SUCCESS);
115         commandResult.setStdout("");
116         commandResult.setExitCode(0);
117         EasyMock.expect(mMockDevice.executeShellV2Command("find findPath -name \"namePattern\""))
118                 .andReturn(commandResult);
119         EasyMock.replay(mMockDevice);
120         ArrayList<String> findPaths = new ArrayList<>();
121         assertEquals(
122                 findPaths, TargetFileUtils.findFile("findPath", "namePattern", null, mMockDevice));
123     }
124 }
125