<s> """<STR_LIT>""" <EOL> import cStringIO <EOL> import os <EOL> import tempfile <EOL> import unittest <EOL> import common_stub <EOL> from adb import fastboot <EOL> class FastbootTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . usb = common_stub . StubUsb ( ) <EOL> @ staticmethod <EOL> def _SumLengths ( items ) : <EOL> return sum ( len ( item ) for item in items ) <EOL> def ExpectDownload ( self , writes , succeed = True , accept_data = True ) : <EOL> self . usb . ExpectWrite ( '<STR_LIT>' % self . _SumLengths ( writes ) ) <EOL> if accept_data : <EOL> self . usb . ExpectRead ( '<STR_LIT>' % self . _SumLengths ( writes ) ) <EOL> else : <EOL> self . usb . ExpectRead ( '<STR_LIT>' % ( self . _SumLengths ( writes ) - <NUM_LIT:2> ) ) <EOL> for data in writes : <EOL> self . usb . ExpectWrite ( data ) <EOL> if succeed : <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> else : <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> def ExpectFlash ( self , partition , succeed = True ) : <EOL> self . usb . ExpectWrite ( '<STR_LIT>' % partition ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> if succeed : <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> else : <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> def testDownload ( self ) : <EOL> raw = '<STR_LIT>' <EOL> data = cStringIO . StringIO ( raw ) <EOL> self . ExpectDownload ( [ raw ] ) <EOL> commands = fastboot . FastbootCommands ( self . usb ) <EOL> response = commands . Download ( data ) <EOL> self . assertEqual ( '<STR_LIT>' , response ) <EOL> def testDownloadFail ( self ) : <EOL> raw = '<STR_LIT>' <EOL> data = cStringIO . StringIO ( raw ) <EOL> self . ExpectDownload ( [ raw ] , succeed = False ) <EOL> commands = fastboot . FastbootCommands ( self . usb ) <EOL> with self . assertRaises ( fastboot . FastbootRemoteFailure ) : <EOL> commands . Download ( data ) <EOL> data = cStringIO . StringIO ( raw ) <EOL> self . ExpectDownload ( [ raw ] , accept_data = False ) <EOL> with self . assertRaises ( fastboot . FastbootTransferError ) : <EOL> commands . Download ( data ) <EOL> def testFlash ( self ) : <EOL> partition = '<STR_LIT>' <EOL> self . ExpectFlash ( partition ) <EOL> commands = fastboot . FastbootCommands ( self . usb ) <EOL> output = cStringIO . StringIO ( ) <EOL> def InfoCb ( message ) : <EOL> if message . header == '<STR_LIT>' : <EOL> output . write ( message . message ) <EOL> response = commands . Flash ( partition , info_cb = InfoCb ) <EOL> self . assertEqual ( '<STR_LIT>' , response ) <EOL> self . assertEqual ( '<STR_LIT>' , output . getvalue ( ) ) <EOL> def testFlashFail ( self ) : <EOL> partition = '<STR_LIT>' <EOL> self . ExpectFlash ( partition , succeed = False ) <EOL> commands = fastboot . FastbootCommands ( self . usb ) <EOL> with self . assertRaises ( fastboot . FastbootRemoteFailure ) : <EOL> commands . Flash ( partition ) <EOL> def testFlashFromFile ( self ) : <EOL> partition = '<STR_LIT>' <EOL> raw = '<STR_LIT>' * <NUM_LIT> <EOL> tmp = tempfile . NamedTemporaryFile ( delete = False ) <EOL> tmp . write ( raw ) <EOL> tmp . close ( ) <EOL> progresses = [ ] <EOL> pieces = [ ] <EOL> chunk_size = fastboot . FastbootProtocol ( None ) . chunk_kb * <NUM_LIT> <EOL> while raw : <EOL> pieces . append ( raw [ : chunk_size ] ) <EOL> raw = raw [ chunk_size : ] <EOL> self . ExpectDownload ( pieces ) <EOL> self . ExpectFlash ( partition ) <EOL> cb = lambda progress , total : progresses . append ( ( progress , total ) ) <EOL> commands = fastboot . FastbootCommands ( self . usb ) <EOL> commands . FlashFromFile ( <EOL> partition , tmp . name , progress_callback = cb ) <EOL> self . assertEqual ( len ( pieces ) , len ( progresses ) ) <EOL> os . remove ( tmp . name ) <EOL> def testSimplerCommands ( self ) : <EOL> commands = fastboot . FastbootCommands ( self . usb ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> commands . Erase ( '<STR_LIT>' ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , commands . Getvar ( '<STR_LIT>' ) ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> commands . Continue ( ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> commands . Reboot ( ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> commands . RebootBootloader ( ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , commands . Oem ( '<STR_LIT>' ) ) <EOL> def testVariousFailures ( self ) : <EOL> commands = fastboot . FastbootCommands ( self . usb ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> with self . assertRaises ( fastboot . FastbootInvalidResponse ) : <EOL> commands . Continue ( ) <EOL> self . usb . ExpectWrite ( '<STR_LIT>' ) <EOL> self . usb . ExpectRead ( '<STR_LIT>' ) <EOL> with self . assertRaises ( fastboot . FastbootStateMismatch ) : <EOL> commands . Continue ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
