• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
2# reserved. Use of this source code is governed by a BSD-style license that
3# can be found in the LICENSE file.
4
5from __future__ import absolute_import
6from cef_json_builder import cef_json_builder
7import datetime
8import unittest
9
10
11class TestCefJSONBuilder(unittest.TestCase):
12
13  # Test CEF version number matching.
14  def test_valid_version(self):
15    # Old-style version numbers.
16    self.assertTrue(cef_json_builder.is_valid_version('3.2704.1414.g185cd6c'))
17    # New-style version numbers.
18    self.assertTrue(
19        cef_json_builder.is_valid_version(
20            '74.0.1+g62d140e+chromium-74.0.3729.6'))
21    self.assertTrue(
22        cef_json_builder.is_valid_version(
23            '74.0.0-master.1920+g725ed88+chromium-74.0.3729.0'))
24    self.assertTrue(
25        cef_json_builder.is_valid_version(
26            '74.0.0-my_branch.1920+g725ed88+chromium-74.0.3729.0'))
27
28    # Must be a completely qualified version number.
29    self.assertFalse(cef_json_builder.is_valid_version(None))
30    self.assertFalse(cef_json_builder.is_valid_version('foobar'))
31    self.assertFalse(cef_json_builder.is_valid_version('3.2704.1414'))
32    self.assertFalse(cef_json_builder.is_valid_version('74.0.1+g62d140e'))
33    self.assertFalse(
34        cef_json_builder.is_valid_version('74.0.0-master.1920+g725ed88'))
35    self.assertFalse(
36        cef_json_builder.is_valid_version('74.0.0+chromium-74.0.3729.0'))
37
38  # Test Chromium version number matching.
39  def test_valid_chromium_version(self):
40    self.assertTrue(cef_json_builder.is_valid_chromium_version('74.0.3729.0'))
41    self.assertTrue(cef_json_builder.is_valid_chromium_version('74.0.3729.6'))
42    self.assertTrue(
43        cef_json_builder.is_valid_chromium_version('100.0.59200.602'))
44
45    # self.assertFalse(cef_json_builder.is_valid_version(None))
46    self.assertFalse(cef_json_builder.is_valid_chromium_version('foobar'))
47    # Must be 4 numbers.
48    self.assertFalse(cef_json_builder.is_valid_chromium_version('74.0.3729'))
49    self.assertFalse(cef_json_builder.is_valid_chromium_version('74.0.3729.A'))
50    # 1st number is max 3 digits with no leading 0's.
51    self.assertFalse(
52        cef_json_builder.is_valid_chromium_version('7400.0.3729.6'))
53    self.assertFalse(cef_json_builder.is_valid_chromium_version('04.0.3729.6'))
54    # 2nd number is always 0.
55    self.assertFalse(cef_json_builder.is_valid_chromium_version('74.1.3729.6'))
56    # 3rd number is max 5 digits with no leading 0's.
57    self.assertFalse(cef_json_builder.is_valid_chromium_version('74.0.0372.6'))
58    self.assertFalse(
59        cef_json_builder.is_valid_chromium_version('74.0.372900.6'))
60    # 4rd number is max 3 digits with no leading 0's.
61    self.assertFalse(
62        cef_json_builder.is_valid_chromium_version('74.0.3729.6000'))
63    self.assertFalse(cef_json_builder.is_valid_chromium_version('74.0.3729.06'))
64    pass
65
66  # Write builder contents to string and then read in.
67  def _verify_write_read(self, builder):
68    output = str(builder)
69    builder2 = cef_json_builder()
70    builder2.load(output)
71    self.assertEqual(output, str(builder2))
72
73  # Add a file record for testing purposes.
74  def _add_test_file(self,
75                     builder,
76                     platform='linux32',
77                     version='3.2704.1414.g185cd6c',
78                     type='standard',
79                     channel='stable',
80                     attrib_idx=0,
81                     shouldfail=False):
82    name = cef_json_builder.get_file_name(version, platform, type,
83                                          channel) + '.tar.gz'
84
85    # Some random attribute information. sha1 must be different to trigger replacement.
86    attribs = [{
87        'date_str': '2016-05-18T22:42:15.487Z',
88        'date_val': datetime.datetime(2016, 5, 18, 22, 42, 15, 487000),
89        'sha1': '2d48ee05ea6385c8fe80879c98c5dd505ad4b100',
90        'size': 48395610
91    }, {
92        'date_str': '2016-05-14T22:42:15.487Z',
93        'date_val': datetime.datetime(2016, 5, 14, 22, 42, 15, 487000),
94        'sha1': '2d48ee05ea6385c8fe80879c98c5dd505ad4b200',
95        'size': 48395620
96    }]
97
98    # Populate the Chromium version to avoid queries.
99    chromium_version = '49.0.2705.50'
100    self.assertEqual(chromium_version,
101                     builder.set_chromium_version(version, chromium_version))
102    self.assertEqual(0, builder.get_query_count())
103
104    result = builder.add_file(name, attribs[attrib_idx]['size'],
105                              attribs[attrib_idx]['date_str'],
106                              attribs[attrib_idx]['sha1'])
107    # Failure should be expected when adding the same file multiple times with the same sha1.
108    self.assertEqual(not shouldfail, result)
109
110    # Return the result expected from get_files().
111    return {
112        'chromium_version': chromium_version,
113        'sha1': attribs[attrib_idx]['sha1'],
114        'name': name,
115        'platform': platform,
116        'last_modified': attribs[attrib_idx]['date_val'],
117        'cef_version': version,
118        'channel': channel,
119        'type': type,
120        'size': attribs[attrib_idx]['size']
121    }
122
123  # Test with no file contents.
124  def test_empty(self):
125    builder = cef_json_builder()
126
127    self._verify_write_read(builder)
128
129    files = builder.get_files()
130    self.assertEqual(len(files), 0)
131
132  # Test add/get of a single file with the specified type.
133  def _test_add_file(self, type, channel='stable'):
134    builder = cef_json_builder()
135
136    expected = self._add_test_file(builder, type=type, channel=channel)
137    self._verify_write_read(builder)
138
139    files = builder.get_files()
140    self.assertEqual(len(files), 1)
141    self.assertEqual(expected, files[0])
142
143  # Test add/get of a standard type file.
144  def test_add_standard_file(self):
145    self._test_add_file('standard')
146
147  # Test add/get of a minimal type file.
148  def test_add_minimal_file(self):
149    self._test_add_file('minimal')
150
151  # Test add/get of a client type file.
152  def test_add_client_file(self):
153    self._test_add_file('client')
154
155  # Test add/get of a debug_symbols type file.
156  def test_add_debug_symbols_file(self):
157    self._test_add_file('debug_symbols')
158
159  # Test add/get of a release_symbols type file.
160  def test_add_release_symbols_file(self):
161    self._test_add_file('release_symbols')
162
163  # Test add/get of a standard type file in beta channel.
164  def test_add_standard_file_beta(self):
165    self._test_add_file('standard', channel='beta')
166
167  # Test add/get of a minimal type file in beta channel.
168  def test_add_minimal_file_beta(self):
169    self._test_add_file('minimal', channel='beta')
170
171  # Test add/get of a client type file in beta channel.
172  def test_add_client_file_beta(self):
173    self._test_add_file('client', channel='beta')
174
175  # Test add/get of a debug_symbols type file in beta channel.
176  def test_add_debug_symbols_file_beta(self):
177    self._test_add_file('debug_symbols', channel='beta')
178
179  # Test add/get of a release_symbols type file in beta channel.
180  def test_add_release_symbols_file_beta(self):
181    self._test_add_file('release_symbols', channel='beta')
182
183  # Test get_files() behavior with a single file.
184  def test_get_files_single(self):
185    # yapf: disable
186    versions = (
187        ('3.2704.1414.g185cd6c', '3.2704'),
188        ('74.0.1+g62d140e+chromium-74.0.3729.6', '74.0'),
189    )
190    # yapf: enable
191
192    for version, partial_version in versions:
193      builder = cef_json_builder()
194
195      # Specify all values just in case the defaults change.
196      expected = self._add_test_file(
197          builder, platform='linux32', version=version, type='standard')
198
199      # No filter.
200      files = builder.get_files()
201      self.assertEqual(len(files), 1)
202      self.assertEqual(expected, files[0])
203
204      # Platform filter.
205      files = builder.get_files(platform='linux32')
206      self.assertEqual(len(files), 1)
207      self.assertEqual(expected, files[0])
208      files = builder.get_files(platform='linux64')
209      self.assertEqual(len(files), 0)
210
211      # Version filter exact.
212      files = builder.get_files(version=version)
213      self.assertEqual(len(files), 1)
214      self.assertEqual(expected, files[0])
215
216      # Version filter partial.
217      files = builder.get_files(version=partial_version)
218      self.assertEqual(len(files), 1)
219      self.assertEqual(expected, files[0])
220      files = builder.get_files(version='3.2623')
221      self.assertEqual(len(files), 0)
222
223      # Type filter.
224      files = builder.get_files(type='standard')
225      self.assertEqual(len(files), 1)
226      self.assertEqual(expected, files[0])
227      files = builder.get_files(type='client')
228      self.assertEqual(len(files), 0)
229
230      # All filters.
231      files = builder.get_files(
232          platform='linux32', version=partial_version, type='standard')
233      self.assertEqual(len(files), 1)
234      self.assertEqual(expected, files[0])
235      files = builder.get_files(
236          platform='linux32', version=partial_version, type='minimal')
237      self.assertEqual(len(files), 0)
238      files = builder.get_files(
239          platform='linux32', version='3.2623', type='standard')
240      self.assertEqual(len(files), 0)
241      files = builder.get_files(
242          platform='linux64', version=partial_version, type='standard')
243      self.assertEqual(len(files), 0)
244
245  # Test add/get of multiple files.
246  def test_add_multiple_files(self):
247    builder = cef_json_builder()
248
249    expected = []
250
251    platforms = cef_json_builder.get_platforms()
252    versions = [
253        # Old-style version numbers.
254        '3.2704.1414.g185cd6c',
255        '3.2704.1400.gde36543',
256        # New-style version numbers.
257        '74.0.1+g62d140e+chromium-74.0.3729.6'
258    ]
259    types = cef_json_builder.get_distrib_types()
260    for platform in platforms:
261      for version in versions:
262        for type in types:
263          expected.append(
264              self._add_test_file(
265                  builder, platform=platform, type=type, version=version))
266
267    self._verify_write_read(builder)
268
269    # No filter.
270    files = builder.get_files()
271    self.assertEqual(len(files), len(platforms) * len(versions) * len(types))
272
273    # Version filter.
274    for version in versions:
275      files = builder.get_files(version=version)
276      self.assertEqual(len(files), len(platforms) * len(types))
277
278    # Type filter.
279    files = builder.get_files(type='client')
280    self.assertEqual(len(files), len(platforms) * len(versions))
281
282    # Platform filter.
283    files = builder.get_files(platform='windows32')
284    self.assertEqual(len(files), len(types) * len(versions))
285
286    # All filters.
287    idx = 0
288    for platform in platforms:
289      for version in versions:
290        for type in types:
291          files = builder.get_files(
292              platform=platform, type=type, version=version)
293          self.assertEqual(len(files), 1)
294          self.assertEqual(expected[idx], files[0])
295          idx += 1
296
297  # Test add/get/replace of multiple files.
298  def test_replace_all_files(self):
299    versions = ['3.2704.1414.g185cd6c', '74.0.1+g62d140e+chromium-74.0.3729.6']
300    platforms = ['linux32', 'linux64']
301    types = ['standard', 'minimal']
302
303    for version in versions:
304      builder = cef_json_builder()
305
306      # Initial file versions.
307      for platform in platforms:
308        for type in types:
309          self._add_test_file(
310              builder, platform=platform, type=type, version=version)
311
312      # No filter.
313      files = builder.get_files()
314      self.assertEqual(len(files), len(platforms) * len(types))
315
316      expected = []
317
318      # Replace all file versions (due to new sha1).
319      for platform in platforms:
320        for type in types:
321          expected.append(
322              self._add_test_file(
323                  builder,
324                  platform=platform,
325                  type=type,
326                  version=version,
327                  attrib_idx=1))
328
329      # No filter.
330      files = builder.get_files()
331      self.assertEqual(len(files), len(platforms) * len(types))
332
333      # All filters.
334      idx = 0
335      for platform in platforms:
336        for type in types:
337          files = builder.get_files(
338              platform=platform, type=type, version=version)
339          self.assertEqual(len(files), 1)
340          self.assertEqual(expected[idx], files[0])
341          idx += 1
342
343  # Test add/get/no replace of multiple files.
344  def test_replace_no_files(self):
345    versions = ['3.2704.1414.g185cd6c', '74.0.1+g62d140e+chromium-74.0.3729.6']
346    platforms = ['linux32', 'linux64']
347    types = ['standard', 'minimal']
348
349    for version in versions:
350      builder = cef_json_builder()
351
352      # Initial file versions.
353      for platform in platforms:
354        for type in types:
355          self._add_test_file(
356              builder, platform=platform, type=type, version=version)
357
358      # No filter.
359      files = builder.get_files()
360      self.assertEqual(len(files), len(platforms) * len(types))
361
362      expected = []
363
364      # Replace no file versions (due to same sha1).
365      for platform in platforms:
366        for type in types:
367          expected.append(
368              self._add_test_file(
369                  builder,
370                  platform=platform,
371                  type=type,
372                  version=version,
373                  shouldfail=True))
374
375      # No filter.
376      files = builder.get_files()
377      self.assertEqual(len(files), len(platforms) * len(types))
378
379      # All filters.
380      idx = 0
381      for platform in platforms:
382        for type in types:
383          files = builder.get_files(
384              platform=platform, type=type, version=version)
385          self.assertEqual(len(files), 1)
386          self.assertEqual(expected[idx], files[0])
387          idx += 1
388
389  # Test Chromium version.
390  def test_chromium_version(self):
391    builder = cef_json_builder()
392
393    # For old-style CEF version numbers the Git hashes must exist but the rest
394    # of the CEF version can be fake.
395    # yapf: disable
396    versions = (
397        # Old-style version numbers.
398        ('3.2704.1414.g185cd6c', '51.0.2704.47'),
399        ('3.2623.9999.gb90a3be', '49.0.2623.110'),
400        ('3.2623.9999.g2a6491b', '49.0.2623.87'),
401        ('3.9999.9999.gab2636b', 'master'),
402        # New-style version numbers.
403        ('74.0.1+g62d140e+chromium-74.0.3729.6', '74.0.3729.6'),
404        ('74.0.0-master.1920+g725ed88+chromium-74.0.3729.0', '74.0.3729.0'),
405        ('74.0.0-my_branch.1920+g725ed88+chromium-74.0.3729.0', '74.0.3729.0'),
406    )
407    # yapf: enable
408
409    # Test with no query.
410    for (cef, chromium) in versions:
411      self.assertFalse(builder.has_chromium_version(cef))
412      # Valid version specified, so no query sent.
413      self.assertEqual(chromium, builder.set_chromium_version(cef, chromium))
414      self.assertEqual(chromium, builder.get_chromium_version(cef))
415      self.assertTrue(builder.has_chromium_version(cef))
416      # Ignore attempts to set invalid version after setting valid version.
417      self.assertEqual(chromium, builder.set_chromium_version(cef, None))
418
419    self.assertEqual(0, builder.get_query_count())
420    builder.clear()
421
422    # Test with query.
423    for (cef, chromium) in versions:
424      self.assertFalse(builder.has_chromium_version(cef))
425      # No version known, so query sent for old-style version numbers.
426      self.assertEqual(chromium, builder.get_chromium_version(cef))
427      self.assertTrue(builder.has_chromium_version(cef))
428
429    # Only old-style version numbers.
430    self.assertEqual(4, builder.get_query_count())
431
432
433# Program entry point.
434if __name__ == '__main__':
435  unittest.main()
436