Skip to content

Commit

Permalink
Merge pull request #312 from robertbaldyga/rand-generator-improvements
Browse files Browse the repository at this point in the history
Rand generator improvements
  • Loading branch information
Robert Bałdyga authored Oct 4, 2019
2 parents a61664d + 1525e85 commit 17ec78d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 52 deletions.
7 changes: 5 additions & 2 deletions tests/functional/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SRC=$(shell find $(SRCDIR) $(WRAPDIR) -name \*.c)
OBJS=$(patsubst %.c, %.o, $(SRC))
OCFLIB=$(ADAPTERDIR)/libocf.so

all: | sync
all: | sync config_random
$(MAKE) $(OCFLIB)

$(OCFLIB): $(OBJS)
Expand All @@ -36,6 +36,9 @@ sync:
@$(MAKE) -C $(OCFDIR) src O=$(ADAPTERDIR)/ocf
@$(MAKE) -C $(OCFDIR) env O=$(ADAPTERDIR)/ocf OCF_ENV=posix

config_random:
@python3 utils/configure_random.py

clean:
@rm -rf $(OCFLIB) $(OBJS)
@echo " CLEAN "
Expand All @@ -46,4 +49,4 @@ distclean: clean
@rm -rf $(INCDIR)/ocf
@echo " DISTCLEAN "

.PHONY: all clean sync distclean
.PHONY: all clean sync config_random distclean
2 changes: 2 additions & 0 deletions tests/functional/config/random.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file content will be generated by utils/configure_random.py
# triggered from the Makefile
52 changes: 51 additions & 1 deletion tests/functional/tests/security/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@
c_uint16,
c_int
)
from tests.utils.random import RandomStringGenerator, RandomGenerator, DefaultRanges
from tests.utils.random import RandomStringGenerator, RandomGenerator, DefaultRanges, Range

from pyocf.types.cache import CacheMode, EvictionPolicy, MetadataLayout, PromotionPolicy
from pyocf.types.shared import CacheLineSize

import pytest

sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))


def enum_min(enum):
return list(enum)[0].value


def enum_max(enum):
return list(enum)[-1].value


def enum_range(enum):
return Range(enum_min(enum), enum_max(enum))


@pytest.fixture(params=RandomGenerator(DefaultRanges.UINT16))
def c_uint16_randomize(request):
return request.param
Expand Down Expand Up @@ -46,3 +61,38 @@ def c_int_sector_randomize(request):
@pytest.fixture(params=RandomStringGenerator())
def string_randomize(request):
return request.param


@pytest.fixture(
params=RandomGenerator(DefaultRanges.UINT32).exclude_range(enum_range(CacheMode))
)
def not_cache_mode_randomize(request):
return request.param


@pytest.fixture(
params=RandomGenerator(DefaultRanges.UINT32).exclude_range(enum_range(CacheLineSize))
)
def not_cache_line_size_randomize(request):
return request.param


@pytest.fixture(
params=RandomGenerator(DefaultRanges.UINT32).exclude_range(enum_range(EvictionPolicy))
)
def not_eviction_policy_randomize(request):
return request.param


@pytest.fixture(
params=RandomGenerator(DefaultRanges.UINT32).exclude_range(enum_range(PromotionPolicy))
)
def not_promotion_policy_randomize(request):
return request.param


@pytest.fixture(
params=RandomGenerator(DefaultRanges.UINT32).exclude_range(enum_range(MetadataLayout))
)
def not_metadata_layout_randomize(request):
return request.param
81 changes: 34 additions & 47 deletions tests/functional/tests/security/test_management_start_fuzzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pyocf.types.shared import OcfError, CacheLineSize
from pyocf.types.volume import Volume
from pyocf.utils import Size
from tests.utils.random import RandomGenerator, DefaultRanges
from tests.utils.random import RandomGenerator, DefaultRanges, Range

logger = logging.getLogger(__name__)

Expand All @@ -21,38 +21,30 @@ def try_start_cache(**config):
cache = Cache.start_on_device(cache_device, **config)
cache.stop()


