|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'dart:typed_data'; |
| 3 | +import 'package:frontend/utils/base64_utils.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('Base64Utils Tests', () { |
| 7 | + test('encode and decode bytes correctly', () { |
| 8 | + // Create test data |
| 9 | + final testData = Uint8List.fromList([1, 2, 3, 4, 5, 255, 128, 64]); |
| 10 | + |
| 11 | + // Encode |
| 12 | + final encoded = Base64Utils.encode(testData); |
| 13 | + print('Encoded: $encoded'); |
| 14 | + |
| 15 | + // Decode |
| 16 | + final decoded = Base64Utils.decode(encoded); |
| 17 | + print('Decoded: $decoded'); |
| 18 | + |
| 19 | + // Verify |
| 20 | + expect(decoded, equals(testData)); |
| 21 | + }); |
| 22 | + |
| 23 | + test('encode and decode string correctly', () { |
| 24 | + const testString = 'Hello, World! This is a test with special chars: 你好世界 🚀'; |
| 25 | + |
| 26 | + // Encode |
| 27 | + final encoded = Base64Utils.encodeString(testString); |
| 28 | + print('Encoded string: $encoded'); |
| 29 | + |
| 30 | + // Decode |
| 31 | + final decoded = Base64Utils.decodeToString(encoded); |
| 32 | + print('Decoded string: $decoded'); |
| 33 | + |
| 34 | + // Verify |
| 35 | + expect(decoded, equals(testString)); |
| 36 | + }); |
| 37 | + |
| 38 | + test('validate base64 strings', () { |
| 39 | + expect(Base64Utils.isValidBase64('SGVsbG8gV29ybGQ='), isTrue); |
| 40 | + expect(Base64Utils.isValidBase64('not-valid-base64!!!'), isFalse); |
| 41 | + expect(Base64Utils.isValidBase64(''), isFalse); |
| 42 | + }); |
| 43 | + |
| 44 | + test('convert between base64 and base64url', () { |
| 45 | + const base64 = 'SGVsbG8gV29ybGQ='; |
| 46 | + final base64url = Base64Utils.base64ToBase64Url(base64); |
| 47 | + print('Base64: $base64'); |
| 48 | + print('Base64url: $base64url'); |
| 49 | + |
| 50 | + expect(base64url, equals('SGVsbG8gV29ybGQ')); |
| 51 | + |
| 52 | + final backToBase64 = Base64Utils.base64UrlToBase64(base64url); |
| 53 | + expect(backToBase64, equals(base64)); |
| 54 | + }); |
| 55 | + |
| 56 | + test('safely decode both base64 and base64url formats', () { |
| 57 | + final testData = Uint8List.fromList([255, 128, 64, 32]); |
| 58 | + |
| 59 | + // Standard base64 |
| 60 | + final standardBase64 = Base64Utils.encode(testData); |
| 61 | + final decoded1 = Base64Utils.safelyDecode(standardBase64); |
| 62 | + expect(decoded1, equals(testData)); |
| 63 | + |
| 64 | + // Base64url format |
| 65 | + final base64url = Base64Utils.base64ToBase64Url(standardBase64); |
| 66 | + final decoded2 = Base64Utils.safelyDecode(base64url); |
| 67 | + expect(decoded2, equals(testData)); |
| 68 | + }); |
| 69 | + |
| 70 | + test('prevent double encoding issue', () { |
| 71 | + // Simulate what was happening in the old code |
| 72 | + final originalData = Uint8List.fromList([1, 2, 3, 4, 5]); |
| 73 | + |
| 74 | + // First encoding (correct) |
| 75 | + final firstEncode = Base64Utils.encode(originalData); |
| 76 | + print('First encode: $firstEncode'); |
| 77 | + |
| 78 | + // WRONG: decode then re-encode (creates double encoding) |
| 79 | + final doubleEncoded = Base64Utils.encode(Base64Utils.decode(firstEncode)); |
| 80 | + print('Double encoded: $doubleEncoded'); |
| 81 | + |
| 82 | + // They should be the same! |
| 83 | + expect(doubleEncoded, equals(firstEncode)); |
| 84 | + |
| 85 | + // Verify the decoded data is correct |
| 86 | + expect(Base64Utils.decode(firstEncode), equals(originalData)); |
| 87 | + }); |
| 88 | + |
| 89 | + test('debug info provides useful information', () { |
| 90 | + const validBase64 = 'SGVsbG8gV29ybGQ='; |
| 91 | + const invalidBase64 = 'not-valid!!!'; |
| 92 | + |
| 93 | + final validInfo = Base64Utils.getDebugInfo(validBase64); |
| 94 | + print('Valid base64 info: $validInfo'); |
| 95 | + expect(validInfo['isValid'], isTrue); |
| 96 | + expect(validInfo['hasPadding'], isTrue); |
| 97 | + |
| 98 | + final invalidInfo = Base64Utils.getDebugInfo(invalidBase64); |
| 99 | + print('Invalid base64 info: $invalidInfo'); |
| 100 | + expect(invalidInfo['isValid'], isFalse); |
| 101 | + }); |
| 102 | + |
| 103 | + test('simulate PQC attachment flow (no double encoding)', () { |
| 104 | + // Simulate picking a file |
| 105 | + final fileBytes = Uint8List.fromList(List.generate(1000, (i) => i % 256)); |
| 106 | + print('Original file size: ${fileBytes.length} bytes'); |
| 107 | + |
| 108 | + // Step 1: Encode file to base64 (in compose_screen.dart) |
| 109 | + final contentBase64 = Base64Utils.encode(fileBytes); |
| 110 | + print('After base64 encode: ${contentBase64.length} chars'); |
| 111 | + |
| 112 | + // Step 2: In backend, use AsIs() - NO decode/encode! |
| 113 | + // This is what we fixed: |
| 114 | + // OLD: var bytes = Convert.FromBase64String(a.ContentBase64); |
| 115 | + // var contentText = Convert.ToBase64String(bytes); |
| 116 | + // NEW: var contentText = Base64Utils.AsIs(a.ContentBase64); |
| 117 | + |
| 118 | + // Step 3: Backend encrypts the base64 string (simulated) |
| 119 | + final encrypted = 'ENCRYPTED_${contentBase64}_ENCRYPTED'; |
| 120 | + |
| 121 | + // Step 4: Backend decrypts (simulated) |
| 122 | + final decryptedBase64 = encrypted.substring(10, encrypted.length - 10); |
| 123 | + |
| 124 | + // Step 5: Frontend decodes base64 to get original file |
| 125 | + final recoveredBytes = Base64Utils.decode(decryptedBase64); |
| 126 | + print('Recovered file size: ${recoveredBytes.length} bytes'); |
| 127 | + |
| 128 | + // Verify no data loss |
| 129 | + expect(recoveredBytes, equals(fileBytes)); |
| 130 | + print('✅ PQC attachment flow test PASSED - no double encoding!'); |
| 131 | + }); |
| 132 | + }); |
| 133 | +} |
0 commit comments