gyptest-per-config-settings.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 Google Inc. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """
  6. Verifies that device and simulator bundles are built correctly.
  7. """
  8. import plistlib
  9. import TestGyp
  10. import os
  11. import struct
  12. import subprocess
  13. import sys
  14. import tempfile
  15. def CheckFileType(file, expected):
  16. proc = subprocess.Popen(['lipo', '-info', file], stdout=subprocess.PIPE)
  17. o = proc.communicate()[0].strip()
  18. assert not proc.returncode
  19. if not expected in o:
  20. print 'File: Expected %s, got %s' % (expected, o)
  21. test.fail_test()
  22. def HasCerts():
  23. # Because the bots do not have certs, don't check them if there are no
  24. # certs available.
  25. proc = subprocess.Popen(['security','find-identity','-p', 'codesigning',
  26. '-v'], stdout=subprocess.PIPE)
  27. return "0 valid identities found" not in proc.communicate()[0].strip()
  28. def CheckSignature(file):
  29. proc = subprocess.Popen(['codesign', '-v', file], stdout=subprocess.PIPE)
  30. o = proc.communicate()[0].strip()
  31. assert not proc.returncode
  32. if "code object is not signed at all" in o:
  33. print 'File %s not properly signed.' % (file)
  34. test.fail_test()
  35. def CheckEntitlements(file, expected_entitlements):
  36. with tempfile.NamedTemporaryFile() as temp:
  37. proc = subprocess.Popen(['codesign', '--display', '--entitlements',
  38. temp.name, file], stdout=subprocess.PIPE)
  39. o = proc.communicate()[0].strip()
  40. assert not proc.returncode
  41. data = temp.read()
  42. entitlements = ParseEntitlements(data)
  43. if not entitlements:
  44. print 'No valid entitlements found in %s.' % (file)
  45. test.fail_test()
  46. if entitlements != expected_entitlements:
  47. print 'Unexpected entitlements found in %s.' % (file)
  48. test.fail_test()
  49. def ParseEntitlements(data):
  50. if len(data) < 8:
  51. return None
  52. magic, length = struct.unpack('>II', data[:8])
  53. if magic != 0xfade7171 or length != len(data):
  54. return None
  55. return data[8:]
  56. def GetProductVersion():
  57. args = ['xcodebuild','-version','-sdk','iphoneos','ProductVersion']
  58. job = subprocess.Popen(args, stdout=subprocess.PIPE)
  59. return job.communicate()[0].strip()
  60. def CheckPlistvalue(plist, key, expected):
  61. if key not in plist:
  62. print '%s not set in plist' % key
  63. test.fail_test()
  64. return
  65. actual = plist[key]
  66. if actual != expected:
  67. print 'File: Expected %s, got %s for %s' % (expected, actual, key)
  68. test.fail_test()
  69. def CheckPlistNotSet(plist, key):
  70. if key in plist:
  71. print '%s should not be set in plist' % key
  72. test.fail_test()
  73. return
  74. def ConvertBinaryPlistToXML(path):
  75. proc = subprocess.call(['plutil', '-convert', 'xml1', path],
  76. stdout=subprocess.PIPE)
  77. if sys.platform == 'darwin':
  78. test = TestGyp.TestGyp(formats=['ninja', 'xcode'])
  79. test.run_gyp('test-device.gyp', chdir='app-bundle')
  80. test_configs = ['Default-iphoneos', 'Default']
  81. # TODO(justincohen): Disabling 'Default-iphoneos' for xcode until bots are
  82. # configured with signing certs.
  83. if test.format == 'xcode':
  84. test_configs.remove('Default-iphoneos')
  85. for configuration in test_configs:
  86. test.set_configuration(configuration)
  87. test.build('test-device.gyp', 'test_app', chdir='app-bundle')
  88. result_file = test.built_file_path('Test App Gyp.bundle/Test App Gyp',
  89. chdir='app-bundle')
  90. test.must_exist(result_file)
  91. info_plist = test.built_file_path('Test App Gyp.bundle/Info.plist',
  92. chdir='app-bundle')
  93. # plistlib doesn't support binary plists, but that's what Xcode creates.
  94. if test.format == 'xcode':
  95. ConvertBinaryPlistToXML(info_plist)
  96. plist = plistlib.readPlist(info_plist)
  97. CheckPlistvalue(plist, 'UIDeviceFamily', [1, 2])
  98. if configuration == 'Default-iphoneos':
  99. CheckFileType(result_file, 'armv7')
  100. CheckPlistvalue(plist, 'DTPlatformVersion', GetProductVersion())
  101. CheckPlistvalue(plist, 'CFBundleSupportedPlatforms', ['iPhoneOS'])
  102. CheckPlistvalue(plist, 'DTPlatformName', 'iphoneos')
  103. else:
  104. CheckFileType(result_file, 'i386')
  105. CheckPlistNotSet(plist, 'DTPlatformVersion')
  106. CheckPlistvalue(plist, 'CFBundleSupportedPlatforms', ['iPhoneSimulator'])
  107. CheckPlistvalue(plist, 'DTPlatformName', 'iphonesimulator')
  108. if HasCerts() and configuration == 'Default-iphoneos':
  109. test.build('test-device.gyp', 'sig_test', chdir='app-bundle')
  110. result_file = test.built_file_path('sig_test.bundle/sig_test',
  111. chdir='app-bundle')
  112. CheckSignature(result_file)
  113. info_plist = test.built_file_path('sig_test.bundle/Info.plist',
  114. chdir='app-bundle')
  115. plist = plistlib.readPlist(info_plist)
  116. CheckPlistvalue(plist, 'UIDeviceFamily', [1])
  117. entitlements_file = test.built_file_path('sig_test.xcent',
  118. chdir='app-bundle')
  119. if os.path.isfile(entitlements_file):
  120. expected_entitlements = open(entitlements_file).read()
  121. CheckEntitlements(result_file, expected_entitlements)
  122. test.pass_test()