|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +#if NETFX_CORE |
| 6 | +using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; |
| 7 | +using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; |
| 8 | +using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; |
| 9 | +using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; |
| 10 | +#else |
| 11 | +using NUnit.Framework; |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace SQLite.Tests |
| 15 | +{ |
| 16 | + [TestFixture] |
| 17 | + public class SQLCipherTest |
| 18 | + { |
| 19 | + class TestTable |
| 20 | + { |
| 21 | + [PrimaryKey, AutoIncrement] |
| 22 | + public int Id { get; set; } |
| 23 | + |
| 24 | + public string Value { get; set; } |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public void SetStringKey () |
| 29 | + { |
| 30 | + string path; |
| 31 | + |
| 32 | + var key = "SecretPassword"; |
| 33 | + |
| 34 | + using (var db = new TestDb ()) { |
| 35 | + path = db.DatabasePath; |
| 36 | + |
| 37 | + db.SetKey (key); |
| 38 | + |
| 39 | + db.CreateTable<TestTable> (); |
| 40 | + db.Insert (new TestTable { Value = "Hello" }); |
| 41 | + } |
| 42 | + |
| 43 | + using (var db = new TestDb (path)) { |
| 44 | + path = db.DatabasePath; |
| 45 | + |
| 46 | + db.SetKey (key); |
| 47 | + |
| 48 | + var r = db.Table<TestTable> ().First (); |
| 49 | + |
| 50 | + Assert.AreEqual ("Hello", r.Value); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + public void SetBytesKey () |
| 56 | + { |
| 57 | + string path; |
| 58 | + |
| 59 | + var rand = new Random (); |
| 60 | + var key = new byte[32]; |
| 61 | + rand.NextBytes (key); |
| 62 | + |
| 63 | + using (var db = new TestDb ()) { |
| 64 | + path = db.DatabasePath; |
| 65 | + |
| 66 | + db.SetKey (key); |
| 67 | + |
| 68 | + db.CreateTable<TestTable> (); |
| 69 | + db.Insert (new TestTable { Value = "Hello" }); |
| 70 | + } |
| 71 | + |
| 72 | + using (var db = new TestDb (path)) { |
| 73 | + path = db.DatabasePath; |
| 74 | + |
| 75 | + db.SetKey (key); |
| 76 | + |
| 77 | + var r = db.Table<TestTable> ().First (); |
| 78 | + |
| 79 | + Assert.AreEqual ("Hello", r.Value); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void SetBadBytesKey () |
| 85 | + { |
| 86 | + try { |
| 87 | + using (var db = new TestDb ()) { |
| 88 | + db.SetKey (new byte[] { 1, 2, 3, 4 }); |
| 89 | + } |
| 90 | + Assert.Fail ("Should have thrown"); |
| 91 | + } |
| 92 | + catch (ArgumentException) { |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments