๐Ÿ“ฆ amake / orgro

๐Ÿ“„ serialization_test.dart ยท 128 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:org_flutter/org_flutter.dart';
import 'package:orgro/src/encryption.dart';
import 'package:orgro/src/serialization.dart';

import 'utils/encryption.dart';

Iterable<File> get testFiles => Directory('assets/test')
    .listSync(recursive: true)
    .whereType<File>()
    .where(
      (file) =>
          file.path.endsWith('.org') &&
          !file.path.endsWith('encoding-sjis.org'),
    );

void main() {
  setUp(() {
    mockOpenPGP();
  });
  tearDown(() {
    restoreOpenPGP();
  });
  group('Serialization', () {
    test('OrgroSerializer', () {
      for (final file in testFiles) {
        final serializer = OrgroSerializer();
        final content = file.readAsStringSync();
        final doc = OrgDocument.parse(content);
        expect(
          content,
          doc.toMarkup(serializer: serializer),
          reason: '${file.path} should round-trip',
        );
      }
    });
    group('Decrypted content', () {
      const content = '''
* foo

-----BEGIN PGP MESSAGE-----

jA0ECQMI05i+dd7lsRry0joBcGZQ4m+N9M/Z3I2Xw7SSn2uPMRpWbH9UIRkzPTXU
28y0FO/Aug4JaE/n+wqbmgY0b37dZzWI2BWi
=uuok
-----END PGP MESSAGE-----
''';
      const password = 'foobar';
      final doc = OrgDocument.parse(content);
      final found = doc.find<OrgPgpBlock>((_) => true);
      final (path: _, node: block) = found!;
      // We don't need to actually decrypt here because it's slow
      final serializer = OrgroDecryptedContentSerializer(
        block,
        cleartext: 'bar\n',
        password: password,
      );
      final replacement = OrgDecryptedContent.fromDecryptedResult(
        1,
        'bar\n',
        serializer,
      );
      final newDoc = doc.editNode(block)!.replace(replacement).commit();

      test('Default serializer returns cyphertext', () {
        expect(newDoc.toMarkup(), content);
      });
      test('OrgroPlaintextSerializer returns plain text', () {
        expect(
          newDoc.toMarkup(serializer: OrgroPlaintextSerializer()),
          '* foo\n\nbar\n',
        );
      });
      test('OrgroCyphertextSerializer returns cyphertext', () {
        expect(
          newDoc.toMarkup(serializer: OrgroCyphertextSerializer([])),
          content,
        );
      });
    });
    group('Encryption', () {
      const content = '''
* foo :crypt:

bar
''';
      final doc = OrgDocument.parse(content);
      test('Missing password', () {
        expect(
          () => doc.toMarkup(serializer: OrgroCyphertextSerializer([])),
          throwsStateError,
        );
      });
      test('No matching password', () {
        final password = (password: 'foobar', predicate: (_) => false);
        final serializer = OrgroCyphertextSerializer([password]);
        expect(() => doc.toMarkup(serializer: serializer), throwsStateError);
      });
      test('Password provided', () {
        final password = (password: 'foobar', predicate: (_) => true);
        final serializer = OrgroCyphertextSerializer([password]);
        final output = doc.toMarkup(serializer: serializer);
        // dart-pg cyphertext is apparently not stable, so we can't compare exactly
        expect(
          output.startsWith('''* foo :crypt:

-----BEGIN PGP MESSAGE-----
'''),
          isTrue,
        );
      });
      test('Round-trip', () {
        final password = (password: 'foobar', predicate: (_) => true);
        final serializer = OrgroCyphertextSerializer([password]);
        final output = doc.toMarkup(serializer: serializer);
        expect(output.contains('-----BEGIN PGP MESSAGE-----'), isTrue);
        expect(output.contains('bar'), isFalse);
        final newDoc = OrgDocument.parse(output);
        final (path: _, node: block) = newDoc.find<OrgPgpBlock>((_) => true)!;
        final [plaintext] = decrypt(([block], password.password));
        expect(plaintext, 'bar\n');
      });
    });
  });
}