forked from hyphanet/pyFreenet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·387 lines (272 loc) · 7.77 KB
/
test.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
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
#!/usr/bin/env python2
# encoding: utf-8
"""
Tests:
put/get ksk ssk usk dir freesite → data file async
wrong node ip
different IP Port
"""
import sys, os, tempfile, random, uuid
import fcp
fcpHost = "127.0.0.1"
workdir = tempfile.mkdtemp()
os.chdir(workdir)
myid = str(uuid.uuid4().hex)
with open("index.html", "w") as f:
f.write("<html><head><title>Test</title></head><body>Test</body></html>\n")
node = fcp.FCPNode(host=fcpHost, verbosity=fcp.FATAL)
def genkey(*args, **kwds):
'''
>>> public, private = genkey()
>>> fcp.node.uriIsPrivate(public)
False
>>> fcp.node.uriIsPrivate(private)
True
'''
return node.genkey(*args, **kwds)
def getUniqueId(*args, **kwds):
'''
>>> getUniqueId().startswith("id")
True
'''
return node._getUniqueId(*args, **kwds)
def submitCmd(*args, **kwds):
'''
>>> connid = getUniqueId()
>>> job = submitCmd(connid, "GetNode", Identifier=connid, **{"async": True})
>>> job.wait()["Identifier"] == connid
True
>>> submitCmd(connid, "GetNode", Identifier=connid)["Identifier"] == connid
True
>>> raw = """GetNode
... WithPrivate=true
... WithVolatile=false
... Identifier=""" + connid + """
... EndMessage
... """
>>> submitCmd(connid, "GetNode", rawcmd=raw)["Identifier"] == connid
True
'''
return node._submitCmd(*args, **kwds)
def fcpPluginMessage(*args, **kwds):
'''
# this requires the hello plugin loaded in the node
# >>> fcpPluginMessage(id="pyfreenet",
# ... plugin_name="plugins.HelloFCP.HelloFCP")
# [{'header': 'FCPPluginReply', 'PluginName': 'plugins.HelloFCP.HelloFCP', 'Identifier': 'pyfreenet'}]
'''
return node.fcpPluginMessage(*args, **kwds)
def put(*args, **kwds):
'''
- uri=ksk ssk usk × data file dir redirect
- dontcompress, chkonly, mimetype, waituntilsent, async
- persistence, Verbosity, priority, realtime, Global
- timeout (for the insertion)
>>> data = "23"
>>> kwds = {"async": True, "realtime": True, "priority": 1, "waituntilsent": True}
>>> public, private = genkey()
>>> privateusk = "U" + private[1:] + "test/0/data"
>>> publicusk = "U" + public[1:] + "test/0/data"
>>> chkput = put(data=data, **kwds)
>>> uskput = put(uri=privateusk, data=data, **kwds)
>>> sskput = put(uri=private + "test", data=data, **kwds)
>>> kskput = put(uri="KSK@"+myid, data=data, **kwds)
>>> redirput = put(uri="KSK@redirectto"+myid, redirect="KSK@"+myid, **kwds)
>>> dirput = put(dir=workdir, **kwds)
>>> # the following wait until freenet is really finished
>>> # so they are horribly slow. Activate them manually.
>>> # chkput.wait()
>>> True # uskput.wait() == publicusk
True
>>> True # sskput.wait() == public + "test"
True
>>> # kskput.wait()
>>> # redirput.wait()
>>> # dirput.wait()
'''
return node.put(*args, **kwds)
def get(uri, *args, **kwds):
'''
>>> # warning: this can be slow the first time you run it.
>>> mime, data = get("[email protected]", realtime=True, priority=0)[:2]
>>> data[:9]
'\\t\\t GNU'
>>> # rest tested in put
'''
return node.get(uri, *args, **kwds)
f
def putdir(*args, **kwds):
'''
putdir is a specialization of dir. It’s tested with put.
- name, usk (for dir-mode)
>>> # putdir()
'''
return node.putdir(*args, **kwds)
def modifyconfig(*args, **kwds):
'''
>>> # modifyconfig()
'''
return node.modifyconfig(*args, **kwds)
def getconfig(*args, **kwds):
'''
>>> # getconfig()
'''
return node.getconfig(*args, **kwds)
def invertprivate(*args, **kwds):
'''
>>> # invertprivate()
'''
return node.invertprivate(*args, **kwds)
def redirect(*args, **kwds):
'''
>>> # redirect()
'''
return node.redirect(*args, **kwds)
def genchk(*args, **kwds):
'''
>>> data = "test"
>>> chk = genchk(data=data)
>>> chkres = put(data=data, priority=0, realtime=True)
>>> chk == chkres
True
>>> got = get(uri=chk, priority=0, realtime=True)
>>> gotmime, gotdata, gotdict = got
>>> gotdata == gotdict["Data"]
True
>>> gotdata == data
True
>>> foomime = "bla/foo"
>>> # chkfoo = genchk(data=data, mimetype=foomime)
>>> # got2 = get(uri=chkfoo, priority=0, realtime=True)
>>> True # got2[1] == data
True
>>> True # got2[0] == foomime
True
'''
return node.genchk(*args, **kwds)
def listpeers(*args, **kwds):
'''
>>> # listpeers()
'''
return node.listpeers(*args, **kwds)
def listpeernotes(*args, **kwds):
'''
>>> # listpeernotes()
'''
return node.listpeernotes(*args, **kwds)
def refstats(*args, **kwds):
'''
>>> # refstats()
'''
return node.refstats(*args, **kwds)
def testDDA(*args, **kwds):
'''
>>> # testDDA()
'''
return node.testDDA(*args, **kwds)
def addpeer(*args, **kwds):
'''
>>> # addpeer()
'''
return node.addpeer(*args, **kwds)
def listpeer(*args, **kwds):
'''
>>> # listpeer()
'''
return node.listpeer(*args, **kwds)
def modifypeer(*args, **kwds):
'''
>>> # modifypeer()
'''
return node.modifypeer(*args, **kwds)
def modifypeernote(*args, **kwds):
'''
>>> # modifypeernote()
'''
return node.modifypeernote(*args, **kwds)
def removepeer(*args, **kwds):
'''
>>> # removepeer()
'''
return node.removepeer(*args, **kwds)
def namesiteAddLocal(*args, **kwds):
'''
>>> # namesiteAddLocal()
'''
return node.namesiteAddLocal(*args, **kwds)
def namesiteAddRecord(*args, **kwds):
'''
>>> # namesiteAddRecord()
'''
return node.namesiteAddRecord(*args, **kwds)
def namesiteLookup(*args, **kwds):
'''
>>> # namesiteLookup()
'''
return node.namesiteLookup(*args, **kwds)
def listenGlobal(*args, **kwds):
'''
>>> # listenGlobal()
'''
return node.listenGlobal(*args, **kwds)
def ignoreGlobal(*args, **kwds):
'''
>>> # ignoreGlobal()
'''
return node.ignoreGlobal(*args, **kwds)
def purgePersistentJobs(*args, **kwds):
'''
>>> # purgePersistentJobs()
'''
return node.purgePersistentJobs(*args, **kwds)
def getAllJobs(*args, **kwds):
'''
>>> # getAllJobs()
'''
return node.getAllJobs(*args, **kwds)
def getPersistentJobs(*args, **kwds):
'''
>>> # getPersistentJobs()
'''
return node.getPersistentJobs(*args, **kwds)
def getGlobalJobs(*args, **kwds):
'''
>>> # getGlobalJobs()
'''
return node.getGlobalJobs(*args, **kwds)
def getTransientJobs(*args, **kwds):
'''
>>> # getTransientJobs()
'''
return node.getTransientJobs(*args, **kwds)
def refreshPersistentRequests(*args, **kwds):
'''
>>> # refreshPersistentRequests()
'''
return node.refreshPersistentRequests(*args, **kwds)
def clearGlobalJob(*args, **kwds):
'''
>>> # clearGlobalJob()
'''
return node.clearGlobalJob(*args, **kwds)
def shutdown(*args, **kwds):
'''
>>> # shutdown()
'''
return node.shutdown(*args, **kwds)
def _base30hex(integer):
"""Turn an integer into a simple lowercase base30hex encoding."""
base30 = "0123456789abcdefghijklmnopqrst"
b30 = []
while integer:
b30.append(base30[integer%30])
integer = int(integer / 30)
return "".join(reversed(b30))
def _test():
import doctest
tests = doctest.testmod()
if tests.failed:
return "☹"*tests.failed + " / " + str(tests.attempted)
return "^_^ (" + _base30hex(tests.attempted) + ")"
if __name__ == "__main__":
print(_test())