11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_cicommon.requireNoPackageJSONAbove();
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ciconst crypto = require('crypto');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst depPolicy = fixtures.path('policy', 'dep-policy.json');
161cb0ef41Sopenharmony_ciconst dep = fixtures.path('policy', 'dep.js');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst emptyHash = crypto.createHash('sha512');
191cb0ef41Sopenharmony_ciemptyHash.update('');
201cb0ef41Sopenharmony_ciconst emptySRI = `sha512-${emptyHash.digest('base64')}`;
211cb0ef41Sopenharmony_ciconst policyHash = crypto.createHash('sha512');
221cb0ef41Sopenharmony_cipolicyHash.update(fs.readFileSync(depPolicy));
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci/* eslint-disable max-len */
251cb0ef41Sopenharmony_ci// When using \n only
261cb0ef41Sopenharmony_ciconst nixPolicySRI = 'sha512-u/nXI6UacK5fKDC2bopcgnuQY4JXJKlK3dESO3GIKKxwogVHjJqpF9rgk7Zw+TJXIc96xBUWKHuUgOzic8/4tQ==';
271cb0ef41Sopenharmony_ci// When \n is turned into \r\n
281cb0ef41Sopenharmony_ciconst windowsPolicySRI = 'sha512-OeyCPRo4OZMosHyquZXDHpuU1F4KzG9UHFnn12FMaHsvqFUt3TFZ+7wmZE7ThZ5rsQWkUjc9ZH0knGZ2e8BYPQ==';
291cb0ef41Sopenharmony_ci/* eslint-enable max-len */
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst depPolicySRI = `${nixPolicySRI} ${windowsPolicySRI}`;
321cb0ef41Sopenharmony_ci{
331cb0ef41Sopenharmony_ci  const { status, stderr } = spawnSync(
341cb0ef41Sopenharmony_ci    process.execPath,
351cb0ef41Sopenharmony_ci    [
361cb0ef41Sopenharmony_ci      '--policy-integrity', emptySRI,
371cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
381cb0ef41Sopenharmony_ci    ]
391cb0ef41Sopenharmony_ci  );
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  assert.ok(stderr.includes('ERR_MANIFEST_ASSERT_INTEGRITY'));
421cb0ef41Sopenharmony_ci  assert.strictEqual(status, 1);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  const { status, stderr } = spawnSync(
461cb0ef41Sopenharmony_ci    process.execPath,
471cb0ef41Sopenharmony_ci    [
481cb0ef41Sopenharmony_ci      '--policy-integrity', '',
491cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
501cb0ef41Sopenharmony_ci    ]
511cb0ef41Sopenharmony_ci  );
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  assert.ok(stderr.includes('--policy-integrity'));
541cb0ef41Sopenharmony_ci  assert.strictEqual(status, 9);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci{
571cb0ef41Sopenharmony_ci  const { status, stderr } = spawnSync(
581cb0ef41Sopenharmony_ci    process.execPath,
591cb0ef41Sopenharmony_ci    [
601cb0ef41Sopenharmony_ci      '--policy-integrity', depPolicySRI,
611cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
621cb0ef41Sopenharmony_ci    ]
631cb0ef41Sopenharmony_ci  );
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  assert.strictEqual(status, 0, `status: ${status}\nstderr: ${stderr}`);
661cb0ef41Sopenharmony_ci}
67