From 04acfb99ed71398b42d11b8dfec6a1b158d77759 Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Wed, 14 Aug 2013 06:36:12 +0200 Subject: [PATCH] Fixed problem with opening non-existing files through CachingFileStorage --- .../Io/BuiltInFileStorage.cs | 2 +- .../Io/CachingFileStorage.cs | 3 ++ src/Kp2aUnitTests/TestCachingFileStorage.cs | 29 ++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Kp2aBusinessLogic/Io/BuiltInFileStorage.cs b/src/Kp2aBusinessLogic/Io/BuiltInFileStorage.cs index 0fb391aa..da2a2553 100644 --- a/src/Kp2aBusinessLogic/Io/BuiltInFileStorage.cs +++ b/src/Kp2aBusinessLogic/Io/BuiltInFileStorage.cs @@ -66,7 +66,7 @@ namespace keepass2android.Io { if ((ex.Response is HttpWebResponse) && (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)) { - throw new FileNotFoundException("404!", ioc.Path, ex); + throw new FileNotFoundException(ex.Message, ioc.Path, ex); } throw; } diff --git a/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs b/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs index c418844c..508c73ea 100644 --- a/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs +++ b/src/Kp2aBusinessLogic/Io/CachingFileStorage.cs @@ -146,6 +146,9 @@ namespace keepass2android.Io } catch (Exception ex) { + if (!IsCached(ioc)) + throw; + Kp2aLog.Log("couldn't open from remote " + ioc.Path); Kp2aLog.Log(ex.ToString()); diff --git a/src/Kp2aUnitTests/TestCachingFileStorage.cs b/src/Kp2aUnitTests/TestCachingFileStorage.cs index 9c90e98f..85d47c06 100644 --- a/src/Kp2aUnitTests/TestCachingFileStorage.cs +++ b/src/Kp2aUnitTests/TestCachingFileStorage.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; @@ -46,6 +47,32 @@ namespace Kp2aUnitTests _testCacheSupervisor.AssertSingleCall(TestCacheSupervisor.CouldntOpenFromRemoteId); + } + + /// + /// Tests correct behavior in case that a file is to be opened which is not in the cache + /// + [TestMethod] + public void TestOpenNonExistingNonCachedFiles() + { + SetupFileStorage(); + + //read the file once. Should now be in the cache. + try + { + MemoryStream fileContents = ReadToMemoryStream(_fileStorage, "nonexistingfile.txt"); + } + catch (Exception e) + { + _testCacheSupervisor.AssertNoCall(); + Assert.IsInstanceOfType(e, typeof(FileNotFoundException)); + + return; + } + _testCacheSupervisor.AssertNoCall(); + Assert.Fail("didn't get exception!"); + + } private string MemoryStreamToString(MemoryStream stream)