/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.servicecomb.foundation.common.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import mockit.Deencapsulation;

public class TestNetUtils {
  @Test
  public void testIpPort() {
    IpPort oIPPort = new IpPort("10.145.154.45", 8080);
    Assert.assertEquals("10.145.154.45", oIPPort.getHostOrIp());
    Assert.assertEquals(8080, oIPPort.getPort());
    oIPPort.setPort(9090);
    Assert.assertEquals(9090, oIPPort.getPort());
    Assert.assertNotEquals(null, oIPPort.getSocketAddress());
  }

  @Test
  public void testNetutils() {
    Assert.assertEquals("127.0.0.1", NetUtils.parseIpPort("127.0.0.1:8080").getHostOrIp());
    Assert.assertEquals(8080, NetUtils.parseIpPort("127.0.0.1:8080").getPort());
    Assert.assertEquals(null, NetUtils.parseIpPort(null));
    Assert.assertEquals(null, NetUtils.parseIpPort("127.0.0.18080"));
    Assert.assertEquals(NetUtils.parseIpPortFromURI(null), null);
    Assert.assertEquals(NetUtils.parseIpPortFromURI("ss"), null);
    Assert.assertEquals(NetUtils.parseIpPortFromURI("rest://127.0.0.1:8080").getHostOrIp(), "127.0.0.1");
    Assert.assertEquals(NetUtils.parseIpPortFromURI("http://127.0.0.1:8080").getPort(), 8080);
    Assert.assertEquals(NetUtils.parseIpPortFromURI("http://127.0.0.1").getPort(), 80);
    Assert.assertEquals(NetUtils.parseIpPortFromURI("https://127.0.0.1:8080").getPort(), 8080);
    Assert.assertEquals(NetUtils.parseIpPortFromURI("https://127.0.0.1").getPort(), 443);
  }

  @Test
  public void testFullOperation() {
    Assert.assertNotNull(NetUtils.getHostAddress());
    Assert.assertNotNull(NetUtils.getHostName());
  }

  @Test
  public void testGetRealListenAddress() {
    Assert.assertNull(NetUtils.getRealListenAddress("http", null));
    Assert.assertNull(NetUtils.getRealListenAddress("http:1", "1.1.1.1:8080"));
    Assert.assertEquals("http://1.1.1.1:8080", NetUtils.getRealListenAddress("http", "1.1.1.1:8080"));
  }

  @Test
  public void testNetworkInterface() {
    Map<String, InetAddress> org = Deencapsulation.getField(NetUtils.class, "allInterfaceAddresses");

    Map<String, InetAddress> newValue = new HashMap<>();
    InetAddress addr = Mockito.mock(InetAddress.class);
    newValue.put("eth100", addr);
    Deencapsulation.setField(NetUtils.class, "allInterfaceAddresses", newValue);

    Assert.assertEquals(addr, NetUtils.getInterfaceAddress("eth100"));
    Assert.assertEquals(addr, NetUtils.ensureGetInterfaceAddress("eth100"));

    try {
      NetUtils.ensureGetInterfaceAddress("xxx");
      Assert.fail("must throw exception");
    } catch (IllegalArgumentException e) {
      Assert.assertEquals("Can not find address for interface name: xxx", e.getMessage());
    }

    Deencapsulation.setField(NetUtils.class, "allInterfaceAddresses", org);
  }

  @Test
  public void testCanTcpListenNo() throws IOException {
    InetAddress address = InetAddress.getByName("127.0.0.1");
    try (ServerSocket ss = new ServerSocket(0, 0, address)) {
      Assert.assertFalse(NetUtils.canTcpListen(address, ss.getLocalPort()));
    }
  }

  @Test
  public void testCanTcpListenYes() throws IOException {
    InetAddress address = InetAddress.getByName("127.0.0.1");
    ServerSocket ss = new ServerSocket(0, 0, address);
    int port = ss.getLocalPort();
    ss.close();

    Assert.assertTrue(NetUtils.canTcpListen(address, port));
  }
}

@@ -1833,32 +1833,38 @@
 rt.assertEquals(
+null, 
 NetUtils.parseIp
@@ -1880,22 +1880,16 @@
 RI(null)
-, null
 );%0A    A
@@ -1899,32 +1899,38 @@
 rt.assertEquals(
+null, 
 NetUtils.parseIp
@@ -1946,22 +1946,16 @@
 RI(%22ss%22)
-, null
 );%0A    A
@@ -1965,32 +1965,45 @@
 rt.assertEquals(
+%22127.0.0.1%22, 
 NetUtils.parseIp
@@ -2056,21 +2056,8 @@
 Ip()
-, %22127.0.0.1%22
 );%0A 
@@ -2071,32 +2071,38 @@
 rt.assertEquals(
+8080, 
 NetUtils.parseIp
@@ -2139,38 +2139,32 @@
 8080%22).getPort()
-, 8080
 );%0A    Assert.as
@@ -2166,32 +2166,36 @@
 rt.assertEquals(
+80, 
 NetUtils.parseIp
@@ -2235,20 +2235,16 @@
 etPort()
-, 80
 );%0A    A
@@ -2254,32 +2254,38 @@
 rt.assertEquals(
+8080, 
 NetUtils.parseIp
@@ -2331,22 +2331,16 @@
 etPort()
-, 8080
 );%0A    A
@@ -2358,16 +2358,21 @@
 tEquals(
+443, 
 NetUtils
@@ -2425,13 +2425,8 @@
 rt()
-, 443
 );%0A 

fix ut