qr-panel-readable.test.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { render } from '@testing-library/react';
  2. import { describe, expect, it } from 'vitest';
  3. import { genAmneziaWGConfig } from '@/lib/xray/inbound-link';
  4. import QrPanel from '@/pages/inbounds/qr/QrPanel';
  5. import { AmneziawgInboundSettingsSchema } from '@/schemas/protocols/inbound/amneziawg';
  6. const KEY = `${'A'.repeat(43)}=`;
  7. function awgConfig(disableCookies: boolean): string {
  8. const settings = AmneziawgInboundSettingsSchema.parse({
  9. server: {
  10. publicKey: KEY,
  11. mtu: 1420,
  12. primaryDns: '8.8.8.8',
  13. secondaryDns: '8.8.4.4',
  14. jc: 4,
  15. jmin: 65,
  16. jmax: 220,
  17. s1: 87,
  18. s2: 44,
  19. s3: 21,
  20. s4: 19,
  21. h1: '462980921-463150218',
  22. h2: '1177681572-1177787900',
  23. h3: '1907413509-1907903969',
  24. h4: '2029908558-2030313135',
  25. i1: '<r 148>',
  26. headerProtectionKey: KEY,
  27. contentPaddingAddition: '17-49',
  28. rekeyAfterTime: '111-139',
  29. rekeyTimeout: '4-7',
  30. rejectAfterTime: '187-251',
  31. keepaliveTimeout: '9-14',
  32. maxHandshakeAttempts: '19-36',
  33. randomTrailers: true,
  34. disableCookies,
  35. },
  36. clients: [
  37. {
  38. email: 'my-client',
  39. privateKey: KEY,
  40. preSharedKey: KEY,
  41. allowedIPs: ['10.8.1.2/32'],
  42. keepAlive: 25,
  43. },
  44. ],
  45. });
  46. return genAmneziaWGConfig({
  47. settings,
  48. address: 'your-server.example.com',
  49. port: 443,
  50. remark: 'my-client',
  51. peerIndex: 0,
  52. });
  53. }
  54. function qrGeometry(value: string): { viewBox: string; foreground: string } {
  55. const { container } = render(<QrPanel value={value} />);
  56. const svg = container.querySelector('.qr-code svg');
  57. const paths = svg?.querySelectorAll('path');
  58. expect(svg).not.toBeNull();
  59. expect(paths).toHaveLength(2);
  60. return {
  61. viewBox: svg?.getAttribute('viewBox') ?? '',
  62. foreground: paths?.item(1).getAttribute('d') ?? '',
  63. };
  64. }
  65. describe('QrPanel dense AmneziaWG config', () => {
  66. it('keeps the complete 3.1 config readable across the DisableCookies QR boundary', () => {
  67. const complete = awgConfig(true);
  68. const withoutDisableCookies = awgConfig(false);
  69. expect(complete).toContain('DisableCookies = on\n');
  70. expect(complete.length - withoutDisableCookies.length).toBe(20);
  71. const completeQr = qrGeometry(complete);
  72. const shorterQr = qrGeometry(withoutDisableCookies);
  73. expect(completeQr.viewBox).toBe('0 0 101 101');
  74. expect(shorterQr.viewBox).toBe('0 0 97 97');
  75. expect(completeQr.foreground).toMatch(/^M2 2h7/);
  76. expect(shorterQr.foreground).toMatch(/^M2 2h7/);
  77. });
  78. });