def testImageCorrupted(self):
    """Verify error when firmware extraction corrupts SSD image.

    The primary motivator is potentially calling mount_gpt_image.sh improperly.
    """
    cb_command_lib.CheckEnvironment(self.image_name,
                                    self.firmware_dest,
                                    self.mount_point).AndReturn(True)
    cb_command_lib.RunCommand(mox.IsA(list))
    os.path.exists(self.mount_point).AndReturn(True)
    os.listdir(self.mount_point).AndReturn(['stuff', 'is', 'here'])
    cb_command_lib.ListFirmware(
        mox.IsA(str), mox.IsA(str), self.board).AndReturn(
            dict(bios='_ignore', ec='_ignore', ec2='_ignore'))
    cb_command_lib.ExtractFiles(mox.IsA(str)).AndReturn('/tmp/firmware_dir')
    for i in range(3):  # Test for alex only
      os.path.exists(mox.IsA(str)).AndReturn(True)
      shutil.copy(mox.IsA(str), mox.IsA(str))
    shutil.copy(mox.IsA(str), mox.IsA(str))
    cb_command_lib.RunCommand(mox.IsA(list))
    cb_command_lib.CheckMd5(mox.IsA(str), mox.IsA(str)).AndReturn(False)
    self.mox.ReplayAll()
    self.assertRaises(cb_constants.BundlingError,
                      cb_command_lib.ExtractFirmware,
                      self.image_name,
                      self.firmware_dest,
                      self.mount_point,
                      self.board)
 def testMountSsdFailsMountPointNotThere(self):
   """Verify error when SSD image is not mounted."""
   cb_command_lib.CheckEnvironment(self.image_name,
                                   self.firmware_dest,
                                   self.mount_point).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list))
   os.path.exists(self.mount_point).AndReturn(False)
   cb_command_lib.RunCommand(mox.IsA(list))
   self.mox.ReplayAll()
   self.assertRaises(cb_constants.BundlingError,
                     cb_command_lib.ExtractFirmware,
                     self.image_name,
                     self.firmware_dest,
                     self.mount_point,
                     self.board)
 def testConvertRecoveryToSsdSuccess(self):
   """Verify return value when recovery to full ssd conversion succeeds."""
   cb_command_lib.HandleGitExists(self.force)
   cb_command_lib.RunCommand(mox.IsA(list))
   cb_command_lib.ResolveRecoveryUrl(
       self.board, self.recovery, alt_naming=0).AndReturn(
           (self.rec_url, self.index_page))
   cb_command_lib.DetermineUrl(self.index_page,
                           mox.IsA(list)).AndReturn(self.zip_url)
   cb_command_lib.Download(self.zip_url).AndReturn(True)
   cb_command_lib.HandleSsdExists(self.ssd_name, self.force)
   cb_command_lib.InstallCgpt(self.index_page, self.force)
   cb_command_lib.RunCommand(mox.IsA(list))
   self.mox.ReplayAll()
   actual = cb_command_lib.ConvertRecoveryToSsd(self.image_name, self)
   self.assertEqual(self.ssd_name, actual)
 def testEnvironmentIsGood(self):
   """Verify return value when environment is all good."""
   os.getcwd().AndReturn('/home/$USER/chromiumos/src/scripts')
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   self.mox.ReplayAll()
   self.assertTrue(cb_command_lib.CheckEnvironment(self.image_name,
                                                   self.firmware_dest,
                                                   self.mount_point))
 def testNotInScriptsDirectory(self):
   """Verify return value when script not run from <cros_root/src/scripts."""
   os.getcwd().AndReturn('/home/$USER/chromiumos/src/scripts/lib')
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   self.mox.ReplayAll()
   self.assertFalse(cb_command_lib.CheckEnvironment(self.image_name,
                                                    self.firmware_dest,
                                                    self.mount_point))
 def testExtractFilesSuccess(self):
   """Verify return value when all goes well."""
   self.mock_cres.output = 'Files extracted to ' + self.fw_dir
   os.path.exists(self.cros_fw).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   os.path.exists(self.fw_dir).AndReturn(True)
   self.mox.ReplayAll()
   self.assertEqual(self.fw_dir, cb_command_lib.ExtractFiles(self.cros_fw))
 def testRecoveryNameNotResolved(self):
   """Verify error when recovery image url cannot be determined."""
   cb_command_lib.HandleSsdExists(self.ssd_name, self.force)
   cb_command_lib.HandleGitExists(self.force)
   cb_command_lib.RunCommand(mox.IsA(list))
   cb_command_lib.ResolveRecoveryUrl(
       self.board, self.recovery, alt_naming=0).AndReturn(
           (None, None))
   _AssertConvertRecoveryError(self)
 def testFileExists(self):
   """Verify call sequence when file to upload exists."""
   filename = 'fakefilename'
   self.mox.StubOutWithMock(os.path, 'exists')
   self.mox.StubOutWithMock(cb_command_lib, 'RunCommand')
   os.path.exists(mox.IsA(str)).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list))
   self.mox.ReplayAll()
   cb_command_lib.UploadToGsd(filename)
 def testNoUudecode(self):
   """Verify return value when uudecode not present."""
   os.getcwd().AndReturn('/home/$USER/chromiumos/src/scripts')
   self.mock_cres.output = ''
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   self.mox.ReplayAll()
   self.assertFalse(cb_command_lib.CheckEnvironment(self.image_name,
                                                    self.firmware_dest,
                                                    self.mount_point))
 def testBadSsdName(self):
   """Verify return value when given ssd image has bad name."""
   self.image = tempfile.NamedTemporaryFile(suffix='recovery.bin')
   os.getcwd().AndReturn('/home/$USER/chromiumos/src/scripts')
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   self.mox.ReplayAll()
   self.assertFalse(cb_command_lib.CheckEnvironment(self.image_name,
                                                    self.firmware_dest,
                                                    self.mount_point))
 def testCannotDetermineBaseImageZipUrl(self):
   """Verify error when name of zip with base image cannot be determined."""
   cb_command_lib.HandleSsdExists(self.ssd_name, self.force)
   cb_command_lib.HandleGitExists(self.force)
   cb_command_lib.RunCommand(mox.IsA(list))
   cb_command_lib.ResolveRecoveryUrl(
       self.board, self.recovery, alt_naming=0).AndReturn(
           (self.rec_url, self.index_page))
   cb_command_lib.DetermineUrl(self.index_page, mox.IsA(list)).AndReturn(None)
   _AssertConvertRecoveryError(self)
 def testNoMountPoint(self):
   """Verify return value when mount point not given or does not exist."""
   self.mount_point = ''
   os.getcwd().AndReturn('/home/$USER/chromiumos/src/scripts')
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   self.mox.ReplayAll()
   self.assertFalse(cb_command_lib.CheckEnvironment(self.image_name,
                                                    self.firmware_dest,
                                                    self.mount_point))
 def testTmpDirectoryDoesNotExist(self):
   """Verify return value when extractor fails."""
   self.mock_cres.output = 'Lying that files were extracted to ' + self.fw_dir
   os.path.exists(self.cros_fw).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   os.path.exists(self.fw_dir).AndReturn(False)
   self.mox.ReplayAll()
   expected = None
   actual = cb_command_lib.ExtractFiles(self.cros_fw)
   self.assertEqual(expected, actual)
 def testMountPointNotEmpty(self):
   """Verify return value when provided mount point is not empty."""
   self.mox.StubOutWithMock(os, 'listdir')
   os.getcwd().AndReturn('/home/$USER/chromiumos/src/scripts')
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   os.listdir(self.mount_point).AndReturn(['there_is_a_file'])
   self.mox.ReplayAll()
   self.assertFalse(cb_command_lib.CheckEnvironment(self.image_name,
                                                    self.firmware_dest,
                                                    self.mount_point))
 def testFirmwareDestinationNotWritable(self):
   """Verify return value when firmware destination directory not writable."""
   self.mox.StubOutWithMock(os, 'access')
   os.getcwd().AndReturn('/home/$USER/chromiumos/src/scripts')
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   os.access(self.firmware_dest, os.W_OK).AndReturn(False)
   self.mox.ReplayAll()
   self.assertFalse(cb_command_lib.CheckEnvironment(self.image_name,
                                                    self.firmware_dest,
                                                    self.mount_point))
 def testFirmwareExtractionFails(self):
   """Verify error when firmware extraction fails."""
   cb_command_lib.CheckEnvironment(self.image_name,
                                   self.firmware_dest,
                                   self.mount_point).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list))
   os.path.exists(self.mount_point).AndReturn(True)
   os.listdir(self.mount_point).AndReturn(['stuff', 'is', 'here'])
   cb_command_lib.ListFirmware(
       mox.IsA(str), mox.IsA(str), self.board).AndReturn(
           ('_ignore', '_ignore', '_ignore'))
   cb_command_lib.ExtractFiles(mox.IsA(str))
   cb_command_lib.RunCommand(mox.IsA(list))
   self.mox.ReplayAll()
   self.assertRaises(cb_constants.BundlingError,
                     cb_command_lib.ExtractFirmware,
                     self.image_name,
                     self.firmware_dest,
                     self.mount_point,
                     self.board)
 def testBaseImageZipDownloadFails(self):
   """Verify error when zip containing base image is not downloaded."""
   cb_command_lib.HandleSsdExists(self.ssd_name, self.force)
   cb_command_lib.HandleGitExists(self.force)
   cb_command_lib.RunCommand(mox.IsA(list))
   cb_command_lib.ResolveRecoveryUrl(
       self.board, self.recovery, alt_naming=0).AndReturn(
           (self.rec_url, self.index_page))
   cb_command_lib.DetermineUrl(self.index_page, mox.IsA(list)).AndReturn(
       self.zip_url)
   cb_command_lib.Download(self.zip_url).AndReturn(False)
   _AssertConvertRecoveryError(self)
 def _VerifySuccess(self, fake_output, expected):
   self.mock_cres.output = fake_output
   self.mox.StubOutWithMock(os.path, 'exists')
   self.mox.StubOutWithMock(cb_command_lib, 'RunCommand')
   os.path.exists(mox.IsA(str)).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   self.mox.ReplayAll()
   actual = cb_command_lib.ListFirmware(self.image_name,
                                        self.cros_fw,
                                        self.board)
   self.assertEqual(expected, actual)
  def testTmpDirectoryNotNamed(self):
    """Verify return value when extractor fails to tell where it extracted.

    This could be due to extraction script failing or changing output format.
    """
    self.fw_dir = ''
    self.mock_cres.output = 'Not listing tmp results directory'
    os.path.exists(self.cros_fw).AndReturn(True)
    cb_command_lib.RunCommand(mox.IsA(list),
                              redirect_stdout=True).AndReturn(self.mock_cres)
    self.mox.ReplayAll()
    expected = None
    actual = cb_command_lib.ExtractFiles(self.cros_fw)
    self.assertEqual(expected, actual)
 def testInstallCgptFails(self):
   """Verify error when installing cgpt utility fails."""
   cb_command_lib.HandleGitExists(self.force)
   cb_command_lib.RunCommand(mox.IsA(list))
   cb_command_lib.ResolveRecoveryUrl(
       self.board, self.recovery, alt_naming=0).AndReturn(
           (self.rec_url, self.index_page))
   cb_command_lib.DetermineUrl(self.index_page, mox.IsA(list)).AndReturn(
       self.zip_url)
   cb_command_lib.Download(self.zip_url).AndReturn(True)
   cb_command_lib.HandleSsdExists(self.ssd_name, self.force)
   cb_command_lib.InstallCgpt(self.index_page, self.force).AndRaise(
       cb_constants.BundlingError(''))
   _AssertConvertRecoveryError(self)
 def testExtractFirmwareSuccess(self):
   """Verify behavior of quiet success when all goes well."""
   cb_command_lib.CheckEnvironment(self.image_name,
                                   self.firmware_dest,
                                   self.mount_point).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list))
   os.path.exists(self.mount_point).AndReturn(True)
   os.listdir(self.mount_point).AndReturn(['stuff', 'is', 'here'])
   cb_command_lib.ListFirmware(
       mox.IsA(str), mox.IsA(str), self.board).AndReturn(
           dict(ec='ec_name', ec2='ec2_name', bios='bios_name'))
   cb_command_lib.ExtractFiles(mox.IsA(str)).AndReturn('/tmp/firmware_dir')
   for i in range(3):  # Test for alex only
     os.path.exists(mox.IsA(str)).AndReturn(True)
     shutil.copy(mox.IsA(str), mox.IsA(str))
   shutil.copy(mox.IsA(str), mox.IsA(str))
   cb_command_lib.RunCommand(mox.IsA(list))
   cb_command_lib.CheckMd5(mox.IsA(str), mox.IsA(str)).AndReturn(True)
   self.mox.ReplayAll()
   cb_command_lib.ExtractFirmware(self.image_name,
                                  self.firmware_dest,
                                  self.mount_point,
                                  self.board)
 def testAlexRenamingFails(self):
   """Verify graceful behavior when specific naming info not provided."""
   self.mock_cres.output = '\n'.join(['./' + self.alex_ec,
                                      './' + self.alex_ec2,
                                      './' + self.alex_bios])
   self.mox.StubOutWithMock(os.path, 'exists')
   self.mox.StubOutWithMock(cb_command_lib, 'RunCommand')
   os.path.exists(mox.IsA(str)).AndReturn(True)
   cb_command_lib.RunCommand(mox.IsA(list),
                             redirect_stdout=True).AndReturn(self.mock_cres)
   self.mox.ReplayAll()
   expected = dict(ec=self.alex_ec, ec2=self.alex_ec2, bios=self.alex_bios)
   actual = cb_command_lib.ListFirmware(self.image_name,
                                        self.cros_fw,
                                        self.board)
   self.assertEqual(expected, actual)
def _AssertFirmwareError(obj):
  """Common logic to assert an error while listing firmware.

  Args:
    obj: an instance of mox.MoxTestBase pertaining to ListFirmware
  """
  obj.mox.StubOutWithMock(os.path, 'exists')
  obj.mox.StubOutWithMock(cb_command_lib, 'RunCommand')
  os.path.exists(mox.IsA(str)).AndReturn(True)
  cb_command_lib.RunCommand(mox.IsA(list),
                            redirect_stdout=True).AndReturn(obj.mock_cres)
  obj.mox.ReplayAll()
  obj.assertRaises(cb_constants.BundlingError,
                   cb_command_lib.ListFirmware,
                   obj.image_name,
                   obj.cros_fw,
                   obj.board)