def testMultipleFingerprintFirmwareROVersionsValid(self):
     config = {
         'chromeos': {
             'configs': [
                 {
                     'identity': {
                         'platform-name': 'foo',
                         'sku-id': 1
                     },
                     'fingerprint': {
                         'board': 'bloonchipper',
                         'ro-version': '123'
                     }
                 },
                 {
                     'identity': {
                         'platform-name': 'foo',
                         'sku-id': 2
                     },
                     'fingerprint': {
                         'board': 'dartmonkey',
                         'ro-version': '456'
                     }
                 },
             ],
         },
     }
     cros_config_schema.ValidateConfig(json.dumps(config))
    def testWhitelabelWithOtherThanBrandChanges(self):
        config = """
chromeos:
  devices:
    - $name: 'whitelabel'
      products:
        - $key-id: 'WL1'
          $wallpaper: 'WL1_WALLPAPER'
          $whitelabel-tag: 'WL1_TAG'
          $brand-code: 'WL1_BRAND_CODE'
          $powerd-prefs: 'WL1_POWERD_PREFS'
        - $key-id: 'WL2'
          $wallpaper: 'WL2_WALLPAPER'
          $whitelabel-tag: 'WL2_TAG'
          $brand-code: 'WL2_BRAND_CODE'
          $powerd-prefs: 'WL2_POWERD_PREFS'
      skus:
        - config:
            identity:
              sku-id: 0
              whitelabel-tag: '{{$whitelabel-tag}}'
            name: '{{$name}}'
            brand-code: '{{$brand-code}}'
            wallpaper: '{{$wallpaper}}'
            # THIS WILL CAUSE THE FAILURE
            powerd-prefs: '{{$powerd-prefs}}'
"""
        try:
            cros_config_schema.ValidateConfig(
                cros_config_schema.TransformConfig(config))
        except cros_config_schema.ValidationError as err:
            self.assertIn('Whitelabel configs can only', err.__str__())
    def testHardwarePropertiesInvalid(self):
        config = \
    """
chromeos:
  devices:
    - $name: 'bad_device'
      skus:
        - config:
            identity:
              sku-id: 0
            # THIS WILL CAUSE THE FAILURE
            hardware-properties:
              has-base-accelerometer: true
              has-base-gyroscope: 7
              has-lid-accelerometer: false
              is-lid-convertible: false
              has-base-magnetometer: false
              has-touchscreen: true
"""
        try:
            cros_config_schema.ValidateConfig(
                cros_config_schema.TransformConfig(config))
        except cros_config_schema.ValidationError as err:
            self.assertIn('must be boolean', err.__str__())
        else:
            self.fail('ValidationError not raised')
    def testIdentitiesNotUnique(self):
        config = """
reef-9042-fw: &reef-9042-fw
  bcs-overlay: 'overlay-reef-private'
  ec-image: 'Reef_EC.9042.87.1.tbz2'
  main-image: 'Reef.9042.87.1.tbz2'
  main-rw-image: 'Reef.9042.110.0.tbz2'
  build-targets:
    coreboot: 'reef'
chromeos:
  devices:
    - $name: 'astronaut'
      products:
        - $key-id: 'OEM2'
      skus:
        - config:
            identity:
              sku-id: 0
            audio:
              main:
                cras-config-dir: '{{$name}}'
                ucm-suffix: '{{$name}}'
            name: '{{$name}}'
            firmware: *reef-9042-fw
            firmware-signing:
              key-id: '{{$key-id}}'
              signature-id: '{{$name}}'
    - $name: 'astronaut'
      products:
        - $key-id: 'OEM2'
      skus:
        - config:
            identity:
              sku-id: 0
            audio:
              main:
                cras-config-dir: '{{$name}}'
                ucm-suffix: '{{$name}}'
            name: '{{$name}}'
            firmware: *reef-9042-fw
            firmware-signing:
              key-id: '{{$key-id}}'
              signature-id: '{{$name}}'
"""
        try:
            cros_config_schema.ValidateConfig(
                cros_config_schema.TransformConfig(config))
        except cros_config_schema.ValidationError as err:
            self.assertIn('Identities are not unique', err.__str__())
    def testMultipleMosysPlatformsInvalid(self):
        config = {
            'chromeos': {
                'configs': [
                    {
                        'identity': {
                            'platform-name': 'SomePlatform',
                            'sku-id': 1
                        }
                    },
                    {
                        'identity': {
                            'platform-name': 'SomePlatform',
                            'sku-id': 2
                        }
                    },
                    # This causes the ValidationError.
                    {
                        'identity': {
                            'platform-name': 'AnotherPlatform',
                            'sku-id': 3
                        }
                    },
                ],
            },
        }
        with self.assertRaises(cros_config_schema.ValidationError):
            cros_config_schema.ValidateConfig(json.dumps(config))

        # Removing the offending config should clear the ValidationError.
        config['chromeos']['configs'].pop()
        try:
            cros_config_schema.ValidateConfig(json.dumps(config))
        except cros_config_schema.ValidationError:
            self.fail('Removing the offending config should have cleared the '
                      'ValidationError.')
    def testMultipleFingerprintFirmwareROVersionInvalid(self):
        config = {
            'chromeos': {
                'configs': [
                    {
                        'identity': {
                            'platform-name': 'foo',
                            'sku-id': 1
                        },
                        'fingerprint': {
                            'board': 'bloonchipper',
                            'ro-version': '123'
                        }
                    },
                    {
                        'identity': {
                            'platform-name': 'foo',
                            'sku-id': 2
                        },
                        'fingerprint': {
                            'board': 'bloonchipper',
                            'ro-version': '123'
                        }
                    },
                    # This causes the ValidationError.
                    {
                        'identity': {
                            'platform-name': 'foo',
                            'sku-id': 3
                        },
                        'fingerprint': {
                            'board': 'bloonchipper',
                            'ro-version': '456'
                        }
                    }
                ],
            },
        }
        with self.assertRaises(cros_config_schema.ValidationError) as ctx:
            cros_config_schema.ValidateConfig(json.dumps(config))

        self.assertRegex(
            str(ctx.exception),
            re.compile(
                'You may not use different fingerprint firmware RO versions on the '
                'same board:.*'))
    def testHardwarePropertiesBoolean(self):
        config = \
    """
chromeos:
  devices:
    - $name: 'good_device'
      skus:
        - config:
            identity:
              sku-id: 0
            hardware-properties:
              has-base-accelerometer: true
              has-base-gyroscope: true
              has-lid-accelerometer: true
              has-fingerprint-sensor: true
              is-lid-convertible: false
"""
        cros_config_schema.ValidateConfig(
            cros_config_schema.TransformConfig(config))
 def testFingerprintFirmwareROVersionsValid(self):
     config = {
         'chromeos': {
             'configs': [
                 {
                     'identity': {
                         'platform-name': 'foo',
                         'sku-id': 1
                     },
                     'fingerprint': {
                         'ro-version': '123'
                     }
                 },
                 # This device does not have fingerprint
                 {
                     'identity': {
                         'platform-name': 'foo',
                         'sku-id': 2
                     },
                 },
             ],
         },
     }
     cros_config_schema.ValidateConfig(json.dumps(config))
 def testWhitelabelWithOtherThanBrandChanges(self):
     config = WHITELABEL_CONFIG + INVALID_WHITELABEL_CONFIG
     with self.assertRaises(cros_config_schema.ValidationError) as ctx:
         cros_config_schema.ValidateConfig(
             cros_config_schema.TransformConfig(config))
     self.assertIn('Whitelabel configs can only', str(ctx.exception))
 def testWhitelabelWithExternalStylus(self):
     config = WHITELABEL_CONFIG
     cros_config_schema.ValidateConfig(
         cros_config_schema.TransformConfig(config))
 def testBasicValidation(self):
     cros_config_schema.ValidateConfig(
         cros_config_schema.TransformConfig(BASIC_CONFIG))