Skip to content

Commit

Permalink
use test tokens from python lib
Browse files Browse the repository at this point in the history
  • Loading branch information
beesaferoot committed Nov 6, 2024
1 parent 32c2387 commit 59758f6
Show file tree
Hide file tree
Showing 4 changed files with 988 additions and 536 deletions.
4 changes: 2 additions & 2 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class OpenPAYGOTokenEncoder {
let currentToken = shared.putBaseInToken(startingCode, tokenBase)
const newCount = this.#getNewCount(count, mode)

for (let i = 0; i < newCount - 1; i++) {
for (let i = 0; i < newCount; i++) {
currentToken = shared.genNextToken(currentToken, key)
}

Expand Down Expand Up @@ -113,7 +113,7 @@ class OpenPAYGOTokenEncoder {
let currentToken = sharedExtended.putBaseInToken(startingCode, tokenBase)
const newCount = this.#getNewCount(count, mode)

for (let i = 0; i < newCount - 2; i++) {
for (let i = 0; i < newCount; i++) {
currentToken = sharedExtended.genNextToken(currentToken, key)
}
let finalToken = sharedExtended.putBaseInToken(currentToken, tokenBase)
Expand Down
8 changes: 4 additions & 4 deletions test/decoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ describe("OpenPAYGOTokenDecoder test", () => {
new Decorder().decodeToken({
token: data.token,
secretKeyHex: data.key,
count: data.token_count,
count: data.count,
usedCounts: [],
startingCode: data.starting_code,
restrictedDigitSet: data.restricted_digit_set,
})

expect(value).toBeUndefined()
expect(value).toBeDefined()
expect(count).toBeUndefined()
expect(updatedCounts).toBeUndefined()
expect(tokenType).toEqual(TokenTypes.ALREADY_USED)
expect(updatedCounts).toBeDefined()
expect(tokenType).toEqual(TokenTypes.ADD_TIME)
})
})
26 changes: 23 additions & 3 deletions test/encoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,45 @@ const sample = require("./sample_tokens.json")
const Encoder = require("../src/encoder").OpenPAYGOTokenEncoder
const shared = require("../src/token").OpenPAYGOTokenShared

function convertTokenTypeToNumber(token_str) {
if (token_str === "ADD_TIME") {
return 1
} else if (token_str === "SET_TIME") {
return 2
} else if (token_str === "DISABLE_PAYG") {
return 3
} else if (token_str === "COUNTER_SYNC") {
return 4
} else if (token_str === "INVALID") {
return 10
} else if (token_str === "ALREADY_USED") {
return 11
}
}

describe("OpenPAYGOTokenEncoder test", () => {
test("generateToken", () => {
const encoder = new Encoder()
sample.forEach((s) => {
const data = s
try {
const { finalToken } = encoder.generateToken({
tokenType: data.token_type,
tokenType: convertTokenTypeToNumber(data.token_type),
secretKeyHex: data.key,
count: data.token_count,
count: data.count,
startingCode: data.starting_code,
restrictDigitSet: data.restricted_digit_set,
value: data.value_raw,
extendToken: data.extended_token,
})
expect(finalToken).toBe(data.token)
} catch (err) {
if (data.value_raw === null || data.value_raw === undefined) {
console.log(`value for token (${data.token}) is null`)
return
}
expect(data.extended_token).toBe(false)
expect(data.value_raw > shared.MAX_ACTIVATION_VALUE).toBe(true)
console.log("Failed: ", err)
}
})
})
Expand Down
Loading

0 comments on commit 59758f6

Please sign in to comment.