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