Commit 5c89bbed by eguven Committed by Oliver Woodman

Fix leftover bytes in cached content index file

Extra calls to CipherOutputStream.close() causes each time extra 16 bytes written to the
underlying OutputStream. Prevented close() is called more than once and also discarded any
data in ReusableBufferedOutputStream buffer on reset().

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=144063120
parent deefe50a
...@@ -181,7 +181,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -181,7 +181,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
// Assert file content is different // Assert file content is different
FileInputStream fis1 = new FileInputStream(file1); FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2); FileInputStream fis2 = new FileInputStream(file2);
for (int b; (b = fis1.read()) == fis2.read();) { for (int b; (b = fis1.read()) == fis2.read(); ) {
assertTrue(b != -1); assertTrue(b != -1);
} }
...@@ -205,6 +205,12 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -205,6 +205,12 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
// Non encrypted index file can be read even when encryption key provided. // Non encrypted index file can be read even when encryption key provided.
assertStoredAndLoadedEqual(new CachedContentIndex(cacheDir), assertStoredAndLoadedEqual(new CachedContentIndex(cacheDir),
new CachedContentIndex(cacheDir, key)); new CachedContentIndex(cacheDir, key));
// Test multiple store() calls
CachedContentIndex index = new CachedContentIndex(cacheDir, key);
index.addNew(new CachedContent(15, "key3", 110));
index.store();
assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir, key));
} }
private void assertStoredAndLoadedEqual(CachedContentIndex index, CachedContentIndex index2) private void assertStoredAndLoadedEqual(CachedContentIndex index, CachedContentIndex index2)
......
...@@ -302,6 +302,9 @@ import javax.crypto.spec.SecretKeySpec; ...@@ -302,6 +302,9 @@ import javax.crypto.spec.SecretKeySpec;
} }
output.writeInt(hashCode); output.writeInt(hashCode);
atomicFile.endWrite(output); atomicFile.endWrite(output);
// Avoid calling close twice. Duplicate CipherOutputStream.close calls did
// not used to be no-ops: https://android-review.googlesource.com/#/c/272799/
output = null;
} catch (IOException e) { } catch (IOException e) {
throw new CacheException(e); throw new CacheException(e);
} finally { } finally {
......
...@@ -67,6 +67,7 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream { ...@@ -67,6 +67,7 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
public void reset(OutputStream out) { public void reset(OutputStream out) {
Assertions.checkState(closed); Assertions.checkState(closed);
this.out = out; this.out = out;
count = 0;
closed = false; closed = false;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment