commit 67d4d19931a9db81cc015829d77aec6d27fc3940
parent c3d09f9f95db54ae3fa460909e043e949b0578f1
Author: Florian Dold <dold@taler.net>
Date: Wed, 22 Jul 2026 10:49:33 +0200
wallet: test the validity interval check for tokens
Covers a contained interval, an exact match and an interval that reaches
outside the validity window on either side.
Diffstat:
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/packages/taler-wallet-core/src/tokenSelection.test.ts b/packages/taler-wallet-core/src/tokenSelection.test.ts
@@ -13,10 +13,15 @@
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
-import { MerchantContractTokenKind } from "@gnu-taler/taler-util";
+import {
+ MerchantContractTokenKind,
+ TalerProtocolTimestamp,
+} from "@gnu-taler/taler-util";
import { test } from "node:test";
import assert from "node:assert";
+import { WalletToken } from "./db-common.js";
import {
+ isTokenValidBetween,
TokenMerchantVerificationResult,
verifyTokenMerchant,
} from "./tokenSelection.js";
@@ -169,3 +174,28 @@ test("domains may contain hyphens", (t) => {
TokenMerchantVerificationResult.Unexpected,
);
});
+
+function tokenValidFromTo(fromSec: number, toSec: number): WalletToken {
+ return {
+ tokenIssuePub: {
+ signature_validity_start: TalerProtocolTimestamp.fromSeconds(fromSec),
+ signature_validity_end: TalerProtocolTimestamp.fromSeconds(toSec),
+ },
+ } as WalletToken;
+}
+
+test("a token is valid between two points only if it covers both", (t) => {
+ const tok = tokenValidFromTo(1000, 2000);
+ const at = (s: number) => TalerProtocolTimestamp.fromSeconds(s);
+
+ // The requested interval is contained in the validity window.
+ assert.strictEqual(isTokenValidBetween(tok, at(1200), at(1800)), true);
+ // The validity window is matched exactly.
+ assert.strictEqual(isTokenValidBetween(tok, at(1000), at(2000)), true);
+ // Starts before the token is valid.
+ assert.strictEqual(isTokenValidBetween(tok, at(900), at(1800)), false);
+ // Ends after the token has expired.
+ assert.strictEqual(isTokenValidBetween(tok, at(1200), at(2100)), false);
+ // The validity window is strictly inside the requested interval.
+ assert.strictEqual(isTokenValidBetween(tok, at(900), at(2100)), false);
+});