summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xtools/crx_id/crx_id.py71
-rwxr-xr-xtools/crx_id/crx_id_unittest.py78
2 files changed, 115 insertions, 34 deletions
diff --git a/tools/crx_id/crx_id.py b/tools/crx_id/crx_id.py
index 299e1d6..767f03b 100755
--- a/tools/crx_id/crx_id.py
+++ b/tools/crx_id/crx_id.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-# Copyright (c) 2011 The Chromium Authors. All rights reserved.
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -10,14 +10,19 @@ and 'http://stackoverflow.com/questions/'
for docs on the format.
"""
+import base64
+import os
import sys
import hashlib
+try:
+ import json
+except Exception:
+ import simplejson as json
EXPECTED_CRX_MAGIC_NUM = 'Cr24'
EXPECTED_CRX_VERSION = 2
-
def usage(argv):
print "%s: crx_file" % argv[0]
@@ -54,36 +59,70 @@ def HexTo256(hex_chars):
result.append('0x' + hex(dig1)[2:] + hex(dig2)[2:])
return '{%s}' % ', '.join(result)
-def GetPublicKey(f):
+def GetPublicKeyPacked(f):
magic_num = f.read(4)
if magic_num != EXPECTED_CRX_MAGIC_NUM:
- raise 'Invalid magic number: %s (expecting %s)' % (magic_num,
- EXPECTED_CRX_MAGIC_NUM)
+ raise Exception('Invalid magic number: %s (expecting %s)' %
+ (magic_num,
+ EXPECTED_CRX_MAGIC_NUM))
version = f.read(4)
if not version[0] != EXPECTED_CRX_VERSION:
- raise 'Invalid version number: %s (expecting %s)' % (version,
- EXPECTED_CRX_VERSION)
+ raise Exception('Invalid version number: %s (expecting %s)' %
+ (version,
+ EXPECTED_CRX_VERSION))
pub_key_len_bytes = HexToInt(f.read(4))
sig_len_bytes = HexToInt(f.read(4))
pub_key = f.read(pub_key_len_bytes)
return pub_key
-def GetCRXHash(filename):
- f = open(filename, 'rb')
- pub_key = GetPublicKey(f)
- f.close()
+def GetPublicKeyFromPath(filepath):
+ # Normalize the path for windows to have capital drive letters.
+ # We intentionally don't check if sys.platform == 'win32' and just
+ # check if this looks like drive letter so that we can test this
+ # even on posix systems.
+ if (len(filepath) >= 2 and
+ filepath[0].islower() and
+ filepath[1] == ':'):
+ return filepath[0].upper() + filepath[1:]
+ return filepath
+
+def GetPublicKeyUnpacked(f, filepath):
+ manifest = json.load(f)
+ if 'key' not in manifest:
+ # Use the path as the public key.
+ # See Extension::GenerateIdForPath in extension.cc
+ return GetPublicKeyFromPath(filepath)
+ else:
+ return base64.standard_b64decode(manifest['key'])
+
+def GetPublicKey(filename, from_test_path):
+ if from_test_path:
+ return GetPublicKeyFromPath(filename)
+
+ pub_key = ''
+ if os.path.isdir(filename):
+ # Assume it's an unpacked extension
+ f = open(os.path.join(filename, 'manifest.json'), 'rb')
+ pub_key = GetPublicKeyUnpacked(f, filename)
+ f.close()
+ else:
+ # Assume it's a packed extension.
+ f = open(filename, 'rb')
+ pub_key = GetPublicKeyPacked(f)
+ f.close()
+ return pub_key
+
+def GetCRXHash(filename, from_test_path=False):
+ pub_key = GetPublicKey(filename, from_test_path)
pub_key_hash = hashlib.sha256(pub_key).digest()
return HexTo256(pub_key_hash)
-def GetCRXAppID(filename):
- f = open(filename, 'rb')
- pub_key = GetPublicKey(f)
- f.close()
+def GetCRXAppID(filename, from_test_path=False):
+ pub_key = GetPublicKey(filename, from_test_path)
pub_key_hash = hashlib.sha256(pub_key).digest()
# AppID is the MPDecimal of only the first 128 bits of the hash.
return HexToMPDecimal(pub_key_hash[:128/8])
-
def main(argv):
if len(argv) != 2:
usage(argv)
diff --git a/tools/crx_id/crx_id_unittest.py b/tools/crx_id/crx_id_unittest.py
index ce5d197..39ea816 100755
--- a/tools/crx_id/crx_id_unittest.py
+++ b/tools/crx_id/crx_id_unittest.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
-# Copyright (c) 2011 The Chromium Authors. All rights reserved.
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -9,28 +9,70 @@
import crx_id
import os
+import shutil
import sys
import unittest
+import tempfile
-CANNED_CRX = os.path.join(os.path.dirname(sys.argv[0]),
- 'jebgalgnebhfojomionfpkfelancnnkf.crx')
-CRX_ID_SCRIPT = os.path.join(os.path.dirname(sys.argv[0]),
- 'crx_id.py')
-EXPECTED_HASH_BYTES = \
- '{0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec,' \
- ' 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5,' \
- ' 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4,' \
- ' 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40}'
-
-EXPECTED_APP_ID = 'jebgalgnebhfojomionfpkfelancnnkf'
+CRX_ID_DIR = os.path.dirname(sys.argv[0])
class CrxIdUnittest(unittest.TestCase):
- def testHashAppId(self):
- """ Test that the output generated for a canned CRX. """
- self.assertEqual(crx_id.GetCRXAppID(CANNED_CRX),
- EXPECTED_APP_ID)
- self.assertEqual(crx_id.GetCRXHash(CANNED_CRX),
- EXPECTED_HASH_BYTES)
+
+ PACKED_CRX = os.path.join(CRX_ID_DIR,
+ 'jebgalgnebhfojomionfpkfelancnnkf.crx')
+
+ PACKED_APP_ID = 'jebgalgnebhfojomionfpkfelancnnkf'
+ PACKED_HASH_BYTES = \
+ '{0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec,' \
+ ' 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5,' \
+ ' 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4,' \
+ ' 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40}'
+
+ def testPackedHashAppId(self):
+ """ Test the output generated for a canned, packed CRX. """
+ self.assertEqual(crx_id.GetCRXAppID(self.PACKED_CRX),
+ self.PACKED_APP_ID)
+ self.assertEqual(crx_id.GetCRXHash(self.PACKED_CRX),
+ self.PACKED_HASH_BYTES)
+
+
+ # ../../chrome/test/data/extensions/unpacked/manifest_with_key.json
+ BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(sys.argv[0])))
+ UNPACKED_TEST_DIR = os.path.join(
+ BASE_DIR,
+ 'chrome', 'test', 'data', 'extensions', 'unpacked')
+ UNPACKED_APP_ID = 'cbcdidchbppangcjoddlpdjlenngjldk'
+ UNPACKED_HASH_BYTES = \
+ '{0x21, 0x23, 0x83, 0x27, 0x1f, 0xf0, 0xd6, 0x29,' \
+ ' 0xe3, 0x3b, 0xf3, 0x9b, 0x4d, 0xd6, 0x9b, 0x3a,' \
+ ' 0xff, 0x7d, 0x6b, 0xc4, 0x78, 0x30, 0x47, 0xa6,' \
+ ' 0x23, 0x12, 0x72, 0x84, 0x9b, 0x9a, 0xf6, 0x3c}'
+
+ def testUnpackedHashAppId(self):
+ """ Test the output generated for a canned, unpacked extension. """
+ temp_unpacked_crx = tempfile.mkdtemp()
+ shutil.copy2(os.path.join(self.UNPACKED_TEST_DIR,
+ 'manifest_with_key.json'),
+ os.path.join(temp_unpacked_crx,
+ 'manifest.json'))
+ self.assertEqual(crx_id.GetCRXAppID(temp_unpacked_crx),
+ self.UNPACKED_APP_ID)
+ self.assertEqual(crx_id.GetCRXHash(temp_unpacked_crx),
+ self.UNPACKED_HASH_BYTES)
+ # This uses the path to compute the AppID.
+ self.assertEqual(crx_id.GetCRXAppID('/tmp/temp_extension',
+ from_test_path=True),
+ 'ajbbicncdkdlchpjplgjaglppbcbmaji')
+ # Test drive letter normalization.
+ kWinPathId = 'popnagglbbhjlobnnbcjnckakjoegnjp'
+ self.assertEqual(crx_id.GetCRXAppID('c:\temp_extension',
+ from_test_path=True),
+ kWinPathId)
+ self.assertEqual(crx_id.GetCRXAppID('C:\temp_extension',
+ from_test_path=True),
+ kWinPathId)
+ shutil.rmtree(temp_unpacked_crx)
+
if __name__ == '__main__':
unittest.main()