<NME> inet-address-tests.js
<BEF> /*
 * Copyright DataStax, Inc.
 *
 * 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.
 */
'use strict';
const assert = require('assert');
const helper = require('../test-helper');
const utils = require('../../lib/utils');
const InetAddress = require('../../lib/types').InetAddress;

describe('InetAddress', function () {
  describe('constructor', function () {
    it('should validate the Buffer length', function () {
      assert.throws(function () {
        return new InetAddress(utils.allocBufferUnsafe(10));
      }, TypeError);
      assert.throws(function () {
  describe('#toString()', function () {
    it('should convert IPv6 to string representation', function () {
      var val = new InetAddress(new Buffer('aabb0000eeff00112233445566778899', 'hex'));
      assert.strictEqual(val.toString(), 'aabb::eeff:0011:2233:4455:6677:8899');
      val = new InetAddress(new Buffer('aabbccddeeff00112233445566778899', 'hex'));
      assert.strictEqual(val.toString(), 'aabb:ccdd:eeff:0011:2233:4455:6677:8899');
      val = new InetAddress(new Buffer('aabb0000000000112233445566778899', 'hex'));
      assert.strictEqual(val.toString(), 'aabb::0011:2233:4455:6677:8899');
      val = new InetAddress(new Buffer('aabb0001000100112233445500000000', 'hex'));
      assert.strictEqual(val.toString(), 'aabb:0001:0001:0011:2233:4455::');
      val = new InetAddress(new Buffer('00000000000100112233445500aa00bb', 'hex'));
      assert.strictEqual(val.toString(), '::0001:0011:2233:4455:00aa:00bb');
      val = new InetAddress(new Buffer('000000000000000022330000000000bb', 'hex'));
      assert.strictEqual(val.toString(), '::2233:0000:0000:00bb');
    });
    it('should convert IPv4 to string representation', function () {
      var val = new InetAddress(new Buffer([127, 0, 0, 1]));
      assert.strictEqual(val.toString(), '127.0.0.1');
      val = new InetAddress(new Buffer([198, 168, 1, 1]));
      assert.strictEqual(val.toString(), '198.168.1.1');
      val = new InetAddress(utils.allocBufferFromString('00000000000100112233445500aa00bb', 'hex'));
      assert.strictEqual(val.toString(), '::1:11:2233:4455:aa:bb');
      val = new InetAddress(utils.allocBufferFromString('000000000000000022330000000000bb', 'hex'));
      assert.strictEqual(val.toString(), '::2233:0:0:bb');
      val = new InetAddress(utils.allocBufferFromString('00000000000000000000000000000001', 'hex'));
      assert.strictEqual(val.toString(), '::1');
    });
    it('should convert IPv4 to string representation', function () {
      let val = new InetAddress(utils.allocBufferFromArray([127, 0, 0, 1]));
      assert.strictEqual(val.version, 4);
      assert.strictEqual(val.toString(), '127.0.0.1');
      val = new InetAddress(utils.allocBufferFromArray([198, 168, 1, 1]));
      assert.strictEqual(val.toString(), '198.168.1.1');
      assert.ok(!val1.equals(val2));
    });
  });
});
      assert.strictEqual(val.toString(), '127.0.0.1');
      val = InetAddress.fromString('198.168.1.1');
      helper.assertInstanceOf(val, InetAddress);
      assert.strictEqual(val.toString(), '198.168.1.1');
      val = InetAddress.fromString('10.11.12.13');
      helper.assertInstanceOf(val, InetAddress);
      assert.strictEqual(val.toString(), '10.11.12.13');
    });
    it('should parse IPv4-Mapped IPv6 addresses', function () {
      /* eslint-disable no-multi-spaces */
      [
        ['0:0:0:0:0:FFFF:129.144.52.38',    '00000000000000000000ffff81903426'],
        ['::ffff:129.144.52.38',            '00000000000000000000ffff81903426'],
        ['::ffff:254.255.52.32',            '00000000000000000000fffffeff3420']
      ].forEach(function (item) {
        const ip = InetAddress.fromString(item[0]);
        assert.strictEqual(ip.toString('hex'), item[1]);
      });
      /* eslint-enable no-multi-spaces */
    });
    it('should throw TypeError for invalid IPv6 address with embedded IPv4 address', function () {
      [
        ['0:0:0:0:0:0:129.144.52.38'],
        ['::13.1.68.3'],
        ['fe::13.1.68.3']
      ].forEach(function (address) {
        assert.throws(function () {
          InetAddress.fromString(address);
        }, TypeError);
      });
    });
    it('should throw TypeError when the string is not a valid address', function () {
      assert.throws(function () {
        InetAddress.fromString('127.0.0.1.10');
      }, TypeError);
      assert.throws(function () {
        InetAddress.fromString('127.0.');
      }, TypeError);
      assert.throws(function () {
        InetAddress.fromString(' 127.10.0.1');
      }, TypeError);
      assert.throws(function () {
        InetAddress.fromString('z');
      }, TypeError);
      assert.throws(function () {
        InetAddress.fromString('::Z:11:2233:4455:aa:bb');
      }, TypeError);
      assert.throws(function () {
        InetAddress.fromString(' ::a:11');
      }, TypeError);
    });
  });
});
<MSG> InetAddress.fromString() method NODEJS-84

