• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2017 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import posixpath
7import unittest
8
9import six
10
11from devil.android import flag_changer
12
13_CMDLINE_FILE = 'chrome-command-line'
14
15
16class _FakeDevice(object):
17  def __init__(self):
18    self.build_type = 'user'
19    self.has_root = True
20    self.file_system = {}
21
22  def HasRoot(self):
23    return self.has_root
24
25  def PathExists(self, filepath):
26    return filepath in self.file_system
27
28  def RemovePath(self, path, **_kwargs):
29    self.file_system.pop(path)
30
31  def WriteFile(self, path, contents, **_kwargs):
32    self.file_system[path] = contents
33
34  def ReadFile(self, path, **_kwargs):
35    return self.file_system[path]
36
37
38class FlagChangerTest(unittest.TestCase):
39  def setUp(self):
40    self.device = _FakeDevice()
41    # pylint: disable=protected-access
42    self.cmdline_path = posixpath.join(flag_changer._CMDLINE_DIR, _CMDLINE_FILE)
43    self.cmdline_path_legacy = posixpath.join(flag_changer._CMDLINE_DIR_LEGACY,
44                                              _CMDLINE_FILE)
45
46  def testFlagChanger_removeAlternateCmdLine(self):
47    self.device.WriteFile(self.cmdline_path_legacy, 'chrome --old --stuff')
48    self.assertTrue(self.device.PathExists(self.cmdline_path_legacy))
49
50    changer = flag_changer.FlagChanger(self.device, 'chrome-command-line')
51    self.assertEquals(
52        changer._cmdline_path,  # pylint: disable=protected-access
53        self.cmdline_path)
54    self.assertFalse(self.device.PathExists(self.cmdline_path_legacy))
55
56  def testFlagChanger_removeAlternateCmdLineLegacyPath(self):
57    self.device.WriteFile(self.cmdline_path, 'chrome --old --stuff')
58    self.assertTrue(self.device.PathExists(self.cmdline_path))
59
60    changer = flag_changer.FlagChanger(
61        self.device, 'chrome-command-line', use_legacy_path=True)
62    self.assertEquals(
63        changer._cmdline_path,  # pylint: disable=protected-access
64        self.cmdline_path_legacy)
65    self.assertFalse(self.device.PathExists(self.cmdline_path))
66
67  def testFlagChanger_mustBeFileName(self):
68    with self.assertRaises(ValueError):
69      flag_changer.FlagChanger(self.device, '/data/local/chrome-command-line')
70
71
72class ParseSerializeFlagsTest(unittest.TestCase):
73  def _testQuoteFlag(self, flag, expected_quoted_flag):
74    # Start with an unquoted flag, check that it's quoted as expected.
75    # pylint: disable=protected-access
76    quoted_flag = flag_changer._QuoteFlag(flag)
77    self.assertEqual(quoted_flag, expected_quoted_flag)
78    # Check that it survives a round-trip.
79    parsed_flags = flag_changer._ParseFlags('_ %s' % quoted_flag)
80    self.assertEqual(len(parsed_flags), 1)
81    self.assertEqual(flag, parsed_flags[0])
82
83  def testQuoteFlag_simple(self):
84    self._testQuoteFlag('--simple-flag', '--simple-flag')
85
86  def testQuoteFlag_withSimpleValue(self):
87    self._testQuoteFlag('--key=value', '--key=value')
88
89  def testQuoteFlag_withQuotedValue1(self):
90    self._testQuoteFlag('--key=valueA valueB', '--key="valueA valueB"')
91
92  def testQuoteFlag_withQuotedValue2(self):
93    self._testQuoteFlag('--key=this "should" work',
94                        r'--key="this \"should\" work"')
95
96  def testQuoteFlag_withQuotedValue3(self):
97    self._testQuoteFlag("--key=this is 'fine' too",
98                        '''--key="this is 'fine' too"''')
99
100  def testQuoteFlag_withQuotedValue4(self):
101    self._testQuoteFlag("--key='I really want to keep these quotes'",
102                        '''--key="'I really want to keep these quotes'"''')
103
104  def testQuoteFlag_withQuotedValue5(self):
105    self._testQuoteFlag("--this is a strange=flag",
106                        '"--this is a strange=flag"')
107
108  def testQuoteFlag_withEmptyValue(self):
109    self._testQuoteFlag('--some-flag=', '--some-flag=')
110
111  def _testParseCmdLine(self, command_line, expected_flags):
112    # Start with a command line, check that flags are parsed as expected.
113    # pylint: disable=protected-access
114    # pylint: disable=no-member
115    flags = flag_changer._ParseFlags(command_line)
116    if six.PY2:
117      self.assertItemsEqual(flags, expected_flags)
118    else:
119      self.assertCountEqual(flags, expected_flags)
120
121    # Check that flags survive a round-trip.
122    # Note: Although new_command_line and command_line may not match, they
123    # should describe the same set of flags.
124    new_command_line = flag_changer._SerializeFlags(flags)
125    new_flags = flag_changer._ParseFlags(new_command_line)
126    if six.PY2:
127      self.assertItemsEqual(new_flags, expected_flags)
128    else:
129      self.assertCountEqual(new_flags, expected_flags)
130
131  def testParseCmdLine_simple(self):
132    self._testParseCmdLine('chrome --foo --bar="a b" --baz=true --fine="ok"',
133                           ['--foo', '--bar=a b', '--baz=true', '--fine=ok'])
134
135  def testParseCmdLine_withFancyQuotes(self):
136    self._testParseCmdLine(
137        r'''_ --foo="this 'is' ok"
138              --bar='this \'is\' too'
139              --baz="this \'is\' tricky"
140        ''', [
141            "--foo=this 'is' ok", "--bar=this 'is' too",
142            r"--baz=this \'is\' tricky"
143        ])
144
145  def testParseCmdLine_withUnterminatedQuote(self):
146    self._testParseCmdLine('_ --foo --bar="I forgot something',
147                           ['--foo', '--bar=I forgot something'])
148
149
150if __name__ == '__main__':
151  unittest.main(verbosity=2)
152