Exemple #1
0
  def __init__(self, testcase_url):
    testcase_url_parts = parse.urlparse(testcase_url)
    config_url = testcase_url_parts._replace(
        path=REPRODUCE_TOOL_CONFIG_HANDLER).geturl()
    response, content = http_utils.request(config_url, body={})
    if response.status != 200:
      raise errors.ReproduceToolUnrecoverableError('Failed to access server.')

    self._config = json_utils.loads(content)
  def test_multiple_auth_failures(self):
    """Ensure that we don't recurse indefinitely if auth fails persistently."""
    http = FakeHttp([(FakeResponse(401), {}), (FakeResponse(401), {})])
    self.mock.Http.return_value = http
    response, _ = http_utils.request('https://url/', configuration=self.config)

    # Ensure that all expected requests were made.
    self.assertEqual(http.replies, [])

    self.assertEqual(response.status, 401)
Exemple #3
0
    def test_unauthenticated_request(self):
        """Ensure that we can make an unauthenticated request."""
        http = FakeHttp([(FakeResponse(200), {})])
        self.mock.Http.return_value = http
        response, _ = http_utils.request('https://url/', body='test body')

        # Ensure that all expected requests were made.
        self.assertEqual(http.replies, [])

        self.assertEqual(http.last_body, '"test body"')
        self.assertEqual(http.last_headers, {})
        self.assertEqual(response.status, 200)
Exemple #4
0
def _get_testcase(testcase_id, configuration):
    """Retrieve the json representation of the test case with the given id."""
    response, content = http_utils.request(
        configuration.get('testcase_info_url'),
        body={'testcaseId': testcase_id},
        configuration=configuration)

    if response.status != 200:
        raise errors.ReproduceToolUnrecoverableError(
            'Unable to fetch test case information.')

    testcase_map = json_utils.loads(content)
    return SerializedTestcase(testcase_map)
  def test_authentication(self):
    """Ensure that we can authenticate properly if needed."""
    http = FakeHttp([(FakeResponse(401), {}), (FakeResponse(
        200, include_auth_header=True), {})])
    self.mock.Http.return_value = http
    response, _ = http_utils.request('https://url/', configuration=self.config)

    # Ensure that all expected requests were made.
    self.assertEqual(http.replies, [])

    self.mock.write_data_to_file.assert_called_once_with(
        'fake auth token', http_utils.AUTHORIZATION_CACHE_FILE)
    self.assertEqual(response.status, 200)
Exemple #6
0
def _download_testcase(testcase_id, testcase, configuration):
  """Download the test case and return its path."""
  print('Downloading testcase...')
  testcase_download_url = '{url}?id={id}'.format(
      url=configuration.get('testcase_download_url'), id=testcase_id)
  response, content = http_utils.request(
      testcase_download_url,
      method=http_utils.GET_METHOD,
      configuration=configuration)

  if response.status != 200:
    raise errors.ReproduceToolUnrecoverableError(
        'Unable to download test case.')

  bot_absolute_filename = response['x-goog-meta-filename']
  # Store the test case in the config directory for debuggability.
  testcase_directory = os.path.join(CONFIG_DIRECTORY, 'current-testcase')
  shell.remove_directory(testcase_directory, recreate=True)
  environment.set_value('FUZZ_INPUTS', testcase_directory)
  testcase_path = os.path.join(testcase_directory,
                               os.path.basename(bot_absolute_filename))

  utils.write_data_to_file(content, testcase_path)

  # Unpack the test case if it's archived.
  # TODO(mbarbella): Rewrite setup.unpack_testcase and share this code.
  if testcase.minimized_keys and testcase.minimized_keys != 'NA':
    mask = data_types.ArchiveStatus.MINIMIZED
  else:
    mask = data_types.ArchiveStatus.FUZZED

  if testcase.archive_state & mask:
    archive.unpack(testcase_path, testcase_directory)
    file_list = archive.get_file_list(testcase_path)

    testcase_path = None
    for file_name in file_list:
      if os.path.basename(file_name) == os.path.basename(
          testcase.absolute_path):
        testcase_path = os.path.join(testcase_directory, file_name)
        break

    if not testcase_path:
      raise errors.ReproduceToolUnrecoverableError(
          'Test case file was not found in archive.\n'
          'Original filename: {absolute_path}.\n'
          'Archive contents: {file_list}'.format(
              absolute_path=testcase.absolute_path, file_list=file_list))

  return testcase_path
  def test_authenticated_request(self):
    """Ensure that we reuse credentials if we've previously authenticated."""
    http = FakeHttp([(FakeResponse(200), {})])
    self.mock.Http.return_value = http
    self.mock.read_data_from_file.return_value = 'cached auth token'
    response, _ = http_utils.request('https://url/', configuration=self.config)

    # Ensure that all expected requests were made.
    self.assertEqual(http.replies, [])

    self.assertEqual(http.last_headers, {
        'Authorization': 'cached auth token',
        'User-Agent': 'clusterfuzz-reproduce'
    })
    self.assertEqual(response.status, 200)