tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

binary_sizes_test.py (3865B)


      1 #!/usr/bin/env vpython3
      2 # Copyright 2020 The Chromium Authors
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 import json
      7 import os
      8 import shutil
      9 import tempfile
     10 import unittest
     11 
     12 import binary_sizes
     13 
     14 
     15 _EXAMPLE_BLOBS = """
     16 {
     17  "web_engine": [
     18    {
     19      "merkle": "77e876447dd2daaaab7048d646e87fe8b6d9fecef6cbfcc4af30b8fbfa50b881",
     20      "path": "locales/ta.pak",
     21      "bytes": 17916,
     22      "is_counted": true,
     23      "size": 16384
     24    },
     25    {
     26      "merkle": "5f1932b8c9fe954f3c3fdb34ab2089d2af34e5a0cef90cad41a1cd37d92234bf",
     27      "path": "lib/libEGL.so",
     28      "bytes": 226960,
     29      "is_counted": true,
     30      "size": 90112
     31    },
     32    {
     33      "merkle": "9822fc0dd95cdd1cc46b5c6632a928a6ad19b76ed0157397d82a2f908946fc34",
     34      "path": "meta.far",
     35      "bytes": 24576,
     36      "is_counted": true,
     37      "size": 16384
     38    },
     39    {
     40      "merkle": "090aed4593c4f7d04a3ad80e9971c0532dd5b1d2bdf4754202cde510a88fd220",
     41      "path": "locales/ru.pak",
     42      "bytes": 11903,
     43      "is_counted": true,
     44      "size": 16384
     45    }
     46  ]
     47 }
     48 """
     49 
     50 
     51 class TestBinarySizes(unittest.TestCase):
     52  tmpdir = None
     53 
     54  @classmethod
     55  def setUpClass(cls):
     56    cls.tmpdir = tempfile.mkdtemp()
     57 
     58  @classmethod
     59  def tearDownClass(cls):
     60    shutil.rmtree(cls.tmpdir)
     61 
     62 
     63  def testReadAndWritePackageBlobs(self):
     64    # TODO(crbug.com/40219667): Disabled on Windows because Windows doesn't allow opening a
     65    # NamedTemporaryFile by name.
     66    if os.name == 'nt':
     67      return
     68    with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
     69      tmp_file.write(_EXAMPLE_BLOBS)
     70      tmp_file.flush()
     71 
     72      package_blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
     73 
     74    tmp_package_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
     75    tmp_package_file.close()
     76    try:
     77      binary_sizes.WritePackageBlobsJson(tmp_package_file.name, package_blobs)
     78 
     79      self.assertEqual(binary_sizes.ReadPackageBlobsJson(tmp_package_file.name),
     80                       package_blobs)
     81    finally:
     82      os.remove(tmp_package_file.name)
     83 
     84  def testReadAndWritePackageSizes(self):
     85    # TODO(crbug.com/40219667): Disabled on Windows because Windows doesn't allow opening a
     86    # NamedTemporaryFile by name.
     87    if os.name == 'nt':
     88      return
     89    with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
     90      tmp_file.write(_EXAMPLE_BLOBS)
     91      tmp_file.flush()
     92      blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
     93 
     94    sizes = binary_sizes.GetPackageSizes(blobs)
     95 
     96    new_sizes = {}
     97    with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
     98      binary_sizes.WritePackageSizesJson(tmp_file.name, sizes)
     99      new_sizes = binary_sizes.ReadPackageSizesJson(tmp_file.name)
    100      self.assertEqual(new_sizes, sizes)
    101      self.assertIn('web_engine', new_sizes)
    102 
    103  def testGetPackageSizesUsesBlobMerklesForCount(self):
    104    # TODO(crbug.com/40219667): Disabled on Windows because Windows doesn't allow opening a
    105    # NamedTemporaryFile by name.
    106    if os.name == 'nt':
    107      return
    108    blobs = json.loads(_EXAMPLE_BLOBS)
    109 
    110    # Make a duplicate of the last blob.
    111    last_blob = dict(blobs['web_engine'][-1])
    112    blobs['cast_runner'] = []
    113    last_blob['path'] = 'foo'  # Give a non-sense name, but keep merkle.
    114 
    115    # If the merkle is the same, the blob_count increases by 1.
    116    # This effectively reduces the size of the blobs size by half.
    117    # In both packages, despite it appearing in both and under different
    118    # names.
    119    blobs['cast_runner'].append(last_blob)
    120 
    121    with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
    122      tmp_file.write(json.dumps(blobs))
    123      tmp_file.flush()
    124      blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
    125 
    126    sizes = binary_sizes.GetPackageSizes(blobs)
    127 
    128    self.assertEqual(sizes['cast_runner'].compressed, last_blob['size'] / 2)
    129 
    130 
    131 if __name__ == '__main__':
    132  unittest.main()