@pytest.mark.security
@pytest.mark.parametrize("cls", CacheLineSize)
def test_fuzzy_start_cache_mode(pyocf_ctx, cls, c_uint32_randomize):
def test_fuzzy_start_cache_mode(pyocf_ctx, cls, not_cache_mode_randomize):
"""
Test whether it is impossible to start cache with invalid cache mode value.
:param pyocf_ctx: basic pyocf context fixture
:param cls: cache line size value to start cache with
:param c_uint32_randomize: cache mode enum value to start cache with
"""
if c_uint32_randomize not in [item.value for item in CacheMode]:
with pytest.raises(OcfError, match="OCF_ERR_INVALID_CACHE_MODE"):
try_start_cache(cache_mode=c_uint32_randomize, cache_line_size=cls)
else:
logger.warning(f"Test skipped for valid cache mode enum value: '{c_uint32_randomize}'. ")
with pytest.raises(OcfError, match="OCF_ERR_INVALID_CACHE_MODE"):
try_start_cache(cache_mode=not_cache_mode_randomize, cache_line_size=cls)


@pytest.mark.security
@pytest.mark.parametrize("cm", CacheMode)
def test_fuzzy_start_cache_line_size(pyocf_ctx, c_uint64_randomize, cm):
def test_fuzzy_start_cache_line_size(pyocf_ctx, not_cache_line_size_randomize, cm):
"""
Test whether it is impossible to start cache with invalid cache line size value.
:param pyocf_ctx: basic pyocf context fixture
:param c_uint64_randomize: cache line size enum value to start cache with
:param cm: cache mode value to start cache with
"""
if c_uint64_randomize not in [item.value for item in CacheLineSize]:
with pytest.raises(OcfError, match="OCF_ERR_INVALID_CACHE_LINE_SIZE"):
try_start_cache(cache_mode=cm, cache_line_size=c_uint64_randomize)
else:
logger.warning(
f"Test skipped for valid cache line size enum value: '{c_uint64_randomize}'. ")
with pytest.raises(OcfError, match="OCF_ERR_INVALID_CACHE_LINE_SIZE"):
try_start_cache(cache_mode=cm, cache_line_size=not_cache_line_size_randomize)


@pytest.mark.security
Expand Down Expand Up @@ -84,39 +76,39 @@ def test_fuzzy_start_name(pyocf_ctx, string_randomize, cm, cls):
@pytest.mark.security
@pytest.mark.parametrize("cm", CacheMode)
@pytest.mark.parametrize("cls", CacheLineSize)
def test_fuzzy_start_eviction_policy(pyocf_ctx, c_uint32_randomize, cm, cls):
def test_fuzzy_start_eviction_policy(pyocf_ctx, not_eviction_policy_randomize, cm, cls):
"""
Test whether it is impossible to start cache with invalid eviction policy value.
:param pyocf_ctx: basic pyocf context fixture
:param c_uint32_randomize: eviction policy enum value to start cache with
:param cm: cache mode value to start cache with
:param cls: cache line size value to start cache with
"""
if c_uint32_randomize not in [item.value for item in EvictionPolicy]:
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(eviction_policy=c_uint32_randomize, cache_mode=cm, cache_line_size=cls)
else:
logger.warning(
f"Test skipped for valid eviction policy enum value: '{c_uint32_randomize}'. ")
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(
eviction_policy=not_eviction_policy_randomize,
cache_mode=cm,
cache_line_size=cls
)


@pytest.mark.security
@pytest.mark.parametrize("cm", CacheMode)
@pytest.mark.parametrize("cls", CacheLineSize)
def test_fuzzy_start_metadata_layout(pyocf_ctx, c_uint32_randomize, cm, cls):
def test_fuzzy_start_metadata_layout(pyocf_ctx, not_metadata_layout_randomize, cm, cls):
"""
Test whether it is impossible to start cache with invalid metadata layout value.
:param pyocf_ctx: basic pyocf context fixture
:param c_uint32_randomize: metadata layout enum value to start cache with
:param cm: cache mode value to start cache with
:param cls: cache line size value to start cache with
"""
if c_uint32_randomize not in [item.value for item in MetadataLayout]:
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(metadata_layout=c_uint32_randomize, cache_mode=cm, cache_line_size=cls)
else:
logger.warning(
f"Test skipped for valid metadata layout enum value: '{c_uint32_randomize}'. ")
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(
metadata_layout=not_metadata_layout_randomize,
cache_mode=cm,
cache_line_size=cls
)


