public void WriteToStreamAndVerify() { var path = Env.ResolvePath("~/Resources/NewDatabase23.kdbx"); if (System.IO.File.Exists(path)) System.IO.File.Delete(path); try { var key = new MasterKey(); key.Add(new MasterKeyPassword("megatron")); // terrible pw. var package = new KeePassPackage(key); using (var fs = System.IO.File.OpenWrite(path)) { var group = new Group(); group.Name = "Sample"; package.Root.Groups.Add(group); var entry = new Entry(true); var fields = entry.AsEntryFields(); fields.Title = "Test"; fields.Password = "******"; group.Entries.Add(entry); package.Save(fs); using (var fs2 = System.IO.File.OpenRead(Env.ResolvePath("~/Resources/NewDatabase23.kdbx"))) { var package2 = new KeePassPackage(key); package2.Open(fs2); Assert.NotNull(package2); Assert.NotNull(package2.Root); Assert.NotNull(package2.Root.Groups); Assert.Equal(1, package2.Root.Groups.Count); Assert.NotNull(package2.Root.Groups[0].Entries); Assert.Equal(1, package2.Root.Groups[0].Entries.Count); var entry2 = package2.Root.Groups[0].Entries[0]; var titleString = entry2.Strings.SingleOrDefault(o => o.Key == "Title"); var passwordString = entry2.Strings.SingleOrDefault(o => o.Key == "Password"); Assert.Equal("Test", titleString.Value.Unprotect()); Assert.Equal("Password", passwordString.Value.Unprotect()); } } } finally { //if (System.IO.File.Exists(path)) // System.IO.File.Delete(path); } }
public void OpenStreamAndRead() { var key = new MasterKey(); key.Add(new MasterKeyPassword("password")); // terrible pw. var package = new KeePassPackage(key); Assert.Null(package.Meta.DatabaseName); using (var fs = System.IO.File.OpenRead(Env.ResolvePath("~/Resources/NewDatabase.kdbx"))) { package.Open(fs); var meta = package.Meta; var predicates = new List<Func<IEntryFields, bool>>(); var matches = new List<Entry>(); if (!string.IsNullOrWhiteSpace(this.Title)) predicates.Add(this.CreateSeelctor("Title", this.Title, false)); if (package.Root != null && package.Root.Groups != null) { foreach (var group in package.Root.Groups) { if (!Collect(group, predicates, matches)) { break; } } } Assert.NotEmpty(matches); Assert.Equal("Password", matches[0].AsEntryFields().Password); foreach(var match in matches) { var t = match.AsEntryFields().Title; var v = match.AsEntryFields().Password; if(v == null) Console.WriteLine("{0} {1} null", t, v); else Console.WriteLine("{0} {1}", t, v); } } }