Merge pull request #1045 from murgatroid99/grpc-js_dns_fix

Fix DNS name regex and add tests
This commit is contained in:
Michael Lumish 2019-09-25 13:43:29 -07:00 committed by GitHub
commit c6b2f423ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 130 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@grpc/grpc-js",
"version": "0.6.0",
"version": "0.6.1",
"description": "gRPC Library for Node - pure JS implementation",
"homepage": "https://grpc.io/",
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",
@ -22,7 +22,6 @@
"@types/mocha": "^5.2.6",
"@types/node": "^12.7.5",
"@types/ncp": "^2.0.1",
"@types/node": "^12.7.5",
"@types/pify": "^3.0.2",
"@types/semver": "^6.0.1",
"clang-format": "^1.0.55",

View File

@ -50,11 +50,11 @@ const IPV6_BRACKET_REGEX = /^\[([0-9a-f]{0,4}(?::{1,2}[0-9a-f]{0,4})+)\](?::(\d+
/**
* Matches `[dns:][//authority/]host[:port]`, where `authority` and `host` are
* both arbitrary sequences of alphanumeric characters and `port` is a sequence
* of digits. Group 1 contains the hostname and group 2 contains the port
* number if provided.
* both arbitrary sequences of dot-separated strings of alphanumeric characters
* and `port` is a sequence of digits. Group 1 contains the hostname and group
* 2 contains the port number if provided.
*/
const DNS_REGEX = /^(?:dns:)?(?:\/\/\w+\/)?(\w+)(?::(\d+))?$/;
const DNS_REGEX = /^(?:dns:)?(?:\/\/(?:[a-zA-Z0-9-]+\.?)+\/)?((?:[a-zA-Z0-9-]+\.?)+)(?::(\d+))?$/;
/**
* The default TCP port to connect to if not explicitly specified in the target.

View File

@ -0,0 +1,125 @@
/*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Allow `any` data type for testing runtime type checking.
// tslint:disable no-any
import * as assert from 'assert';
import * as resolverManager from '../src/resolver';
import { ServiceConfig } from '../src/service-config';
import { StatusObject } from '../src/call-stream';
describe('Name Resolver', () => {
describe('DNS Names', () => {
before(() => {
resolverManager.registerAll();
});
it('Should resolve localhost properly', done => {
const target = 'localhost:50051';
const listener: resolverManager.ResolverListener = {
onSuccessfulResolution: (
addressList: string[],
serviceConfig: ServiceConfig | null,
serviceConfigError: StatusObject | null
) => {
assert(addressList.includes('127.0.0.1:50051'));
// We would check for the IPv6 address but it needs to be omitted on some Node versions
done();
},
onError: (error: StatusObject) => {
done(new Error(`Failed with status ${error.details}`));
},
};
const resolver = resolverManager.createResolver(target, listener);
resolver.updateResolution();
});
it('Should default to port 443', done => {
const target = 'localhost';
const listener: resolverManager.ResolverListener = {
onSuccessfulResolution: (
addressList: string[],
serviceConfig: ServiceConfig | null,
serviceConfigError: StatusObject | null
) => {
assert(addressList.includes('127.0.0.1:443'));
// We would check for the IPv6 address but it needs to be omitted on some Node versions
done();
},
onError: (error: StatusObject) => {
done(new Error(`Failed with status ${error.details}`));
},
};
const resolver = resolverManager.createResolver(target, listener);
resolver.updateResolution();
});
it('Should resolve a public address', done => {
const target = 'example.com';
const listener: resolverManager.ResolverListener = {
onSuccessfulResolution: (
addressList: string[],
serviceConfig: ServiceConfig | null,
serviceConfigError: StatusObject | null
) => {
assert(addressList.length > 0);
done();
},
onError: (error: StatusObject) => {
done(new Error(`Failed with status ${error.details}`));
},
};
const resolver = resolverManager.createResolver(target, listener);
resolver.updateResolution();
});
it('Should resolve a name with multiple dots', done => {
const target = 'loopback4.unittest.grpc.io';
const listener: resolverManager.ResolverListener = {
onSuccessfulResolution: (
addressList: string[],
serviceConfig: ServiceConfig | null,
serviceConfigError: StatusObject | null
) => {
assert(addressList.length > 0);
done();
},
onError: (error: StatusObject) => {
done(new Error(`Failed with status ${error.details}`));
},
};
const resolver = resolverManager.createResolver(target, listener);
resolver.updateResolution();
});
it('Should resolve a name with a hyphen', done => {
/* TODO(murgatroid99): Find or create a better domain name to test this with.
* This is just the first one I found with a hyphen. */
const target = 'network-tools.com';
const listener: resolverManager.ResolverListener = {
onSuccessfulResolution: (
addressList: string[],
serviceConfig: ServiceConfig | null,
serviceConfigError: StatusObject | null
) => {
assert(addressList.length > 0);
done();
},
onError: (error: StatusObject) => {
done(new Error(`Failed with status ${error.details}`));
},
};
const resolver = resolverManager.createResolver(target, listener);
resolver.updateResolution();
});
});
});