1/*
2 * Copyright (C) 2008 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
17package com.android.cts;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.security.NoSuchAlgorithmException;
24import java.util.ArrayList;
25import java.util.List;
26import java.util.zip.ZipEntry;
27import java.util.zip.ZipOutputStream;
28
29import com.android.cts.HostConfig.CaseRepository;
30
31/**
32 * Test the console commands.
33 */
34public class ConsoleTests extends CtsTestBase {
35
36    private String mPath1;
37    private String mPath2;
38    private String mPath3;
39
40    /** {@inheritDoc} */
41    @Override
42    public void tearDown() {
43        HostConfig.getInstance().removeTestPacakges();
44        deleteTestPackage(mPath1);
45        deleteTestPackage(mPath2);
46        deleteTestPackage(mPath3);
47
48        super.tearDown();
49    }
50
51    /**
52     * Test adding package to test case repository and then getting the package names.
53     */
54    public void testAddPackage() throws Exception {
55        ConsoleUi cui = new ConsoleUi(TestHost.getInstance());
56
57        mPath1 = ROOT + File.separator + "com.google.android.cts.p1.zip";
58        mPath2 = ROOT + File.separator + "com.google.android.cts.p2.zip";
59        mPath3 = ROOT + File.separator + "net.sf.jlee.cts.p3.zip";
60
61        final String expPackageName1 = "com.google.android.cts.p1";
62        final String expPackageName2 = "com.google.android.cts.p2";
63        final String expPackageName3 = "net.sf.jlee.cts.p3";
64
65        HostConfig.getInstance().removeTestPacakges();
66
67        ArrayList<String> pNames = HostConfig.getInstance().getCaseRepository()
68                .getPackageBinaryNames();
69
70        assertEquals(0, pNames.size());
71        createTestPackageZip(mPath1, expPackageName1);
72        createTestPackageZip(mPath2, expPackageName2);
73        createTestPackageZip(mPath3, expPackageName3);
74
75        // add package 1
76        String cmdline = CTSCommand.ADD + " " + "-p" + " "
77                + mPath1;
78        cui.processCommand(CommandParser.parse(cmdline));
79
80        pNames = HostConfig.getInstance().getCaseRepository().getPackageBinaryNames();
81        assertEquals(1, pNames.size());
82        assertTrue(pNames.contains(expPackageName1));
83
84        // add package 2
85        cmdline = CTSCommand.ADD + " " + "-p" + " " + mPath2;
86        cui.processCommand(CommandParser.parse(cmdline));
87
88        pNames = HostConfig.getInstance().getCaseRepository().getPackageBinaryNames();
89        assertEquals(2, pNames.size());
90        assertTrue(pNames.contains(expPackageName1));
91        assertTrue(pNames.contains(expPackageName2));
92
93        // add package 2
94        cmdline = CTSCommand.ADD + " " + "-p" + " " + mPath3;
95        cui.processCommand(CommandParser.parse(cmdline));
96
97        pNames = HostConfig.getInstance().getCaseRepository().getPackageBinaryNames();
98        assertEquals(3, pNames.size());
99        assertTrue(pNames.contains(expPackageName1));
100        assertTrue(pNames.contains(expPackageName2));
101        assertTrue(pNames.contains(expPackageName3));
102
103        deleteTestPackage(expPackageName1);
104        deleteTestPackage(expPackageName2);
105        deleteTestPackage(expPackageName3);
106    }
107
108    /**
109     * Test removing package after adding the packages into the test case repository.
110     */
111    public void testRemovePackage() throws Exception {
112        ConsoleUi cui = new ConsoleUi(TestHost.getInstance());
113        mPath1 = ROOT + File.separator + "com.google.android.cts.p1.zip";
114        mPath2 = ROOT + File.separator + "com.google.android.cts.p2.zip";
115        mPath3 = ROOT + File.separator + "net.sf.jlee.cts.p3.zip";
116
117        final String expPackageName1 = "com.google.android.cts.p1";
118        final String expPackageName2 = "com.google.android.cts.p2";
119        final String expPackageName3 = "net.sf.jlee.cts.p3";
120
121        HostConfig.getInstance().removeTestPacakges();
122        createTestPackageZip(mPath1, expPackageName1);
123        createTestPackageZip(mPath2, expPackageName2);
124        createTestPackageZip(mPath3, expPackageName3);
125
126        // add package 1
127        String cmdLine = CTSCommand.ADD + " -p " + mPath1;
128        cui.processCommand(CommandParser.parse(cmdLine));
129        cmdLine = CTSCommand.ADD + " -p " + mPath2;
130        cui.processCommand(CommandParser.parse(cmdLine));
131        cmdLine = CTSCommand.ADD + " -p " + mPath3;
132        cui.processCommand(CommandParser.parse(cmdLine));
133
134        ArrayList<String> pNames = HostConfig.getInstance().getCaseRepository()
135                .getPackageBinaryNames();
136        assertEquals(3, pNames.size());
137        assertTrue(pNames.contains(expPackageName1));
138        assertTrue(pNames.contains(expPackageName2));
139        assertTrue(pNames.contains(expPackageName3));
140
141        cmdLine = CTSCommand.REMOVE + " " + "-p" + " "
142                + expPackageName1;
143        cui.processCommand(CommandParser.parse(cmdLine));
144        pNames = HostConfig.getInstance().getCaseRepository().getPackageBinaryNames();
145        assertEquals(2, pNames.size());
146        assertTrue(pNames.contains(expPackageName2));
147        assertTrue(pNames.contains(expPackageName3));
148
149        cmdLine = CTSCommand.REMOVE + " " + "-p" + " "
150                + expPackageName2;
151        cui.processCommand(CommandParser.parse(cmdLine));
152        pNames = HostConfig.getInstance().getCaseRepository().getPackageBinaryNames();
153        assertEquals(1, pNames.size());
154        assertTrue(pNames.contains(expPackageName3));
155
156        cmdLine = CTSCommand.REMOVE + " " + "-p" + " "
157                + expPackageName3;
158        cui.processCommand(CommandParser.parse(cmdLine));
159        pNames = HostConfig.getInstance().getCaseRepository().getPackageBinaryNames();
160        assertEquals(0, pNames.size());
161
162        deleteTestPackage(expPackageName1);
163        deleteTestPackage(expPackageName2);
164        deleteTestPackage(expPackageName3);
165    }
166
167    /**
168     * Test validating partial zipped package when adding package..
169     */
170    public void testValidatePartialZipPackageName() throws Exception {
171        final String pkgName1 = "com.google.android.cts.apkPartial.zip";
172        final String pkgName2 = "com.google.android.cts.xmlPartial.zip";
173        mPath1 = ROOT + File.separator + pkgName1;
174        mPath2 = ROOT + File.separator + pkgName2;
175
176        HostConfig config = HostConfig.getInstance();
177        HostConfig.CaseRepository caseRepos = config.getCaseRepository();
178
179        createPartialTestPackageZip(mPath1, HostConfig.FILE_SUFFIX_APK);
180        assertFalse(caseRepos.addPackage(mPath1));
181
182        createPartialTestPackageZip(mPath2, HostConfig.FILE_SUFFIX_XML);
183        assertFalse(caseRepos.addPackage(mPath2));
184    }
185
186    /**
187     * Test validating package file suffix when adding package.
188     */
189    public void testValidatePackageSuffix() throws Exception {
190        final String content = "test test test";
191        final String pkgName1 = "com.google.android.cts.invalidSuffix.txt";
192        mPath1 = ROOT + File.separator + pkgName1;
193
194        createFile(content, mPath1);
195
196        HostConfig config = HostConfig.getInstance();
197        HostConfig.CaseRepository caseRepos = config.getCaseRepository();
198
199        assertFalse(caseRepos.isValidPackageName(mPath1));
200    }
201
202    /**
203     * Test validate duplicate package name by adding the same package twice.
204     */
205    public void testValidateDuplicatePackageName() throws Exception {
206        final String name = "com.google.android.cts.mypackage";
207        mPath1 = ROOT + File.separator + name + HostConfig.FILE_SUFFIX_ZIP;
208
209        HostConfig config = HostConfig.getInstance();
210        HostConfig.CaseRepository caseRepos = config.getCaseRepository();
211        createTestPackageZip(mPath1, name);
212
213        caseRepos.removePackages(name);
214        assertTrue(caseRepos.addPackage(mPath1));
215        assertFalse(caseRepos.isValidPackageName(mPath1));
216        caseRepos.removePackages(name);
217
218        deleteTestPackage(name);
219    }
220
221    /**
222     * Create zipped test package.
223     *
224     * @param zipFilePath The file name with path.
225     * @param packageName The package name.
226     */
227    private void createTestPackageZip(String zipFilePath, final String packageName)
228            throws IOException {
229        final String descriptionConfigStr = "<TestPackage name=\""
230            + packageName + "\" " + "appPackageName=\"" + packageName
231            + "\""
232            + " version=\"1.0\" AndroidFramework=\"Android 1.0\""
233            + " runner=\"android.test.InstrumentationTestRunner\" jarPath=\"\">\n"
234            + "  <Description>something extracted from java doc</Description>\n"
235            + "  <TestSuite name=\"com.google\">\n"
236            + "     <TestCase name=\"TestCaseName\" priority=\"mandatory\">\n"
237            + "         <Description>" + "something extracted from java doc"
238            + "         </Description>\n"
239            + "         <!-- Test Methods -->\n"
240            + "         <Test method=\"testName1\" type=\"automatic\"/>\n"
241            + "     </TestCase>\n"
242            + "  </TestSuite>\n" + "</TestPackage>\n";
243
244        createTestPackage(descriptionConfigStr, packageName);
245        String apkFile = ROOT + File.separator + packageName + HostConfig.FILE_SUFFIX_APK;
246        String xmlFile = ROOT + File.separator + packageName + HostConfig.FILE_SUFFIX_XML;
247        String zipFile = ROOT + File.separator + packageName + ".zip";
248        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
249        addEntry(out, apkFile);
250        addEntry(out, xmlFile);
251        out.close();
252
253        deleteTestPackage(packageName);
254    }
255
256    /**
257     * Add entry into the zip ouput stream.
258     *
259     * @param out The zip output stream.
260     * @param filePath The entry to be added into the zip output stream.
261     */
262    private void addEntry(ZipOutputStream out, String filePath) throws IOException {
263        byte[] buf = new byte[1024];
264        FileInputStream in = new FileInputStream(filePath);
265        out.putNextEntry(new ZipEntry(filePath));
266        int len;
267        while ((len = in.read(buf)) > 0) {
268            out.write(buf, 0, len);
269        }
270        out.closeEntry();
271        in.close();
272    }
273
274    /**
275     * Create test package with the package name and the xml message as the content.
276     *
277     * @param xmlMsg The message as the content of the package.
278     * @param packageName The package name.
279     */
280    @Override
281    protected void createTestPackage(String xmlMsg, String packageName) throws IOException {
282        String caseRoot = ROOT;
283
284        String apkPath = caseRoot + File.separator + packageName + APK_SUFFIX;
285        String xmlPath = caseRoot + File.separator + packageName + DESCRITION_SUFFIX;
286
287        createFile(null, apkPath);
288        createFile(xmlMsg, xmlPath);
289    }
290    /**
291     * Create partial test package.
292     *
293     * @param zipFilePath The file name with path.
294     * @param suffix The file suffix.
295     */
296    private void createPartialTestPackageZip(String zipFilePath, String suffix)
297                 throws IOException {
298        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
299                zipFilePath));
300
301        String packageName = zipFilePath.substring(zipFilePath
302                .lastIndexOf(File.separator), zipFilePath.lastIndexOf("."));
303        String file = packageName + suffix;
304
305        addFileToZip(file, out);
306
307        out.close();
308    }
309
310    /**
311     * Add file to zip output stream.
312     *
313     * @param filename The file to be added to the zip output stream.
314     * @param out The zip output stream.
315     */
316    private void addFileToZip(String filename, ZipOutputStream out)
317            throws IOException {
318        out.putNextEntry(new ZipEntry(filename));
319
320        out.closeEntry();
321    }
322
323    /**
324     * Test listing package contents with different levels of expectation.
325     */
326    public void testListPackage() throws IOException, NoSuchAlgorithmException {
327
328        List<ArrayList<String>> list = null;
329        ArrayList<String> packageList = null;
330        ArrayList<String> suiteList = null;
331        ArrayList<String> caseList = null;
332        ArrayList<String> testList = null;
333
334        final String packageName = "com.google";
335        final String suiteName   = "com.google.cts";
336        final String caseName    = "CtsTest";
337        final String testName    = "testHello";
338
339        final String expect1 = "com";
340        final String expect2 = "com.google";
341        final String expect3 = "com.google.cts";
342        final String expect4 = "com.google.cts.CtsTest";
343        final String expect5 = "com.google.cts.CtsTest#testHello";
344        final String expect6 = "com.google.cts.CtsTest#test";
345        final CaseRepository caseRepository = HostConfig.getInstance()
346                .getCaseRepository();
347
348        final String descriptionConfigStr = "<TestPackage name=\""
349                + packageName + "\" appPackageName=\"" + packageName
350                + "\""
351                + " version=\"1.0\" AndroidFramework=\"Android 1.0\""
352                + " runner=\"android.test.InstrumentationTestRunner\">\n"
353                + "  <Description>something extracted from java doc</Description>\n"
354                + "      <TestSuite name=\"" + suiteName + "\">\n"
355                + "         <TestCase name=\"" + caseName + "\" priority=\"mandatory\">\n"
356                + "             <Description>" + "something extracted from java doc"
357                + "             </Description>\n"
358                + "             <!-- Test Cases -->\n"
359                + "             <Test name=\"" + testName + "\"" + " type=\"automatic\"" + ">\n"
360                + "                 <Description>Simple deadloop test</Description>"
361                + "             </Test>"
362                + "        </TestCase>"
363                + "     </TestSuite>\n"
364                + "</TestPackage>\n";
365
366        final String caseDescPath = caseRepository.getXmlPath(packageName);
367        final String caseAPK = caseRepository.getApkPath(packageName);
368
369        try {
370            createFile(descriptionConfigStr, caseDescPath);
371            createFile("", caseAPK);
372
373            HostConfig.getInstance().loadTestPackages();
374
375            list = caseRepository.listAvailablePackage(expect1);
376            packageList = list.get(0);
377            suiteList = list.get(1);
378            caseList = list.get(2);
379            testList = list.get(3);
380            assertEquals(1, packageList.size());
381            assertEquals(expect2, packageList.get(0));
382            assertEquals(0, suiteList.size());
383            assertEquals(0, caseList.size());
384            assertEquals(0, testList.size());
385
386            list = caseRepository.listAvailablePackage(expect2);
387            packageList = list.get(0);
388            suiteList = list.get(1);
389            caseList = list.get(2);
390            testList = list.get(3);
391            assertEquals(0, packageList.size());
392            assertEquals(1, suiteList.size());
393            assertEquals(expect3, suiteList.get(0));
394            assertEquals(0, caseList.size());
395            assertEquals(0, testList.size());
396
397            list = caseRepository.listAvailablePackage(expect3);
398            packageList = list.get(0);
399            suiteList = list.get(1);
400            caseList = list.get(2);
401            testList = list.get(3);
402            assertEquals(0, packageList.size());
403            assertEquals(0, suiteList.size());
404            assertEquals(1, caseList.size());
405            assertEquals(expect4, caseList.get(0));
406            assertEquals(0, testList.size());
407
408            list = caseRepository.listAvailablePackage(expect4);
409            packageList = list.get(0);
410            suiteList = list.get(1);
411            caseList = list.get(2);
412            testList = list.get(3);
413            assertEquals(0, packageList.size());
414            assertEquals(0, suiteList.size());
415            assertEquals(0, caseList.size());
416            assertEquals(1, testList.size());
417            assertEquals(expect5, testList.get(0));
418
419            list = caseRepository.listAvailablePackage(expect6);
420            packageList = list.get(0);
421            suiteList = list.get(1);
422            caseList = list.get(2);
423            testList = list.get(3);
424            assertEquals(0, packageList.size());
425            assertEquals(0, suiteList.size());
426            assertEquals(0, caseList.size());
427            assertEquals(1, testList.size());
428            assertEquals(expect5, testList.get(0));
429        } finally {
430            deleteFile(caseDescPath);
431            deleteFile(caseAPK);
432        }
433    }
434
435    /**
436     * Test starting console UI.
437     */
438    public void testStartUi() throws Exception {
439        String cmdLine = CONFIG_PATH;
440        ConsoleUi cui = new ConsoleUi(TestHost.getInstance());
441        CommandParser cp = TestHost.init(cui, cmdLine.split(" "));
442        assertEquals(null, cp);
443        assertEquals(TestHost.MODE.CONSOLE, TestHost.sMode);
444
445        cmdLine = ROOT;
446        cp = TestHost.init(cui, cmdLine.split(" "));
447        assertEquals(null, cp);
448        assertEquals(TestHost.MODE.CONSOLE, TestHost.sMode);
449    }
450
451    /**
452     * Test starting test plan directly when activating CTS from console.
453     */
454    public void testStartPlanDirectly() throws Exception {
455        String cmdLine = "start --plan demo --config " + CONFIG_PATH;
456        ConsoleUi cui = new ConsoleUi(TestHost.getInstance());
457        CommandParser cp = TestHost.init(cui, cmdLine.split(" "));
458        assertEquals(CTSCommand.START, cp.getAction());
459        assertTrue(cp.containsKey(CTSCommand.OPTION_CFG));
460        assertTrue(cp.getValue(CTSCommand.OPTION_CFG) != null);
461        assertTrue(cp.containsKey(CTSCommand.OPTION_PLAN));
462        assertTrue(cp.getValue(CTSCommand.OPTION_PLAN) != null);
463        cp.removeKey(CTSCommand.OPTION_CFG);
464        assertFalse(cp.containsKey(CTSCommand.OPTION_CFG));
465        assertEquals(null, cp.getValue(CTSCommand.OPTION_CFG));
466
467        cmdLine = "start --plan demo --config " + ROOT;
468        cp = TestHost.init(cui, cmdLine.split(" "));
469        assertTrue(cp.containsKey(CTSCommand.OPTION_CFG));
470        assertTrue(cp.getValue(CTSCommand.OPTION_CFG) != null);
471        assertTrue(cp.containsKey(CTSCommand.OPTION_PLAN));
472        assertTrue(cp.getValue(CTSCommand.OPTION_PLAN) != null);
473        cp.removeKey(CTSCommand.OPTION_CFG);
474        assertFalse(cp.containsKey(CTSCommand.OPTION_CFG));
475        assertEquals(null, cp.getValue(CTSCommand.OPTION_CFG));
476    }
477
478    /**
479     * Test starting package directly when activating CTS from console.
480     */
481    public void testStartPackageDirectly() throws Exception {
482        String cmdLine = "start -p demo.zip --config " + CONFIG_PATH;
483        ConsoleUi cui = new ConsoleUi(TestHost.getInstance());
484        CommandParser cp = TestHost.init(cui, cmdLine.split(" "));
485        assertEquals(CTSCommand.START, cp.getAction());
486        assertTrue(cp.containsKey(CTSCommand.OPTION_CFG));
487        assertTrue(cp.getValue(CTSCommand.OPTION_CFG) != null);
488        assertTrue(cp.containsKey(
489                CTSCommand.OPTION_PACKAGE) || cp.containsKey(CTSCommand.OPTION_P));
490        assertEquals("demo.zip", cp.getValue(CTSCommand.OPTION_PACKAGE));
491        cp.removeKey(CTSCommand.OPTION_CFG);
492        assertFalse(cp.containsKey(CTSCommand.OPTION_CFG));
493        assertEquals(null, cp.getValue(CTSCommand.OPTION_CFG));
494
495        cmdLine = "start --package demo.zip --config " + ROOT;
496        cp = TestHost.init(cui, cmdLine.split(" "));
497        assertTrue(cp.containsKey(CTSCommand.OPTION_CFG));
498        assertTrue(cp.getValue(CTSCommand.OPTION_CFG) != null);
499        assertTrue(cp.containsKey(
500                CTSCommand.OPTION_PACKAGE) || cp.containsKey(CTSCommand.OPTION_P));
501        assertEquals("demo.zip", cp.getValue(CTSCommand.OPTION_PACKAGE));
502        cp.removeKey(CTSCommand.OPTION_CFG);
503        assertFalse(cp.containsKey(CTSCommand.OPTION_CFG));
504        assertEquals(null, cp.getValue(CTSCommand.OPTION_CFG));
505    }
506
507    /**
508     * Test getting device ID from the device ID string list.
509     */
510    public void testGetDeviceId() {
511        ConsoleUi cui = new ConsoleUi(TestHost.getInstance());
512        ArrayList<TestDevice> devList = new ArrayList<TestDevice>();
513        devList.add(new TestDevice("dev-100"));
514        devList.add(new TestDevice("dev-101"));
515        TestDevice[] devices = devList.toArray(new TestDevice[4]);
516
517        int deviceId = cui.getDeviceId(devices, "dev-100");
518        assertEquals(0, deviceId);
519
520        deviceId = cui.getDeviceId(devices, "dev-101");
521        assertEquals(1, deviceId);
522    }
523}
524