UNPKG

3.18 kBJavaScriptView Raw
1var tape = require("tape");
2var ssbkeys = require("../");
3var path = require("path");
4var os = require("os");
5var fs = require("fs");
6
7const keyPath = path.join(os.tmpdir(), `ssb-keys-${Date.now()}`);
8if (process.env.VERBOSE_TESTS) console.log(keyPath);
9
10tape("create and load presigil-legacy async", function (t) {
11 var keys = ssbkeys.generate("ed25519");
12 keys.id = keys.id.substring(1);
13 fs.writeFileSync(keyPath, JSON.stringify(keys));
14
15 var k2 = ssbkeys.loadSync(keyPath);
16 t.equal(k2.id, "@" + keys.id);
17 t.end();
18});
19
20tape("create and load presigil-legacy", function (t) {
21 var keys = ssbkeys.generate("ed25519");
22 keys.id = keys.id.substring(1);
23 fs.writeFileSync(keyPath, JSON.stringify(keys));
24
25 ssbkeys.load(keyPath, function (err, k2) {
26 if (err) throw err;
27 t.equal(k2.id, "@" + keys.id);
28 t.end();
29 });
30});
31
32tape("prevent clobbering existing keys", function (t) {
33 fs.writeFileSync(keyPath, "this file intentionally left blank", "utf8");
34 t.throws(function () {
35 ssbkeys.createSync(keyPath);
36 });
37 ssbkeys.create(keyPath, function (err) {
38 t.ok(err);
39 fs.unlinkSync(keyPath);
40 t.end();
41 });
42});
43
44tape("loadOrCreate can load", function (t) {
45 var keyPath = path.join(os.tmpdir(), `ssb-keys-1-${Date.now()}`);
46 var keys = ssbkeys.generate("ed25519");
47 keys.id = keys.id.substring(1);
48 fs.writeFileSync(keyPath, JSON.stringify(keys));
49
50 ssbkeys.loadOrCreate(keyPath, (err, k2) => {
51 t.error(err);
52 t.equal(k2.id, "@" + keys.id);
53 t.end();
54 });
55});
56
57tape("loadOrCreate can create", function (t) {
58 var keyPath = path.join(os.tmpdir(), `ssb-keys-2-${Date.now()}`);
59 t.equal(fs.existsSync(keyPath), false);
60
61 ssbkeys.loadOrCreate(keyPath, (err, keys) => {
62 t.error(err);
63 t.true(keys.public.length > 20, "keys.public is a long string");
64 t.true(keys.private.length > 20, "keys.private is a long string");
65 t.true(keys.id.length > 20, "keys.id is a long string");
66 t.end();
67 });
68});
69
70tape("loadOrCreateSync can load", function (t) {
71 var keyPath = path.join(os.tmpdir(), `ssb-keys-3-${Date.now()}`);
72 var keys = ssbkeys.generate("ed25519");
73 keys.id = keys.id.substring(1);
74 fs.writeFileSync(keyPath, JSON.stringify(keys));
75
76 var k2 = ssbkeys.loadOrCreateSync(keyPath);
77 t.equal(k2.id, "@" + keys.id);
78 t.end();
79});
80
81tape("loadOrCreateSync can create", function (t) {
82 var keyPath = path.join(os.tmpdir(), `ssb-keys-4-${Date.now()}`);
83 t.equal(fs.existsSync(keyPath), false);
84
85 var keys = ssbkeys.loadOrCreateSync(keyPath);
86 t.true(keys.public.length > 20, "keys.public is a long string");
87 t.true(keys.private.length > 20, "keys.private is a long string");
88 t.true(keys.id.length > 20, "keys.id is a long string");
89 t.end();
90});
91
92tape("don't create dir for fully-specified path", function (t) {
93 const keyPath = path.join(os.tmpdir(), `ssb-keys-5-${Date.now()}`);
94 t.equal(fs.existsSync(keyPath), false);
95 ssbkeys.loadOrCreate(keyPath, (err) => {
96 t.error(err);
97 t.true(fs.lstatSync(keyPath).isFile());
98
99 ssbkeys.loadOrCreate(keyPath, (err, keys) => {
100 t.error(err);
101 t.equal(keys.public.length, 52);
102 fs.unlinkSync(keyPath);
103 t.end();
104 });
105 });
106});