/// <exception cref="System.Exception"></exception> public virtual void TestBasicOperation() { BlobStore attachments = database.GetAttachments(); InputStream attachmentStream = GetAsset("attachment.png"); byte[] bytes = IOUtils.ToByteArray(attachmentStream); BlobStoreWriter blobStoreWriter = new BlobStoreWriter(attachments); blobStoreWriter.AppendData(bytes); blobStoreWriter.Finish(); blobStoreWriter.Install(); string sha1DigestKey = blobStoreWriter.SHA1DigestString(); BlobKey keyFromSha1 = new BlobKey(sha1DigestKey); NUnit.Framework.Assert.IsTrue(attachments.GetSizeOfBlob(keyFromSha1) == bytes.Length ); }
public void TestBlobStoreWriter() { var writer = new BlobStoreWriter(_store); writer.AppendData(Encoding.UTF8.GetBytes("part 1, ")); writer.AppendData(Encoding.UTF8.GetBytes("part 2, ")); writer.AppendData(Encoding.UTF8.GetBytes("part 3")); writer.Finish(); writer.Install(); var expectedData = Encoding.UTF8.GetBytes("part 1, part 2, part 3"); var readItem = _store.BlobForKey(writer.GetBlobKey()); readItem.Should().Equal(expectedData, "because the writer should correctly write contents to disk"); Verify(writer.GetBlobKey(), expectedData); }
public void TestStreamAttachmentBlobStoreWriter() { var attachments = database.Attachments; var blobWriter = new BlobStoreWriter(attachments); var testBlob = "foo"; blobWriter.AppendData(Runtime.GetBytesForString(testBlob)); blobWriter.Finish(); var sha1Base64Digest = "sha1-C+7Hteo/D9vJXQ3UfzxbwnXaijM="; Assert.AreEqual(blobWriter.SHA1DigestString(), sha1Base64Digest); Assert.AreEqual(blobWriter.MD5DigestString(), "md5-rL0Y20zC+Fzt72VPzMSk2A=="); // install it blobWriter.Install(); // look it up in blob store and make sure it's there var blobKey = new BlobKey(sha1Base64Digest); var blob = attachments.BlobForKey(blobKey); Assert.IsTrue(Arrays.Equals(Runtime.GetBytesForString(testBlob).ToArray(), blob)); }
public void TestBasicOperation() { var attachmentStream = (InputStream)GetAsset("attachment.png"); var memoryStream = new MemoryStream(); attachmentStream.Wrapped.CopyTo(memoryStream); var bytes = memoryStream.ToArray(); var attachments = database.Attachments; var blobStoreWriter = new BlobStoreWriter(attachments); blobStoreWriter.AppendData(bytes); blobStoreWriter.Finish(); blobStoreWriter.Install(); var sha1DigestKey = blobStoreWriter.SHA1DigestString(); Assert.IsTrue(sha1DigestKey.Contains("LmsoqJJ6LOn4YS60pYnvrKbBd64=")); var keyFromSha1 = new BlobKey(sha1DigestKey); Assert.IsTrue(attachments.GetSizeOfBlob(keyFromSha1) == bytes.Length); }
public void TestStreamAttachmentBlobStoreWriter() { var attachments = database.Attachments; var blobWriter = new BlobStoreWriter(attachments); var testBlob = "foo"; blobWriter.AppendData(Encoding.UTF8.GetBytes(testBlob)); blobWriter.Finish(); var sha1Base64Digest = "sha1-C+7Hteo/D9vJXQ3UfzxbwnXaijM="; Assert.AreEqual(blobWriter.SHA1DigestString(), sha1Base64Digest); // install it blobWriter.Install(); // look it up in blob store and make sure it's there var blobKey = new BlobKey(sha1Base64Digest); var blob = attachments.BlobForKey(blobKey); CollectionAssert.AreEqual(Encoding.UTF8.GetBytes(testBlob).ToArray(), blob); }
public virtual void TestStreamAttachmentBlobStoreWriter() { BlobStore attachments = database.GetAttachments(); BlobStoreWriter blobWriter = new BlobStoreWriter(attachments); string testBlob = "foo"; blobWriter.AppendData(Sharpen.Runtime.GetBytesForString(new string(testBlob))); blobWriter.Finish(); string sha1Base64Digest = "sha1-C+7Hteo/D9vJXQ3UfzxbwnXaijM="; NUnit.Framework.Assert.AreEqual(blobWriter.SHA1DigestString(), sha1Base64Digest); NUnit.Framework.Assert.AreEqual(blobWriter.MD5DigestString(), "md5-rL0Y20zC+Fzt72VPzMSk2A==" ); // install it blobWriter.Install(); // look it up in blob store and make sure it's there BlobKey blobKey = new BlobKey(sha1Base64Digest); byte[] blob = attachments.BlobForKey(blobKey); NUnit.Framework.Assert.IsTrue(Arrays.Equals(Sharpen.Runtime.GetBytesForString(testBlob , Sharpen.Extensions.GetEncoding("UTF-8")), blob)); }
public void TestBasicOperation() { var attachmentStream = GetAsset("attachment.png"); var memoryStream = new MemoryStream(); attachmentStream.CopyTo(memoryStream); var bytes = memoryStream.ToArray(); var attachments = database.Attachments; var blobStoreWriter = new BlobStoreWriter(attachments); blobStoreWriter.AppendData(bytes); blobStoreWriter.Finish(); blobStoreWriter.Install(); var sha1DigestKey = blobStoreWriter.SHA1DigestString(); Assert.IsTrue(sha1DigestKey.Contains("LmsoqJJ6LOn4YS60pYnvrKbBd64=")); var keyFromSha1 = new BlobKey(sha1DigestKey); Assert.IsTrue(attachments.GetSizeOfBlob(keyFromSha1) == bytes.Length); }
public void TestStreamAttachmentBlobStoreWriter() { var attachments = database.Attachments; var blobWriter = new BlobStoreWriter(attachments); var testBlob = "foo"; blobWriter.AppendData(Encoding.UTF8.GetBytes(testBlob)); blobWriter.Finish(); var sha1Base64Digest = "sha1-C+7Hteo/D9vJXQ3UfzxbwnXaijM="; Assert.AreEqual(blobWriter.SHA1DigestString(), sha1Base64Digest); // install it blobWriter.Install(); // look it up in blob store and make sure it's there var blobKey = new BlobKey(sha1Base64Digest); var blob = attachments.BlobForKey(blobKey); Assert.IsTrue(Arrays.Equals(Encoding.UTF8.GetBytes(testBlob).ToArray(), blob)); }
public AtomicAction ActionToChangeEncryptionKey(SymmetricKey newKey) { var action = new AtomicAction(); // Find all the blob files: var blobs = default(string[]); var oldKey = EncryptionKey; blobs = Directory.GetFiles(_path, "*" + FileExtension); if (blobs.Length == 0) { // No blobs, so nothing to encrypt. Just add/remove the encryption marker file: action.AddLogic(() => { Log.D(TAG, "{0} {1}", (newKey != null) ? "encrypting" : "decrypting", _path); Log.D(TAG, " No blobs to copy; done."); EncryptionKey = newKey; MarkEncrypted(newKey != null); }, () => { EncryptionKey = oldKey; MarkEncrypted(oldKey != null); }, null); return(action); } // Create a new directory for the new blob store. Have to do this now, before starting the // action, because farther down we create an action to move it... var tempPath = Path.Combine(Path.GetTempPath(), String.Format("CouchbaseLite-Temp-{0}", Misc.CreateGUID())); action.AddLogic(() => { Log.D(TAG, "{0} {1}", (newKey != null) ? "encrypting" : "decrypting", _path); Directory.CreateDirectory(tempPath); }, () => Directory.Delete(tempPath, true), null); var tempStore = default(BlobStore); action.AddLogic(() => { tempStore = new BlobStore(tempPath, newKey); tempStore.MarkEncrypted(true); }, null, null); // Copy each of my blobs into the new store (which will update its encryption): action.AddLogic(() => { foreach (var blobName in blobs) { // Copy file by reading with old key and writing with new one: Log.D(TAG, " Copying {0}", blobName); Stream readStream = File.Open(blobName, FileMode.Open, FileAccess.Read, FileShare.Read); if (EncryptionKey != null) { readStream = EncryptionKey.DecryptStream(readStream); } var writer = new BlobStoreWriter(tempStore); try { writer.Read(readStream); writer.Finish(); writer.Install(); } catch (Exception) { writer.Cancel(); throw; } finally { readStream.Dispose(); } } }, null, null); // Replace the attachment dir with the new one: action.AddLogic(AtomicAction.MoveDirectory(tempPath, _path)); // Finally update EncryptionKey: action.AddLogic(() => { EncryptionKey = newKey; }, () => { EncryptionKey = oldKey; }, null); return(action); }
public AtomicAction ActionToChangeEncryptionKey(SymmetricKey newKey) { var action = new AtomicAction(); // Find all the blob files: var blobs = default(string[]); var oldKey = EncryptionKey; blobs = Directory.GetFiles(_path, "*" + FileExtension); if (blobs.Length == 0) { // No blobs, so nothing to encrypt. Just add/remove the encryption marker file: action.AddLogic(() => { Log.To.NoDomain.D(TAG, "{0} {1}", (newKey != null) ? "encrypting" : "decrypting", _path); Log.To.NoDomain.D(TAG, " No blobs to copy; done."); EncryptionKey = newKey; MarkEncrypted(newKey != null); }, () => { EncryptionKey = oldKey; MarkEncrypted(oldKey != null); }, null); return action; } // Create a new directory for the new blob store. Have to do this now, before starting the // action, because farther down we create an action to move it... var tempPath = Path.Combine(Path.GetTempPath(), String.Format("CouchbaseLite-Temp-{0}", Misc.CreateGUID())); action.AddLogic(() => { Log.To.NoDomain.D(TAG, "{0} {1}", (newKey != null) ? "encrypting" : "decrypting", _path); Directory.CreateDirectory(tempPath); }, () => Directory.Delete(tempPath, true), null); var tempStore = default(BlobStore); action.AddLogic(() => { tempStore = new BlobStore(tempPath, newKey); tempStore.MarkEncrypted(true); }, null, null); // Copy each of my blobs into the new store (which will update its encryption): action.AddLogic(() => { foreach(var blobName in blobs) { // Copy file by reading with old key and writing with new one: Log.To.NoDomain.D(TAG, " Copying {0}", blobName); Stream readStream = File.Open(blobName, FileMode.Open, FileAccess.Read, FileShare.Read); if(EncryptionKey != null) { readStream = EncryptionKey.DecryptStream(readStream); } var writer = new BlobStoreWriter(tempStore); try { writer.Read(readStream); writer.Finish(); writer.Install(); } catch(Exception) { writer.Cancel(); throw; } finally { readStream.Dispose(); } } }, null, null); // Replace the attachment dir with the new one: action.AddLogic(AtomicAction.MoveDirectory(tempPath, _path)); // Finally update EncryptionKey: action.AddLogic(() => { EncryptionKey = newKey; }, () => { EncryptionKey = oldKey; }, null); return action; }