private void ProcessUpdateFile(Guid appGuid, string filename, bool isHeroicUpdate = false) { if (!File.Exists(filename)) { if (isHeroicUpdate) { throw new CBLoaderException($"Expected file {filename} not found."); } return; } Log.Debug($" - Parsing {Path.GetFileName(filename)}"); var document = XDocument.Load(filename); switch (document.Root.Name.LocalName.ToLower()) { case "applications": var applicationTag = document.Root.Elements() .First(x => x.Name.LocalName == "Application" && x.Attribute("ID").Value == appGuid.ToString()); foreach (var element in applicationTag.Elements()) { var match = UPDATE_REGEX.Match(element.Name.LocalName); var id = new Guid(match.Groups[1].Value); var key = Convert.FromBase64String(element.Value); keyStore.AddKey(id, key); } if (isHeroicUpdate) { keyStore.WriteGuid = new Guid(applicationTag.Attribute("CurrentUpdate").Value); } break; case "cbloaderkeystore": var otherStore = (KeyStore)SERIALIZER.Deserialize(new StringReader(document.ToString())); foreach (var key in otherStore.UpdateKeys) { keyStore.AddKey(key.Id, key.KeyData); } if (otherStore.WriteGuid != null) { keyStore.WriteGuid = otherStore.WriteGuid; } if (otherStore.FallbackKey != null) { keyStore.FallbackKey = otherStore.FallbackKey; } break; default: throw new Exception("Unknown key store type."); } if (isHeroicUpdate && (keyStore.WriteGuid == null || keyStore.Get(keyStore.WriteGuid) == null)) { throw new CBLoaderException($"Key file {filename} is not valid."); } }
public void Should_be_able_to_use_key_store() { var store = new KeyStore(DatabaseGateway, new KeyStoreQueryFactory()); var id = Guid.NewGuid(); var value = string.Concat("value=", id.ToString()); var anotherValue = string.Concat("anotherValue=", id.ToString()); using (DatabaseContextFactory.Create(EventStoreConnectionStringName)) { store.Add(id, value); Assert.Throws <SqlException>(() => store.Add(id, value)); var idGet = store.Get(value); Assert.IsNotNull(idGet); Assert.AreEqual(id, idGet); idGet = store.Get(anotherValue); Assert.IsNull(idGet); } }
public async Task KeyStore_Get() { var store = new Mock <IBlobStore>(); var policy = new EncryptionPolicy(); var idPolicy = policy.Id; var keyStore = new KeyStore(store.Object, new PassThruEncryptor()); var key = new Key(policy); var cbResult = ""; store.Setup(s => s.Put(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Encoding>())).Callback <string, string, Encoding>((id, data, encoding) => { cbResult = data; }); await keyStore.Add(key); var store2 = new Mock <IBlobStore>(); keyStore = new KeyStore(store2.Object, new PassThruEncryptor()); store2.Setup(s => s.Get(It.IsAny <string>(), It.IsAny <Encoding>())).ReturnsAsync(cbResult); var result = await keyStore.Get(idPolicy); Assert.AreEqual(policy.Id, result.Id); Assert.AreEqual(policy.Id, result.Policy.Id); Assert.AreEqual(policy.KeySize, result.Policy.KeySize); Assert.AreEqual(policy.BlockSize, result.Policy.BlockSize); Assert.AreEqual(policy.Padding, result.Policy.Padding); Assert.AreEqual(policy.Mode, result.Policy.Mode); Assert.AreEqual(policy.Algorithm, result.Policy.Algorithm); Assert.AreEqual(policy.Expires, result.Policy.Expires); }
public void Should_be_able_to_use_key_store() { var store = new KeyStore(DatabaseGateway, new KeyStoreQueryFactory(new ScriptProvider(new ScriptProviderConfiguration()))); using (DatabaseContextFactory.Create(EventStoreConnectionStringName)) { var id = OrderId; var value = string.Concat("value=", id.ToString()); var anotherValue = string.Concat("anotherValue=", id.ToString()); store.Add(id, value); Assert.Throws <DuplicateKeyException>(() => store.Add(id, value), $"Should not be able to add duplicate key / id = {id} / key = '{value}' / (ensure that your implementation throws a `DuplicateKeyException`)"); var idGet = store.Get(value); Assert.IsNotNull(idGet, $"Should be able to retrieve the id of the associated key / id = {id} / key = '{value}'"); Assert.AreEqual(id, idGet, $"Should be able to retrieve the correct id of the associated key / id = {id} / key = '{value}' / id retrieved = {idGet}"); idGet = store.Get(anotherValue); Assert.IsNull(idGet, $"Should not be able to get id of non-existent / id = {id} / key = '{anotherValue}'"); store.Remove(id); idGet = store.Get(value); Assert.IsNull(idGet, $"Should be able to remove association using id (was not removed) / id = {id} / key = '{value}'"); store.Add(id, value); store.Remove(value); idGet = store.Get(value); Assert.IsNull(idGet, $"Should be able to remove association using key (was not removed) / id = {id} / key = '{value}'"); } }
public void RemoveKeyTest() { var path = new Settings("remove.dat"); var rdb = new KeyStore<long>(_fileSystem, path); rdb.Set(1, "a"); rdb.Set(2, "b"); rdb.Dispose(); rdb = new KeyStore<long>(_fileSystem, path); rdb.Remove(1L); rdb.Dispose(); rdb = new KeyStore<long>(_fileSystem, path); string data; bool result = rdb.Get(1, out data); if (result) Assert.Fail(); }
private static void readthread(KeyStore<Guid> rap, List<Guid> guids, int count, char c) { Thread.Sleep(5000); int notfound = 0; for (int i = 0; i < count; i++) { string bb; if (rap.Get(guids[i], out bb)) { if (bb != "" + i) notfound++; } else notfound++; if (i % 100000 == 0) { Console.Write(c); } } if (notfound > 0) { Console.WriteLine("not found = " + notfound); Assert.Fail(); } Console.WriteLine("read done"); }