def testDecryptValues(self): """Test _DecryptValues().""" cars_schema = test_util.GetCarsSchema() jobs_schema = test_util.GetJobsSchema() master_key = test_util.GetMasterKey() field = '%sInvoice_Price' % util.HOMOMORPHIC_INT_PREFIX table = [[1], [2], [3]] cipher = ecrypto.HomomorphicIntCipher(master_key) ciphers = {util.HOMOMORPHIC_INT_PREFIX: cipher} table = self._EncryptTable(cipher, table, 0) table.append([None]) column = encrypted_bigquery_client._DecryptValues( field, table, 0, ciphers, cars_schema, util.HOMOMORPHIC_INT_PREFIX) self.assertEqual(column, [1, 2, 3, util.LiteralToken('null', None)]) field = 'citiesLived.job.%sposition' % util.PSEUDONYM_PREFIX table = [[0, unicode('Hello')], [1, unicode('My')], [-1, unicode('job')]] cipher = ecrypto.PseudonymCipher(master_key) ciphers = {util.PSEUDONYM_PREFIX: cipher} table = self._EncryptTable(cipher, table, 1) table.insert(1, [100, None]) column = encrypted_bigquery_client._DecryptValues( field, table, 1, ciphers, jobs_schema, util.PSEUDONYM_PREFIX) self.assertEqual(column, [ util.StringLiteralToken('"Hello"'), util.LiteralToken('null', None), util.StringLiteralToken('"My"'), util.StringLiteralToken('"job"') ]) field = '%snonexistent_field' % util.HOMOMORPHIC_FLOAT_PREFIX self.assertRaises(ValueError, encrypted_bigquery_client._DecryptValues, field, table, 1, ciphers, cars_schema, util.HOMOMORPHIC_FLOAT_PREFIX)
def testDecryptGroupConcatValues(self): cars_schema = test_util.GetCarsSchema() jobs_schema = test_util.GetJobsSchema() master_key = test_util.GetMasterKey() query = 'GROUP_CONCAT(%sModel)' % util.PROBABILISTIC_PREFIX cipher = ecrypto.ProbabilisticCipher(master_key) ciphers = {util.PROBABILISTIC_PREFIX: cipher} unencrypted_values = ([['A', 'B', 'C', 'D'], ['1', '2', '3', '4'], ['Hello', 'Bye']]) table = [] for values in unencrypted_values: encrypted_values = [] for token in values: encrypted_values.append(cipher.Encrypt(unicode(token))) table.append([','.join(encrypted_values), random.random()]) table.insert(0, [None, None]) column = encrypted_bigquery_client._DecryptGroupConcatValues( query, table, 0, ciphers, cars_schema, util.PROBABILISTIC_PREFIX) self.assertEqual(column, [ util.LiteralToken('null', None), util.StringLiteralToken('"A,B,C,D"'), util.StringLiteralToken('"1,2,3,4"'), util.StringLiteralToken('"Hello,Bye"') ]) query = ( 'GROUP_CONCAT(citiesLived.job.%sposition) within citiesLived.job' % util.PSEUDONYM_PREFIX) cipher = ecrypto.PseudonymCipher(master_key) ciphers = {util.PSEUDONYM_PREFIX: cipher} table = [] for values in unencrypted_values: encrypted_values = [] for token in values: encrypted_values.append(cipher.Encrypt(unicode(token))) table.append([','.join(encrypted_values)]) column = encrypted_bigquery_client._DecryptGroupConcatValues( query, table, 0, ciphers, jobs_schema, util.PSEUDONYM_PREFIX) self.assertEqual(column, [ util.StringLiteralToken('"A,B,C,D"'), util.StringLiteralToken('"1,2,3,4"'), util.StringLiteralToken('"Hello,Bye"') ]) query = '%sModel' % util.PROBABILISTIC_PREFIX self.assertRaises(ValueError, encrypted_bigquery_client._DecryptGroupConcatValues, query, table, 0, ciphers, cars_schema, util.PROBABILISTIC_PREFIX) query = ( 'GROUP_CONCAT(citiesLived.%snumberOfYears) within citiesLived' % util.HOMOMORPHIC_FLOAT_PREFIX) self.assertRaises(bigquery_client.BigqueryInvalidQueryError, encrypted_bigquery_client._DecryptGroupConcatValues, query, table, 0, ciphers, jobs_schema, util.HOMOMORPHIC_FLOAT_PREFIX)
def testGetEntryFromSchema(self): simple_schema = test_util.GetCarsSchema() nested_schema = test_util.GetJobsSchema() row = util.GetEntryFromSchema('Year', simple_schema) self.assertEqual(row['name'], 'Year') self.assertEqual(row['encrypt'], 'none') row = util.GetEntryFromSchema('citiesLived.place', nested_schema) self.assertEqual(row['name'], 'place') self.assertEqual(row['encrypt'], 'searchwords') row = util.GetEntryFromSchema('citiesLived.job.position', nested_schema) self.assertEqual(row['name'], 'position') self.assertEqual(row['encrypt'], 'pseudonym') row = util.GetEntryFromSchema('citiesLived.job', nested_schema) self.assertEqual(row, None) row = util.GetEntryFromSchema('citiesLived.non_existent_field', nested_schema) self.assertEqual(row, None)