public async Task MaskingService_UpdateOccurrencesAsync_OccurrencesChange(string hash, string actual, int originalOccurrences, int updatedOccurrences) { MapRecord record = new MapRecord(hash, actual, originalOccurrences); await _mapDAO.CreateAsync(record).ConfigureAwait(false); await _maskingService.UpdateOccurrencesAsync(hash, updatedOccurrences).ConfigureAwait(false); MapObject obj = (MapObject)await _mapDAO.ReadByIdAsync(hash).ConfigureAwait(false); Assert.IsTrue(obj.Occurrences.Equals(updatedOccurrences)); await _mapDAO.DeleteByIdsAsync(new List <string>() { hash }).ConfigureAwait(false); }
/// <summary> /// Takes in a IMaskableRecord and masks all the data necessary based on which columns are masked in /// the DAO. /// </summary> /// <param name="record">The record to mask.</param> /// <param name="isCreate">Whether this is being used for a create (lets the function know whether to increment /// the mapping).</param> /// <returns>(IMaskableRecord) the masked version of the record</returns> public async Task <IMaskableRecord> MaskAsync(IMaskableRecord record, bool isCreate) { // If the record is already masked we will simply return what was passed. if (!record.IsMasked()) { // Get the mask information: a list of tuples<object, bool>, the data and whether it's column is masked. List <Tuple <object, bool> > info = record.GetMaskInformation(); // The list of objects to pass to the constructor. object[] parameters = new object[info.Count]; for (int i = 0; i < info.Count; i++) { // Loop through the list and get all individual tuples. Tuple <object, bool> data = info[i]; // If the data's column is masked... if (data.Item2) { // Only compute the hash if data is being inserted into the table (i.e. not null or -1). bool computeHash = true; if (data.Item1 is int) { if ((int)data.Item1 == -1) { computeHash = false; } } if (data.Item1 == null) { computeHash = false; } if (data.Item1 is long) { if ((long)data.Item1 == -1) { computeHash = false; } } if (computeHash) { // Convert everything to a string including numerical values (to store in the "actual" column of the MapDAO). string input = data.Item1.ToString(); string hashString; // If the data is a string, simply double hash the input. if (data.Item1 is string) { string hash = SecurityService.HashWithSHA256(SecurityService.HashWithSHA256(input)); parameters[i] = hash; hashString = hash; } else { // Otherwise get the hash is the count of the amount of times this code block has been reached. // Our current approach to hashing integer values: int count = Int32.Parse(Environment.GetEnvironmentVariable("COUNT", EnvironmentVariableTarget.User)); int hash = count; parameters[i] = hash; hashString = hash.ToString(); Environment.SetEnvironmentVariable("COUNT", (count + 1).ToString(), EnvironmentVariableTarget.User); } // If this is the first time this hash string has been seen, create the record in the MapDAO with an occurrence of 1. if (!await _mapDAO.CheckHashExistenceAsync(hashString).ConfigureAwait(false)) { MapRecord mapRecord = new MapRecord(hashString, input, 1); await _mapDAO.CreateAsync(mapRecord).ConfigureAwait(false); } else if (isCreate) { // Otherwise, if this function is being used in a creation, increment the occurrences. Do nothing otherwise. MapObject mapObj = await _mapDAO.ReadByIdAsync(hashString).ConfigureAwait(false) as MapObject; await UpdateOccurrencesAsync(hashString, mapObj.Occurrences + 1).ConfigureAwait(false); } // HACK: should log here but this ruins the tests... (ASK VONG) //await LoggingService.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString), Constants.MapTableModifiedOperation, // Constants.SystemIdentifier, Constants.LocalHost).ConfigureAwait(false); } else { // If the computation was not needed, keep the same data. parameters[i] = data.Item1; } } else { // If the column was not masked, keep the same data. parameters[i] = data.Item1; } } // Create a record of the type that was passed to this function by getting the constructor and invoking it with // the collected parameters. IMaskableRecord resultRecord = (IMaskableRecord)record.GetType().GetConstructor(record.GetParameterTypes()).Invoke(parameters); resultRecord.SetToMasked(); return(resultRecord); } else { return(record); } }