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.tradefed.util; 18 19 import com.android.tradefed.util.ProcessInfo; 20 21 import java.util.List; 22 23 import junit.framework.TestCase; 24 25 /** Simple unit test for {@link PsParser}. */ 26 public class PsParserTest extends TestCase { 27 28 /** 29 * Test behavior when there are no lines to parse. 30 */ 31 public void testEmptyPsOutput() { 32 List<ProcessInfo> psInfo = PsParser.getProcesses(""); 33 assertEquals("ProcessInfo list is not empty when ps output is empty", 0, psInfo.size()); 34 } 35 36 /** 37 * Test valid "ps -A || ps" output for builds after N. 38 */ 39 public void testNewerValidPsOutput() { 40 String psOutput = "USER PID PPID VSZ RSS WCHAN PC S NAME\n" 41 + "root 1 0 11136 1828 epoll_wait 4d8064 S init\n" 42 + "root 2 0 0 0 kthreadd 0 S [kthreadd]\n"; 43 List<ProcessInfo> psInfo = PsParser.getProcesses(psOutput); 44 assertEquals("PsParser did not return expected number of processes info ", 2, 45 psInfo.size()); 46 assertEquals("First process user name did not match", "root", psInfo.get(0).getUser()); 47 assertEquals("First process id did not match", 1, psInfo.get(0).getPid()); 48 assertEquals("First process name did not match", "init", psInfo.get(0).getName()); 49 assertEquals("Second process user name did not match", "root", psInfo.get(1).getUser()); 50 assertEquals("Second process id did not match", 2, psInfo.get(1).getPid()); 51 assertEquals("Second process name did not match", "[kthreadd]", psInfo.get(1).getName()); 52 } 53 54 /** 55 * Test valid "ps -A || ps" output for N and older builds. 56 */ 57 public void testOlderValidPsOutput() { 58 String psOutput = "bad pid '-A'\n" 59 + "USER PID PPID VSZ RSS WCHAN PC S NAME\n" 60 + "root 1 0 11136 1828 epoll_wait 4d8064 S init\n" 61 + "root 2 0 0 0 kthreadd 0 S [kthreadd]\n"; 62 List<ProcessInfo> psInfo = PsParser.getProcesses(psOutput); 63 assertEquals("PsParser did not return expected number of processes info ", 2, 64 psInfo.size()); 65 assertEquals("First process user name did not match", "root", psInfo.get(0).getUser()); 66 assertEquals("First process id did not match", 1, psInfo.get(0).getPid()); 67 assertEquals("First process name did not match", "init", psInfo.get(0).getName()); 68 assertEquals("Second process user name did not match", "root", psInfo.get(1).getUser()); 69 assertEquals("Second process id did not match", 2, psInfo.get(1).getPid()); 70 assertEquals("Second process name did not match", "[kthreadd]", psInfo.get(1).getName()); 71 } 72 73 /** 74 * Test if "ps -A || ps" output has missing data and returns the expected ps info 75 */ 76 public void testMissingInfoPsOutput() { 77 String psMissingInfo = "bad pid '-A'\n" 78 + "USER PID PPID VSZ RSS WCHAN PC S NAME\n" 79 + "root 1 0 11136 1828 4d8064 S init\n" 80 + "root 2 0 0 kthreadd 0 S [kthreadd]\n"; 81 List<ProcessInfo> psInfo = PsParser.getProcesses(psMissingInfo); 82 assertEquals("PsParser did not return expected number of processes info ", 2, 83 psInfo.size()); 84 assertEquals("First process user name did not match", "root", psInfo.get(0).getUser()); 85 assertEquals("First process id did not match", 1, psInfo.get(0).getPid()); 86 assertEquals("First process name did not match", "init", psInfo.get(0).getName()); 87 assertEquals("Second process user name did not match", "root", psInfo.get(1).getUser()); 88 assertEquals("Second process id did not match", 2, psInfo.get(1).getPid()); 89 assertEquals("Second process name did not match", "[kthreadd]", psInfo.get(1).getName()); 90 } 91 92 } 93