Пример #1
0
        public void NullFilterTest()
        {
            var filter1 = new FindFilter();
            var filter2 = new FindFilter();

            foreach (var field in Enum.GetValues(typeof(StringField)).Cast <StringField>())
            {
                filter1.KeyIsNull(field);
                filter2.KeyIsNotNull(field);
            }

            foreach (var field in Enum.GetValues(typeof(NumberField)).Cast <NumberField>())
            {
                filter1.KeyIsNull(field);
                filter2.KeyIsNotNull(field);
            }

            using var provider = new CryptoProvider();
            var transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8));

            Assert.IsTrue(JsonEquals(JsonFilterNull,
                                     JsonConvert.SerializeObject(transformer.TransformFilter(filter1), s_jsonSettings)));
            Assert.IsTrue(JsonEquals(JsonFilterNotNull,
                                     JsonConvert.SerializeObject(transformer.TransformFilter(filter2), s_jsonSettings)));
        }
Пример #2
0
        public void GetFilterWithNonHashingModeTest()
        {
            using var cryptoManager = new CryptoProvider();
            var hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer  = new DtoTransformer(cryptoManager, hashProvider, false);

            var filter = new FindFilter()
                         .KeyEq(StringField.RecordKey, "some record key")
                         .KeyEq(StringField.Key1, "some value of key1");
            var filterContainer = transformer.TransformFilter(filter);

            var jsonSettings = new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver {
                    NamingStrategy = new SnakeCaseNamingStrategy()
                },
                Formatting           = Formatting.None,
                NullValueHandling    = NullValueHandling.Ignore,
                DefaultValueHandling = DefaultValueHandling.Include
            };
            var json = JsonConvert.SerializeObject(filterContainer, jsonSettings);

            Assert.AreEqual("{\"filter\":{\"record_key\":[\"2ab632ee5ebf3af90be1ae6accea46d99843fdc4676bb29a376919fa9c530364\"]," +
                            "\"key1\":[\"some value of key1\"]}," +
                            "\"options\":{\"limit\":100,\"offset\":0}}",
                            json);
        }
Пример #3
0
        public void GetSecretsDataNegativeTest()
        {
            using var cryptoManager = new CryptoProvider();
            var          hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            const string errorMessage = "testError";
            var          transformer  = new DtoTransformer(cryptoManager, hashProvider, true,
                                                           () => throw new StorageClientException(errorMessage));
            var exception = Assert.Throws <StorageClientException>(() =>
                                                                   transformer.GetRecord(new TransferRecord("recordKey", "recordBody")));

            Assert.AreEqual(errorMessage, exception.Message);

            transformer = new DtoTransformer(cryptoManager, hashProvider, true,
                                             () => throw new NotImplementedException());
            exception = Assert.Throws <StorageClientException>(() =>
                                                               transformer.GetRecord(new TransferRecord("recordKey", "recordBody")));
            Assert.AreEqual("Unexpected error", exception.Message);
            Assert.IsNotNull(exception.InnerException);
            Assert.IsInstanceOf <NotImplementedException>(exception.InnerException);

            transformer = new DtoTransformer(cryptoManager, hashProvider, true, () => null);
            exception   = Assert.Throws <StorageClientException>(() =>
                                                                 transformer.GetRecord(new TransferRecord("recordKey", "recordBody")));
            Assert.AreEqual("Secret accessor returns null secret", exception.Message);
        }
Пример #4
0
        public void TransformFindFilterTest()
        {
            using var cryptoManager = new CryptoProvider();
            var hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer  = new DtoTransformer(cryptoManager, hashProvider);

            var filter = new FindFilter()
                         .KeyEq(StringField.Key1, "value1")
                         .KeyEq(NumberField.Version, 0);

            var jsonSettings = new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver {
                    NamingStrategy = new SnakeCaseNamingStrategy()
                },
                Formatting           = Formatting.None,
                NullValueHandling    = NullValueHandling.Ignore,
                DefaultValueHandling = DefaultValueHandling.Include
            };

            var transformFilter = transformer.TransformFilter(filter);

            Assert.IsTrue(FindFilterTest.JsonEquals(
                              "{\"filter\":{\"key1\":[\"327c84457a2ff2c6da36314dc0ffb3216a283570a5c4654d5f51947e74742cf0\"]," +
                              "\"version\":[0]},\"options\":{\"limit\":100,\"offset\":0}}",
                              JsonConvert.SerializeObject(transformFilter, jsonSettings)));
        }
Пример #5
0
        public void GetRecordFromNullTest()
        {
            using var cryptoManager = new CryptoProvider();
            var hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer  = new DtoTransformer(cryptoManager, hashProvider);
            var record       = transformer.GetRecord(null);

            Assert.IsNull(record);
        }
