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 bool Export(Items items) { try { using CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)); if (items.HasFlag(Items.UserConfig)) { stream.WriteFile("config", App.ConfigManager.UserPath); } if (items.HasFlag(Items.SystemConfig)) { stream.WriteFile("system", App.ConfigManager.SystemPath); } if (items.HasFlag(Items.PluginData)) { stream.WriteFile("plugin.config", App.ConfigManager.PluginsPath); foreach (Plugin plugin in plugins.Plugins) { foreach (PathInfo path in EnumerateFilesRelative(plugin.GetPluginFolder(PluginFolder.Data))) { try { stream.WriteFile(new string[] { "plugin.data", plugin.Identifier, path.Relative }, path.Full); } catch (ArgumentOutOfRangeException e) { FormMessage.Warning("Export Profile", "Could not include a plugin file in the export. " + e.Message, FormMessage.OK); } } } } if (items.HasFlag(Items.Session)) { string authToken = ReadAuthCookie(); if (authToken != null) { stream.WriteString("cookie.auth", authToken); } else { FormMessage.Warning("Export Profile", "Could not find any login session.", FormMessage.OK); } } stream.Flush(); return(true); } catch (Exception e) { App.ErrorHandler.HandleException("Profile Export Error", "An exception happened while exporting TweetDuck profile.", true, e); return(false); } }
public void TestLongIdentifierFail() { File.WriteAllText("long_identifier_fail_in", "test"); using (CombinedFileStream cfs = new CombinedFileStream(File.OpenWrite("long_identifier_fail"))){ cfs.WriteFile(string.Join("", Enumerable.Repeat("x", 256)), "long_identifier_fail_in"); } }
public bool Export(Items items) { try{ using (CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){ if (items.HasFlag(Items.UserConfig)) { stream.WriteFile("config", Program.UserConfigFilePath); } if (items.HasFlag(Items.SystemConfig)) { stream.WriteFile("system", Program.SystemConfigFilePath); } if (items.HasFlag(Items.PluginData)) { stream.WriteFile("plugin.config", Program.PluginConfigFilePath); foreach (Plugin plugin in plugins.Plugins) { foreach (PathInfo path in EnumerateFilesRelative(plugin.GetPluginFolder(PluginFolder.Data))) { try{ stream.WriteFile(new string[] { "plugin.data", plugin.Identifier, path.Relative }, path.Full); }catch (ArgumentOutOfRangeException e) { FormMessage.Warning("Export Profile", "Could not include a plugin file in the export. " + e.Message, FormMessage.OK); } } } } if (items.HasFlag(Items.Session)) { stream.WriteFile("cookies", CookiesPath); } stream.Flush(); } return(true); }catch (Exception e) { Program.Reporter.HandleException("Profile Export Error", "An exception happened while exporting TweetDuck profile.", true, e); return(false); } }
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); } }