Files
dawnline/web/tests/shared.test.ts
T
Aemeath ac90575b0b
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
Dawnline 0.20.0
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode.
- **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V).
- **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request.
- **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds.
- **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person.
- **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router.
- **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch.
- **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away.
- **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once.
- **Undo.** An action done from a card or a request is undone from its toast.
- **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own.
- **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name.
- **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking.
- **Check access and Health know the networks behind routers.**
- **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st.
- Extra data lasts until that person's month ends; «Reset usage» starts the month anew.
- A limit is for a month or for the whole term; the admin and the portal show when it resets.
- Charts that said «this month» show the last 30 days.
- After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st.
- **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped.
- **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone.
- **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence.
- **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
2026-11-13 09:43:50 +03:00

153 lines
6.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { inflateSync } from 'node:zlib';
import { byDeviceState, bytes, daysLeft, initials, quotaText, slugify } from '../src/shared/format';
import { amneziaJson, base64Url, fromBase64Url, vpnKey, vpnQrText, withPrivateKey } from '../src/shared/vpnkey';
import type { ClientConfig } from '../src/shared/types';
const cfg: ClientConfig = {
name: 'Phone',
person: 'Myata',
server: 'Home',
filename: 'myata-phone.conf',
conf: '[Interface]\nPrivateKey = AAA=\nAddress = 10.77.0.2/32\n\n[Peer]\nPublicKey = BBB=\n',
has_private_key: true,
amnezia: {
host: 'vpn.example.com',
port: 51830,
mtu: '1380',
dns: ['10.0.1.1'],
allowed_ips: ['0.0.0.0/0', '::/0'],
client_ip: '10.77.0.2',
client_priv_key: 'AAA=',
client_pub_key: 'CCC=',
psk_key: 'DDD=',
server_pub_key: 'BBB=',
persistent_keep_alive: '25',
obfuscation: { jc: '4', jmin: '70', jmax: '120', s1: '20', s2: '30', h1: '11', h2: '22', h3: '33', h4: '44' },
},
};
describe('format', () => {
it('formats bytes', () => {
expect(bytes(0)).toBe('0 B');
expect(bytes(1536)).toBe('1.5 KB');
expect(bytes(5 * 1073741824)).toBe('5.0 GB');
});
it('makes initials and slugs', () => {
expect(initials('myata')).toBe('MY');
expect(initials('Anna Maria Lee')).toBe('AL');
// an emoji is one sign, never half of one; a word that begins with a letter wins over it
expect(initials('🦊 Лис')).toBe('ЛИ');
expect(initials('🦊')).toBe('🦊');
expect(initials('👨‍👩‍👧 Family Chat')).toBe('FC');
expect(initials('Ю')).toBe('Ю');
expect(initials('a.b@example.com')).toBe('AB');
expect(initials(' ')).toBe('?');
expect(slugify('Ünïcode Name!')).toBe('unicode-name');
});
it('describes quotas', () => {
expect(quotaText(1073741824, 0, 'month')).toBe('1.0 GB this month');
expect(quotaText(1073741824, 50, 'month')).toBe('1.0 GB of 50 GB this month');
expect(quotaText(0, 10, 'total')).toBe('0 B of 10 GB');
});
it('counts days left', () => {
const now = new Date(2026, 9, 14, 12).getTime();
expect(daysLeft('2026-10-14', now)).toBe(1);
expect(daysLeft('2026-10-20', now)).toBe(7);
expect(daysLeft('2026-10-01', now)).toBeLessThan(0);
expect(daysLeft('', now)).toBeNull();
});
});
describe('vpn:// keys', () => {
it('round-trips base64url', () => {
const b = new Uint8Array([0, 1, 250, 251, 252, 253, 254, 255]);
expect(Array.from(fromBase64Url(base64Url(b)))).toEqual(Array.from(b));
});
it('builds the AmneziaVPN structure', () => {
const j = amneziaJson(cfg, 'Home · Myata · Phone') as any;
expect(j.defaultContainer).toBe('amnezia-awg');
expect(j.dns1).toBe('10.0.1.1');
const last = JSON.parse(j.containers[0].awg.last_config);
expect(last.Jc).toBe('4');
expect(last.H1).toBe('11');
expect(last.port).toBe(51830);
expect(last.config).toContain('\r\n');
});
it('leaves S3 and S4 empty when the server sets none, so the app does not call the key version 2', () => {
const last = (c: ClientConfig) => JSON.parse((amneziaJson(c, 'x') as any).containers[0].awg.last_config);
expect(last(cfg).S3).toBe('');
expect(last(cfg).S4).toBe('');
expect(last(cfg).S1).toBe('20');
const off = {
...cfg,
amnezia: { ...cfg.amnezia, obfuscation: { jc: '0', jmin: '0', jmax: '0', s1: '0', s2: '0', h1: '1', h2: '2', h3: '3', h4: '4' } },
};
expect(last(off).S3).toBe('');
expect(last(off).Jc).toBe('0');
expect(last(off).I1).toBe('');
const bal = { ...cfg, amnezia: { ...cfg.amnezia, obfuscation: { ...cfg.amnezia.obfuscation, s3: '24', s4: '8' } } };
expect(last(bal).S3).toBe('24');
expect(last(bal).S4).toBe('8');
});
it('gives the one DNS server as both, so the app adds none of its own', () => {
const json = (dns: string[]) => amneziaJson({ ...cfg, amnezia: { ...cfg.amnezia, dns } }, 'x') as any;
expect(json(['10.0.1.1'])).toMatchObject({ dns1: '10.0.1.1', dns2: '10.0.1.1' });
expect(json(['10.0.1.1', '1.1.1.1'])).toMatchObject({ dns1: '10.0.1.1', dns2: '1.1.1.1' });
// Home network only, without the home DNS: nothing to give.
expect(json([]).dns1).toBeUndefined();
expect(json([]).dns2).toBeUndefined();
});
it('passes the keep-alive through as the router sends it: a range on 3.1, a number before', () => {
const awg = (persistent_keep_alive: string, obfuscation: Record<string, string>) =>
(amneziaJson({ ...cfg, amnezia: { ...cfg.amnezia, persistent_keep_alive, obfuscation } }, 'x') as any).containers[0].awg;
const max = awg('25-35', { ...cfg.amnezia.obfuscation, header_protection_key: 'HPK=' });
expect(JSON.parse(max.last_config).persistent_keep_alive).toBe('25-35');
expect(JSON.parse(max.last_config).HeaderProtectionKey).toBe('HPK=');
expect(max.protocol_version).toBe('3.1');
const bal = awg('25', cfg.amnezia.obfuscation);
expect(JSON.parse(bal.last_config).persistent_keep_alive).toBe('25');
expect(bal.protocol_version).toBe('2');
expect(JSON.parse(awg('0', {}).last_config).persistent_keep_alive).toBe('0');
});
it('produces a qCompress payload AmneziaVPN can read', async () => {
const key = await vpnKey(cfg, 'x');
expect(key.startsWith('vpn://')).toBe(true);
const raw = fromBase64Url(key.slice(6));
const len = new DataView(raw.buffer).getUint32(0, false);
const json = inflateSync(Buffer.from(raw.slice(4))).toString('utf8');
expect(json.length).toBe(len);
expect(JSON.parse(json).hostName).toBe('vpn.example.com');
});
it('puts the bare payload in the QR code: AmneziaVPN’s scanner does not drop vpn://', async () => {
const key = await vpnKey(cfg, 'x');
const qr = vpnQrText(key);
expect(qr).toBe(key.slice(6));
expect(qr).toMatch(/^[A-Za-z0-9_-]+$/);
expect(vpnQrText(qr)).toBe(qr);
});
it('substitutes a browser-held private key', () => {
const c = withPrivateKey(
{ ...cfg, has_private_key: false, conf: cfg.conf.replace('AAA=', '<private key stays on your device>') },
'ZZZ=',
);
expect(c.conf).toContain('PrivateKey = ZZZ=');
expect(c.amnezia.client_priv_key).toBe('ZZZ=');
});
});
describe('byDeviceState', () => {
it('puts online first, then offline, not set up and turned off, keeping the order within each', () => {
const d = (name: string, online: boolean, handshake: number, disabled = false) => ({ name, online, handshake, disabled });
const list = [d('off', false, 5, true), d('new', false, 0), d('away1', false, 9), d('live', true, 10), d('away2', false, 3)];
expect(byDeviceState(list).map((x) => x.name)).toEqual(['live', 'away1', 'away2', 'new', 'off']);
});
});