Skip to content

Commit

Permalink
Merge pull request #8 from ianacaburian/develop
Browse files Browse the repository at this point in the history
Added expiry time validation.
  • Loading branch information
ianacaburian authored Oct 22, 2024
2 parents 717e5d1 + f033fe6 commit 2e85a79
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 137 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ npm run build # Lint, install tests, and build package.
```
npm run test # Start vitest to run all tests.
npm run test -- -t "divideBy" # Start vitest to run one test.
npm run clean:test # Clean test build.
npm run open:test/console # Open test/console project in Xcode.
npm run install:test/console # Build and install the test/console bins.
npm run cm:clean # Clean test build.
npm run cm:open # Open test/console project in Xcode.
npm run cm:install # Build and install the test/console bins.
```

- Optional: Set "FC_NUM_RUMS" (default=1) to specify how many times to run
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ianacaburian/generate-key-file",
"version": "1.0.0",
"version": "1.0.1",
"description": "Ports juce_KeyGeneration::generateKeyFile() to node.",
"type": "module",
"main": "./dist/index.js",
Expand All @@ -10,12 +10,12 @@
"dist"
],
"scripts": {
"clean": "rm -rf dist && npm run clean:test",
"clean": "rm -rf dist && npm run cm:clean",
"lint": "eslint src",
"clean:test": "rm -rf test/bin/* && cd test/console && ./script/clean-build.sh",
"open:test/console": "cd test/console && ./script/open-xcode-macos-dev.sh",
"install:test/console": "cmake -S test/console -B test/console/build && cmake --build test/console/build && cmake --install test/console/build --prefix test",
"build": "npm run lint && npm run install:test/console && tsup",
"cm:clean": "rm -rf test/bin/* && cd test/console && ./cmake/clean-build.sh",
"cm:open": "cd test/console && ./cmake/open-xcode-macos-dev.sh",
"cm:install": "cmake -S test/console -B test/console/build && cmake --build test/console/build && cmake --install test/console/build --prefix test",
"build": "npm run lint && npm run cm:install && tsup",
"test": "vitest"
},
"repository": {
Expand Down
23 changes: 0 additions & 23 deletions src/juce/JuceDateString.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/juce/JuceKeyFileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ const xmlBuilder = new XMLBuilder({
})

export class JuceKeyFileUtils {
static toString(date: Date): string {
// Ports juce::Time::getCurrentTime().toString (true, true)
// prettier-ignore
const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
const day = date.getDate().toString()
const month = months[date.getMonth()]
const year = date.getFullYear()
let hours = date.getHours()
const minutes = date.getMinutes().toString().padStart(2, '0')
const seconds = date.getSeconds().toString().padStart(2, '0')
const ampm = hours >= 12 ? 'pm' : 'am'
hours = hours % 12
hours = hours ? hours : 12
return `${day} ${month} ${year} ${hours}:${minutes}:${seconds}${ampm}`
}

static toHexStringMilliseconds(date: Date): string {
// Ports juce::String::toHexString (juce::Time::getCurrentTime().toMilliseconds())
return date.getTime().toString(16)
}

static createKeyFileContentLine(
{
appName,
Expand Down
13 changes: 6 additions & 7 deletions src/juce/JuceKeyGeneration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GenerateExpiringKeyFileParams, GenerateKeyFileParams } from 'src/types'

import { JuceDateString } from './JuceDateString'
import { JuceKeyFileUtils } from './JuceKeyFileUtils'
import { JuceRSAKey } from './JuceRSAKey'

Expand All @@ -17,7 +16,7 @@ export class JuceKeyGeneration {
machineNumbers: params.machineNumbers,
machineNumbersAttributeName: 'mach'
},
JuceDateString.inHexMs(date)
JuceKeyFileUtils.toHexStringMilliseconds(date)
)
const comment = JuceKeyFileUtils.createKeyFileComment(
{
Expand All @@ -26,7 +25,7 @@ export class JuceKeyGeneration {
userName: params.userName,
machineNumbers: params.machineNumbers
},
JuceDateString.inFormattedComment(date)
JuceKeyFileUtils.toString(date)
)
return JuceKeyFileUtils.createKeyFile(
comment,
Expand All @@ -47,8 +46,8 @@ export class JuceKeyGeneration {
machineNumbers: params.machineNumbers,
machineNumbersAttributeName: 'expiring_mach'
},
JuceDateString.inHexMs(date),
JuceDateString.inHexMs(params.expiryTime)
JuceKeyFileUtils.toHexStringMilliseconds(date),
JuceKeyFileUtils.toHexStringMilliseconds(params.expiryTime)
)
const comment = JuceKeyFileUtils.createKeyFileComment(
{
Expand All @@ -57,8 +56,8 @@ export class JuceKeyGeneration {
userName: params.userName,
machineNumbers: params.machineNumbers
},
JuceDateString.inFormattedComment(date),
JuceDateString.inFormattedComment(params.expiryTime)
JuceKeyFileUtils.toString(date),
JuceKeyFileUtils.toString(params.expiryTime)
)
return JuceKeyFileUtils.createKeyFile(
comment,
Expand Down
65 changes: 49 additions & 16 deletions src/test/JuceKeyFileUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fc from 'fast-check'
import { JuceDateString } from 'src/juce/JuceDateString'
import { JuceKeyFileUtils } from 'src/juce/JuceKeyFileUtils'
import { JuceRSAKey } from 'src/juce/JuceRSAKey'
import {
Expand All @@ -9,43 +8,63 @@ import {
createKeyFileContentLineParamsValidator
} from 'src/types'
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { ZodFastCheck } from 'zod-fast-check'

import { execTestBin, hexArbitrary } from './test-utils'

describe('JuceKeyFileUtils', () => {
it('createKeyFileContentLine', ctx => {
console.log(`Testing ${ctx.task.name}...`)
const toResult = (params: CreateKeyFileContentLineParams) => {
const date = JuceDateString.inHexMs(new Date())
type TestParams = CreateKeyFileContentLineParams & {
date: string
}
const toResult = (params: TestParams) => {
return {
fromJuce: execTestBin(
'create-key-file-content-line',
JSON.stringify({ ...params, date })
JSON.stringify({ ...params })
),
fromUtil: JuceKeyFileUtils.createKeyFileContentLine(
params,
date
{
appName: params.appName,
userEmail: params.userEmail,
userName: params.userName,
machineNumbers: params.machineNumbers,
machineNumbersAttributeName:
params.machineNumbersAttributeName
},
params.date
)
}
}
const baseDate = JuceKeyFileUtils.toHexStringMilliseconds(
new Date('1970-01-01T00:00:00.000Z')
)
const baseString =
`{"appName":"'",` +
`"userEmail":"a@a.a",` +
`"userName":"&",` +
`"machineNumbers":"\\"",` +
`"machineNumbersAttributeName":"mach"}`
`"machineNumbersAttributeName":"mach",` +
`"date":"${baseDate}"}`
const baseCase = JSON.parse(baseString)
const baseResult = toResult(baseCase)
console.log({ baseCase, baseResult })
expect(baseResult.fromUtil).toBe(baseResult.fromJuce)
const createKeyFileContentLineParamsArbitrary = ZodFastCheck().inputOf(
createKeyFileContentLineParamsValidator
createKeyFileContentLineParamsValidator.extend({
date: z.date(),
expiryTime: z.date()
})
)
let latest
fc.assert(
fc.property(createKeyFileContentLineParamsArbitrary, input => {
const result = toResult(input)
const result = toResult({
...input,
date: JuceKeyFileUtils.toHexStringMilliseconds(input.date)
})
latest = { input, result }
const parse =
createKeyFileContentLineParamsValidator.safeParse(input)
Expand All @@ -57,32 +76,46 @@ describe('JuceKeyFileUtils', () => {

it('createKeyFileComment', ctx => {
console.log(`Testing ${ctx.task.name}...`)
const toResult = (params: CreateKeyFileCommentParams) => {
const date = JuceDateString.inFormattedComment(new Date())
type TestParams = CreateKeyFileCommentParams & {
created: string
}
const toResult = (params: TestParams) => {
return {
fromJuce: execTestBin(
'create-key-file-comment',
JSON.stringify({ ...params, date })
JSON.stringify({ ...params })
),
fromUtil: JuceKeyFileUtils.createKeyFileComment(params, date)
fromUtil: JuceKeyFileUtils.createKeyFileComment(
params,
params.created
)
}
}
const baseDate = JuceKeyFileUtils.toString(
new Date('1970-01-01T00:00:00.000Z')
)
const baseString =
`{"appName":"'",` +
`"userEmail":"a@a.a",` +
`"userName":"&",` +
`"machineNumbers":"\\""}`
`"machineNumbers":"\\"",` +
`"created":"${baseDate}"}`
const baseCase = JSON.parse(baseString)
const baseResult = toResult(baseCase)
console.log({ baseCase, baseResult })
expect(baseResult.fromUtil).toBe(baseResult.fromJuce)
const createKeyFileCommentParamsArbitrary = ZodFastCheck().inputOf(
createKeyFileCommentParamsValidator
createKeyFileCommentParamsValidator.extend({
created: z.date()
})
)
let latest
fc.assert(
fc.property(createKeyFileCommentParamsArbitrary, input => {
const result = toResult(input)
const result = toResult({
...input,
created: JuceKeyFileUtils.toString(input.created)
})
latest = { input, result }
const parse =
createKeyFileCommentParamsValidator.safeParse(input)
Expand Down
Loading

0 comments on commit 2e85a79

Please sign in to comment.