-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathserializable.test.ts
123 lines (122 loc) · 5.07 KB
/
serializable.test.ts
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
import { BatchEncoder } from '../implementation/batch-encoder'
import { Context } from '../implementation/context'
import { EncryptionParameters } from '../implementation/encryption-parameters'
import { Encryptor } from '../implementation/encryptor'
import { KeyGenerator } from '../implementation/key-generator'
import { PlainText } from '../implementation/plain-text'
import { SEALLibrary } from '../implementation/seal'
import SEAL from '../throws_wasm_node_umd'
let seal: SEALLibrary
let parms: EncryptionParameters
let context: Context
let encoder: BatchEncoder
let keyGenerator: KeyGenerator
let encryptor: Encryptor
beforeAll(async () => {
seal = await SEAL()
parms = seal.EncryptionParameters(seal.SchemeType.bfv)
parms.setPolyModulusDegree(4096)
parms.setCoeffModulus(
seal.CoeffModulus.BFVDefault(4096, seal.SecurityLevel.tc128)
)
parms.setPlainModulus(seal.PlainModulus.Batching(4096, 20))
context = seal.Context(parms, true, seal.SecurityLevel.tc128)
encoder = seal.BatchEncoder(context)
keyGenerator = seal.KeyGenerator(context)
const secretKey = keyGenerator.secretKey()
const publicKey = keyGenerator.createPublicKey()
encryptor = seal.Encryptor(context, publicKey, secretKey)
})
describe('SecretKey', () => {
test('It should be a factory', () => {
expect(seal.Serializable).toBeDefined()
expect(typeof seal.Serializable.constructor).toBe('function')
expect(seal.Serializable).toBeInstanceOf(Object)
expect(seal.Serializable.constructor).toBe(Function)
expect(seal.Serializable.constructor.name).toBe('Function')
})
test('It should have properties', () => {
const item = seal.Serializable()
// Test properties
expect(item).toHaveProperty('instance')
expect(item).toHaveProperty('unsafeInject')
expect(item).toHaveProperty('delete')
expect(item).toHaveProperty('save')
expect(item).toHaveProperty('saveArray')
})
test('It should not have an instance by default', () => {
const item = seal.Serializable()
expect(item.instance).toBeUndefined()
})
test('It should inject', () => {
const arr = Int32Array.from({ length: encoder.slotCount }, (_, i) => i)
const plain = encoder.encode(arr) as PlainText
const item = encryptor.encryptSymmetricSerializable(plain)
const saved = item.save()
const newItem = seal.Serializable()
newItem.delete()
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item.instance)
expect(spyOn).toHaveBeenCalledWith(item.instance)
expect(newItem.save()).toEqual(saved)
})
test('It should delete the old instance and inject', () => {
const arr = Int32Array.from({ length: encoder.slotCount }, (_, i) => i)
const plain = encoder.encode(arr) as PlainText
const item = encryptor.encryptSymmetricSerializable(plain)
const item2 = encryptor.encryptSymmetricSerializable(plain)
const saved = item2.save()
const newItem = seal.Serializable()
newItem.unsafeInject(item.instance)
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item2.instance)
expect(spyOn).toHaveBeenCalledWith(item2.instance)
expect(newItem.save()).toEqual(saved)
})
test("It should delete it's instance", () => {
const arr = Int32Array.from({ length: encoder.slotCount }, (_, i) => i)
const plain = encoder.encode(arr) as PlainText
const item = encryptor.encryptSymmetricSerializable(plain)
const newItem = seal.Serializable()
newItem.unsafeInject(item.instance)
const spyOn = jest.spyOn(newItem, 'delete')
newItem.delete()
expect(spyOn).toHaveBeenCalled()
expect(newItem.instance).toBeUndefined()
expect(() => newItem.save()).toThrow(TypeError)
})
test('It should skip deleting twice', () => {
const arr = Int32Array.from({ length: encoder.slotCount }, (_, i) => i)
const plain = encoder.encode(arr) as PlainText
const item = encryptor.encryptSymmetricSerializable(plain)
const newItem = seal.Serializable()
newItem.unsafeInject(item.instance)
newItem.delete()
const spyOn = jest.spyOn(newItem, 'delete')
newItem.delete()
expect(spyOn).toHaveBeenCalled()
expect(newItem.instance).toBeUndefined()
})
test('It should save to a string', () => {
const arr = Int32Array.from({ length: encoder.slotCount }, (_, i) => i)
const plain = encoder.encode(arr) as PlainText
const item = encryptor.encryptSymmetricSerializable(plain)
const newItem = seal.Serializable()
newItem.unsafeInject(item.instance)
const spyOn = jest.spyOn(newItem, 'save')
const str = newItem.save()
expect(spyOn).toHaveBeenCalledWith()
expect(typeof str).toBe('string')
})
test('It should save to an array', () => {
const arr = Int32Array.from({ length: encoder.slotCount }, (_, i) => i)
const plain = encoder.encode(arr) as PlainText
const item = encryptor.encryptSymmetricSerializable(plain)
const newItem = seal.Serializable()
newItem.unsafeInject(item.instance)
const spyOn = jest.spyOn(newItem, 'saveArray')
const array = newItem.saveArray()
expect(spyOn).toHaveBeenCalledWith()
expect(array.constructor).toBe(Uint8Array)
})
})