Пример #6
0
        public void GetTransferRecordFromNullTest()
        {
            using var cryptoManager = new CryptoProvider();
            var hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer  = new DtoTransformer(cryptoManager, hashProvider);
            var exception    = Assert.Throws <StorageClientException>(() => transformer.GetTransferRecord(null));

            Assert.AreEqual("Record is null", exception.Message);
        }
Пример #7
0
        public void AllFieldsFilterTest()
        {
            var filter = new FindFilter()
                         .KeyEq(StringField.RecordKey, "rec_key")
                         .KeyEq(StringField.ParentKey, "par_key")
                         .KeyEq(StringField.ProfileKey, "prof_key")
                         .KeyEq(StringField.ServiceKey1, "serv1_key")
                         .KeyEq(StringField.ServiceKey2, "serv2_key")
                         .KeyEq(StringField.Key1, "_key1")
                         .KeyEq(StringField.Key2, "_key2")
                         .KeyEq(StringField.Key3, "_key3")
                         .KeyEq(StringField.Key4, "_key4")
                         .KeyEq(StringField.Key5, "_key5")
                         .KeyEq(StringField.Key6, "_key6")
                         .KeyEq(StringField.Key7, "_key7")
                         .KeyEq(StringField.Key8, "_key8")
                         .KeyEq(StringField.Key9, "_key9")
                         .KeyEq(StringField.Key10, "_key10")
                         .KeyEq(StringField.Key11, "_key11")
                         .KeyEq(StringField.Key12, "_key12")
                         .KeyEq(StringField.Key13, "_key13")
                         .KeyEq(StringField.Key14, "_key14")
                         .KeyEq(StringField.Key15, "_key15")
                         .KeyEq(StringField.Key16, "_key16")
                         .KeyEq(StringField.Key17, "_key17")
                         .KeyEq(StringField.Key18, "_key18")
                         .KeyEq(StringField.Key19, "_key19")
                         .KeyEq(StringField.Key20, "_key20")
                         .KeyEq(NumberField.Version, 123)
                         .KeyEq(NumberField.RangeKey1, 1)
                         .KeyEq(NumberField.RangeKey2, 2)
                         .KeyEq(NumberField.RangeKey3, 3)
                         .KeyEq(NumberField.RangeKey4, 4)
                         .KeyEq(NumberField.RangeKey5, 5)
                         .KeyEq(NumberField.RangeKey6, 6)
                         .KeyEq(NumberField.RangeKey7, 7)
                         .KeyEq(NumberField.RangeKey8, 8)
                         .KeyEq(NumberField.RangeKey9, 9)
                         .KeyEq(NumberField.RangeKey10, 10);

            using var provider = new CryptoProvider();
            var transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8));

            Assert.IsTrue(JsonEquals(JsonFilterRegular,
                                     JsonConvert.SerializeObject(transformer.TransformFilter(filter), s_jsonSettings)));

            transformer = new DtoTransformer(provider, new HashUtils("envId", true, Encoding.UTF8));
            Assert.IsTrue(JsonEquals(JsonFilterNormalizeKeys,
                                     JsonConvert.SerializeObject(transformer.TransformFilter(filter), s_jsonSettings)));

            transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8), false);
            Assert.IsTrue(JsonEquals(JsonFilterNonHashing,
                                     JsonConvert.SerializeObject(transformer.TransformFilter(filter), s_jsonSettings)));
        }
Пример #8
0
        public void GetRecordWithNonHashingModeTest()
        {
            using var cryptoManager = new CryptoProvider();
            var hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer  = new DtoTransformer(cryptoManager, hashProvider, false);

            var record          = new Record("hashed_RecordKey", key1: "non_hashed_key1");
            var transformRecord = transformer.GetTransferRecord(record);

            Assert.AreEqual("f09b57bd0932fd2ec1f4ba63f1cbc77a2f02efac25c31a9a0d1ed3a078330524",
                            transformRecord.RecordKey);
            Assert.AreEqual("non_hashed_key1", transformRecord.Key1);
        }
Пример #9
0
        public void StringKeyNotEqTest()
        {
            using var provider = new CryptoProvider();
            var transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8));
            var filter      = new FindFilter()
                              .KeyNotEq(StringField.Key1, "value1", "value2");

            Assert.AreEqual(
                "{\"filter\":{\"key1\":{\"$not\":[\"327c84457a2ff2c6da36314dc0ffb3216a283570a5c4654d5f51947e74742cf0\"," +
                "\"04a29359b09bb94b4d6d923fc169c125b3b5253aac37c405dc5f5241535627b4\"]}}," +
                "\"options\":{\"limit\":100,\"offset\":0}}",
                JsonConvert.SerializeObject(transformer.TransformFilter(filter), s_jsonSettings));
        }
