def test_file_dispatcher_with_path_only(self): # Create a temporaryFile to get a file name tf = tempfile.NamedTemporaryFile('r') filename = tf.name tf.close() self.CONF.dispatcher_file.file_path = filename self.CONF.dispatcher_file.max_bytes = None self.CONF.dispatcher_file.backup_count = None dispatcher = file.FileDispatcher(self.CONF) # The number of the handlers should be 1 self.assertEqual(1, len(dispatcher.log.handlers)) # The handler should be RotatingFileHandler handler = dispatcher.log.handlers[0] self.assertTrue(isinstance(handler, logging.FileHandler)) msg = {'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, } msg['message_signature'] = rpc.compute_signature( msg, self.CONF.publisher_rpc.metering_secret, ) # The record_metering_data method should exist and not produce errors. dispatcher.record_metering_data(None, msg) # After the method call above, the file should have been created. self.assertTrue(os.path.exists(handler.baseFilename))
def test_file_dispatcher_with_path_only(self): # Create a temporaryFile to get a file name tf = tempfile.NamedTemporaryFile('r') filename = tf.name tf.close() cfg.CONF.dispatcher_file.file_path = filename cfg.CONF.dispatcher_file.max_bytes = None cfg.CONF.dispatcher_file.backup_count = None dispatcher = file.FileDispatcher(cfg.CONF) # The number of the handlers should be 1 self.assertEqual(1, len(dispatcher.log.handlers)) # The handler should be RotatingFileHandler handler = dispatcher.log.handlers[0] self.assertTrue(isinstance(handler, logging.FileHandler)) msg = { 'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, } msg['message_signature'] = rpc.compute_signature( msg, cfg.CONF.publisher_rpc.metering_secret, ) # The record_metering_data method should exist and not produce errors. dispatcher.record_metering_data(None, msg) # After the method call above, the file should have been created. self.assertTrue(os.path.exists(handler.baseFilename))
def test_verify_signature_nested(self): data = {'a': 'A', 'b': 'B', 'nested': {'a': 'A', 'b': 'B', }, } data['message_signature'] = rpc.compute_signature( data, 'not-so-secret') self.assertTrue(rpc.verify_signature(data, 'not-so-secret'))
def test_verify_signature_nested(): data = { 'a': 'A', 'b': 'B', 'nested': { 'a': 'A', 'b': 'B', }, } data['message_signature'] = rpc.compute_signature(data, 'not-so-secret') assert rpc.verify_signature(data, 'not-so-secret')
def test_verify_signature_nested_list_of_dict(self): small = 1 big = 1 << 64 nested = {small: 99, big: 42} data = {'a': 'A', 'b': 'B', 'nested': {'list': [nested]}} data['message_signature'] = rpc.compute_signature( data, 'not-so-secret') # the keys 1 and 1<<64 cause a hash collision on 64bit platforms data['nested']['list'] = [{big: 42, small: 99}] self.assertTrue(rpc.verify_signature(data, 'not-so-secret'))
def test_verify_signature_nested_json(): data = { 'a': 'A', 'b': 'B', 'nested': { 'a': 'A', 'b': 'B', 'c': ('c', ), 'd': ['d'] }, } data['message_signature'] = rpc.compute_signature(data, 'not-so-secret') jsondata = jsonutils.loads(jsonutils.dumps(data)) assert rpc.verify_signature(jsondata, 'not-so-secret')
def test_verify_signature_nested_json(self): data = {'a': 'A', 'b': 'B', 'nested': {'a': 'A', 'b': 'B', 'c': ('c',), 'd': ['d'] }, } data['message_signature'] = rpc.compute_signature( data, 'not-so-secret') jsondata = jsonutils.loads(jsonutils.dumps(data)) self.assertTrue(rpc.verify_signature(jsondata, 'not-so-secret'))
def test_valid_message(self): msg = {'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, } msg['message_signature'] = rpc.compute_signature( msg, self.CONF.publisher_rpc.metering_secret, ) with mock.patch.object(self.dispatcher.storage_conn, 'record_metering_data') as record_metering_data: self.dispatcher.record_metering_data(msg) record_metering_data.assert_called_once_with(msg)
def test_valid_message(self): msg = {'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, } msg['message_signature'] = rpc.compute_signature( msg, self.CONF.publisher_rpc.metering_secret, ) self.dispatcher.storage_conn = self.mox.CreateMock(base.Connection) self.dispatcher.storage_conn.record_metering_data(msg) self.mox.ReplayAll() self.dispatcher.record_metering_data(self.ctx, msg) self.mox.VerifyAll()
def test_timestamp_conversion(self): msg = {'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, 'timestamp': '2012-07-02T13:53:40Z', } msg['message_signature'] = rpc.compute_signature( msg, self.CONF.publisher_rpc.metering_secret, ) expected = msg.copy() expected['timestamp'] = datetime.datetime(2012, 7, 2, 13, 53, 40) with mock.patch.object(self.dispatcher.storage_conn, 'record_metering_data') as record_metering_data: self.dispatcher.record_metering_data(msg) record_metering_data.assert_called_once_with(expected)
def test_timestamp_tzinfo_conversion(self): msg = {'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, 'timestamp': '2012-09-30T15:31:50.262-08:00', } msg['message_signature'] = rpc.compute_signature( msg, self.CONF.publisher_rpc.metering_secret, ) expected = msg.copy() expected['timestamp'] = datetime.datetime(2012, 9, 30, 23, 31, 50, 262000) with mock.patch.object(self.dispatcher.storage_conn, 'record_metering_data') as record_metering_data: self.dispatcher.record_metering_data(msg) record_metering_data.assert_called_once_with(expected)
def test_timestamp_conversion(self): msg = {'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, 'timestamp': '2012-07-02T13:53:40Z', } msg['message_signature'] = rpc.compute_signature( msg, self.CONF.publisher_rpc.metering_secret, ) expected = {} expected.update(msg) expected['timestamp'] = datetime(2012, 7, 2, 13, 53, 40) self.dispatcher.storage_conn = self.mox.CreateMock(base.Connection) self.dispatcher.storage_conn.record_metering_data(expected) self.mox.ReplayAll() self.dispatcher.record_metering_data(self.ctx, msg)
def test_timestamp_tzinfo_conversion(self): msg = {'counter_name': 'test', 'resource_id': self.id(), 'counter_volume': 1, 'timestamp': '2012-09-30T15:31:50.262-08:00', } msg['message_signature'] = rpc.compute_signature( msg, self.CONF.publisher_rpc.metering_secret, ) expected = {} expected.update(msg) expected['timestamp'] = datetime(2012, 9, 30, 23, 31, 50, 262000) self.dispatcher.storage_conn = self.mox.CreateMock(base.Connection) self.dispatcher.storage_conn.record_metering_data(expected) self.mox.ReplayAll() self.dispatcher.record_metering_data(self.ctx, msg)
def test_file_dispatcher_with_all_config(self): # Create a temporaryFile to get a file name tf = tempfile.NamedTemporaryFile("r") filename = tf.name tf.close() cfg.CONF.dispatcher_file.file_path = filename cfg.CONF.dispatcher_file.max_bytes = 50 cfg.CONF.dispatcher_file.backup_count = 5 dispatcher = file.FileDispatcher(cfg.CONF) # The number of the handlers should be 1 self.assertEqual(1, len(dispatcher.log.handlers)) # The handler should be RotatingFileHandler handler = dispatcher.log.handlers[0] self.assertTrue(isinstance(handler, logging.handlers.RotatingFileHandler)) msg = {"counter_name": "test", "resource_id": self.id(), "counter_volume": 1} msg["message_signature"] = rpc.compute_signature(msg, cfg.CONF.publisher_rpc.metering_secret) # The record_metering_data method should exist and not produce errors. dispatcher.record_metering_data(None, msg) # After the method call above, the file should have been created. self.assertTrue(os.path.exists(handler.baseFilename))
def test_compute_signature_use_configured_secret(self): data = {'a': 'A', 'b': 'B'} sig1 = rpc.compute_signature(data, 'not-so-secret') sig2 = rpc.compute_signature(data, 'different-value') self.assertNotEqual(sig1, sig2)
def test_compute_signature_signed(): data = {'a': 'A', 'b': 'B'} sig1 = rpc.compute_signature(data, 'not-so-secret') data['message_signature'] = sig1 sig2 = rpc.compute_signature(data, 'not-so-secret') assert sig1 == sig2
def test_verify_signature_signed(self): data = {'a': 'A', 'b': 'B'} sig1 = rpc.compute_signature(data, 'not-so-secret') data['message_signature'] = sig1 self.assertTrue(rpc.verify_signature(data, 'not-so-secret'))
def test_compute_signature_signed(self): data = {'a': 'A', 'b': 'B'} sig1 = rpc.compute_signature(data, 'not-so-secret') data['message_signature'] = sig1 sig2 = rpc.compute_signature(data, 'not-so-secret') self.assertEqual(sig1, sig2)
def test_compute_signature_change_value(): sig1 = rpc.compute_signature({'a': 'A', 'b': 'B'}, 'not-so-secret') sig2 = rpc.compute_signature({'a': 'a', 'b': 'B'}, 'not-so-secret') assert sig1 != sig2
def test_compute_signature_use_configured_secret(): data = {'a': 'A', 'b': 'B'} sig1 = rpc.compute_signature(data, 'not-so-secret') sig2 = rpc.compute_signature(data, 'different-value') assert sig1 != sig2
def test_verify_signature_signed(): data = {'a': 'A', 'b': 'B'} sig1 = rpc.compute_signature(data, 'not-so-secret') data['message_signature'] = sig1 assert rpc.verify_signature(data, 'not-so-secret')
def test_compute_signature_same(): sig1 = rpc.compute_signature({'a': 'A', 'b': 'B'}, 'not-so-secret') sig2 = rpc.compute_signature({'a': 'A', 'b': 'B'}, 'not-so-secret') assert sig1 == sig2
def test_compute_signature_signed(self): data = {"a": "A", "b": "B"} sig1 = rpc.compute_signature(data, "not-so-secret") data["message_signature"] = sig1 sig2 = rpc.compute_signature(data, "not-so-secret") self.assertEqual(sig1, sig2)
def test_compute_signature_use_configured_secret(self): data = {"a": "A", "b": "B"} sig1 = rpc.compute_signature(data, "not-so-secret") sig2 = rpc.compute_signature(data, "different-value") self.assertNotEqual(sig1, sig2)
def test_compute_signature_same(self): sig1 = rpc.compute_signature({'a': 'A', 'b': 'B'}, 'not-so-secret') sig2 = rpc.compute_signature({'a': 'A', 'b': 'B'}, 'not-so-secret') self.assertEqual(sig1, sig2)
def test_verify_signature_signed(self): data = {"a": "A", "b": "B"} sig1 = rpc.compute_signature(data, "not-so-secret") data["message_signature"] = sig1 self.assertTrue(rpc.verify_signature(data, "not-so-secret"))
def test_verify_signature_nested(self): data = {"a": "A", "b": "B", "nested": {"a": "A", "b": "B"}} data["message_signature"] = rpc.compute_signature(data, "not-so-secret") self.assertTrue(rpc.verify_signature(data, "not-so-secret"))
def test_verify_signature_nested_json(self): data = {"a": "A", "b": "B", "nested": {"a": "A", "b": "B", "c": ("c",), "d": ["d"]}} data["message_signature"] = rpc.compute_signature(data, "not-so-secret") jsondata = jsonutils.loads(jsonutils.dumps(data)) self.assertTrue(rpc.verify_signature(jsondata, "not-so-secret"))
def test_compute_signature_same(self): sig1 = rpc.compute_signature({"a": "A", "b": "B"}, "not-so-secret") sig2 = rpc.compute_signature({"a": "A", "b": "B"}, "not-so-secret") self.assertEqual(sig1, sig2)