This file is indexed.

/usr/lib/python2.7/dist-packages/hyperv/tests/unit/neutron/test_security_groups_driver.py is in python-networking-hyperv 2.0.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# Copyright 2014 Cloudbase Solutions SRL
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Unit tests for the Hyper-V Security Groups Driver.
"""

import mock
from os_win import exceptions
from os_win import utilsfactory
from oslo_config import cfg

from hyperv.neutron import security_groups_driver as sg_driver
from hyperv.tests import base

CONF = cfg.CONF


class SecurityGroupRuleTestHelper(base.BaseTestCase):
    _FAKE_DIRECTION = 'egress'
    _FAKE_ETHERTYPE = 'IPv4'
    _FAKE_ETHERTYPE_IPV6 = 'IPv6'
    _FAKE_PROTOCOL = 'tcp'
    _FAKE_ACTION = sg_driver.ACL_PROP_MAP['action']['allow']
    _FAKE_DEST_IP_PREFIX = '10.0.0.0/24'
    _FAKE_SOURCE_IP_PREFIX = '10.0.1.0/24'
    _FAKE_MEMBER_IP = '10.0.0.1'
    _FAKE_IPV6_LEN128_IP = 'fddd:cafd:e664:0:f816:3eff:fe8d:59d2/128'
    _FAKE_SG_ID = 'fake_sg_id'

    _FAKE_PORT_MIN = 9001
    _FAKE_PORT_MAX = 9011

    def _create_security_rule(self):
        return {
            'direction': self._FAKE_DIRECTION,
            'ethertype': self._FAKE_ETHERTYPE,
            'protocol': self._FAKE_PROTOCOL,
            'dest_ip_prefix': self._FAKE_DEST_IP_PREFIX,
            'source_ip_prefix': self._FAKE_SOURCE_IP_PREFIX,
            'port_range_min': self._FAKE_PORT_MIN,
            'port_range_max': self._FAKE_PORT_MAX,
            'security_group_id': self._FAKE_SG_ID
        }

    @classmethod
    def _acl(self, key1, key2):
        return sg_driver.ACL_PROP_MAP[key1][key2]


class TestHyperVSecurityGroupsDriver(SecurityGroupRuleTestHelper):

    _FAKE_DEVICE = 'fake_device'
    _FAKE_ID = 'fake_id'
    _FAKE_PARAM_NAME = 'fake_param_name'
    _FAKE_PARAM_VALUE = 'fake_param_value'

    def setUp(self):
        super(TestHyperVSecurityGroupsDriver, self).setUp()
        utilsfactory_patcher = mock.patch.object(utilsfactory, '_get_class')
        utilsfactory_patcher.start()
        self.addCleanup(utilsfactory_patcher.stop)

        self._driver = sg_driver.HyperVSecurityGroupsDriver()
        self._driver._utils = mock.MagicMock()
        self._driver._sg_gen = mock.MagicMock()

    def test__select_sg_rules_for_port(self):
        mock_port = self._get_port()
        mock_port['fixed_ips'] = [mock.MagicMock()]
        mock_port['security_groups'] = [self._FAKE_SG_ID]

        fake_sg_template = self._create_security_rule()
        fake_sg_template['direction'] = 'ingress'
        self._driver._sg_rule_templates[self._FAKE_SG_ID] = [fake_sg_template]

        # Test without remote_group_id
        rule_list = self._driver._select_sg_rules_for_port(mock_port,
                                                           'ingress')
        self.assertEqual(self._FAKE_SG_ID, rule_list[0]['security_group_id'])

        # Test with remote_group_id
        fake_sg_template['remote_group_id'] = self._FAKE_SG_ID
        self._driver._sg_members[self._FAKE_SG_ID] = {self._FAKE_ETHERTYPE:
                                                      [self._FAKE_MEMBER_IP]}
        rule_list = self._driver._select_sg_rules_for_port(mock_port,
                                                           'ingress')
        self.assertEqual(self._FAKE_SG_ID, rule_list[0]['security_group_id'])
        self.assertEqual('10.0.0.1/32', rule_list[0]['source_ip_prefix'])

        # Test for fixed 'ip' existing in 'sg_members'
        self._driver._sg_members[self._FAKE_SG_ID][self._FAKE_ETHERTYPE] = [
            '10.0.0.2']

        mock_port['fixed_ips'] = ['10.0.0.2']
        rule_list = self._driver._select_sg_rules_for_port(mock_port,
                                                           'ingress')
        self.assertEqual([], rule_list)

        # Test for 'egress' direction
        fake_sg_template['direction'] = 'egress'
        fix_ip = [self._FAKE_MEMBER_IP, '10.0.0.2']
        self._driver._sg_members[self._FAKE_SG_ID][self._FAKE_ETHERTYPE] = (
            fix_ip)

        rule_list = self._driver._select_sg_rules_for_port(mock_port,
                                                           'egress')
        self.assertEqual('10.0.0.1/32', rule_list[0]['dest_ip_prefix'])

        # Test for rules with a different direction
        rule_list = self._driver._select_sg_rules_for_port(mock_port,
                                                           'ingress')
        self.assertEqual([], rule_list)

    def test_update_security_group_rules(self):
        mock_rule = [self._create_security_rule()]
        self._driver.update_security_group_rules(self._FAKE_ID, mock_rule)
        self.assertEqual(mock_rule,
                         self._driver._sg_rule_templates[self._FAKE_ID])

    def test_update_security_group_members(self):
        mock_member = ['10.0.0.1/32']
        self._driver.update_security_group_members(self._FAKE_ID, mock_member)
        self.assertEqual(mock_member, self._driver._sg_members[self._FAKE_ID])

    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_select_sg_rules_for_port')
    def test__generate_rules(self, mock_select_sg_rules):
        mock_rule = [self._create_security_rule()]
        mock_port = self._get_port()
        mock_select_sg_rules.return_value = mock_rule
        ports = self._driver._generate_rules([mock_port])

        # Expected result
        mock_rule.append(mock_rule[0])
        expected = {self._FAKE_ID: mock_rule}
        self.assertEqual(expected, ports)

    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_generate_rules')
    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_create_port_rules')
    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_add_sg_port_rules')
    def test_prepare_port_filter(self, mock_add_rules, mock_create_rules,
                                 mock_gen_rules):
        mock_port = self._get_port()
        mock_create_default = self._driver._sg_gen.create_default_sg_rules

        fake_rule = self._create_security_rule()
        self._driver._get_rule_remote_address = mock.MagicMock(
            return_value=self._FAKE_SOURCE_IP_PREFIX)
        mock_gen_rules.return_value = {mock_port['id']: [fake_rule]}

        self._driver.prepare_port_filter(mock_port)

        self.assertEqual(mock_port,
                         self._driver._security_ports[self._FAKE_DEVICE])
        mock_gen_rules.assert_called_with([self._driver._security_ports
                                          [self._FAKE_DEVICE]])
        mock_add_rules.assert_called_once_with(
            self._FAKE_ID, mock_create_default.return_value)

        self._driver._create_port_rules.assert_called_with(
            self._FAKE_ID, [fake_rule])

    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_generate_rules')
    def test_update_port_filter(self, mock_gen_rules):
        mock_port = self._get_port()
        new_mock_port = self._get_port()
        new_mock_port['id'] += '2'
        new_mock_port['security_group_rules'][0]['ethertype'] += "2"

        fake_rule_new = self._create_security_rule()
        self._driver._get_rule_remote_address = mock.MagicMock(
            return_value=self._FAKE_SOURCE_IP_PREFIX)

        mock_gen_rules.return_value = {new_mock_port['id']: [fake_rule_new]}

        self._driver._security_ports[mock_port['device']] = mock_port
        self._driver._sec_group_rules[new_mock_port['id']] = []

        self._driver._create_port_rules = mock.MagicMock()
        self._driver._remove_port_rules = mock.MagicMock()
        self._driver.update_port_filter(new_mock_port)

        self._driver._remove_port_rules.assert_called_once_with(
            mock_port['id'], mock_port['security_group_rules'])
        self._driver._create_port_rules.assert_called_once_with(
            new_mock_port['id'], [new_mock_port['security_group_rules'][0],
                                  fake_rule_new])
        self.assertEqual(new_mock_port,
                         self._driver._security_ports[new_mock_port['device']])

    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       'prepare_port_filter')
    def test_update_port_filter_new_port(self, mock_method):
        mock_port = self._get_port()
        new_mock_port = self._get_port()
        new_mock_port['id'] += '2'
        new_mock_port['device'] += '2'
        new_mock_port['security_group_rules'][0]['ethertype'] += "2"

        self._driver._security_ports[mock_port['device']] = mock_port
        self._driver.update_port_filter(new_mock_port)

        self.assertNotIn(new_mock_port['device'], self._driver._security_ports)

    def test_remove_port_filter(self):
        mock_port = self._get_port()
        mock_rule = mock.MagicMock()
        self._driver._sec_group_rules[self._FAKE_ID] = [mock_rule]
        self._driver._security_ports[mock_port['device']] = mock_port
        self._driver.remove_port_filter(mock_port)
        self.assertNotIn(mock_port['device'], self._driver._security_ports)
        self.assertNotIn(mock_port['id'], self._driver._sec_group_rules)
        self._driver._utils.clear_port_sg_acls_cache(mock_port['id'])

    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_add_sg_port_rules')
    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_remove_sg_port_rules')
    def test_create_port_rules(self, mock_remove, mock_add):
        mock_rule = mock.MagicMock()
        self._driver._sec_group_rules[self._FAKE_ID] = [mock_rule]
        self._driver._sg_gen.create_security_group_rules.return_value = [
            mock_rule]
        self._driver._sg_gen.compute_new_rules_add.return_value = (
            [mock_rule, mock_rule], [mock_rule, mock_rule])

        self._driver._create_port_rules(self._FAKE_ID, [mock_rule])

        self._driver._sg_gen.compute_new_rules_add.assert_called_once_with(
            [mock_rule], [mock_rule])
        mock_remove.assert_called_once_with(self._FAKE_ID, [mock_rule])
        mock_add.assert_called_once_with(self._FAKE_ID, [mock_rule])

    @mock.patch.object(sg_driver.HyperVSecurityGroupsDriver,
                       '_remove_sg_port_rules')
    def test_remove_port_rules(self, mock_remove):
        mock_rule = mock.MagicMock()
        self._driver._sec_group_rules[self._FAKE_ID] = [mock_rule]
        self._driver._sg_gen.create_security_group_rules.return_value = [
            mock_rule]

        self._driver._remove_port_rules(self._FAKE_ID, [mock_rule])

        mock_remove.assert_called_once_with(self._FAKE_ID, [mock_rule])

    def test_add_sg_port_rules_exception(self):
        mock_rule = mock.MagicMock()
        self._driver._sec_group_rules[self._FAKE_ID] = []
        self._driver._utils.create_security_rules.side_effect = (
            exceptions.HyperVException(msg='Generated Exception for testing.'))

        self.assertRaises(exceptions.HyperVException,
                          self._driver._add_sg_port_rules,
                          self._FAKE_ID, [mock_rule])

        self.assertNotIn(mock_rule,
                         self._driver._sec_group_rules[self._FAKE_ID])

    def test_add_sg_port_rules_port_not_found(self):
        self._driver._sec_group_rules[self._FAKE_ID] = []
        self._driver._utils.create_security_rules.side_effect = (
            exceptions.NotFound(resource='port_id'))

        self.assertRaises(exceptions.NotFound,
                          self._driver._add_sg_port_rules,
                          self._FAKE_ID, [mock.sentinel.rule])

        self.assertNotIn(self._FAKE_ID, self._driver._sec_group_rules)

    def test_add_sg_port_rules(self):
        mock_rule = mock.MagicMock()
        self._driver._sec_group_rules[self._FAKE_ID] = []
        self._driver._add_sg_port_rules(self._FAKE_ID, [mock_rule])

        self._driver._utils.create_security_rules.assert_called_once_with(
            self._FAKE_ID, [mock_rule])
        self.assertIn(mock_rule, self._driver._sec_group_rules[self._FAKE_ID])

    def test_add_sg_port_rules_empty(self):
        self._driver._add_sg_port_rules(mock.sentinel.id, [])
        self.assertFalse(self._driver._utils.create_security_rules.called)

    def test_remove_sg_port_rules_exception(self):
        mock_rule = mock.MagicMock()
        self._driver._sec_group_rules[self._FAKE_ID] = [mock_rule]
        self._driver._utils.remove_security_rules.side_effect = (
            exceptions.HyperVException(msg='Generated Exception for testing.'))

        self.assertRaises(exceptions.HyperVException,
                          self._driver._remove_sg_port_rules,
                          self._FAKE_ID, [mock_rule])

        self.assertIn(mock_rule, self._driver._sec_group_rules[self._FAKE_ID])

    def test_remove_sg_port_rules_port_not_found(self):
        self._driver._sec_group_rules[self._FAKE_ID] = []
        self._driver._utils.remove_security_rules.side_effect = (
            exceptions.NotFound(resource='port_id'))

        self.assertRaises(exceptions.NotFound,
                          self._driver._remove_sg_port_rules,
                          self._FAKE_ID, [mock.sentinel.rule])

        self.assertNotIn(self._FAKE_ID, self._driver._sec_group_rules)

    def test_remove_sg_port_rules(self):
        mock_rule = mock.MagicMock()
        self._driver._sec_group_rules[self._FAKE_ID] = [mock_rule]
        self._driver._remove_sg_port_rules(
            self._FAKE_ID, [mock_rule, mock.sentinel.other_rule])

        self._driver._utils.remove_security_rules.assert_called_once_with(
            self._FAKE_ID, [mock_rule, mock.sentinel.other_rule])
        self.assertNotIn(mock_rule,
                         self._driver._sec_group_rules[self._FAKE_ID])

    def test_remove_sg_port_rules_empty(self):
        self._driver._remove_sg_port_rules(mock.sentinel.id, [])
        self.assertFalse(self._driver._utils.remove_security_rules.called)

    def _get_port(self):
        return {
            'device': self._FAKE_DEVICE,
            'id': self._FAKE_ID,
            'security_group_rules': [mock.MagicMock()]
        }


class SecurityGroupRuleR2BaseTestCase(SecurityGroupRuleTestHelper):
    def _create_sg_rule(self, protocol=None, action=None, direction='egress'):
        protocol = protocol or self._FAKE_PROTOCOL
        action = action or self._FAKE_ACTION
        remote_addr = (self._FAKE_DEST_IP_PREFIX if direction is 'egress' else
                       self._FAKE_SOURCE_IP_PREFIX)
        return sg_driver.SecurityGroupRuleR2(
            self._acl('direction', self._FAKE_DIRECTION),
            '%s-%s' % (self._FAKE_PORT_MIN, self._FAKE_PORT_MAX),
            protocol, remote_addr, action)


class SecurityGroupRuleGeneratorTestCase(SecurityGroupRuleR2BaseTestCase):

    def setUp(self):
        super(SecurityGroupRuleGeneratorTestCase, self).setUp()

        self.sg_gen = sg_driver.SecurityGroupRuleGenerator()

    @mock.patch.object(sg_driver.SecurityGroupRuleGenerator,
                       'create_security_group_rule')
    def test_create_security_group_rules(self, mock_create_sec_group_rule):
        sg_rule = self._create_sg_rule()
        mock_create_sec_group_rule.return_value = [sg_rule]
        expected = [sg_rule] * 2
        rules = [self._create_security_rule()] * 2

        actual = self.sg_gen.create_security_group_rules(rules)
        self.assertEqual(expected, actual)

    def test_convert_any_address_to_same_ingress(self):
        rule = self._create_security_rule()
        rule['direction'] = 'ingress'
        actual = self.sg_gen._get_rule_remote_address(rule)
        self.assertEqual(self._FAKE_SOURCE_IP_PREFIX, actual)

    def test_convert_any_address_to_same_egress(self):
        rule = self._create_security_rule()
        rule['direction'] += '2'
        actual = self.sg_gen._get_rule_remote_address(rule)
        self.assertEqual(self._FAKE_DEST_IP_PREFIX, actual)

    def test_convert_any_address_to_ipv4(self):
        rule = self._create_security_rule()
        del rule['dest_ip_prefix']
        actual = self.sg_gen._get_rule_remote_address(rule)
        self.assertEqual(self._acl('address_default', 'IPv4'), actual)

    def test_convert_any_address_to_ipv6(self):
        rule = self._create_security_rule()
        del rule['dest_ip_prefix']
        rule['ethertype'] = self._FAKE_ETHERTYPE_IPV6
        actual = self.sg_gen._get_rule_remote_address(rule)
        self.assertEqual(self._acl('address_default', 'IPv6'), actual)


class SecurityGroupRuleGeneratorR2TestCase(SecurityGroupRuleR2BaseTestCase):

    def setUp(self):
        super(SecurityGroupRuleGeneratorR2TestCase, self).setUp()

        self.sg_gen = sg_driver.SecurityGroupRuleGeneratorR2()

    def test_create_security_group_rule(self):
        expected = [self._create_sg_rule()]
        rule = self._create_security_rule()

        actual = self.sg_gen.create_security_group_rule(rule)
        self.assertEqual(expected, actual)

    def test_create_security_group_rule_len128(self):
        expected = [self._create_sg_rule()]
        expected[0].RemoteIPAddress = self._FAKE_IPV6_LEN128_IP.split(
            '/128', 1)[0]
        rule = self._create_security_rule()
        rule['dest_ip_prefix'] = self._FAKE_IPV6_LEN128_IP

        actual = self.sg_gen.create_security_group_rule(rule)
        self.assertEqual(expected, actual)

    def test_create_security_group_rule_any(self):
        sg_rule1 = self._create_sg_rule(self._acl('protocol', 'tcp'))
        sg_rule2 = self._create_sg_rule(self._acl('protocol', 'udp'))
        sg_rule3 = self._create_sg_rule(self._acl('protocol', 'icmp'))
        sg_rule4 = self._create_sg_rule(self._acl('protocol', 'ipv6-icmp'))

        rule = self._create_security_rule()
        rule['protocol'] = sg_driver.ACL_PROP_MAP["default"]

        actual = self.sg_gen.create_security_group_rule(rule)
        expected = [sg_rule1, sg_rule2, sg_rule3, sg_rule4]
        self.assertEqual(sorted(expected), sorted(actual))

    def test_create_default_sg_rules(self):
        actual = self.sg_gen.create_default_sg_rules()
        self.assertEqual(16, len(actual))

    def test_compute_new_rules_add(self):
        new_rule = self._create_sg_rule()
        old_rule = self._create_sg_rule()
        old_rule.Direction = mock.sentinel.FAKE_DIRECTION

        add_rules, remove_rules = self.sg_gen.compute_new_rules_add(
            [old_rule], [new_rule, old_rule])

        self.assertEqual([new_rule], add_rules)

    def test_get_rule_port_range(self):
        rule = self._create_security_rule()
        expected = '%s-%s' % (self._FAKE_PORT_MIN, self._FAKE_PORT_MAX)
        actual = self.sg_gen._get_rule_port_range(rule)

        self.assertEqual(expected, actual)

    def test_get_rule_port_range_default(self):
        rule = self._create_security_rule()
        del rule['port_range_min']
        expected = sg_driver.ACL_PROP_MAP['default']
        actual = self.sg_gen._get_rule_port_range(rule)

        self.assertEqual(expected, actual)

    def test_get_rule_protocol_icmp(self):
        self._check_get_rule_protocol('icmp', self._acl('protocol', 'icmp'))

    def test_get_rule_protocol_no_icmp(self):
        self._check_get_rule_protocol('tcp', 'tcp')

    def _check_get_rule_protocol(self, protocol, expected):
        rule = self._create_security_rule()
        rule['protocol'] = protocol
        actual = self.sg_gen._get_rule_protocol(rule)

        self.assertEqual(expected, actual)


class SecurityGroupRuleR2TestCase(SecurityGroupRuleR2BaseTestCase):

    def test_sg_rule_to_dict(self):
        expected = {'Direction': self._acl('direction', self._FAKE_DIRECTION),
                    'Action': self._FAKE_ACTION,
                    'Protocol': self._FAKE_PROTOCOL,
                    'LocalPort': '%s-%s' % (self._FAKE_PORT_MIN,
                                            self._FAKE_PORT_MAX),
                    'RemoteIPAddress': self._FAKE_DEST_IP_PREFIX,
                    'Stateful': True,
                    'IdleSessionTimeout': 0}

        sg_rule = self._create_sg_rule()
        self.assertEqual(expected, sg_rule.to_dict())

    def test_localport(self):
        sg_rule = self._create_sg_rule()
        expected = '%s-%s' % (self._FAKE_PORT_MIN, self._FAKE_PORT_MAX)
        self.assertEqual(expected, sg_rule.LocalPort)

    def test_localport_icmp(self):
        sg_rule = self._create_sg_rule(self._acl('protocol', 'icmp'))
        self.assertEqual('', sg_rule.LocalPort)

    def test_stateful_icmp(self):
        sg_rule = self._create_sg_rule(self._acl('protocol', 'icmp'))
        self.assertFalse(sg_rule.Stateful)

    def test_stateful_ipv6_icmp(self):
        sg_rule = self._create_sg_rule(self._acl('protocol', 'ipv6-icmp'))
        self.assertFalse(sg_rule.Stateful)

    def test_stateful_deny(self):
        sg_rule = self._create_sg_rule(action=self._acl('action', 'deny'))
        self.assertFalse(sg_rule.Stateful)

    def test_stateful_true(self):
        sg_rule = self._create_sg_rule()
        self.assertTrue(sg_rule.Stateful)

    def test_rule_uniqueness(self):
        sg_rule = self._create_sg_rule()
        sg_rule2 = self._create_sg_rule(self._acl('protocol', 'icmp'))

        self.assertEqual([sg_rule], list(set([sg_rule] * 2)))
        self.assertEqual(sorted([sg_rule, sg_rule2]),
                         sorted(list(set([sg_rule, sg_rule2]))))