Example #1
0
        /// <exception cref="System.Exception"/>
        internal static void CheckSpecificProvider(Configuration conf, string ourUrl)
        {
            KeyProvider provider = KeyProviderFactory.GetProviders(conf)[0];

            byte[] key1 = new byte[16];
            byte[] key2 = new byte[16];
            byte[] key3 = new byte[16];
            for (int i = 0; i < key1.Length; ++i)
            {
                key1[i] = unchecked ((byte)i);
                key2[i] = unchecked ((byte)(i * 2));
                key3[i] = unchecked ((byte)(i * 3));
            }
            // ensure that we get nulls when the key isn't there
            Assert.Equal(null, provider.GetKeyVersion("no-such-key"));
            Assert.Equal(null, provider.GetMetadata("key"));
            // create a new key
            try
            {
                provider.CreateKey("key3", key3, KeyProvider.Options(conf));
            }
            catch (Exception e)
            {
                Runtime.PrintStackTrace(e);
                throw;
            }
            // check the metadata for key3
            KeyProvider.Metadata meta = provider.GetMetadata("key3");
            Assert.Equal(KeyProvider.DefaultCipher, meta.GetCipher());
            Assert.Equal(KeyProvider.DefaultBitlength, meta.GetBitLength()
                         );
            Assert.Equal(1, meta.GetVersions());
            // make sure we get back the right key
            Assert.AssertArrayEquals(key3, provider.GetCurrentKey("key3").GetMaterial());
            Assert.Equal("key3@0", provider.GetCurrentKey("key3").GetVersionName
                             ());
            // try recreating key3
            try
            {
                provider.CreateKey("key3", key3, KeyProvider.Options(conf));
                Assert.True("should throw", false);
            }
            catch (IOException e)
            {
                Assert.Equal("Key key3 already exists in " + ourUrl, e.Message
                             );
            }
            provider.DeleteKey("key3");
            try
            {
                provider.DeleteKey("key3");
                Assert.True("should throw", false);
            }
            catch (IOException e)
            {
                Assert.Equal("Key key3 does not exist in " + ourUrl, e.Message
                             );
            }
            provider.CreateKey("key3", key3, KeyProvider.Options(conf));
            try
            {
                provider.CreateKey("key4", key3, KeyProvider.Options(conf).SetBitLength(8));
                Assert.True("should throw", false);
            }
            catch (IOException e)
            {
                Assert.Equal("Wrong key length. Required 8, but got 128", e.Message
                             );
            }
            provider.CreateKey("key4", new byte[] { 1 }, KeyProvider.Options(conf).SetBitLength
                                   (8));
            provider.RollNewVersion("key4", new byte[] { 2 });
            meta = provider.GetMetadata("key4");
            Assert.Equal(2, meta.GetVersions());
            Assert.AssertArrayEquals(new byte[] { 2 }, provider.GetCurrentKey("key4").GetMaterial
                                         ());
            Assert.AssertArrayEquals(new byte[] { 1 }, provider.GetKeyVersion("key4@0").GetMaterial
                                         ());
            Assert.Equal("key4@1", provider.GetCurrentKey("key4").GetVersionName
                             ());
            try
            {
                provider.RollNewVersion("key4", key1);
                Assert.True("should throw", false);
            }
            catch (IOException e)
            {
                Assert.Equal("Wrong key length. Required 8, but got 128", e.Message
                             );
            }
            try
            {
                provider.RollNewVersion("no-such-key", key1);
                Assert.True("should throw", false);
            }
            catch (IOException e)
            {
                Assert.Equal("Key no-such-key not found", e.Message);
            }
            provider.Flush();
            // get a new instance of the provider to ensure it was saved correctly
            provider = KeyProviderFactory.GetProviders(conf)[0];
            Assert.AssertArrayEquals(new byte[] { 2 }, provider.GetCurrentKey("key4").GetMaterial
                                         ());
            Assert.AssertArrayEquals(key3, provider.GetCurrentKey("key3").GetMaterial());
            Assert.Equal("key3@0", provider.GetCurrentKey("key3").GetVersionName
                             ());
            IList <string> keys = provider.GetKeys();

            Assert.True("Keys should have been returned.", keys.Count == 2);
            Assert.True("Returned Keys should have included key3.", keys.Contains
                            ("key3"));
            Assert.True("Returned Keys should have included key4.", keys.Contains
                            ("key4"));
            IList <KeyProvider.KeyVersion> kvl = provider.GetKeyVersions("key3");

            Assert.True("KeyVersions should have been returned for key3.",
                        kvl.Count == 1);
            Assert.True("KeyVersions should have included key3@0.", kvl[0].
                        GetVersionName().Equals("key3@0"));
            Assert.AssertArrayEquals(key3, kvl[0].GetMaterial());
        }
 /// <exception cref="System.IO.IOException"/>
 public override IList <KeyProvider.KeyVersion> GetKeyVersions(string name)
 {
     return(keyProvider.GetKeyVersions(name));
 }