Home | History | Annotate | Download | only in native_client
      1 #! -*- python -*-
      2 # Copyright (c) 2012 The Native Client Authors. 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 import json
      7 import os
      8 import shutil
      9 import sys
     10 
     11 sys.path.append(Dir('#/tools').abspath)
     12 import command_tester
     13 import test_lib
     14 
     15 Import(['pre_base_env'])
     16 
     17 # Underlay things migrating to ppapi repo.
     18 Dir('#/..').addRepository(Dir('#/../ppapi'))
     19 
     20 # Append a list of files to another, filtering out the files that already exist.
     21 # Filtering helps migrate declarations between repos by preventing redundant
     22 # declarations from causing an error.
     23 def ExtendFileList(existing, additional):
     24   # Avoid quadratic behavior by using a set.
     25   combined = set()
     26   for file_name in existing + additional:
     27     if file_name in combined:
     28       print 'WARNING: two references to file %s in the build.' % file_name
     29     combined.add(file_name)
     30   return sorted(combined)
     31 
     32 
     33 ppapi_scons_files = {}
     34 ppapi_scons_files['trusted_scons_files'] = []
     35 ppapi_scons_files['untrusted_irt_scons_files'] = []
     36 
     37 ppapi_scons_files['nonvariant_test_scons_files'] = [
     38     'tests/breakpad_crash_test/nacl.scons',
     39     'tests/nacl_browser/browser_dynamic_library/nacl.scons',
     40     'tests/ppapi_test_lib/nacl.scons',
     41 ]
     42 
     43 ppapi_scons_files['irt_variant_test_scons_files'] = [
     44     # 'inbrowser_test_runner' must be in the irt_variant list
     45     # otherwise it will run no tests.
     46     'tests/nacl_browser/inbrowser_test_runner/nacl.scons',
     47     # Disabled by Brad Chen 4 Sep to try to green Chromium
     48     # nacl_integration tests
     49     #'tests/nacl_browser/fault_injection/nacl.scons',
     50 ]
     51 
     52 ppapi_scons_files['untrusted_scons_files'] = [
     53     'src/shared/ppapi/nacl.scons',
     54     'src/untrusted/irt_stub/nacl.scons',
     55     'src/untrusted/nacl_ppapi_util/nacl.scons',
     56 ]
     57 
     58 
     59 EXTRA_ENV = [
     60     'XAUTHORITY', 'HOME', 'DISPLAY', 'SSH_TTY', 'KRB5CCNAME',
     61     'CHROME_DEVEL_SANDBOX' ]
     62 
     63 def SetupBrowserEnv(env):
     64   for var_name in EXTRA_ENV:
     65     if var_name in os.environ:
     66       env['ENV'][var_name] = os.environ[var_name]
     67 
     68 pre_base_env.AddMethod(SetupBrowserEnv)
     69 
     70 
     71 def GetHeadlessPrefix(env):
     72   if env.Bit('browser_headless') and env.Bit('host_linux'):
     73     return ['xvfb-run', '--auto-servernum']
     74   else:
     75     # Mac and Windows do not seem to have an equivalent.
     76     return []
     77 
     78 pre_base_env.AddMethod(GetHeadlessPrefix)
     79 
     80 
     81 # A fake file to depend on if a path to Chrome is not specified.
     82 no_browser = pre_base_env.File('chrome_browser_path_not_specified')
     83 
     84 
     85 # SCons attempts to run a test that depends on "no_browser", detect this at
     86 # runtime and cause a build error.
     87 def NoBrowserError(target, source, env):
     88   print target, source, env
     89   print ("***\nYou need to specificy chrome_browser_path=... on the " +
     90          "command line to run these tests.\n***\n")
     91   return 1
     92 
     93 pre_base_env.Append(BUILDERS = {
     94     'NoBrowserError': Builder(action=NoBrowserError)
     95 })
     96 
     97 pre_base_env.NoBrowserError([no_browser], [])
     98 
     99 
    100 def ChromeBinary(env):
    101   if 'chrome_browser_path' in ARGUMENTS:
    102     return env.File(env.SConstructAbsPath(ARGUMENTS['chrome_browser_path']))
    103   else:
    104     return no_browser
    105 
    106 pre_base_env.AddMethod(ChromeBinary)
    107 
    108 
    109 def GetPPAPIPluginPath(env, allow_64bit_redirect=True):
    110   if 'force_ppapi_plugin' in ARGUMENTS:
    111     return env.SConstructAbsPath(ARGUMENTS['force_ppapi_plugin'])
    112   if env.Bit('mac'):
    113     fn = env.File('${STAGING_DIR}/ppNaClPlugin')
    114   else:
    115     fn = env.File('${STAGING_DIR}/${SHLIBPREFIX}ppNaClPlugin${SHLIBSUFFIX}')
    116   if allow_64bit_redirect and env.Bit('target_x86_64'):
    117     # On 64-bit Windows and on Mac, we need the 32-bit plugin because
    118     # the browser is 32-bit.
    119     # Unfortunately it is tricky to build the 32-bit plugin (and all the
    120     # libraries it needs) in a 64-bit build... so we'll assume it has already
    121     # been built in a previous invocation.
    122     # TODO(ncbray) better 32/64 builds.
    123     if env.Bit('windows'):
    124       fn = env.subst(fn).abspath.replace('-win-x86-64', '-win-x86-32')
    125     elif env.Bit('mac'):
    126       fn = env.subst(fn).abspath.replace('-mac-x86-64', '-mac-x86-32')
    127   return fn
    128 
    129 pre_base_env.AddMethod(GetPPAPIPluginPath)
    130 
    131 
    132 # runnable-ld.so log has following format:
    133 # lib_name => path_to_lib (0x....address)
    134 def ParseLibInfoInRunnableLdLog(line):
    135   pos = line.find(' => ')
    136   if pos < 0:
    137     return None
    138   lib_name = line[:pos].strip()
    139   lib_path = line[pos+4:]
    140   pos1 = lib_path.rfind(' (')
    141   if pos1 < 0:
    142     return None
    143   lib_path = lib_path[:pos1]
    144   return lib_name, lib_path
    145 
    146 
    147 # Expected name of the temporary .libs file which stores glibc library
    148 # dependencies in "lib_name => lib_info" format
    149 # (see ParseLibInfoInRunnableLdLog)
    150 def GlibcManifestLibsListFilename(manifest_base_name):
    151   return '${STAGING_DIR}/%s.libs' % manifest_base_name
    152 
    153 
    154 # Copy libs and manifest to the target directory.
    155 # source[0] is a manifest file
    156 # source[1] is a .libs file with a list of libs generated by runnable-ld.so
    157 def CopyLibsForExtensionCommand(target, source, env):
    158   source_manifest = str(source[0])
    159   target_manifest = str(target[0])
    160   shutil.copyfile(source_manifest, target_manifest)
    161   target_dir = os.path.dirname(target_manifest)
    162   libs_file = open(str(source[1]), 'r')
    163   for line in libs_file.readlines():
    164     lib_info = ParseLibInfoInRunnableLdLog(line)
    165     if lib_info:
    166       lib_name, lib_path = lib_info
    167       if lib_path == 'NaClMain':
    168         # This is a fake file name, which we cannot copy.
    169         continue
    170       shutil.copyfile(lib_path, os.path.join(target_dir, lib_name))
    171   shutil.copyfile(env.subst('${NACL_SDK_LIB}/runnable-ld.so'),
    172                   os.path.join(target_dir, 'runnable-ld.so'))
    173   libs_file.close()
    174 
    175 
    176 # Extensions are loaded from directory on disk and so all dynamic libraries
    177 # they use must be copied to extension directory. The option --extra_serving_dir
    178 # does not help us in this case.
    179 def CopyLibsForExtension(env, target_dir, manifest):
    180   if not env.Bit('nacl_glibc'):
    181     return env.Install(target_dir, manifest)
    182   manifest_base_name = os.path.basename(str(env.subst(manifest)))
    183   lib_list_node = env.File(GlibcManifestLibsListFilename(manifest_base_name))
    184   nmf_node = env.Command(
    185       target_dir + '/' + manifest_base_name,
    186       [manifest, lib_list_node],
    187       CopyLibsForExtensionCommand)
    188   return nmf_node
    189 
    190 pre_base_env.AddMethod(CopyLibsForExtension)
    191 
    192 
    193 
    194 def WhitelistLibsForExtensionCommand(target, source, env):
    195   # Load existing extension manifest.
    196   src_file = open(source[0].abspath, 'r')
    197   src_json = json.load(src_file)
    198   src_file.close()
    199 
    200   # Load existing 'web_accessible_resources' key.
    201   if 'web_accessible_resources' not in src_json:
    202     src_json['web_accessible_resources'] = []
    203   web_accessible = src_json['web_accessible_resources']
    204 
    205   # Load list of libraries, and add libraries to web_accessible list.
    206   libs_file = open(source[1].abspath, 'r')
    207   for line in libs_file.readlines():
    208     lib_info = ParseLibInfoInRunnableLdLog(line)
    209     if lib_info:
    210       web_accessible.append(lib_info[0])
    211   # Also add the dynamic loader, which won't be in the libs_file.
    212   web_accessible.append('runnable-ld.so')
    213   libs_file.close()
    214 
    215   # Write out the appended-to extension manifest.
    216   target_file = open(target[0].abspath, 'w')
    217   json.dump(src_json, target_file, sort_keys=True, indent=2)
    218   target_file.close()
    219 
    220 
    221 # Whitelist glibc shared libraries (if necessary), so that they are
    222 # 'web_accessible_resources'.  This allows the libraries hosted at the origin
    223 # chrome-extension://[PACKAGE ID]/
    224 # to be made available to webpages that use this NaCl extension,
    225 # which are in a different origin.
    226 # See: http://code.google.com/chrome/extensions/manifest.html
    227 def WhitelistLibsForExtension(env, target_dir, nmf, extension_manifest):
    228   if env.Bit('nacl_static_link'):
    229     # For static linking, assume the nexe and nmf files are already
    230     # whitelisted, so there is no need to add entries to the extension_manifest.
    231     return env.Install(target_dir, extension_manifest)
    232   nmf_base_name = os.path.basename(env.File(nmf).abspath)
    233   lib_list_node = env.File(GlibcManifestLibsListFilename(nmf_base_name))
    234   manifest_base_name = os.path.basename(env.File(extension_manifest).abspath)
    235   extension_manifest_node = env.Command(
    236       target_dir + '/' + manifest_base_name,
    237       [extension_manifest, lib_list_node],
    238       WhitelistLibsForExtensionCommand)
    239   return extension_manifest_node
    240 
    241 pre_base_env.AddMethod(WhitelistLibsForExtension)
    242 
    243 
    244 # Generate manifest from newlib manifest and the list of libs generated by
    245 # runnable-ld.so.
    246 def GenerateManifestFunc(target, source, env):
    247   # Open the original manifest and parse it.
    248   source_file = open(str(source[0]), 'r')
    249   obj = json.load(source_file)
    250   source_file.close()
    251   # Open the file with ldd-format list of NEEDED libs and parse it.
    252   libs_file = open(str(source[1]), 'r')
    253   lib_names = []
    254   arch = env.subst('${TARGET_FULLARCH}')
    255   for line in libs_file.readlines():
    256     lib_info = ParseLibInfoInRunnableLdLog(line)
    257     if lib_info:
    258       lib_name, _ = lib_info
    259       lib_names.append(lib_name)
    260   libs_file.close()
    261   # Inject the NEEDED libs into the manifest.
    262   if 'files' not in obj:
    263     obj['files'] = {}
    264   for lib_name in lib_names:
    265     obj['files'][lib_name] = {}
    266     obj['files'][lib_name][arch] = {}
    267     obj['files'][lib_name][arch]['url'] = lib_name
    268   # Put what used to be specified under 'program' into 'main.nexe'.
    269   obj['files']['main.nexe'] = {}
    270   for k, v in obj['program'].items():
    271     obj['files']['main.nexe'][k] = v.copy()
    272     v['url'] = 'runnable-ld.so'
    273   # Write the new manifest!
    274   target_file = open(str(target[0]), 'w')
    275   json.dump(obj, target_file, sort_keys=True, indent=2)
    276   target_file.close()
    277   return 0
    278 
    279 
    280 def GenerateManifestDynamicLink(env, dest_file, lib_list_file,
    281                                 manifest, exe_file):
    282   # Run sel_ldr on the nexe to trace the NEEDED libraries.
    283   lib_list_node = env.Command(
    284       lib_list_file,
    285       [env.GetSelLdr(),
    286        '${NACL_SDK_LIB}/runnable-ld.so',
    287        exe_file,
    288        '${SCONSTRUCT_DIR}/DEPS'],
    289       # We ignore the return code using '-' in order to build tests
    290       # where binaries do not validate.  This is a Scons feature.
    291       '-${SOURCES[0]} -a -E LD_TRACE_LOADED_OBJECTS=1 ${SOURCES[1]} '
    292       '--library-path ${NACL_SDK_LIB}:${LIB_DIR} ${SOURCES[2].posix} '
    293       '> ${TARGET}')
    294   return env.Command(dest_file,
    295                      [manifest, lib_list_node],
    296                      GenerateManifestFunc)[0]
    297 
    298 
    299 def GenerateSimpleManifestStaticLink(env, dest_file, exe_name):
    300   def Func(target, source, env):
    301     archs = ('x86-32', 'x86-64', 'arm')
    302     nmf_data = {'program': dict((arch, {'url': '%s_%s.nexe' % (exe_name, arch)})
    303                                 for arch in archs)}
    304     fh = open(target[0].abspath, 'w')
    305     json.dump(nmf_data, fh, sort_keys=True, indent=2)
    306     fh.close()
    307   node = env.Command(dest_file, [], Func)[0]
    308   # Scons does not track the dependency of dest_file on exe_name or on
    309   # the Python code above, so we should always recreate dest_file when
    310   # it is used.
    311   env.AlwaysBuild(node)
    312   return node
    313 
    314 
    315 def GenerateSimpleManifest(env, dest_file, exe_name):
    316   if env.Bit('nacl_static_link'):
    317     return GenerateSimpleManifestStaticLink(env, dest_file, exe_name)
    318   else:
    319     static_manifest = GenerateSimpleManifestStaticLink(
    320         env, '%s.static' % dest_file, exe_name)
    321     return GenerateManifestDynamicLink(
    322         env, dest_file, '%s.tmp_lib_list' % dest_file, static_manifest,
    323         '${STAGING_DIR}/%s.nexe' % env.ProgramNameForNmf(exe_name))
    324 
    325 pre_base_env.AddMethod(GenerateSimpleManifest)
    326 
    327 
    328 # Returns a pair (main program, is_portable), based on the program
    329 # specified in manifest file.
    330 def GetMainProgramFromManifest(env, manifest):
    331   obj = json.loads(env.File(manifest).get_contents())
    332   program_dict = obj['program']
    333   return program_dict[env.subst('${TARGET_FULLARCH}')]['url']
    334 
    335 
    336 # Returns scons node for generated manifest.
    337 def GeneratedManifestNode(env, manifest):
    338   manifest = env.subst(manifest)
    339   manifest_base_name = os.path.basename(manifest)
    340   main_program = GetMainProgramFromManifest(env, manifest)
    341   result = env.File('${STAGING_DIR}/' + manifest_base_name)
    342   # Always generate the manifest for nacl_glibc.
    343   # For nacl_glibc, generating the mapping of shared libraries is non-trivial.
    344   if not env.Bit('nacl_glibc'):
    345     env.Install('${STAGING_DIR}', manifest)
    346     return result
    347   return GenerateManifestDynamicLink(
    348       env, '${STAGING_DIR}/' + manifest_base_name,
    349       # Note that CopyLibsForExtension() and WhitelistLibsForExtension()
    350       # assume that it can find the library list file under this filename.
    351       GlibcManifestLibsListFilename(manifest_base_name),
    352       manifest,
    353       env.File('${STAGING_DIR}/' + os.path.basename(main_program)))
    354   return result
    355 
    356 
    357 # Compares output_file and golden_file.
    358 # If they are different, prints the difference and returns 1.
    359 # Otherwise, returns 0.
    360 def CheckGoldenFile(golden_file, output_file,
    361                     filter_regex, filter_inverse, filter_group_only):
    362   golden = open(golden_file).read()
    363   actual = open(output_file).read()
    364   if filter_regex is not None:
    365     actual = test_lib.RegexpFilterLines(
    366         filter_regex,
    367         filter_inverse,
    368         filter_group_only,
    369         actual)
    370   if command_tester.DifferentFromGolden(actual, golden, output_file):
    371     return 1
    372   return 0
    373 
    374 
    375 # Returns action that compares output_file and golden_file.
    376 # This action can be attached to the node with
    377 # env.AddPostAction(target, action)
    378 def GoldenFileCheckAction(env, output_file, golden_file,
    379                           filter_regex=None, filter_inverse=False,
    380                           filter_group_only=False):
    381   def ActionFunc(target, source, env):
    382     return CheckGoldenFile(env.subst(golden_file), env.subst(output_file),
    383                            filter_regex, filter_inverse, filter_group_only)
    384 
    385   return env.Action(ActionFunc)
    386 
    387 
    388 def PPAPIBrowserTester(env,
    389                        target,
    390                        url,
    391                        files,
    392                        nmfs=None,
    393                        # List of executable basenames to generate
    394                        # manifest files for.
    395                        nmf_names=(),
    396                        map_files=(),
    397                        extensions=(),
    398                        mime_types=(),
    399                        timeout=30,
    400                        log_verbosity=2,
    401                        args=[],
    402                        # list of key/value pairs that are passed to the test
    403                        test_args=(),
    404                        # list of "--flag=value" pairs (no spaces!)
    405                        browser_flags=None,
    406                        # redirect streams of NaCl program to files
    407                        nacl_exe_stdin=None,
    408                        nacl_exe_stdout=None,
    409                        nacl_exe_stderr=None,
    410                        python_tester_script=None,
    411                        **extra):
    412   if 'TRUSTED_ENV' not in env:
    413     return []
    414 
    415   # Handle issues with mutating any python default arg lists.
    416   if browser_flags is None:
    417     browser_flags = []
    418 
    419   # Lint the extra arguments that are being passed to the tester.
    420   special_args = ['--ppapi_plugin', '--sel_ldr', '--irt_library', '--file',
    421                   '--map_file', '--extension', '--mime_type', '--tool',
    422                   '--browser_flag', '--test_arg']
    423   for arg_name in special_args:
    424     if arg_name in args:
    425       raise Exception('%s: %r is a test argument provided by the SCons test'
    426                       ' wrapper, do not specify it as an additional argument' %
    427                       (target, arg_name))
    428 
    429   env = env.Clone()
    430   env.SetupBrowserEnv()
    431 
    432   if 'scale_timeout' in ARGUMENTS:
    433     timeout = timeout * int(ARGUMENTS['scale_timeout'])
    434 
    435   if python_tester_script is None:
    436     python_tester_script = env.File('${SCONSTRUCT_DIR}/tools/browser_tester'
    437                              '/browser_tester.py')
    438   command = env.GetHeadlessPrefix() + [
    439       '${PYTHON}', python_tester_script,
    440       '--browser_path', env.ChromeBinary(),
    441       '--url', url,
    442       # Fail if there is no response for X seconds.
    443       '--timeout', str(timeout)]
    444   for dep_file in files:
    445     command.extend(['--file', dep_file])
    446   for extension in extensions:
    447     command.extend(['--extension', extension])
    448   for dest_path, dep_file in map_files:
    449     command.extend(['--map_file', dest_path, dep_file])
    450   for file_ext, mime_type in mime_types:
    451     command.extend(['--mime_type', file_ext, mime_type])
    452   command.extend(['--serving_dir', '${NACL_SDK_LIB}'])
    453   command.extend(['--serving_dir', '${LIB_DIR}'])
    454   if 'browser_tester_bw' in ARGUMENTS:
    455     command.extend(['-b', ARGUMENTS['browser_tester_bw']])
    456   if not nmfs is None:
    457     for nmf_file in nmfs:
    458       generated_manifest = GeneratedManifestNode(env, nmf_file)
    459       # We need to add generated manifests to the list of default targets.
    460       # The manifests should be generated even if the tests are not run -
    461       # the manifests may be needed for manual testing.
    462       for group in env['COMPONENT_TEST_PROGRAM_GROUPS']:
    463         env.Alias(group, generated_manifest)
    464       # Generated manifests are served in the root of the HTTP server
    465       command.extend(['--file', generated_manifest])
    466   for nmf_name in nmf_names:
    467     tmp_manifest = '%s.tmp/%s.nmf' % (target, nmf_name)
    468     command.extend(['--map_file', '%s.nmf' % nmf_name,
    469                     env.GenerateSimpleManifest(tmp_manifest, nmf_name)])
    470   if 'browser_test_tool' in ARGUMENTS:
    471     command.extend(['--tool', ARGUMENTS['browser_test_tool']])
    472 
    473   # Suppress debugging information on the Chrome waterfall.
    474   if env.Bit('disable_flaky_tests') and '--debug' in args:
    475     args.remove('--debug')
    476 
    477   command.extend(args)
    478   for flag in browser_flags:
    479     if flag.find(' ') != -1:
    480       raise Exception('Spaces not allowed in browser_flags: '
    481                       'use --flag=value instead')
    482     command.extend(['--browser_flag', flag])
    483   for key, value in test_args:
    484     command.extend(['--test_arg', str(key), str(value)])
    485 
    486   # Set a given file to be the nexe's stdin.
    487   if nacl_exe_stdin is not None:
    488     command.extend(['--nacl_exe_stdin', env.subst(nacl_exe_stdin['file'])])
    489 
    490   post_actions = []
    491   side_effects = []
    492   # Set a given file to be the nexe's stdout or stderr.  The tester also
    493   # compares this output against a golden file.
    494   for stream, params in (
    495       ('stdout', nacl_exe_stdout),
    496       ('stderr', nacl_exe_stderr)):
    497     if params is None:
    498       continue
    499     stream_file = env.subst(params['file'])
    500     side_effects.append(stream_file)
    501     command.extend(['--nacl_exe_' + stream, stream_file])
    502     if 'golden' in params:
    503       golden_file = env.subst(params['golden'])
    504       filter_regex = params.get('filter_regex', None)
    505       filter_inverse = params.get('filter_inverse', False)
    506       filter_group_only = params.get('filter_group_only', False)
    507       post_actions.append(
    508           GoldenFileCheckAction(
    509               env, stream_file, golden_file,
    510               filter_regex, filter_inverse, filter_group_only))
    511 
    512   if env.ShouldUseVerboseOptions(extra):
    513     env.MakeVerboseExtraOptions(target, log_verbosity, extra)
    514   # Heuristic for when to capture output...
    515   capture_output = (extra.pop('capture_output', False)
    516                     or 'process_output_single' in extra)
    517   node = env.CommandTest(target,
    518                          command,
    519                          # Set to 'huge' so that the browser tester's timeout
    520                          # takes precedence over the default of the test_suite.
    521                          size='huge',
    522                          capture_output=capture_output,
    523                          **extra)
    524   for side_effect in side_effects:
    525     env.SideEffect(side_effect, node)
    526   # We can't check output if the test is not run.
    527   if not env.Bit('do_not_run_tests'):
    528     for action in post_actions:
    529       env.AddPostAction(node, action)
    530   return node
    531 
    532 pre_base_env.AddMethod(PPAPIBrowserTester)
    533 
    534 
    535 # Disabled for ARM and MIPS because Chrome binaries for ARM and MIPS are not
    536 # available.
    537 def PPAPIBrowserTesterIsBroken(env):
    538   return env.Bit('target_arm') or env.Bit('target_mips32')
    539 
    540 pre_base_env.AddMethod(PPAPIBrowserTesterIsBroken)
    541 
    542 # 3D is disabled everywhere
    543 def PPAPIGraphics3DIsBroken(env):
    544   return True
    545 
    546 pre_base_env.AddMethod(PPAPIGraphics3DIsBroken)
    547 
    548 
    549 def AddChromeFilesFromGroup(env, file_group):
    550   env['BUILD_SCONSCRIPTS'] = ExtendFileList(
    551       env.get('BUILD_SCONSCRIPTS', []),
    552       ppapi_scons_files[file_group])
    553 
    554 pre_base_env.AddMethod(AddChromeFilesFromGroup)
    555