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

6
import 'package:crypto/crypto.dart';
Matt Perry's avatar
Matt Perry committed
7
import 'package:flx/bundle.dart';
8
import 'package:flx/signing.dart';
Matt Perry's avatar
Matt Perry committed
9 10
import 'package:test/test.dart';

Ian Hickson's avatar
Ian Hickson committed
11
Future<Null> main() async {
Matt Perry's avatar
Matt Perry committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  // 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');
29
  String bundlePath = tempDir.path + '/bundle.flx';
Matt Perry's avatar
Matt Perry committed
30

Ian Hickson's avatar
Ian Hickson committed
31
  AsymmetricKeyPair<PublicKey, PrivateKey> keyPair = keyPairFromPrivateKeyBytes(kPrivateKeyDER);
Matt Perry's avatar
Matt Perry committed
32
  Map<String, dynamic> manifest = JSON.decode(UTF8.decode(
Ian Hickson's avatar
Ian Hickson committed
33 34
    serializeManifest(kManifest, keyPair.publicKey, kTestBytes)
  ));
Matt Perry's avatar
Matt Perry committed
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

  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);
  });
}