grr/server/grr_response_server/flows/general/transfer_test.py
def testMultiGetFileOfSpecialFiles(self):
    """Test that special /proc/ files are handled correctly.

    /proc/ files have the property that they are non seekable from their end
    (i.e. seeking them relative to the end is not supported). They also return
    an st_size of 0. For example:

    $ stat /proc/self/maps
    File: '/proc/self/maps'
    Size: 0   Blocks: 0   IO Block: 1024 regular empty file

    $ head /proc/self/maps
    00400000-00409000 r-xp 00000000 fc:01 9180740 /usr/bin/head
    00608000-00609000 r--p 00008000 fc:01 9180740 /usr/bin/head
    ...

    When we try to use the MultiGetFile flow, it deduplicates the files and
    since it thinks the file has a zero size, the flow will not download the
    file, and instead copy the zero size file into it.
    """
    client_mock = action_mocks.MultiGetFileClientMock()

    # # Create a zero sized file.
    zero_sized_filename = os.path.join(self.temp_dir, "zero_size")
    with open(zero_sized_filename, "wb") as fd:
      pass

    pathspec = rdf_paths.PathSpec(
        pathtype=rdf_paths.PathSpec.PathType.OS, path=zero_sized_filename)

    flow_test_lib.TestFlowHelper(
        transfer.MultiGetFile.__name__,
        client_mock,
        token=self.token,
        file_size="1MiB",
        client_id=self.client_id,
        pathspecs=[pathspec])

    # Now if we try to fetch a real /proc/ filename this will fail because the
    # filestore already contains the zero length file
    # aff4:/files/nsrl/da39a3ee5e6b4b0d3255bfef95601890afd80709.
    pathspec = rdf_paths.PathSpec(
        pathtype=rdf_paths.PathSpec.PathType.OS, path="/proc/self/environ")

    flow_test_lib.TestFlowHelper(
        transfer.MultiGetFile.__name__,
        client_mock,
        token=self.token,
        file_size=1024 * 1024,
        client_id=self.client_id,
        pathspecs=[pathspec])

    data = open(pathspec.last.path, "rb").read()

    if data_store.RelationalDBReadEnabled(category="filestore"):
      cp = db.ClientPath.FromPathSpec(self.client_id.Basename(), pathspec)
      fd_rel_db = file_store.OpenFile(cp)
      self.assertEqual(fd_rel_db.size, len(data))
      self.assertMultiLineEqual(fd_rel_db.read(), data)

      # Check that SHA256 hash of the file matches the contents
      # hash and that MD5 and SHA1 are set.
      history = data_store.REL_DB.ReadPathInfoHistory(
          cp.client_id, cp.path_type, cp.components)
      self.assertEqual(history[-1].hash_entry.sha256,
                       fd_rel_db.hash_id.AsBytes())
      self.assertIsNotNone(history[-1].hash_entry.sha1)
      self.assertIsNotNone(history[-1].hash_entry.md5)
    else:
      # Test the AFF4 file that was created - it should be empty since by
      # default we judge the file size based on its stat.st_size.
      urn = pathspec.AFF4Path(self.client_id)
      fd = aff4.FACTORY.Open(urn, token=self.token)
      self.assertEqual(fd.size, len(data))
      self.assertMultiLineEqual(fd.read(len(data)), data)
Test that special /proc/ files are handled correctly.

    /proc/ files have the property that they are non seekable from their end
    (i.e. seeking them relative to the end is not supported). They also return
    an st_size of 0. For example:

    $ stat /proc/self/maps
    File: '/proc/self/maps'
    Size: 0   Blocks: 0   IO Block: 1024 regular empty file

    $ head /proc/self/maps
    00400000-00409000 r-xp 00000000 fc:01 9180740 /usr/bin/head
    00608000-00609000 r--p 00008000 fc:01 9180740 /usr/bin/head
    ...

    When we try to use the MultiGetFile flow, it deduplicates the files and
    since it thinks the file has a zero size, the flow will not download the
    file, and instead copy the zero size file into it.
    
Test that special /proc/ files are handled correctly.
proc/ files have the property that they are non seekable from their end
. They also return
an st_size of 0. For example.





