flash: fix encoding handling of files/programs

Make sure we use bytes when reading binary image data, and UTF-8
encoding when parsing simple command output as strings.

BUG=chromium:997354
TEST=`./run_tests` passes

Change-Id: I679d06dbabef76962c2d175aa64aeecb8e24da80
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1932720
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/cli/flash_unittest.py b/cli/flash_unittest.py
index 2798d01..bc511b7 100644
--- a/cli/flash_unittest.py
+++ b/cli/flash_unittest.py
@@ -257,7 +257,8 @@
 
     # Check that flash.UsbImagerOperation.Run() is called correctly.
     run_mock.assert_called_with(cros_build_lib.sudo_run, expected_cmd,
-                                debug_level=logging.NOTICE, update_period=0.5)
+                                debug_level=logging.NOTICE, encoding='utf-8',
+                                update_period=0.5)
 
   def testSudoRunCommandCalled(self):
     """Test that sudo_run is called when log level > NOTICE."""
@@ -340,10 +341,10 @@
     """Tests the GPT image probing."""
     # pylint: disable=protected-access
 
-    INVALID_PMBR = ' ' * 0x200
-    INVALID_GPT = ' ' * 0x200
-    VALID_PMBR = (' ' * 0x1fe) + '\x55\xaa'
-    VALID_GPT = 'EFI PART' + (' ' * 0x1f8)
+    INVALID_PMBR = b' ' * 0x200
+    INVALID_GPT = b' ' * 0x200
+    VALID_PMBR = (b' ' * 0x1fe) + b'\x55\xaa'
+    VALID_GPT = b'EFI PART' + (b' ' * 0x1f8)
     TESTCASES = (
         (False, False, INVALID_PMBR + INVALID_GPT),
         (False, False, VALID_PMBR + INVALID_GPT),
@@ -353,7 +354,7 @@
 
     img = os.path.join(self.tempdir, 'img.bin')
     for exp_pmbr_t, exp_pmbr_f, data in TESTCASES:
-      osutils.WriteFile(img, data)
+      osutils.WriteFile(img, data, mode='wb')
       self.assertEqual(
           flash._IsFilePathGPTDiskImage(img, require_pmbr=True), exp_pmbr_t)
       self.assertEqual(