Ejemplo n.º 1
0
        /// <exception cref="System.Exception"/>
        internal static void CheckSpecificProvider(Configuration conf, string ourUrl)
        {
            CredentialProvider provider = CredentialProviderFactory.GetProviders(conf)[0];

            char[] passwd = GeneratePassword(16);
            // ensure that we get nulls when the key isn't there
            Assert.Equal(null, provider.GetCredentialEntry("no-such-key"));
            Assert.Equal(null, provider.GetCredentialEntry("key"));
            // create a new key
            try
            {
                provider.CreateCredentialEntry("pass", passwd);
            }
            catch (Exception e)
            {
                Runtime.PrintStackTrace(e);
                throw;
            }
            // make sure we get back the right key
            Assert.AssertArrayEquals(passwd, provider.GetCredentialEntry("pass").GetCredential
                                         ());
            // try recreating pass
            try
            {
                provider.CreateCredentialEntry("pass", passwd);
                Assert.True("should throw", false);
            }
            catch (IOException e)
            {
                Assert.Equal("Credential pass already exists in " + ourUrl, e.
                             Message);
            }
            provider.DeleteCredentialEntry("pass");
            try
            {
                provider.DeleteCredentialEntry("pass");
                Assert.True("should throw", false);
            }
            catch (IOException e)
            {
                Assert.Equal("Credential pass does not exist in " + ourUrl, e.
                             Message);
            }
            char[] passTwo = new char[] { '1', '2', '3' };
            provider.CreateCredentialEntry("pass", passwd);
            provider.CreateCredentialEntry("pass2", passTwo);
            Assert.AssertArrayEquals(passTwo, provider.GetCredentialEntry("pass2").GetCredential
                                         ());
            // write them to disk so that configuration.getPassword will find them
            provider.Flush();
            // configuration.getPassword should get this from provider
            Assert.AssertArrayEquals(passTwo, conf.GetPassword("pass2"));
            // configuration.getPassword should get this from config
            conf.Set("onetwothree", "123");
            Assert.AssertArrayEquals(passTwo, conf.GetPassword("onetwothree"));
            // configuration.getPassword should NOT get this from config since
            // we are disabling the fallback to clear text config
            conf.Set(CredentialProvider.ClearTextFallback, "false");
            Assert.AssertArrayEquals(null, conf.GetPassword("onetwothree"));
            // get a new instance of the provider to ensure it was saved correctly
            provider = CredentialProviderFactory.GetProviders(conf)[0];
            Assert.True(provider != null);
            Assert.AssertArrayEquals(new char[] { '1', '2', '3' }, provider.GetCredentialEntry
                                         ("pass2").GetCredential());
            Assert.AssertArrayEquals(passwd, provider.GetCredentialEntry("pass").GetCredential
                                         ());
            IList <string> creds = provider.GetAliases();

            Assert.True("Credentials should have been returned.", creds.Count
                        == 2);
            Assert.True("Returned Credentials should have included pass.",
                        creds.Contains("pass"));
            Assert.True("Returned Credentials should have included pass2.",
                        creds.Contains("pass2"));
        }