aboutsummaryrefslogtreecommitdiff
path: root/test/sca
diff options
context:
space:
mode:
authorJ08nY2019-11-22 16:56:22 +0100
committerJ08nY2019-11-22 16:58:28 +0100
commite5605f768f407341c3eda6a5d7d3a4a5d63483c9 (patch)
tree4bc911a87db2bacd79eb194116e64126f84f741b /test/sca
parent7acd2278b0c682259bf3ba09bd009f0d7629c14b (diff)
downloadpyecsca-e5605f768f407341c3eda6a5d7d3a4a5d63483c9.tar.gz
pyecsca-e5605f768f407341c3eda6a5d7d3a4a5d63483c9.tar.zst
pyecsca-e5605f768f407341c3eda6a5d7d3a4a5d63483c9.zip
Diffstat (limited to 'test/sca')
-rw-r--r--test/sca/test_match.py29
-rw-r--r--test/sca/utils.py22
2 files changed, 45 insertions, 6 deletions
diff --git a/test/sca/test_match.py b/test/sca/test_match.py
new file mode 100644
index 0000000..9cbd284
--- /dev/null
+++ b/test/sca/test_match.py
@@ -0,0 +1,29 @@
+from unittest import TestCase
+
+import numpy as np
+
+from pyecsca.sca import Trace, match_pattern, match_part, pad
+from .utils import plot
+
+
+class MatchingTests(TestCase):
+
+ def test_simple_match(self):
+ pattern = Trace(None, None,
+ np.array([1, 15, 12, -10, 0, 13, 17, -1, 0], dtype=np.dtype("i1")))
+ base = Trace(None, None, np.array(
+ [0, 1, 3, 1, 2, -2, -3, 1, 15, 12, -10, 0, 13, 17, -1, 0, 3, 1],
+ dtype=np.dtype("i1")))
+ filtered = match_part(base, 7, 9)
+ self.assertListEqual(filtered, [7])
+ plot(self, base=base, pattern=pad(pattern, (filtered[0], 0)))
+
+ def test_multiple_match(self):
+ pattern = Trace(None, None,
+ np.array([1, 15, 12, -10, 0, 13, 17, -1, 0], dtype=np.dtype("i1")))
+ base = Trace(None, None, np.array(
+ [0, 1, 3, 1, 2, -2, -3, 1, 18, 10, -5, 0, 13, 17, -1, 0, 3, 1, 2, 5, 13, 8, -8, 1, 11, 15, 0, 1, 5, 2, 4],
+ dtype=np.dtype("i1")))
+ filtered = match_pattern(base, pattern, 0.9)
+ self.assertListEqual(filtered, [7, 19])
+ plot(self, base=base, pattern1=pad(pattern, (filtered[0], 0)), pattern2=pad(pattern, (filtered[1], 0)))
diff --git a/test/sca/utils.py b/test/sca/utils.py
index 1083b87..643ab68 100644
--- a/test/sca/utils.py
+++ b/test/sca/utils.py
@@ -1,24 +1,34 @@
import matplotlib.pyplot as plt
from unittest import TestCase
from pyecsca.sca import Trace
-from os.path import join, exists
-from os import mkdir, getenv
+from os.path import join, exists, split
+from os import mkdir, getenv, getcwd
+force_plot = True
+
def slow(func):
func.slow = 1
return func
+cases = {}
-def plot(case: TestCase, *traces: Trace):
- if getenv("PYECSCA_TEST_PLOTS") is None:
+def plot(case: TestCase, *traces: Trace, **kwtraces: Trace):
+ if not force_plot and getenv("PYECSCA_TEST_PLOTS") is None:
return
fig = plt.figure()
ax = fig.add_subplot(111)
for i, trace in enumerate(traces):
ax.plot(trace.samples, label=str(i))
+ for name, trace in kwtraces.items():
+ ax.plot(trace.samples, label=name)
ax.legend(loc="best")
- directory = join("test", "plots")
+ if split(getcwd())[1] == "test":
+ directory = "plots"
+ else:
+ directory = join("test", "plots")
if not exists(directory):
mkdir(directory)
- plt.savefig(join(directory, case.id() + ".png"))
+ case_id = cases.setdefault(case.id(), 0) + 1
+ cases[case.id()] = case_id
+ plt.savefig(join(directory, case.id() + str(case_id) + ".png"))