Show More
@@ -0,0 +1,110 b'' | |||||
|
1 | """Test Notebook signing""" | |||
|
2 | #----------------------------------------------------------------------------- | |||
|
3 | # Copyright (C) 2014, The IPython Development Team | |||
|
4 | # | |||
|
5 | # Distributed under the terms of the BSD License. The full license is in | |||
|
6 | # the file COPYING, distributed as part of this software. | |||
|
7 | #----------------------------------------------------------------------------- | |||
|
8 | ||||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | # Imports | |||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | ||||
|
13 | import hashlib | |||
|
14 | ||||
|
15 | from .. import sign | |||
|
16 | from .base import TestsBase | |||
|
17 | ||||
|
18 | from ..current import read | |||
|
19 | from IPython.core.getipython import get_ipython | |||
|
20 | ||||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | # Classes and functions | |||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | ||||
|
25 | class TestNotary(TestsBase): | |||
|
26 | ||||
|
27 | def setUp(self): | |||
|
28 | self.notary = sign.NotebookNotary( | |||
|
29 | secret=b'secret', | |||
|
30 | profile_dir=get_ipython().profile_dir | |||
|
31 | ) | |||
|
32 | with self.fopen(u'test3.ipynb', u'r') as f: | |||
|
33 | self.nb = read(f, u'json') | |||
|
34 | ||||
|
35 | def test_algorithms(self): | |||
|
36 | last_sig = '' | |||
|
37 | for algo in hashlib.algorithms: | |||
|
38 | self.notary.algorithm = algo | |||
|
39 | self.notary.sign(self.nb) | |||
|
40 | sig = self.nb.metadata.signature | |||
|
41 | print(sig) | |||
|
42 | self.assertEqual(sig[:len(self.notary.algorithm)+1], '%s:' % self.notary.algorithm) | |||
|
43 | self.assertNotEqual(last_sig, sig) | |||
|
44 | last_sig = sig | |||
|
45 | ||||
|
46 | def test_sign_same(self): | |||
|
47 | """Multiple signatures of the same notebook are the same""" | |||
|
48 | sig1 = self.notary.compute_signature(self.nb) | |||
|
49 | sig2 = self.notary.compute_signature(self.nb) | |||
|
50 | self.assertEqual(sig1, sig2) | |||
|
51 | ||||
|
52 | def test_change_secret(self): | |||
|
53 | """Changing the secret changes the signature""" | |||
|
54 | sig1 = self.notary.compute_signature(self.nb) | |||
|
55 | self.notary.secret = b'different' | |||
|
56 | sig2 = self.notary.compute_signature(self.nb) | |||
|
57 | self.assertNotEqual(sig1, sig2) | |||
|
58 | ||||
|
59 | def test_sign(self): | |||
|
60 | self.notary.sign(self.nb) | |||
|
61 | sig = self.nb.metadata.signature | |||
|
62 | self.assertEqual(sig[:len(self.notary.algorithm)+1], '%s:' % self.notary.algorithm) | |||
|
63 | ||||
|
64 | def test_check_signature(self): | |||
|
65 | nb = self.nb | |||
|
66 | md = nb.metadata | |||
|
67 | notary = self.notary | |||
|
68 | check_signature = notary.check_signature | |||
|
69 | # no signature: | |||
|
70 | md.pop('signature', None) | |||
|
71 | self.assertFalse(check_signature(nb)) | |||
|
72 | # hash only, no algo | |||
|
73 | md.signature = notary.compute_signature(nb) | |||
|
74 | self.assertFalse(check_signature(nb)) | |||
|
75 | # proper signature, algo mismatch | |||
|
76 | notary.algorithm = 'sha224' | |||
|
77 | notary.sign(nb) | |||
|
78 | notary.algorithm = 'sha256' | |||
|
79 | self.assertFalse(check_signature(nb)) | |||
|
80 | # check correctly signed notebook | |||
|
81 | notary.sign(nb) | |||
|
82 | self.assertTrue(check_signature(nb)) | |||
|
83 | ||||
|
84 | def test_mark_cells_untrusted(self): | |||
|
85 | cells = self.nb.worksheets[0].cells | |||
|
86 | self.notary.mark_cells(self.nb, False) | |||
|
87 | for cell in cells: | |||
|
88 | if cell.cell_type == 'code': | |||
|
89 | self.assertIn('trusted', cell) | |||
|
90 | self.assertFalse(cell.trusted) | |||
|
91 | else: | |||
|
92 | self.assertNotIn('trusted', cell) | |||
|
93 | ||||
|
94 | def test_mark_cells_trusted(self): | |||
|
95 | cells = self.nb.worksheets[0].cells | |||
|
96 | self.notary.mark_cells(self.nb, True) | |||
|
97 | for cell in cells: | |||
|
98 | if cell.cell_type == 'code': | |||
|
99 | self.assertIn('trusted', cell) | |||
|
100 | self.assertTrue(cell.trusted) | |||
|
101 | else: | |||
|
102 | self.assertNotIn('trusted', cell) | |||
|
103 | ||||
|
104 | def test_check_cells(self): | |||
|
105 | nb = self.nb | |||
|
106 | self.notary.mark_cells(nb, True) | |||
|
107 | self.assertTrue(self.notary.check_cells(nb)) | |||
|
108 | self.notary.mark_cells(nb, False) | |||
|
109 | self.assertFalse(self.notary.check_cells(nb)) | |||
|
110 |
General Comments 0
You need to be logged in to leave comments.
Login now