plist_parser_test.dart 6.99 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9
import 'dart:convert';

import 'package:flutter_tools/src/base/file_system.dart';
10
import 'package:flutter_tools/src/base/logger.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/base/terminal.dart';
13 14
import 'package:flutter_tools/src/ios/plist_parser.dart';

15
import '../src/common.dart';
16
import '../src/fakes.dart';
17
import 'test_utils.dart';
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

const String base64PlistXml =
    'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NUWVBFIHBsaXN0I'
    'FBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VOIiAiaHR0cDovL3d3dy5hcHBsZS'
    '5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4wLmR0ZCI+CjxwbGlzdCB2ZXJzaW9uPSIxLjAiPgo'
    '8ZGljdD4KICA8a2V5PkNGQnVuZGxlRXhlY3V0YWJsZTwva2V5PgogIDxzdHJpbmc+QXBwPC9z'
    'dHJpbmc+CiAgPGtleT5DRkJ1bmRsZUlkZW50aWZpZXI8L2tleT4KICA8c3RyaW5nPmlvLmZsd'
    'XR0ZXIuZmx1dHRlci5hcHA8L3N0cmluZz4KPC9kaWN0Pgo8L3BsaXN0Pgo=';

const String base64PlistBinary =
    'YnBsaXN0MDDSAQIDBF8QEkNGQnVuZGxlRXhlY3V0YWJsZV8QEkNGQnVuZGxlSWRlbnRpZmllc'
    'lNBcHBfEBZpby5mbHV0dGVyLmZsdXR0ZXIuYXBwCA0iNzsAAAAAAAABAQAAAAAAAAAFAAAAAA'
    'AAAAAAAAAAAAAAVA==';

const String base64PlistJson =
    'eyJDRkJ1bmRsZUV4ZWN1dGFibGUiOiJBcHAiLCJDRkJ1bmRsZUlkZW50aWZpZXIiOiJpby5mb'
    'HV0dGVyLmZsdXR0ZXIuYXBwIn0=';

36 37 38 39 40 41 42 43 44 45 46 47 48 49
const String base64PlistXmlWithComplexDatatypes =
    'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NUWVBFIHBsaXN0I'
    'FBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VOIiAiaHR0cDovL3d3dy5hcHBsZS'
    '5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4wLmR0ZCI+CjxwbGlzdCB2ZXJzaW9uPSIxLjAiPgo'
    '8ZGljdD4KICA8a2V5PkNGQnVuZGxlRXhlY3V0YWJsZTwva2V5PgogIDxzdHJpbmc+QXBwPC9z'
    'dHJpbmc+CiAgPGtleT5DRkJ1bmRsZUlkZW50aWZpZXI8L2tleT4KICA8c3RyaW5nPmlvLmZsd'
    'XR0ZXIuZmx1dHRlci5hcHA8L3N0cmluZz4KICA8a2V5PmludFZhbHVlPC9rZXk+CiAgPGludG'
    'VnZXI+MjwvaW50ZWdlcj4KICA8a2V5PmRvdWJsZVZhbHVlPC9rZXk+CiAgPHJlYWw+MS41PC9'
    'yZWFsPgogIDxrZXk+YmluYXJ5VmFsdWU8L2tleT4KICA8ZGF0YT5ZV0pqWkE9PTwvZGF0YT4K'
    'ICA8a2V5PmFycmF5VmFsdWU8L2tleT4KICA8YXJyYXk+CiAgICA8dHJ1ZSAvPgogICAgPGZhb'
    'HNlIC8+CiAgICA8aW50ZWdlcj4zPC9pbnRlZ2VyPgogIDwvYXJyYXk+CiAgPGtleT5kYXRlVm'
    'FsdWU8L2tleT4KICA8ZGF0ZT4yMDIxLTEyLTAxVDEyOjM0OjU2WjwvZGF0ZT4KPC9kaWN0Pgo'
    '8L3BsaXN0Pg==';

