private void LoadOptions() { Options = new Options(); var config = pluginHost.CustomConfig; Options.AlwaysConfirm = config.GetBool(alwaysConfirmOptionName, false); Options.ShowBalloon = config.GetBool(showBalloonOptionName, true); Options.LoggingEnabled = config.GetBool(logginEnabledOptionName, false); Options.UnlockOnActivity = config.GetBool(unlockOnActivityOptionName, true); Options.UseCygwinSocket = config.GetBool(useCygwinSocketOptionName, false); Options.CygwinSocketPath = config.GetString(cygwinSocketPathOptionName); Options.UseMsysSocket = config.GetBool(useMsysSocketOptionName, false); Options.MsysSocketPath = config.GetString(msysSocketPathOptionName); Options.UnixSocketPath = config.GetString(unixSocketPathOptionName); Options.UserPicksKeyOnRequestIdentities = config.GetBool(userPicksKeyOnRequestIdentitiesOptionName, false); Options.IgnoreMissingExternalKeyFiles = config.GetBool(ignoreMissingExternalKeyFilesName, false); string defaultLogFileNameValue = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "KeeAgent.log"); string configFileLogFileNameValue = config.GetString(logFileNameOptionName); if (string.IsNullOrEmpty(configFileLogFileNameValue)) { Options.LogFileName = defaultLogFileNameValue; } else { Options.LogFileName = configFileLogFileNameValue; } AgentMode configAgentMode; if (Enum.TryParse<AgentMode>(config.GetString(agentModeOptionName), out configAgentMode)) { Options.AgentMode = configAgentMode; } else { Options.AgentMode = AgentMode.Auto; } /* the Notification option is obsolete, so we read it and then clear it. */ NotificationOptions configFileNotificationValue; if (Enum.TryParse<NotificationOptions>(config .GetString(notificationOptionName), out configFileNotificationValue)) { switch (configFileNotificationValue) { case NotificationOptions.AlwaysAsk: case NotificationOptions.AskOnce: Options.AlwaysConfirm = true; break; case NotificationOptions.Never: Options.ShowBalloon = false; break; } config.SetString(notificationOptionName, string.Empty); } }
public void TestOptionsPersistance() { // used for passing Options objects to/from AppDomain const string optionsPropertyName = "KEEAGENT_OPTIONS"; /* test case options */ bool requestedLoggingEnabled = true; string requestedLogFileName = Environment.GetEnvironmentVariable("TEMP"); if (string.IsNullOrEmpty(requestedLogFileName)) { requestedLogFileName = Environment.GetEnvironmentVariable("TMP"); } if (string.IsNullOrEmpty(requestedLogFileName)) { requestedLogFileName = "/tmp"; } Assert.That (Directory.Exists (requestedLogFileName)); // verify that requested options are not default to ensure a // valid test Options requestedOptions = new Options(); Assert.AreNotEqual(requestedOptions.LoggingEnabled, requestedLoggingEnabled); Assert.AreNotEqual(requestedOptions.LogFileName, requestedLogFileName); requestedOptions.LoggingEnabled = requestedLoggingEnabled; requestedOptions.LogFileName = requestedLogFileName; /* first instance of KeePass is used to set options to * requested values */ using (KeePassAppDomain testDomain1 = new KeePassAppDomain()) { testDomain1.StartKeePass(true, false, 1, true); testDomain1.SetData(optionsPropertyName, requestedOptions); testDomain1.DoCallBack(delegate() { KeePass.Program.MainForm.Invoke((MethodInvoker)delegate() { KeeAgentExt td1KeeAgentExt = new KeeAgentExt(); IPluginHost td1PluginHost = KeePass.Program.MainForm.PluginHost; Options td1RequestedOptions = (Options)AppDomain .CurrentDomain.GetData(optionsPropertyName); td1KeeAgentExt.Initialize(td1PluginHost); td1KeeAgentExt.Options.Notification = td1RequestedOptions.Notification; td1KeeAgentExt.Options.LoggingEnabled = td1RequestedOptions.LoggingEnabled; td1KeeAgentExt.Options.LogFileName = td1RequestedOptions.LogFileName; var extType = td1KeeAgentExt.GetType (); var saveGlobalOptionsMethod = extType.GetMethod ("SaveGlobalOptions", BindingFlags.Instance | BindingFlags.NonPublic); saveGlobalOptionsMethod.Invoke (td1KeeAgentExt, null); td1PluginHost.MainWindow.SaveConfig(); AppDomain.CurrentDomain.SetData(optionsPropertyName, td1KeeAgentExt.Options); }); }); } /* second instance of KeePass reads options to verify that they * were saved in the .config file */ using (KeePassAppDomain testDomain2 = new KeePassAppDomain()) { testDomain2.StartKeePass(true, false, 1, false); testDomain2.DoCallBack(delegate() { KeePass.Program.MainForm.Invoke((MethodInvoker)delegate() { KeeAgentExt td2KeeAgentExt = new KeeAgentExt(); IPluginHost td2PluginHost = KeePass.Program.MainForm.PluginHost; td2KeeAgentExt.Initialize(td2PluginHost); AppDomain.CurrentDomain.SetData(optionsPropertyName, td2KeeAgentExt.Options); }); }); Options actual = (Options)testDomain2.GetData(optionsPropertyName); Assert.AreEqual(requestedLoggingEnabled, actual.LoggingEnabled); Assert.AreEqual(requestedLogFileName, actual.LogFileName); } }