Ejemplo n.º 1
0
  def testReadFileObject(self):
    """Tests the ReadFileObject function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(
        debug=True, output_writer=output_writer)

    test_file_path = self._GetTestFilePath(['Cookies.binarycookies'])
    test_file.Open(test_file_path)
Ejemplo n.º 2
0
def Main():
    """The main program function.

  Returns:
    bool: True if successful or False if not.
  """
    argument_parser = argparse.ArgumentParser(
        description=('Extracts information from Safari Cookies files.'))

    argument_parser.add_argument('-d',
                                 '--debug',
                                 dest='debug',
                                 action='store_true',
                                 default=False,
                                 help='enable debug output.')

    argument_parser.add_argument(
        'source',
        nargs='?',
        action='store',
        metavar='PATH',
        default=None,
        help='path of the Cookies.binarycookies file.')

    options = argument_parser.parse_args()

    if not options.source:
        print('Source file missing.')
        print('')
        argument_parser.print_help()
        print('')
        return False

    logging.basicConfig(level=logging.INFO,
                        format='[%(levelname)s] %(message)s')

    output_writer = output_writers.StdoutWriter()

    try:
        output_writer.Open()
    except IOError as exception:
        print(
            'Unable to open output writer with error: {0!s}'.format(exception))
        print('')
        return False

    binary_cookies_file = safari_cookies.BinaryCookiesFile(
        debug=options.debug, output_writer=output_writer)
    binary_cookies_file.Open(options.source)

    output_writer.WriteText('Safari Cookies information:\n')
    # TODO: print cookies information.

    binary_cookies_file.Close()

    output_writer.Close()

    return True
Ejemplo n.º 3
0
  def testReadRecord(self):
    """Tests the _ReadRecord function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(output_writer=output_writer)

    test_file._ReadRecord(self._PAGE_DATA, 0)

    with self.assertRaises(errors.ParseError):
      test_file._ReadRecord(self._PAGE_DATA[:-1], 0)
Ejemplo n.º 4
0
  def testReadPages(self):
    """Tests the _ReadPages function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(output_writer=output_writer)

    test_file_path = self._GetTestFilePath(['Cookies.binarycookies'])
    with open(test_file_path, 'rb') as file_object:
      test_file._ReadFileHeader(file_object)
      test_file._ReadPages(file_object)
Ejemplo n.º 5
0
  def testReadFileFooter(self):
    """Tests the _ReadFileFooter function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(output_writer=output_writer)

    test_file_path = self._GetTestFilePath(['Cookies.binarycookies'])
    with open(test_file_path, 'rb') as file_object:
      file_object.seek(-8, os.SEEK_END)
      test_file._ReadFileFooter(file_object)
Ejemplo n.º 6
0
  def testReadCString(self):
    """Tests the _ReadCString function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(output_writer=output_writer)

    page_data = b'string\x00'

    cstring = test_file._ReadCString(page_data, 0)
    self.assertEqual(cstring, 'string')

    with self.assertRaises(errors.ParseError):
      test_file._ReadCString(page_data[:-1], 0)
Ejemplo n.º 7
0
  def testDebugPrintFileHeader(self):
    """Tests the _DebugPrintFileHeader function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(output_writer=output_writer)

    data_type_map = test_file._GetDataTypeMap('binarycookies_file_header')

    file_header = data_type_map.CreateStructureValues(
        number_of_pages=1,
        signature=b'cook')

    test_file._DebugPrintFileHeader(file_header)
Ejemplo n.º 8
0
  def testReadFileHeader(self):
    """Tests the _ReadFileHeader function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(output_writer=output_writer)

    file_object = io.BytesIO(self._FILE_HEADER_DATA)

    test_file._ReadFileHeader(file_object)

    file_object = io.BytesIO(self._FILE_HEADER_DATA_BAD_SIGNATURE)

    with self.assertRaises(errors.ParseError):
      test_file._ReadFileHeader(file_object)

    file_object = io.BytesIO(self._FILE_HEADER_DATA[:-1])

    with self.assertRaises(errors.ParseError):
      test_file._ReadFileHeader(file_object)
Ejemplo n.º 9
0
  def testDebugPrintRecordHeader(self):
    """Tests the _DebugPrintRecordHeader function."""
    output_writer = test_lib.TestOutputWriter()
    test_file = safari_cookies.BinaryCookiesFile(output_writer=output_writer)

    data_type_map = test_file._GetDataTypeMap('binarycookies_record_header')

    record_header = data_type_map.CreateStructureValues(
        creation_time=0,
        expiration_time=1,
        flags=2,
        name_offset=3,
        path_offset=4,
        size=5,
        unknown1=6,
        unknown2=7,
        unknown3=8,
        url_offset=9,
        value_offset=10)

    test_file._DebugPrintRecordHeader(record_header)