<DFF> @@ -29,20 +29,24 @@ describe('InetAddress', function () {
   describe('#toString()', function () {
     it('should convert IPv6 to string representation', function () {
       var val = new InetAddress(new Buffer('aabb0000eeff00112233445566778899', 'hex'));
-      assert.strictEqual(val.toString(), 'aabb::eeff:0011:2233:4455:6677:8899');
+      assert.strictEqual(val.version, 6);
+      assert.strictEqual(val.toString(), 'aabb::eeff:11:2233:4455:6677:8899');
       val = new InetAddress(new Buffer('aabbccddeeff00112233445566778899', 'hex'));
-      assert.strictEqual(val.toString(), 'aabb:ccdd:eeff:0011:2233:4455:6677:8899');
+      assert.strictEqual(val.toString(), 'aabb:ccdd:eeff:11:2233:4455:6677:8899');
       val = new InetAddress(new Buffer('aabb0000000000112233445566778899', 'hex'));
-      assert.strictEqual(val.toString(), 'aabb::0011:2233:4455:6677:8899');
+      assert.strictEqual(val.toString(), 'aabb::11:2233:4455:6677:8899');
       val = new InetAddress(new Buffer('aabb0001000100112233445500000000', 'hex'));
-      assert.strictEqual(val.toString(), 'aabb:0001:0001:0011:2233:4455::');
+      assert.strictEqual(val.toString(), 'aabb:1:1:11:2233:4455::');
       val = new InetAddress(new Buffer('00000000000100112233445500aa00bb', 'hex'));
-      assert.strictEqual(val.toString(), '::0001:0011:2233:4455:00aa:00bb');
+      assert.strictEqual(val.toString(), '::1:11:2233:4455:aa:bb');
       val = new InetAddress(new Buffer('000000000000000022330000000000bb', 'hex'));
-      assert.strictEqual(val.toString(), '::2233:0000:0000:00bb');
+      assert.strictEqual(val.toString(), '::2233:0:0:bb');
+      val = new InetAddress(new Buffer('00000000000000000000000000000001', 'hex'));
+      assert.strictEqual(val.toString(), '::1');
     });
     it('should convert IPv4 to string representation', function () {
       var val = new InetAddress(new Buffer([127, 0, 0, 1]));
+      assert.strictEqual(val.version, 4);
       assert.strictEqual(val.toString(), '127.0.0.1');
       val = new InetAddress(new Buffer([198, 168, 1, 1]));
       assert.strictEqual(val.toString(), '198.168.1.1');
@@ -62,4 +66,38 @@ describe('InetAddress', function () {
       assert.ok(!val1.equals(val2));
     });
   });
+  describe('fromString()', function () {
+    it('should parse IPv6 string representation', function () {
+      [
+        'aabb::eeff:11:2233:4455:6677:8899',
+        'aabb:1:eeff:11:2233:4455:6677:8899',
+        'aabb:1:eeff:11:2233:4455:6677:8899',
+        '::1:11:2233:4455:aa:bb',
+        '::2233:0:0:bb',
+        '::1234',
+        '10fa::1'
+      ].forEach(function (item) {
+          var val = InetAddress.fromString(item, 'hex');
+          helper.assertInstanceOf(val, InetAddress);
+          assert.strictEqual(val.toString(), item);
+      });
+    });
+    it('should parse IPv4 string representation', function () {
+      var val = InetAddress.fromString('127.0.0.1');
+      helper.assertInstanceOf(val, InetAddress);
+      assert.strictEqual(val.toString(), '127.0.0.1');
+      val = InetAddress.fromString('198.168.1.1');
+      helper.assertInstanceOf(val, InetAddress);
+      assert.strictEqual(val.toString(), '198.168.1.1');
+      val = InetAddress.fromString('10.11.12.13');
+      helper.assertInstanceOf(val, InetAddress);
+      assert.strictEqual(val.toString(), '10.11.12.13');
+    });
+    it('should throw when can not parse to 4 or 16 bytes', function () {
+      assert.throws(function () {
+        var inet = InetAddress.fromString('127.0.0.1.10');
+        console.log(inet);
+      }, Error);
+    });
+  });
 });
\ No newline at end of file

{'addition_count': 44, 'commit_subject': 'InetAddress.fromString() method NODEJS-84', 'deletion_count': 6, 'file_extension': '.js', 'lang': 'js', 'license': 'apache-2.0', 'repo_name': 'datastax/nodejs-driver'}