@pytest.mark.security
Expand All @@ -131,33 +123,28 @@ def test_fuzzy_start_max_queue_size(pyocf_ctx, max_wb_queue_size, c_uint32_rando
:param c_uint32_randomize: queue unblock size value to start cache with
:param cls: cache line size value to start cache with
"""
if c_uint32_randomize >= max_wb_queue_size:
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(
max_queue_size=max_wb_queue_size,
queue_unblock_size=c_uint32_randomize,
cache_mode=CacheMode.WB,
cache_line_size=cls)
else:
logger.warning(f"Test skipped for valid values: "
f"'max_queue_size={max_wb_queue_size}, "
f"queue_unblock_size={c_uint32_randomize}'.")
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(
max_queue_size=max_wb_queue_size,
queue_unblock_size=max_wb_queue_size + c_uint32_randomize,
cache_mode=CacheMode.WB,
cache_line_size=cls)


@pytest.mark.security
@pytest.mark.parametrize("cm", CacheMode)
@pytest.mark.parametrize("cls", CacheLineSize)
def test_fuzzy_start_promotion_policy(pyocf_ctx, c_uint32_randomize, cm, cls):
def test_fuzzy_start_promotion_policy(pyocf_ctx, not_promotion_policy_randomize, cm, cls):
"""
Test whether it is impossible to start cache with invalid promotion policy
:param pyocf_ctx: basic pyocf context fixture
:param c_uint32_randomize: promotion policy to start with
:param cm: cache mode value to start cache with
:param cls: cache line size to start cache with
"""
if c_uint32_randomize not in [item.value for item in PromotionPolicy]:
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(cache_mode=cm, cache_line_size=cls, promotion_policy=c_uint32_randomize)
else:
logger.warning(
f"Test skipped for valid promotion policy: '{c_uint32_randomize}'. ")
with pytest.raises(OcfError, match="OCF_ERR_INVAL"):
try_start_cache(
cache_mode=cm,
cache_line_size=cls,
promotion_policy=not_promotion_policy_randomize
)
9 changes: 7 additions & 2 deletions tests/functional/tests/utils/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ class DefaultRanges(Range, enum.Enum):

class RandomGenerator:
def __init__(self, base_range=DefaultRanges.INT, count=1000):
with open("config/random.cfg") as f:
self.random = random.Random(int(f.read()))
self.exclude = []
self.range = base_range
self.count = count
self.n = 0

def exclude_range(self, excl_range):
self.exclude.append(excl_range)
return self

def __iter__(self):
return self
Expand All @@ -52,7 +55,7 @@ def __next__(self):
raise StopIteration()
self.n += 1
while True:
val = random.randint(self.range.min, self.range.max)
val = self.random.randint(self.range.min, self.range.max)
if self.exclude:
excl_map = map(lambda e: e.is_within(val), self.exclude)
is_excluded = reduce(lambda a, b: a or b, excl_map)
Expand All @@ -63,6 +66,8 @@ def __next__(self):

class RandomStringGenerator:
def __init__(self, len_range=Range(0, 20), count=700):
with open("config/random.cfg") as f:
self.random = random.Random(int(f.read()))
self.generator = self.__string_generator(len_range)
self.count = count
self.n = 0
Expand All @@ -77,7 +82,7 @@ def __string_generator(self, len_range):
string.punctuation,
string.hexdigits]:
yield ''.join(random.choice(t) for _ in range(
random.randint(len_range.min, len_range.max)
self.random.randint(len_range.min, len_range.max)
))

def __iter__(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/utils/configure_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3

#
# Copyright(c) 2012-2018 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause-Clear
#

import sys
import random


with open("config/random.cfg", "w") as f:
f.write(str(random.randint(0, sys.maxsize)))

0 comments on commit 17ec78d

Please sign in to comment.