When we try to use the MultiGetFile flow, it deduplicates the files and
since it thinks the file has a zero size, the flow will not download the
file, and instead copy the zero size file into it.
Test that special / proc / files are handled correctly . proc / files have the property that they are non seekable from their end . They also return an st_size of 0 . For example . When we try to use the MultiGetFile flow it deduplicates the files and since it thinks the file has a zero size the flow will not download the file and instead copy the zero size file into it .
def testMultiGetFileOfSpecialFiles(self):
    client_mock = action_mocks.MultiGetFileClientMock()
    zero_sized_filename = os.path.join(self.temp_dir, "zero_size")
    with open(zero_sized_filename, "wb") as fd:
      pass
    pathspec = rdf_paths.PathSpec(
        pathtype=rdf_paths.PathSpec.PathType.OS, path=zero_sized_filename)
    flow_test_lib.TestFlowHelper(
        transfer.MultiGetFile.__name__,
        client_mock,
        token=self.token,
        file_size="1MiB",
        client_id=self.client_id,
        pathspecs=[pathspec])
    pathspec = rdf_paths.PathSpec(
        pathtype=rdf_paths.PathSpec.PathType.OS, path="/proc/self/environ")
    flow_test_lib.TestFlowHelper(
        transfer.MultiGetFile.__name__,
        client_mock,
        token=self.token,
        file_size=1024 * 1024,
        client_id=self.client_id,
        pathspecs=[pathspec])
    data = open(pathspec.last.path, "rb").read()
    if data_store.RelationalDBReadEnabled(category="filestore"):
      cp = db.ClientPath.FromPathSpec(self.client_id.Basename(), pathspec)
      fd_rel_db = file_store.OpenFile(cp)
      self.assertEqual(fd_rel_db.size, len(data))
      self.assertMultiLineEqual(fd_rel_db.read(), data)
      history = data_store.REL_DB.ReadPathInfoHistory(
          cp.client_id, cp.path_type, cp.components)
      self.assertEqual(history[-1].hash_entry.sha256,
                       fd_rel_db.hash_id.AsBytes())
      self.assertIsNotNone(history[-1].hash_entry.sha1)
      self.assertIsNotNone(history[-1].hash_entry.md5)
    else:
      urn = pathspec.AFF4Path(self.client_id)
      fd = aff4.FACTORY.Open(urn, token=self.token)
      self.assertEqual(fd.size, len(data))
      self.assertMultiLineEqual(fd.read(len(data)), data)
def testMultiGetFileOfSpecialFiles ( self ) : client_mock = action_mocks . MultiGetFileClientMock ( ) zero_sized_filename = os . path . join ( self . temp_dir , "zero_size" ) with open ( zero_sized_filename , "wb" ) as fd : pass pathspec = rdf_paths . PathSpec ( pathtype = rdf_paths . PathSpec . PathType . OS , path = zero_sized_filename ) flow_test_lib . TestFlowHelper ( transfer . MultiGetFile . __name__ , client_mock , token = self . token , file_size = "1MiB" , client_id = self . client_id , pathspecs = [ pathspec ] ) pathspec = rdf_paths . PathSpec ( pathtype = rdf_paths . PathSpec . PathType . OS , path = "/proc/self/environ" ) flow_test_lib . TestFlowHelper ( transfer . MultiGetFile . __name__ , client_mock , token = self . token , file_size = 1024 * 1024 , client_id = self . client_id , pathspecs = [ pathspec ] ) data = open ( pathspec . last . path , "rb" ) . read ( ) if data_store . RelationalDBReadEnabled ( category = "filestore" ) : cp = db . ClientPath . FromPathSpec ( self . client_id . Basename ( ) , pathspec ) fd_rel_db = file_store . OpenFile ( cp ) self . assertEqual ( fd_rel_db . size , len ( data ) ) self . assertMultiLineEqual ( fd_rel_db . read ( ) , data ) history = data_store . REL_DB . ReadPathInfoHistory ( cp . client_id , cp . path_type , cp . components ) self . assertEqual ( history [ - 1 ] . hash_entry . sha256 , fd_rel_db . hash_id . AsBytes ( ) ) self . assertIsNotNone ( history [ - 1 ] . hash_entry . sha1 ) self . assertIsNotNone ( history [ - 1 ] . hash_entry . md5 ) else : urn = pathspec . AFF4Path ( self . client_id ) fd = aff4 . FACTORY . Open ( urn , token = self . token ) self . assertEqual ( fd . size , len ( data ) ) self . assertMultiLineEqual ( fd . read ( len ( data ) ) , data )
Test that special /proc/ files are handled correctly.
Test that special / proc / files are handled correctly .
"""Test that special /proc/ files are handled correctly.

    /proc/ files have the property that they are non seekable from their end
    (i.e. seeking them relative to the end is not supported). They also return
    an st_size of 0. For example:

    $ stat /proc/self/maps
    File: '/proc/self/maps'
    Size: 0   Blocks: 0   IO Block: 1024 regular empty file

    $ head /proc/self/maps
    00400000-00409000 r-xp 00000000 fc:01 9180740 /usr/bin/head
    00608000-00609000 r--p 00008000 fc:01 9180740 /usr/bin/head
    ...

    When we try to use the MultiGetFile flow, it deduplicates the files and
    since it thinks the file has a zero size, the flow will not download the
    file, and instead copy the zero size file into it.
    """ # # Create a zero sized file. # Now if we try to fetch a real /proc/ filename this will fail because the # filestore already contains the zero length file # aff4:/files/nsrl/da39a3ee5e6b4b0d3255bfef95601890afd80709. # Check that SHA256 hash of the file matches the contents # hash and that MD5 and SHA1 are set. # Test the AFF4 file that was created - it should be empty since by # default we judge the file size based on its stat.st_size.
self