Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b3659cbe authored by weichinweng's avatar weichinweng Committed by Weichin Weng
Browse files

Common Criteria mode: Handle IOException when call newInputStream

Bug: 181293964
Test: atest BluetoothInstrumentationTests
Tag: #feature
Change-Id: I315c7464b224d8acb154e42e8ce216f5d02e9429
parent 9a689cee
Loading
Loading
Loading
Loading
+27 −14
Original line number Diff line number Diff line
@@ -213,6 +213,7 @@ public class BluetoothKeystoreService {
    @VisibleForTesting
    public void cleanupForNiapModeEnable() {
        try {
            Thread.sleep(100);
            setEncryptKeyOrRemoveKey(CONFIG_FILE_PREFIX, CONFIG_FILE_HASH);
        } catch (InterruptedException e) {
            reportBluetoothKeystoreException(e, "Interrupted while operating.");
@@ -520,7 +521,7 @@ public class BluetoothKeystoreService {
     */
    @VisibleForTesting
    public boolean compareFileHash(String hashFilePathString)
            throws IOException, NoSuchAlgorithmException {
            throws InterruptedException, IOException, NoSuchAlgorithmException {
        if (!Files.exists(Paths.get(hashFilePathString))) {
            infoLog("compareFileHash: File does not exist, path: " + hashFilePathString);
            return false;
@@ -555,13 +556,15 @@ public class BluetoothKeystoreService {
    }

    private void readHashFile(String filePathString, String prefixString)
            throws IOException, NoSuchAlgorithmException {
            throws InterruptedException, NoSuchAlgorithmException {
        byte[] dataBuffer = new byte[BUFFER_SIZE];
        int bytesRead  = 0;

        boolean successful = false;
        int counter = 0;
        while (!successful && counter < TRY_MAX) {
            try {
                MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
                InputStream fileStream = Files.newInputStream(Paths.get(filePathString));

                while ((bytesRead = fileStream.read(dataBuffer)) != -1) {
                    messageDigest.update(dataBuffer, 0, bytesRead);
                }
@@ -574,6 +577,16 @@ public class BluetoothKeystoreService {
                }

                mNameDecryptKey.put(prefixString, hashString.toString());
                successful = true;
            } catch (IOException e) {
                infoLog("Fail to open file, try again. counter: " + counter);
                Thread.sleep(50);
                counter++;
            }
        }
        if (counter > 3) {
            errorLog("Fail to open file");
        }
    }

    private void readChecksumFile(String filePathString, String prefixString) throws IOException {
+1 −1
Original line number Diff line number Diff line
@@ -214,7 +214,7 @@ public final class BluetoothKeystoreServiceTest {
    private boolean compareFileHash(String hashFilePathString) {
        try {
            return mBluetoothKeystoreService.compareFileHash(hashFilePathString);
        } catch (IOException | NoSuchAlgorithmException e) {
        } catch (InterruptedException | IOException | NoSuchAlgorithmException e) {
            return false;
        }
    }