bundle_test.dart 2.34 KB
Newer Older
1
import 'dart:convert' hide BASE64;
Matt Perry's avatar
Matt Perry committed
2
import 'dart:io';
3
import 'dart:typed_data';
Matt Perry's avatar
Matt Perry committed
4

5
import 'package:crypto/crypto.dart';
Matt Perry's avatar
Matt Perry committed
6
import 'package:flx/bundle.dart';
7
import 'package:flx/signing.dart';
Matt Perry's avatar
Matt Perry committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import 'package:test/test.dart';

main() async {
  // The following constant was generated via the openssl shell commands:
  // openssl ecparam -genkey -name prime256v1 -out privatekey.pem
  // openssl ec -in privatekey.pem -outform DER | base64
  const String kPrivateKeyBase64 = 'MHcCAQEEIG4Xt+MgsdP/o89kAHz7EVVLKkN+DUfpaBtZfMyFGbUgoAoGCCqGSM49AwEHoUQDQgAElPtbBVPPqKHYXYAgHaxB2hL6sXeFc99YLijTAuAPe2Nbhywan+v4k+nFm0TJJW/mkV+nH+fyBZ98t4UcFCqkOg==';
  final List<int> kPrivateKeyDER = BASE64.decode(kPrivateKeyBase64);

  // Test manifest.
  final Map<String, dynamic> kManifest = <String, dynamic>{
    'name': 'test app',
    'version': '1.0.0'
  };

  // Simple test byte pattern.
  final Uint8List kTestBytes = new Uint8List.fromList(<int>[1, 2, 3]);

  // Create a temp dir and file for the bundle.
  Directory tempDir = await Directory.systemTemp.createTempSync('bundle_test');
28
  String bundlePath = tempDir.path + '/bundle.flx';
Matt Perry's avatar
Matt Perry committed
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

  AsymmetricKeyPair keyPair = keyPairFromPrivateKeyBytes(kPrivateKeyDER);
  Map<String, dynamic> manifest = JSON.decode(UTF8.decode(
      serializeManifest(kManifest, keyPair.publicKey, kTestBytes)));

  test('verifyContent works', () async {
    Bundle bundle = new Bundle.fromContent(
      path: bundlePath,
      manifest: manifest,
      contentBytes: kTestBytes,
      keyPair: keyPair
    );

    bool verifies = await bundle.verifyContent();
    expect(verifies, equals(true));
  });

  test('write/read works', () async {
    Bundle bundle = new Bundle.fromContent(
      path: bundlePath,
      manifest: manifest,
      contentBytes: kTestBytes,
      keyPair: keyPair
    );

    bundle.writeSync();

    Bundle diskBundle = await Bundle.readHeader(bundlePath);
    expect(diskBundle != null, equals(true));
    expect(diskBundle.manifestBytes, equals(bundle.manifestBytes));
    expect(diskBundle.signatureBytes, equals(bundle.signatureBytes));
    expect(diskBundle.manifest['key'], equals(bundle.manifest['key']));
    expect(diskBundle.manifest['key'], equals(manifest['key']));

    bool verifies = await diskBundle.verifyContent();
    expect(verifies, equals(true));
  });

  test('cleanup', () async {
    tempDir.deleteSync(recursive: true);
  });
}