Пример #10
0
        public void GetTransferRecordWithNullRequiredFieldsTest()
        {
            using var cryptoManager = new CryptoProvider();
            var hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer  = new DtoTransformer(cryptoManager, hashProvider);
            var exception    =
                Assert.Throws <StorageServerException>(() => transformer.GetRecord(new TransferRecord(null)));

            Assert.AreEqual("Null required record fields: recordKey", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetRecord(new TransferRecord("recordKey")));
            Assert.AreEqual("Null required record fields: body", exception.Message);
        }
Пример #11
0
        public void GetFindResultNegativeTest()
        {
            using var cryptoManager = new CryptoProvider();
            var hashProvider = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer  = new DtoTransformer(cryptoManager, hashProvider);


            var exception = Assert.Throws <StorageServerException>(() => transformer.GetFindResult(null));

            Assert.AreEqual("Response error: Find response is null", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(null, null)));
            Assert.AreEqual("Response error: Find result metadata is null", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(null, new TransferFindResult.FindMeta(1, 1, 1, -1))));
            Assert.AreEqual("Response error: Negative values in find result metadata", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(null, new TransferFindResult.FindMeta(1, 1, -1, 1))));
            Assert.AreEqual("Response error: Negative values in find result metadata", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(null, new TransferFindResult.FindMeta(1, -1, 1, 1))));
            Assert.AreEqual("Response error: Negative values in find result metadata", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(null, new TransferFindResult.FindMeta(-1, 1, 1, 1))));
            Assert.AreEqual("Response error: Negative values in find result metadata", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(null, new TransferFindResult.FindMeta(1, 1, 1, 1))));
            Assert.AreEqual("Response error: count in find results metadata differs from data size", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(new List <TransferRecord>(),
                                                                                                                new TransferFindResult.FindMeta(1, 1, 1, 1))));
            Assert.AreEqual("Response error: count in find results metadata differs from data size", exception.Message);

            exception = Assert.Throws <StorageServerException>(() =>
                                                               transformer.GetFindResult(new TransferFindResult(
                                                                                             new List <TransferRecord> {
                new TransferRecord("1"), new TransferRecord("2")
            },
                                                                                             new TransferFindResult.FindMeta(1, 1, 1, 2))));
            Assert.AreEqual("Response error: incorrect total in find results metadata, less then received",
                            exception.Message);
        }
Пример #12
0
        public void KeyLessAndGreaterTest()
        {
            using var provider = new CryptoProvider();
            var transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8));
            var filter      = new FindFilter()
                              .KeyGreater(NumberField.RangeKey1, 1)
                              .KeyGreater(NumberField.RangeKey2, 2, false)
                              .KeyLess(NumberField.RangeKey3, 3)
                              .KeyLess(NumberField.RangeKey4, 4, false);

            Assert.AreEqual("{\"filter\":{\"range_key1\":{\"$gte\":1},\"range_key2\":{\"$gt\":2}," +
                            "\"range_key3\":{\"$lte\":3},\"range_key4\":{\"$lt\":4}}," +
                            "\"options\":{\"limit\":100,\"offset\":0}}",
                            JsonConvert.SerializeObject(transformer.TransformFilter(filter), s_jsonSettings));
        }
Пример #13
0
        public void TestDecryptionFromOtherSdk(Record record, string jsonResponse)
        {
            var secretData = SecretsDataGenerator.FromPassword(Password);

            using var cryptoManager = new CryptoProvider();
            var decodedRecord =
                new DtoTransformer(cryptoManager,
                                   new HashUtils(EnvironmentId, false, Encoding.UTF8), true, () => secretData)
                .GetRecord(JsonConvert.DeserializeObject <TransferRecord>(jsonResponse, _jsonSettings));

            Assert.NotNull(decodedRecord);
#pragma warning disable CA1062
            Assert.IsTrue(RecordEquals(record, decodedRecord));
#pragma warning restore CA1062
        }
Пример #14
0
        public void KeyBetweenTest()
        {
            using var provider = new CryptoProvider();
            var transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8));
            var filter      = new FindFilter()
                              .KeyBetween(NumberField.RangeKey1, 1L, 2L)
                              .KeyBetween(NumberField.RangeKey2, 3L, 4L, true, false)
                              .KeyBetween(NumberField.RangeKey3, 5L, 6L, false, false)
                              .KeyBetween(NumberField.RangeKey4, 7L, 8L, false);

            Assert.AreEqual(
                "{\"filter\":{\"range_key1\":{\"$gte\":1,\"$lte\":2},\"range_key2\":{\"$gte\":3,\"$lt\":4}," +
                "\"range_key3\":{\"$gt\":5,\"$lt\":6},\"range_key4\":{\"$gt\":7,\"$lte\":8}}," +
                "\"options\":{\"limit\":100,\"offset\":0}}",
                JsonConvert.SerializeObject(transformer.TransformFilter(filter), s_jsonSettings));
        }
