Skip to content

Commit

Permalink
Refactor link protection test
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Nov 6, 2019
1 parent 2d8b41d commit e88e032
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/vs/workbench/test/contrib/linkProtection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,49 @@ import * as assert from 'assert';
import { isURLDomainTrusted } from 'vs/workbench/contrib/url/common/trustedDomainsValidator';
import { URI } from 'vs/base/common/uri';

suite('Link protection domain matching', () => {
function linkProtectedByRules(link: string, rules: string[]) {
assert.ok(isURLDomainTrusted(URI.parse(link), rules));
}
function linkNotProtectedByRules(link: string, rules: string[]) {
assert.ok(!isURLDomainTrusted(URI.parse(link), rules));
}

suite('Link protection domain matching', () => {
test('simple', () => {
assert.ok(!isURLDomainTrusted(URI.parse('https://x.org'), []));
assert.ok(isURLDomainTrusted(URI.parse('https://x.org'), ['https://x.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://x.org/foo'), ['https://x.org']));
linkNotProtectedByRules('https://x.org', []);

linkProtectedByRules('https://x.org', ['https://x.org']);
linkProtectedByRules('https://x.org/foo', ['https://x.org']);

assert.ok(!isURLDomainTrusted(URI.parse('https://x.org'), ['http://x.org']));
assert.ok(!isURLDomainTrusted(URI.parse('http://x.org'), ['https://x.org']));
linkNotProtectedByRules('https://x.org', ['http://x.org']);
linkNotProtectedByRules('http://x.org', ['https://x.org']);

assert.ok(!isURLDomainTrusted(URI.parse('https://www.x.org'), ['https://x.org']));
linkNotProtectedByRules('https://www.x.org', ['https://x.org']);

assert.ok(isURLDomainTrusted(URI.parse('https://www.x.org'), ['https://www.x.org', 'https://y.org']));
linkProtectedByRules('https://www.x.org', ['https://www.x.org', 'https://y.org']);
});

test('localhost', () => {
assert.ok(isURLDomainTrusted(URI.parse('https://127.0.0.1'), []));
assert.ok(isURLDomainTrusted(URI.parse('https://127.0.0.1:3000'), []));
assert.ok(isURLDomainTrusted(URI.parse('https://localhost'), []));
assert.ok(isURLDomainTrusted(URI.parse('https://localhost:3000'), []));
linkProtectedByRules('https://127.0.0.1', []);
linkProtectedByRules('https://127.0.0.1:3000', []);
linkProtectedByRules('https://localhost', []);
linkProtectedByRules('https://localhost:3000', []);
});

test('* star', () => {
assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://*.x.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.b.x.org'), ['https://*.x.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://a.x.*']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://a.*.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://*.*.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.b.x.org'), ['https://*.b.*.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.a.b.x.org'), ['https://*.b.*.org']));
linkProtectedByRules('https://a.x.org', ['https://*.x.org']);
linkProtectedByRules('https://a.b.x.org', ['https://*.x.org']);
linkProtectedByRules('https://a.x.org', ['https://a.x.*']);
linkProtectedByRules('https://a.x.org', ['https://a.*.org']);
linkProtectedByRules('https://a.x.org', ['https://*.*.org']);
linkProtectedByRules('https://a.b.x.org', ['https://*.b.*.org']);
linkProtectedByRules('https://a.a.b.x.org', ['https://*.b.*.org']);
});

test('no scheme', () => {
assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['a.x.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['*.x.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://a.b.x.org'), ['*.x.org']));
assert.ok(isURLDomainTrusted(URI.parse('https://x.org'), ['*.x.org']));
linkProtectedByRules('https://a.x.org', ['a.x.org']);
linkProtectedByRules('https://a.x.org', ['*.x.org']);
linkProtectedByRules('https://a.b.x.org', ['*.x.org']);
linkProtectedByRules('https://x.org', ['*.x.org']);
});
});

0 comments on commit e88e032

Please sign in to comment.