public void TestTextFilesAndComplexKeys() { File.WriteAllText("input_text_1", "Hello World!" + Environment.NewLine); using (CombinedFileStream cfs = new CombinedFileStream(File.OpenWrite("text_files"))){ cfs.WriteFile(new string[] { "key1", "a", "bb", "ccc", "dddd" }, "input_text_1"); cfs.WriteFile(new string[] { "key2", "a", "bb", "ccc", "dddd" }, "input_text_1"); cfs.Flush(); } Assert.IsTrue(File.Exists("text_files")); using (CombinedFileStream cfs = new CombinedFileStream(File.OpenRead("text_files"))){ CombinedFileStream.Entry entry = cfs.ReadFile(); Assert.AreEqual("key2", cfs.SkipFile()); Assert.IsNull(cfs.ReadFile()); Assert.IsNull(cfs.SkipFile()); Assert.AreEqual("key1|a|bb|ccc|dddd", entry.Identifier); Assert.AreEqual("key1", entry.KeyName); CollectionAssert.AreEqual(new string[] { "a", "bb", "ccc", "dddd" }, entry.KeyValue); entry.WriteToFile("text_file_1"); } Assert.IsTrue(File.Exists("text_file_1")); Assert.AreEqual("Hello World!" + Environment.NewLine, File.ReadAllText("text_file_1")); }
public void TestEmptyFiles() { TestUtils.WriteText("cfs_input_empty_1", string.Empty); TestUtils.WriteText("cfs_input_empty_2", string.Empty); using (CombinedFileStream cfs = new CombinedFileStream(TestUtils.WriteFile("cfs_blank_files"))){ cfs.WriteFile("id1", "cfs_input_empty_1"); cfs.WriteFile("id2", "cfs_input_empty_2"); cfs.WriteFile("id2_clone", "cfs_input_empty_2"); cfs.Flush(); } Assert.IsTrue(File.Exists("cfs_blank_files")); using (CombinedFileStream cfs = new CombinedFileStream(TestUtils.ReadFile("cfs_blank_files"))){ CombinedFileStream.Entry entry1 = cfs.ReadFile(); string entry2key = cfs.SkipFile(); CombinedFileStream.Entry entry3 = cfs.ReadFile(); Assert.IsNull(cfs.ReadFile()); Assert.IsNull(cfs.SkipFile()); Assert.AreEqual("id1", entry1.KeyName); Assert.AreEqual("id1", entry1.Identifier); CollectionAssert.AreEqual(new string[0], entry1.KeyValue); Assert.AreEqual("id2", entry2key); Assert.AreEqual("id2_clone", entry3.KeyName); Assert.AreEqual("id2_clone", entry3.Identifier); CollectionAssert.AreEqual(new string[0], entry3.KeyValue); entry1.WriteToFile("cfs_blank_file_1"); entry3.WriteToFile("cfs_blank_file_2"); TestUtils.DeleteFileOnExit("cfs_blank_file_1"); TestUtils.DeleteFileOnExit("cfs_blank_file_2"); } Assert.IsTrue(File.Exists("cfs_blank_file_1")); Assert.IsTrue(File.Exists("cfs_blank_file_2")); Assert.AreEqual(string.Empty, TestUtils.ReadText("cfs_blank_file_1")); Assert.AreEqual(string.Empty, TestUtils.ReadText("cfs_blank_file_2")); }
public void TestEntryWriteWithDirectory() { if (Directory.Exists("cfs_directory")) { Directory.Delete("cfs_directory", true); } TestUtils.WriteText("cfs_input_dir_1", "test"); using (CombinedFileStream cfs = new CombinedFileStream(TestUtils.WriteFile("cfs_dir_test"))){ cfs.WriteFile("key1", "cfs_input_dir_1"); cfs.WriteFile("key2", "cfs_input_dir_1"); cfs.WriteFile("key3", "cfs_input_dir_1"); cfs.WriteFile("key4", "cfs_input_dir_1"); cfs.Flush(); } Assert.IsTrue(File.Exists("cfs_dir_test")); using (CombinedFileStream cfs = new CombinedFileStream(TestUtils.ReadFile("cfs_dir_test"))){ try{ cfs.ReadFile().WriteToFile("cfs_directory/cfs_dir_test_file", false); Assert.Fail("WriteToFile did not trigger an exception."); }catch (DirectoryNotFoundException) {} cfs.ReadFile().WriteToFile("cfs_directory/cfs_dir_test_file", true); cfs.ReadFile().WriteToFile("cfs_dir_test_file", true); cfs.ReadFile().WriteToFile("cfs_dir_test_file.txt", true); TestUtils.DeleteFileOnExit("cfs_dir_test_file"); TestUtils.DeleteFileOnExit("cfs_dir_test_file.txt"); } Assert.IsTrue(Directory.Exists("cfs_directory")); Assert.IsTrue(File.Exists("cfs_directory/cfs_dir_test_file")); Assert.IsTrue(File.Exists("cfs_dir_test_file")); Assert.IsTrue(File.Exists("cfs_dir_test_file.txt")); Assert.AreEqual("test", TestUtils.ReadText("cfs_directory/cfs_dir_test_file")); Assert.AreEqual("test", TestUtils.ReadText("cfs_dir_test_file")); Assert.AreEqual("test", TestUtils.ReadText("cfs_dir_test_file.txt")); Directory.Delete("cfs_directory", true); }
public void TestLongIdentifierSuccess() { File.WriteAllText("long_identifier_fail_in", "test"); string identifier = string.Join("", Enumerable.Repeat("x", 255)); using (CombinedFileStream cfs = new CombinedFileStream(File.OpenWrite("long_identifier_success"))){ cfs.WriteFile(identifier, "long_identifier_fail_in"); cfs.Flush(); } using (CombinedFileStream cfs = new CombinedFileStream(File.OpenRead("long_identifier_success"))){ Assert.AreEqual(identifier, cfs.ReadFile().Identifier); } }
public void TestNoFiles() { using (CombinedFileStream cfs = new CombinedFileStream(File.OpenWrite("empty"))){ cfs.Flush(); } Assert.IsTrue(File.Exists("empty")); using (CombinedFileStream cfs = new CombinedFileStream(File.OpenRead("empty"))){ Assert.IsNull(cfs.ReadFile()); } using (CombinedFileStream cfs = new CombinedFileStream(File.OpenRead("empty"))){ Assert.IsNull(cfs.SkipFile()); } }
public bool Import(Items items) { try { var missingPlugins = new HashSet <string>(); bool oldCookies = false; using (CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))) { CombinedFileStream.Entry entry; while ((entry = stream.ReadFile()) != null) { switch (entry.KeyName) { case "config": if (items.HasFlag(Items.UserConfig)) { entry.WriteToFile(App.ConfigManager.UserPath); } break; case "system": if (items.HasFlag(Items.SystemConfig)) { entry.WriteToFile(App.ConfigManager.SystemPath); } break; case "plugin.config": if (items.HasFlag(Items.PluginData)) { entry.WriteToFile(App.ConfigManager.PluginsPath); } break; case "plugin.data": if (items.HasFlag(Items.PluginData)) { string[] value = entry.KeyValue; entry.WriteToFile(Path.Combine(plugins.PluginDataFolder, value[0], value[1]), true); if (!plugins.Plugins.Any(plugin => plugin.Identifier.Equals(value[0]))) { missingPlugins.Add(value[0]); } } break; case "cookies": case "localprefs": if (items.HasFlag(Items.Session)) { oldCookies = true; } break; case "cookie.auth": if (items.HasFlag(Items.Session)) { using ICookieManager cookies = Cef.GetGlobalCookieManager(); var _ = cookies.SetCookieAsync(AuthCookieUrl, new Cookie { Name = AuthCookieName, Domain = AuthCookieDomain, Path = AuthCookiePath, Value = Encoding.UTF8.GetString(entry.Contents), Expires = DateTime.Now.Add(TimeSpan.FromDays(365 * 5)), HttpOnly = true, Secure = true }).ContinueWith(t => { // ReSharper disable once AccessToDisposedClosure // ReSharper disable once ConvertToLambdaExpression return(cookies.FlushStoreAsync()); }).Result; } break; } } } if (items.HasFlag(Items.Session) && oldCookies) { FormMessage.Error("Profile Import Error", "Cannot import login session from an older version of TweetDuck.", FormMessage.OK); return(false); } if (missingPlugins.Count > 0) { FormMessage.Information("Profile Import", "Detected missing plugins when importing plugin data:\n" + string.Join("\n", missingPlugins), FormMessage.OK); } return(true); } catch (Exception e) { App.ErrorHandler.HandleException("Profile Import", "An exception happened while importing TweetDuck profile.", true, e); return(false); } }
public bool Import(Items items) { try{ HashSet <string> missingPlugins = new HashSet <string>(); using (CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))){ CombinedFileStream.Entry entry; while ((entry = stream.ReadFile()) != null) { switch (entry.KeyName) { case "config": if (items.HasFlag(Items.UserConfig)) { entry.WriteToFile(Program.UserConfigFilePath); } break; case "system": if (items.HasFlag(Items.SystemConfig)) { entry.WriteToFile(Program.SystemConfigFilePath); } break; case "plugin.config": if (items.HasFlag(Items.PluginData)) { entry.WriteToFile(Program.PluginConfigFilePath); } break; case "plugin.data": if (items.HasFlag(Items.PluginData)) { string[] value = entry.KeyValue; entry.WriteToFile(Path.Combine(Program.PluginDataPath, value[0], value[1]), true); if (!plugins.Plugins.Any(plugin => plugin.Identifier.Equals(value[0]))) { missingPlugins.Add(value[0]); } } break; case "cookies": if (items.HasFlag(Items.Session)) { entry.WriteToFile(Path.Combine(Program.StoragePath, TempCookiesPath)); } break; } } } if (missingPlugins.Count > 0) { FormMessage.Information("Profile Import", "Detected missing plugins when importing plugin data:\n" + string.Join("\n", missingPlugins), FormMessage.OK); } return(true); }catch (Exception e) { Program.Reporter.HandleException("Profile Import", "An exception happened while importing TweetDuck profile.", true, e); return(false); } }