-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathslot.ts
More file actions
132 lines (106 loc) · 4.27 KB
/
slot.ts
File metadata and controls
132 lines (106 loc) · 4.27 KB
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
import assert from "assert";
import pkcs11 from "pkcs11js";
import * as graphene from "../src";
import config from "./config";
context("Slot", () => {
let mod: graphene.Module;
let slots: graphene.SlotCollection;
before(() => {
mod = graphene.Module.load(config.init.lib, config.init.libName);
mod.initialize();
});
after(() => {
mod.finalize();
});
it("getSlots with tokens", () => {
slots = mod.getSlots(true);
assert.strictEqual(slots.length, config.controlValues.slotsCount, "Wrong number of slots");
});
it("getSlots without tokens", () => {
slots = mod.getSlots(false);
assert.strictEqual(slots.length, config.controlValues.slotsCount, "Wrong number of slots");
});
it("for..of slots", () => {
const slots = mod.getSlots();
for (const slot of slots) {
assert(slot.handle);
}
});
it("slot props", () => {
const slot = slots.items(config.controlValues.slot.slotIndex);
// slot
assert.notStrictEqual(slot.flags, 0);
assert.strictEqual(slot.manufacturerID,
config.controlValues.slot.token.manufacturerID);
assert.notStrictEqual(slot.slotDescription, "");
});
it("token props", () => {
const slot = slots.items(config.controlValues.slot.slotIndex);
// token
const token = slot.getToken();
assert.strictEqual(token.flags, config.controlValues.slot.token.flags);
assert.strictEqual(token.label, config.controlValues.slot.token.label);
assert.strictEqual(token.manufacturerID, config.controlValues.slot.token.manufacturerID);
assert.strictEqual(token.minPinLen, config.controlValues.slot.token.minPinLen);
if (token.flags & pkcs11.CKF_CLOCK_ON_TOKEN) {
assert.notStrictEqual(token.utcTime, null);
} else {
assert.strictEqual(token.utcTime, null);
}
});
context("mechanism", () => {
it("create from unsupported mechanism id", () => {
assert.throws(() => {
graphene.Mechanism.create("UNKNOWN_ALGORITHM");
}, (err: Error) => {
assert.strictEqual(err.message, "Unknown mechanism name 'UNKNOWN_ALGORITHM'");
return true;
});
});
context("collection", () => {
it("items", () => {
const slots = mod.getSlots();
const slot = slots.items(config.controlValues.slot.slotIndex);
const mechanisms = slot.getMechanisms();
assert.strictEqual(!!mechanisms.length, true);
const mech = mechanisms.items(0);
assert.strictEqual(!!mech.name, true);
});
it("tryGetItem", () => {
const slots = mod.getSlots();
const slot = slots.items(config.controlValues.slot.slotIndex);
const mechanisms = slot.getMechanisms();
const mech = mechanisms.tryGetItem(99999);
assert.strictEqual(mech, null);
});
it("for..of mechanisms", () => {
const slots = mod.getSlots();
const slot = slots.items(config.controlValues.slot.slotIndex);
const mechanisms = slot.getMechanisms();
for (const mech of mechanisms) {
assert(mech.handle);
}
});
});
context("vendor", () => {
it("json", () => {
graphene.Mechanism.vendor("./vendor/safenet_v5_3.json");
assert.strictEqual((graphene.MechanismEnum as any).DES3_CBC_PAD_IPSEC, 2147483950);
});
it("name, value", () => {
graphene.Mechanism.vendor("CUSTOM_ALG", 888888888);
assert.strictEqual((graphene.MechanismEnum as any).CUSTOM_ALG, 888888888);
});
});
});
it("close all", () => {
const slot = slots.items(config.controlValues.slot.slotIndex);
const session = slot.open();
assert.strictEqual(session.flags, graphene.SessionFlag.SERIAL_SESSION);
slot.closeAll();
// must throw CKR_SESSION_HANDLE_INVALID:179
assert.throws(() => {
session.close();
});
});
});