def testSkuIdOutOfBound(self):
     config = BASIC_CONFIG.replace('$sku-id: 0', '$sku-id: 0x80000000')
     with self.assertRaises(jsonschema.ValidationError) as ctx:
         libcros_schema.ValidateConfigSchema(
             self._schema, cros_config_schema.TransformConfig(config))
     self.assertIn("'sku-id': %i" % 0x80000000, str(ctx.exception))
     self.assertIn('is not valid', str(ctx.exception))
    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 testReferencedNonExistentTemplateVariable(self):
     config = re.sub(r' *$card: .*', '', BASIC_CONFIG)
     try:
         libcros_schema.ValidateConfigSchema(
             self._schema, cros_config_schema.TransformConfig(config))
     except cros_config_schema.ValidationError as err:
         self.assertIn('Referenced template variable', err.__str__())
         self.assertIn('cras-config-dir', err.__str__())
 def testMissingRequiredElement(self):
     config = re.sub(r' *cras-config-dir: .*', '', BASIC_CONFIG)
     config = re.sub(r' *volume: .*', '', BASIC_CONFIG)
     try:
         libcros_schema.ValidateConfigSchema(
             self._schema, cros_config_schema.TransformConfig(config))
     except jsonschema.ValidationError as err:
         self.assertIn('required', err.__str__())
         self.assertIn('cras-config-dir', err.__str__())
 def testBasicTransform(self):
     result = cros_config_schema.TransformConfig(BASIC_CONFIG)
     json_dict = json.loads(result)
     self.assertEqual(len(json_dict), 1)
     json_obj = libcros_schema.GetNamedTuple(json_dict)
     self.assertEqual(1, len(json_obj.chromeos.configs))
     model = json_obj.chromeos.configs[0]
     self.assertEqual('basking', model.name)
     self.assertEqual('basking', model.audio.main.cras_config_dir)
     # Check multi-level template variable evaluation
     self.assertEqual('/etc/cras/basking/dsp.ini',
                      model.audio.main.files[0].destination)
 def testSkuIdOutOfBound(self):
     config = BASIC_CONFIG.replace('$sku-id: 0', '$sku-id: 0x80000000')
     with self.assertRaises(jsonschema.ValidationError) as ctx:
         libcros_schema.ValidateConfigSchema(
             self._schema, cros_config_schema.TransformConfig(config))
     if version.parse(jsonschema.__version__) >= version.Version('3.0.0'):
         self.assertIn('%i is greater than the maximum' % 0x80000000,
                       str(ctx.exception))
         self.assertIn('sku-id', str(ctx.exception))
     else:
         self.assertIn("'sku-id': %i" % 0x80000000, str(ctx.exception))
         self.assertIn('is not valid', str(ctx.exception))
    def testTransformConfig_FilterMatch(self):
        scoped_config = """
reef-9042-fw: &reef-9042-fw
  bcs-overlay: 'overlay-reef-private'
  ec-ro-image: 'Reef_EC.9042.87.1.tbz2'
  main-ro-image: 'Reef.9042.87.1.tbz2'
  main-rw-image: 'Reef.9042.110.0.tbz2'
  build-targets:
    coreboot: 'reef'
chromeos:
  devices:
    - $name: 'foo'
      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: 'bar'
      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}}'
"""

        result = cros_config_schema.TransformConfig(scoped_config,
                                                    model_filter_regex='bar')
        json_dict = json.loads(result)
        json_obj = libcros_schema.GetNamedTuple(json_dict)
        self.assertEqual(1, len(json_obj.chromeos.configs))
        model = json_obj.chromeos.configs[0]
        self.assertEqual('bar', model.name)
    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 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 testTemplateVariableScope(self):
        scoped_config = """
audio_common: &audio_common
  main:
    $ucm: "default"
    $cras: "default"
    ucm-suffix: "{{$ucm}}"
    cras-config-dir: "{{$cras}}"
chromeos:
  devices:
    - $name: "some"
      $ucm: "overridden-by-device-scope"
      products:
        - $key-id: 'SOME-KEY'
          $brand-code: 'SOME-BRAND'
          $cras: "overridden-by-product-scope"
      skus:
        - $sku-id: 0
          config:
            audio: *audio_common
            brand-code: '{{$brand-code}}'
            identity:
              platform-name: "Some"
              smbios-name-match: "Some"
            name: '{{$name}}'
            firmware:
              no-firmware: True
"""
        result = cros_config_schema.TransformConfig(scoped_config)
        json_dict = json.loads(result)
        json_obj = libcros_schema.GetNamedTuple(json_dict)
        config = json_obj.chromeos.configs[0]
        self.assertEqual('overridden-by-product-scope',
                         config.audio.main.cras_config_dir)
        self.assertEqual('overridden-by-device-scope',
                         config.audio.main.ucm_suffix)
 def testBasicFilterBuildElements(self):
     json_dict = json.loads(
         cros_config_schema.FilterBuildElements(
             cros_config_schema.TransformConfig(BASIC_CONFIG),
             ['/firmware']))
     self.assertNotIn('firmware', json_dict['chromeos']['configs'][0])
 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))
 def testTransformConfig_NoMatch(self):
     result = cros_config_schema.TransformConfig(
         BASIC_CONFIG, model_filter_regex='abc123')
     json_dict = json.loads(result)
     json_obj = libcros_schema.GetNamedTuple(json_dict)
     self.assertEqual(0, len(json_obj.chromeos.configs))
 def testBasicSchemaValidation(self):
     libcros_schema.ValidateConfigSchema(
         self._schema, cros_config_schema.TransformConfig(BASIC_CONFIG))