1 /**
2  * Copyright (c) 2016, 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.server.utils;
18 
19 import static com.android.server.utils.PriorityDump.dump;
20 
21 import static org.junit.Assert.assertArrayEquals;
22 import static org.junit.Assert.assertSame;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Matchers.same;
25 import static org.mockito.Mockito.verify;
26 
27 import android.platform.test.annotations.Presubmit;
28 
29 import androidx.test.filters.SmallTest;
30 
31 import com.android.server.utils.PriorityDump.PriorityDumper;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 import java.io.FileDescriptor;
41 import java.io.PrintWriter;
42 
43 @SmallTest
44 @Presubmit
45 @RunWith(JUnit4.class)
46 public class PriorityDumpTest {
47 
48     private static final String[] EMPTY_ARGS = {};
49 
50     @Mock
51     private PriorityDumper mDumper;
52     @Mock
53     private PrintWriter mPw;
54 
55     private final FileDescriptor mFd = FileDescriptor.err;
56 
57     @Before
setup()58     public void setup() {
59         MockitoAnnotations.initMocks(this);
60     }
61 
62     @Test
testNullArgs()63     public void testNullArgs() {
64         dump(mDumper, mFd, mPw, null);
65         verify(mDumper).dump(same(mFd), same(mPw), eq(null), /* asProto= */ eq(false));
66     }
67 
68     @Test
testNoArgs()69     public void testNoArgs() {
70         dump(mDumper, mFd, mPw, EMPTY_ARGS);
71         verify(mDumper).dump(same(mFd), same(mPw), eq(EMPTY_ARGS), /* asProto= */ eq(false));
72     }
73 
74     @Test
testNonPriorityArgs()75     public void testNonPriorityArgs() {
76         final String[] args = {
77                 "--dumb_priority"
78         };
79         dump(mDumper, mFd, mPw, args);
80         verify(mDumper).dump(same(mFd), same(mPw), eq(args), /* asProto= */ eq(false));
81     }
82 
83     @Test
testMissingPriority()84     public void testMissingPriority() {
85         final String[] args = {
86                 "--dump-priority"
87         };
88         dump(mDumper, mFd, mPw, args);
89         verify(mDumper).dump(same(mFd), same(mPw), eq(EMPTY_ARGS), /* asProto= */ eq(false));
90     }
91 
92     @Test
testInvalidPriorityNoExtraArgs()93     public void testInvalidPriorityNoExtraArgs() {
94         final String[] args = {
95                 "--dump-priority", "SUPER_HIGH"
96         };
97         dump(mDumper, mFd, mPw, args);
98         verify(mDumper).dump(same(mFd), same(mPw), eq(EMPTY_ARGS), /* asProto= */ eq(false));
99     }
100 
101     @Test
testInvalidPriorityExtraArgs()102     public void testInvalidPriorityExtraArgs() {
103         final String[] args = {
104                 "--dump-priority", "SUPER_HIGH", "--high", "--five"
105         };
106         dump(mDumper, mFd, mPw, args);
107         verify(mDumper).dump(same(mFd), same(mPw), eq(new String[] {
108             "--high", "--five"
109         }), /* asProto= */ eq(false));
110     }
111 
112     @Test
testNoPriorityCallsAllMethods()113     public void testNoPriorityCallsAllMethods() {
114         final String[] args = {
115                 "1", "2", "3"
116         };
117 
118         // Cannot use mDumper here because it would mock the dump() call.
119         final FakeDumper fakeDumper = new FakeDumper();
120 
121         dump(fakeDumper, mFd, mPw, args);
122 
123         assertSame(mFd, fakeDumper.criticalFd);
124         assertSame(mPw, fakeDumper.criticalPw);
125         assertArrayEquals(args, fakeDumper.criticalArgs);
126         assertSame(mFd, fakeDumper.highFd);
127         assertSame(mPw, fakeDumper.highPw);
128         assertArrayEquals(args, fakeDumper.highArgs);
129         assertSame(mFd, fakeDumper.normalFd);
130         assertSame(mPw, fakeDumper.normalPw);
131         assertArrayEquals(args, fakeDumper.normalArgs);
132     }
133 
134     @Test
testCriticalNoExtraArgs()135     public void testCriticalNoExtraArgs() {
136         dump(mDumper, mFd, mPw, new String[] {
137                 "--dump-priority", "CRITICAL"
138         });
139         verify(mDumper).dumpCritical(same(mFd), same(mPw), eq(EMPTY_ARGS),
140                 /* asProto= */ eq(false));
141     }
142 
143     @Test
testCriticalExtraArgs()144     public void testCriticalExtraArgs() {
145         dump(mDumper, mFd, mPw, new String[] {
146                 "--dump-priority", "CRITICAL", "--high", "--five"
147         });
148         verify(mDumper).dumpCritical(same(mFd), same(mPw), eq(new String[] {
149                 "--high", "--five"
150         }), /* asProto= */ eq(false));
151     }
152 
153     @Test
testCriticalExtraArgsInMiddle()154     public void testCriticalExtraArgsInMiddle() {
155         dump(mDumper, mFd, mPw, new String[] {
156                 "--high", "--dump-priority", "CRITICAL", "--five"
157         });
158         verify(mDumper).dumpCritical(same(mFd), same(mPw), eq(new String[] {
159                 "--high", "--five"
160         }), /* asProto= */ eq(false));
161     }
162 
163     @Test
testCriticalExtraArgsAtEnd()164     public void testCriticalExtraArgsAtEnd() {
165         dump(mDumper, mFd, mPw, new String[] {
166                 "--high", "--five", "--dump-priority", "CRITICAL"
167         });
168         verify(mDumper).dumpCritical(same(mFd), same(mPw), eq(new String[] {
169                 "--high", "--five"
170         }), /* asProto= */ eq(false));
171     }
172 
173     @Test
testHighNoExtraArgs()174     public void testHighNoExtraArgs() {
175         dump(mDumper, mFd, mPw, new String[] {
176                 "--dump-priority", "HIGH"
177         });
178         verify(mDumper).dumpHigh(same(mFd), same(mPw), eq(EMPTY_ARGS), /* asProto= */ eq(false));
179     }
180 
181     @Test
testHighExtraArgs()182     public void testHighExtraArgs() {
183         dump(mDumper, mFd, mPw, new String[] {
184                 "--dump-priority", "HIGH", "--high", "--five"
185         });
186         verify(mDumper).dumpHigh(same(mFd), same(mPw), eq(new String[] {
187                 "--high", "--five"
188         }), /* asProto= */ eq(false));
189     }
190 
191     @Test
testNormalNoExtraArgs()192     public void testNormalNoExtraArgs() {
193         dump(mDumper, mFd, mPw, new String[] {
194                 "--dump-priority", "NORMAL"
195         });
196         verify(mDumper).dumpNormal(same(mFd), same(mPw), eq(EMPTY_ARGS), /* asProto= */ eq(false));
197     }
198 
199     @Test
testNormalExtraArgs()200     public void testNormalExtraArgs() {
201         dump(mDumper, mFd, mPw, new String[]{
202                 "--dump-priority", "NORMAL", "--high", "--five"
203         });
204         verify(mDumper).dumpNormal(same(mFd), same(mPw), eq(new String[]{
205                 "--high", "--five"
206         }), /* asProto= */ eq(false));
207     }
208 
209     @Test
testProtoArgs()210     public void testProtoArgs() {
211         dump(mDumper, mFd, mPw, new String[]{"--proto"});
212         verify(mDumper).dump(same(mFd), same(mPw), eq(EMPTY_ARGS), /* asProto= */ eq(true));
213     }
214 
215     @Test
testProtoArgsWithPriorityArgs()216     public void testProtoArgsWithPriorityArgs() {
217         dump(mDumper, mFd, mPw, new String[]{"--proto", "--dump-priority", "NORMAL", "--five"});
218         verify(mDumper).dumpNormal(same(mFd), same(mPw),
219                 eq(new String[]{"--five"}), /* asProto= */ eq(true));
220     }
221 
222     @Test
testProtoArgsWithPriorityArgsReverseOrder()223     public void testProtoArgsWithPriorityArgsReverseOrder() {
224         dump(mDumper, mFd, mPw, new String[]{"--dump-priority", "NORMAL", "--proto", "--five"});
225         verify(mDumper).dumpNormal(same(mFd), same(mPw),
226                 eq(new String[]{"--five"}), /* asProto= */ eq(true));
227     }
228 
229     @Test
testProtoArgsInMiddle()230     public void testProtoArgsInMiddle() {
231         dump(mDumper, mFd, mPw, new String[]{"--unknown", "--proto", "--five"});
232         verify(mDumper).dump(same(mFd), same(mPw),
233                 eq(new String[]{"--unknown", "--five"}), /* asProto= */ eq(true));
234     }
235 
236     @Test
testProtoArgsAtEnd()237     public void testProtoArgsAtEnd() {
238         dump(mDumper, mFd, mPw, new String[]{"args", "-left", "--behind", "--proto"});
239         verify(mDumper).dump(same(mFd), same(mPw),
240                 eq(new String[]{"args", "-left", "--behind"}), /* asProto= */ eq(true));
241     }
242 
243     @Test
testProtoArgsWithInvalidPriorityType()244     public void testProtoArgsWithInvalidPriorityType() {
245         dump(mDumper, mFd, mPw, new String[]{"--dump-priority", "HIGH?", "--proto"});
246         verify(mDumper).dump(same(mFd), same(mPw),
247                 eq(EMPTY_ARGS), /* asProto= */ eq(true));
248     }
249 
250     private final class FakeDumper implements PriorityDumper {
251 
252         String[] criticalArgs, highArgs, normalArgs;
253         FileDescriptor criticalFd, highFd, normalFd;
254         PrintWriter criticalPw, highPw, normalPw;
255 
256         @Override
dumpCritical(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto)257         public void dumpCritical(FileDescriptor fd, PrintWriter pw, String[] args,
258                 boolean asProto) {
259             criticalFd = fd;
260             criticalPw = pw;
261             criticalArgs = args;
262         }
263 
264         @Override
dumpHigh(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto)265         public void dumpHigh(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) {
266             highFd = fd;
267             highPw = pw;
268             highArgs = args;
269         }
270 
271         @Override
dumpNormal(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto)272         public void dumpNormal(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) {
273             normalFd = fd;
274             normalPw = pw;
275             normalArgs = args;
276         }
277     }
278 }
279