Пример #15
0
        public void SortingTest()
        {
            var filter1 = new FindFilter().KeyEq(NumberField.RangeKey1, 1);
            var filter2 = new FindFilter().KeyEq(NumberField.RangeKey1, 2);

            foreach (var field in Enum.GetValues(typeof(SortField)).Cast <SortField>())
            {
                filter1.SortBy(field, SortOrder.Asc);
                filter2.SortBy(field, SortOrder.Desc);
            }

            using var provider = new CryptoProvider();
            var transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8));

            Assert.IsTrue(JsonEquals(JsonFilterSortingAsc,
                                     JsonConvert.SerializeObject(transformer.TransformFilter(filter1), s_jsonSettings)));
            Assert.IsTrue(JsonEquals(JsonFilterSortingDesc,
                                     JsonConvert.SerializeObject(transformer.TransformFilter(filter2), s_jsonSettings)));
        }
Пример #16
0
        public void SearchKeysTest()
        {
            using var provider = new CryptoProvider();
            var transformer = new DtoTransformer(provider, new HashUtils("envId", false, Encoding.UTF8));
            var filter      = new FindFilter()
                              .KeyEq(StringField.RecordKey, "some record key")
                              .SearchKeysLike("some regex value")
                              .KeyEq(NumberField.RangeKey1, 1)
                              .KeyNotEq(StringField.ProfileKey, "some profile key");


            Assert.IsTrue(JsonEquals(
                              "{\"filter\":{\"record_key\":[\"2ab632ee5ebf3af90be1ae6accea46d99843fdc4676bb29a376919fa9c530364\"]," +
                              "\"profile_key\":{\"$not\":[\"3075bffaf3d67f04c2ab25a53ae970d25350bf7abe63f977aeaa29366baf38fa\"]}," +
                              "\"range_key1\":[1]," +
                              "\"search_keys\":\"some regex value\"}," +
                              "\"options\":{\"limit\":100,\"offset\":0}}",
                              JsonConvert.SerializeObject(transformer.TransformFilter(filter), s_jsonSettings)));
        }
Пример #17
0
        public void GetTransferRecordWithNullBodyTest()
        {
            const string recordKey = "someRecordKey";
            var          record    = new Record(recordKey);

            using var cryptoManager = new CryptoProvider();
            var hashProvider   = new HashUtils(EnvId, false, Encoding.UTF8);
            var transformer    = new DtoTransformer(cryptoManager, hashProvider);
            var transferRecord = transformer.GetTransferRecord(record);

            Assert.NotNull(transferRecord);
            Assert.AreEqual("6717f3b00ef569e5604566335ff0b10a58a236a07a066372ede034ed5b732690",
                            transferRecord.RecordKey);
            Assert.NotNull(transferRecord.Body);
            var restoredRecord = transformer.GetRecord(transferRecord);

            Assert.NotNull(restoredRecord);
            Assert.AreEqual(recordKey, restoredRecord.RecordKey);
            Assert.IsNull(restoredRecord.Body);
        }
Пример #18
0
        private Storage(StorageConfig config)
        {
            s_helper.Check <StorageClientException>(config == null, Messages.Storage.s_errNullConfig);
            _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(VersionInfo.ProductName,
                                                                                       VersionInfo.ProductVersion));
#pragma warning disable CA1062
            _httpClient.Timeout = new TimeSpan(0, 0, config.HttpTimeout);
#pragma warning restore CA1062
            ITokenClient tokenClient = null;
            if (!string.IsNullOrEmpty(config.ApiKey))
            {
                tokenClient = new ApiKeyTokenClient(config.ApiKey);
            }

            if (!(string.IsNullOrEmpty(config.ClientId) || string.IsNullOrEmpty(config.ClientSecret)))
            {
                tokenClient = new OAuthTokenClient(config.DefaultAuthEndpoint, config.AuthEndpoints,
                                                   config.EnvironmentId, config.ClientId, config.ClientSecret, _httpClient);
            }


            s_helper.Check <StorageClientException>(tokenClient == null, Messages.Storage.s_errNullCredentials);
            _cryptoProvider = config.CryptoProvider;
            if (config.CryptoProvider == null)
            {
                _cryptoProvider = new CryptoProvider();
            }

            _cryptoProvider.ValidateCustomCiphers(config.SecretKeyAccessor?.Invoke());
            _hashUtils   = new HashUtils(config.EnvironmentId, config.NormalizeKeys, Encoding.UTF8);
            _transformer = new DtoTransformer(_cryptoProvider, _hashUtils, config.HashSearchKeys,
                                              config.SecretKeyAccessor);
            _dao = HttpDao.NewDao(config.EnvironmentId, tokenClient, _httpClient, config.EndPoint, config.EndpointMask,
                                  config.CountriesEndPoint);
        }