50
void main() {
51 52 53 54 55 56 57 58 59 60 61 62 63
  // The tests herein explicitly don't use `MemoryFileSystem` or a mocked
  // `ProcessManager` because doing so wouldn't actually test what we want to
  // test, which is that the underlying tool we're using to parse Plist files
  // works with the way we're calling it.
  File file;
  PlistParser parser;
  BufferLogger logger;

  setUp(() {
    logger = BufferLogger(
      outputPreferences: OutputPreferences.test(),
      terminal: AnsiTerminal(
        platform: const LocalPlatform(),
64
        stdio: FakeStdio(),
65 66 67 68 69 70 71 72 73 74 75 76
      ),
    );
    parser = PlistParser(
      fileSystem: fileSystem,
      processManager: processManager,
      logger: logger,
    );
    file = fileSystem.file('foo.plist')..createSync();
  });

  tearDown(() {
    file.deleteSync();
77
  });
78

79
  testWithoutContext('PlistParser.getStringValueFromFile works with xml file', () {
80 81
    file.writeAsBytesSync(base64.decode(base64PlistXml));

82 83
    expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
    expect(parser.getStringValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
84 85
    expect(logger.statusText, isEmpty);
    expect(logger.errorText, isEmpty);
86
  }, skip: !platform.isMacOS); // [intended] requires macos tool chain.
87

88
  testWithoutContext('PlistParser.getStringValueFromFile works with binary file', () {
89 90
    file.writeAsBytesSync(base64.decode(base64PlistBinary));

91 92
    expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
    expect(parser.getStringValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
93 94
    expect(logger.statusText, isEmpty);
    expect(logger.errorText, isEmpty);
95
  }, skip: !platform.isMacOS); // [intended] requires macos tool chain.
96

97
  testWithoutContext('PlistParser.getStringValueFromFile works with json file', () {
98 99
    file.writeAsBytesSync(base64.decode(base64PlistJson));

100 101
    expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
    expect(parser.getStringValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
102 103
    expect(logger.statusText, isEmpty);
    expect(logger.errorText, isEmpty);
104
  }, skip: !platform.isMacOS); // [intended] requires macos tool chain.
105

106 107
  testWithoutContext('PlistParser.getStringValueFromFile returns null for non-existent plist file', () {
    expect(parser.getStringValueFromFile('missing.plist', 'CFBundleIdentifier'), null);
108 109
    expect(logger.statusText, isEmpty);
    expect(logger.errorText, isEmpty);
110
  }, skip: !platform.isMacOS); // [intended] requires macos tool chain.
111

112
  testWithoutContext('PlistParser.getStringValueFromFile returns null for non-existent key within plist', () {
113 114
    file.writeAsBytesSync(base64.decode(base64PlistXml));

115 116
    expect(parser.getStringValueFromFile(file.path, 'BadKey'), null);
    expect(parser.getStringValueFromFile(file.absolute.path, 'BadKey'), null);
117 118
    expect(logger.statusText, isEmpty);
    expect(logger.errorText, isEmpty);
119
  }, skip: !platform.isMacOS); // [intended] requires macos tool chain.
120

121
  testWithoutContext('PlistParser.getStringValueFromFile returns null for malformed plist file', () {
122 123
    file.writeAsBytesSync(const <int>[1, 2, 3, 4, 5, 6]);

124
    expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), null);
125 126
    expect(logger.statusText, isNotEmpty);
    expect(logger.errorText, isEmpty);
127
  }, skip: !platform.isMacOS); // [intended] requires macos tool chain.
128

129 130 131
  testWithoutContext('PlistParser.getStringValueFromFile throws when /usr/bin/plutil is not found', () async {
    file.writeAsBytesSync(base64.decode(base64PlistXml));

132
    expect(
133
      () => parser.getStringValueFromFile(file.path, 'unused'),
134 135 136 137
      throwsA(isA<FileNotFoundException>()),
    );
    expect(logger.statusText, isEmpty);
    expect(logger.errorText, isEmpty);
138
  }, skip: platform.isMacOS); // [intended] requires macos tool chain.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  testWithoutContext('PlistParser.parseFile can handle different datatypes', () async {
    file.writeAsBytesSync(base64.decode(base64PlistXmlWithComplexDatatypes));
    final Map<String, Object> values = parser.parseFile(file.path);

    expect(values['CFBundleIdentifier'], 'io.flutter.flutter.app');
    expect(values['CFBundleIdentifier'], 'io.flutter.flutter.app');
    expect(values['intValue'], 2);
    expect(values['doubleValue'], 1.5);
    expect(values['binaryValue'], base64.decode('YWJjZA=='));
    expect(values['arrayValue'], <dynamic>[true, false, 3]);
    expect(values['dateValue'], DateTime.utc(2021, 12, 1, 12, 34, 56));
    expect(logger.statusText, isEmpty);
    expect(logger.errorText, isEmpty);
  }, skip: !platform.isMacOS); // [intended] requires macos tool chain.
154
}