Home

js-spectre @64337f178d031116037b497b1c69ac477df5cfe0 - refs - log -
-
https://git.jolheiser.com/js-spectre.git
JS implementation for spectre/masterpassword
js-spectre / tests / index.test.ts
- raw
 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
import { assert, describe, it } from "vitest"
import { DOMParser, Element } from "@xmldom/xmldom"
import { Spectre } from "../src/spectre.js"
import { Scope, SimpleScoper } from "../src/scope.js"
import { Template } from "../src/template.js"
import { readFileSync } from "node:fs"
import { join } from "node:path"

describe("example", () => {
    it("should generate the example password", () => {
        const s = new Spectre("Robert Lee Mitchell", "banana colored duckling")
        const pw = s.site("masterpasswordapp.com")
        assert.equal(pw, "Jejr5[RepuSosp")
    })
    it("should generate the custom example password", () => {
        const scoper = new SimpleScoper("com.jojodev.jolheiser")
        const s = new Spectre(
            "Robert Lee Mitchell",
            "banana colored duckling",
            scoper,
        )
        const pw = s.site("jojodev.com", Template.MAXIMUM, 2, Scope.IDENTIFICATION)
        assert.equal(pw, "Ig^JIcxD!*)TbefJBi6-")
    })
})

describe("spectre", () => {
    const tests = readFileSync(join(__dirname, "spectre_tests.xml"), "utf8")
    const parser = new DOMParser()
    const xml = parser.parseFromString(tests, "text/xml")
    const cases = xml.getElementsByTagName("case")
    const parseCase = (c: Element): Record<string, string> => {
        const caseData: Record<string, string> = {}
        const id = c.getAttribute("id")
        caseData.id = id ?? ""
        Array.from(c.childNodes).forEach((child) => {
            if (child.textContent === null) return
            caseData[child.nodeName] = child.textContent.trim()
        })
        return caseData
    }
    const def = parseCase(cases[0])
    const getDef = (c: Record<string, string>, key: string): string => {
        if (key in c) return c[key]
        return def[key]
    }
    for (const c of Array.from(cases).slice(1)) {
        const test = parseCase(c)
        it(`should generate the password for ${test.id}`, () => {
            const s = new Spectre(
                getDef(test, "userName"),
                getDef(test, "userSecret"),
            )
            const pw = s.site(
                getDef(test, "siteName"),
                getDef(test, "resultType") as Template,
                Number.parseInt(getDef(test, "keyCounter")),
                getDef(test, "keyPurpose") as Scope,
            )
            assert.equal(pw, test.result)
        })
    }
})