1# Copyright 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import os
5import shutil
6import unittest
7import tempfile
8
9from telemetry.core import util
10from telemetry.internal.backends.chrome import crx_id
11
12
13class CrxIdUnittest(unittest.TestCase):
14  CRX_ID_DIR = util.GetUnittestDataDir()
15  PACKED_CRX = os.path.join(CRX_ID_DIR,
16                            'jebgalgnebhfojomionfpkfelancnnkf.crx')
17
18  PACKED_APP_ID = 'jebgalgnebhfojomionfpkfelancnnkf'
19  PACKED_HASH_BYTES = \
20      '{0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec,' \
21      ' 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5,' \
22      ' 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4,' \
23      ' 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40}'
24
25  UNPACKED_APP_ID = 'cbcdidchbppangcjoddlpdjlenngjldk'
26  UNPACKED_HASH_BYTES = \
27      '{0x21, 0x23, 0x83, 0x27, 0x1f, 0xf0, 0xd6, 0x29,' \
28      ' 0xe3, 0x3b, 0xf3, 0x9b, 0x4d, 0xd6, 0x9b, 0x3a,' \
29      ' 0xff, 0x7d, 0x6b, 0xc4, 0x78, 0x30, 0x47, 0xa6,' \
30      ' 0x23, 0x12, 0x72, 0x84, 0x9b, 0x9a, 0xf6, 0x3c}'
31
32
33  def testPackedHashAppId(self):
34    """ Test the output generated for a canned, packed CRX. """
35    self.assertEqual(crx_id.GetCRXAppID(self.PACKED_CRX),
36                     self.PACKED_APP_ID)
37    self.assertEqual(crx_id.GetCRXHash(self.PACKED_CRX),
38                     self.PACKED_HASH_BYTES)
39
40
41  def testUnpackedHashAppId(self):
42    """ Test the output generated for a canned, unpacked extension. """
43    unpacked_test_manifest_path = os.path.join(
44        self.CRX_ID_DIR, 'manifest_with_key.json')
45    temp_unpacked_crx = tempfile.mkdtemp()
46    shutil.copy2(unpacked_test_manifest_path,
47                 os.path.join(temp_unpacked_crx, 'manifest.json'))
48    self.assertEqual(crx_id.GetCRXAppID(temp_unpacked_crx),
49                     self.UNPACKED_APP_ID)
50    self.assertEqual(crx_id.GetCRXHash(temp_unpacked_crx),
51                     self.UNPACKED_HASH_BYTES)
52    self.assertTrue(crx_id.HasPublicKey(temp_unpacked_crx))
53    shutil.rmtree(temp_unpacked_crx)
54
55
56  def testFromFilePath(self):
57    """ Test calculation of extension id from file paths. """
58    self.assertEqual(crx_id.GetCRXAppID('/tmp/temp_extension',
59                                        from_file_path=True),
60                     'ajbbicncdkdlchpjplgjaglppbcbmaji')
61
62
63  def testFromWindowsPath(self):
64    self.assertEqual(crx_id.GetCRXAppID(r'D:\Documents\chrome\test_extension',
65                                        from_file_path=True,
66                                        is_win_path=True),
67                     'fegemedmbnhglnecjgbdhekaghkccplm')
68
69    # Test drive letter normalization.
70    kWinPathId = 'aiinlcdagjihibappcdnnhcccdokjlaf'
71    self.assertEqual(crx_id.GetCRXAppID(r'c:\temp_extension',
72                                        from_file_path=True,
73                                        is_win_path=True),
74                     kWinPathId)
75    self.assertEqual(crx_id.GetCRXAppID(r'C:\temp_extension',
76                                        from_file_path=True,
77                                        is_win_path=True),
78                     kWinPathId)
79