forked from jupyter/nbformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sign.py
268 lines (231 loc) · 9.12 KB
/
test_sign.py
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
"""Test Notebook signing"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import codecs
import copy
import os
import shutil
import sys
import tempfile
import time
import unittest
from subprocess import PIPE, Popen
import testpath
from traitlets.config import Config
from nbformat import read, sign, write
from .base import TestsBase
class TestNotary(TestsBase):
def setUp(self):
self.data_dir = tempfile.mkdtemp()
self.notary = sign.NotebookNotary(
db_file=":memory:",
secret=b"secret",
data_dir=self.data_dir,
)
with self.fopen("test3.ipynb", "r") as f:
self.nb = read(f, as_version=4)
with self.fopen("test3.ipynb", "r") as f:
self.nb3 = read(f, as_version=3)
def tearDown(self):
self.notary.store.close()
shutil.rmtree(self.data_dir)
def test_invalid_db_file(self):
invalid_sql_file = os.path.join(self.data_dir, "invalid_db_file.db")
with open(invalid_sql_file, "w", encoding="utf-8") as tempfile:
tempfile.write("[invalid data]")
invalid_notary = sign.NotebookNotary(
db_file=invalid_sql_file,
secret=b"secret",
)
invalid_notary.sign(self.nb)
invalid_notary.store.close()
testpath.assert_isfile(os.path.join(self.data_dir, invalid_sql_file))
testpath.assert_isfile(os.path.join(self.data_dir, invalid_sql_file + ".bak"))
def test_algorithms(self):
last_sig = ""
for algo in sign.algorithms:
self.notary.algorithm = algo
sig = self.notary.compute_signature(self.nb)
self.assertNotEqual(last_sig, sig)
last_sig = sig
def test_sign_same(self):
"""Multiple signatures of the same notebook are the same"""
sig1 = self.notary.compute_signature(self.nb)
sig2 = self.notary.compute_signature(self.nb)
self.assertEqual(sig1, sig2)
def test_change_secret(self):
"""Changing the secret changes the signature"""
sig1 = self.notary.compute_signature(self.nb)
self.notary.secret = b"different"
sig2 = self.notary.compute_signature(self.nb)
self.assertNotEqual(sig1, sig2)
def test_sign(self):
self.assertFalse(self.notary.check_signature(self.nb))
self.notary.sign(self.nb)
self.assertTrue(self.notary.check_signature(self.nb))
def test_unsign(self):
self.notary.sign(self.nb)
self.assertTrue(self.notary.check_signature(self.nb))
self.notary.unsign(self.nb)
self.assertFalse(self.notary.check_signature(self.nb))
self.notary.unsign(self.nb)
self.assertFalse(self.notary.check_signature(self.nb))
def test_cull_db(self):
# this test has various sleeps of 2ms
# to ensure low resolution timestamps compare as expected
dt = 2e-3
nbs = [copy.deepcopy(self.nb) for i in range(10)]
for row in self.notary.store.db.execute("SELECT * FROM nbsignatures"):
print(row)
self.notary.store.cache_size = 8
for i, nb in enumerate(nbs[:8]):
nb.metadata.dirty = i
self.notary.sign(nb)
for i, nb in enumerate(nbs[:8]):
time.sleep(dt)
self.assertTrue(self.notary.check_signature(nb), "nb %i is trusted" % i)
# signing the 9th triggers culling of first 3
# (75% of 8 = 6, 9 - 6 = 3 culled)
self.notary.sign(nbs[8])
self.assertFalse(self.notary.check_signature(nbs[0]))
self.assertFalse(self.notary.check_signature(nbs[1]))
self.assertFalse(self.notary.check_signature(nbs[2]))
self.assertTrue(self.notary.check_signature(nbs[3]))
# checking nb3 should keep it from being culled:
self.notary.sign(nbs[0])
self.notary.sign(nbs[1])
self.notary.sign(nbs[2])
self.assertTrue(self.notary.check_signature(nbs[3]))
self.assertFalse(self.notary.check_signature(nbs[4]))
def test_check_signature(self):
nb = self.nb
md = nb.metadata
notary = self.notary
check_signature = notary.check_signature
# no signature:
md.pop("signature", None)
self.assertFalse(check_signature(nb))
# hash only, no algo
md.signature = notary.compute_signature(nb)
self.assertFalse(check_signature(nb))
# proper signature, algo mismatch
notary.algorithm = "sha224"
notary.sign(nb)
notary.algorithm = "sha256"
self.assertFalse(check_signature(nb))
# check correctly signed notebook
notary.sign(nb)
self.assertTrue(check_signature(nb))
def test_mark_cells_untrusted(self):
cells = self.nb.cells
self.notary.mark_cells(self.nb, False)
for cell in cells:
self.assertNotIn("trusted", cell)
if cell.cell_type == "code":
self.assertIn("trusted", cell.metadata)
self.assertFalse(cell.metadata.trusted)
else:
self.assertNotIn("trusted", cell.metadata)
def test_mark_cells_trusted(self):
cells = self.nb.cells
self.notary.mark_cells(self.nb, True)
for cell in cells:
self.assertNotIn("trusted", cell)
if cell.cell_type == "code":
self.assertIn("trusted", cell.metadata)
self.assertTrue(cell.metadata.trusted)
else:
self.assertNotIn("trusted", cell.metadata)
def test_check_cells(self):
nb = self.nb
self.notary.mark_cells(nb, True)
self.assertTrue(self.notary.check_cells(nb))
for cell in nb.cells:
self.assertNotIn("trusted", cell)
self.notary.mark_cells(nb, False)
self.assertFalse(self.notary.check_cells(nb))
for cell in nb.cells:
self.assertNotIn("trusted", cell)
def test_trust_no_output(self):
nb = self.nb
self.notary.mark_cells(nb, False)
for cell in nb.cells:
if cell.cell_type == "code":
cell.outputs = []
self.assertTrue(self.notary.check_cells(nb))
def test_mark_cells_untrusted_v3(self):
nb = self.nb3
cells = nb.worksheets[0].cells
self.notary.mark_cells(nb, False)
for cell in cells:
self.assertNotIn("trusted", cell)
if cell.cell_type == "code":
self.assertIn("trusted", cell.metadata)
self.assertFalse(cell.metadata.trusted)
else:
self.assertNotIn("trusted", cell.metadata)
def test_mark_cells_trusted_v3(self):
nb = self.nb3
cells = nb.worksheets[0].cells
self.notary.mark_cells(nb, True)
for cell in cells:
self.assertNotIn("trusted", cell)
if cell.cell_type == "code":
self.assertIn("trusted", cell.metadata)
self.assertTrue(cell.metadata.trusted)
else:
self.assertNotIn("trusted", cell.metadata)
def test_check_cells_v3(self):
nb = self.nb3
cells = nb.worksheets[0].cells
self.notary.mark_cells(nb, True)
self.assertTrue(self.notary.check_cells(nb))
for cell in cells:
self.assertNotIn("trusted", cell)
self.notary.mark_cells(nb, False)
self.assertFalse(self.notary.check_cells(nb))
for cell in cells:
self.assertNotIn("trusted", cell)
def test_sign_stdin(self):
def sign_stdin(nb):
env = os.environ.copy()
env["JUPYTER_DATA_DIR"] = self.data_dir
p = Popen(
[sys.executable, "-m", "nbformat.sign", "--log-level=0"], # noqa
stdin=PIPE,
stdout=PIPE,
env=env,
)
assert p.stdin is not None
write(nb, codecs.getwriter("utf8")(p.stdin))
p.stdin.close()
p.wait()
self.assertEqual(p.returncode, 0)
assert p.stdout is not None
out = p.stdout.read().decode("utf8", "replace")
p.stdout.close()
return out
out = sign_stdin(self.nb3)
self.assertIn("Signing notebook: <stdin>", out)
out = sign_stdin(self.nb3)
self.assertIn("already signed: <stdin>", out)
def test_config_store():
store = sign.MemorySignatureStore()
c = Config()
c.NotebookNotary.store_factory = lambda: store
notary = sign.NotebookNotary(config=c)
assert notary.store is store
class SignatureStoreTests(unittest.TestCase):
def setUp(self):
self.store = sign.MemorySignatureStore()
def test_basics(self):
digest = "0123457689abcef"
algo = "fake_sha"
assert not self.store.check_signature(digest, algo)
self.store.store_signature(digest, algo)
assert self.store.check_signature(digest, algo)
self.store.remove_signature(digest, algo)
assert not self.store.check_signature(digest, algo)
class SQLiteSignatureStoreTests(SignatureStoreTests):
def setUp(self):
self.store = sign.SQLiteSignatureStore(":memory:") # type:ignore[assignment]