Repository: xbmc/xbmc
File: xbmc/utils/test/TestURIUtils.cpp (C++)

Code before:
  EXPECT_EQ("udf://%2fpath%2fthing/my/archived/path",
            URIUtils::CreateArchivePath("udf", curl, "/my/archived/path").Get());

  EXPECT_EQ("iso9660://%2fpath%2fthing/my/archived/path",
            URIUtils::CreateArchivePath("iso9660", curl, "/my/archived/path").Get());

  EXPECT_EQ("rar://%2fpath%2fthing/my/archived/path",
            URIUtils::CreateArchivePath("rar", curl, "/my/archived/path").Get());
}

TEST_F(TestURIUtils, ServiceAvailabilityInTestEnvironment)
{
  using enum LanCheckMode;
  {
    EXPECT_FALSE(URIUtils::IsOnLAN("0.0.0.0", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsOnLAN("127.0.0.1", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsOnLAN("localhost", ANY_PRIVATE_SUBNET));

    EXPECT_FALSE(URIUtils::IsOnLAN("some_unresolveable_hostname", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsOnLAN("www.google.com", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsOnLAN("www.aol.com", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsOnLAN("168.219.34.129", ANY_PRIVATE_SUBNET));

    EXPECT_FALSE(URIUtils::IsOnLAN("192.168.0.3", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsOnLAN("172.16.3.2", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsOnLAN("10.0.0.9", ANY_PRIVATE_SUBNET));

    EXPECT_FALSE(URIUtils::IsHostOnLAN("0.0.0.0", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsHostOnLAN("127.0.0.1", ANY_PRIVATE_SUBNET));

    // Assumption that hostnames without a period are local (smb, netbios)
    EXPECT_TRUE(URIUtils::IsHostOnLAN("localhost", ANY_PRIVATE_SUBNET));
    EXPECT_TRUE(URIUtils::IsHostOnLAN("some_unresolveable_hostname", ANY_PRIVATE_SUBNET));
    EXPECT_TRUE(URIUtils::IsHostOnLAN("google", ANY_PRIVATE_SUBNET));
    EXPECT_TRUE(URIUtils::IsHostOnLAN("aol", ANY_PRIVATE_SUBNET));

    EXPECT_FALSE(URIUtils::IsHostOnLAN("www.some_unresolveable_hostname.com", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsHostOnLAN("www.google.com", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsHostOnLAN("google.com", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsHostOnLAN("www.aol.com", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsHostOnLAN("aol.com", ANY_PRIVATE_SUBNET));
    EXPECT_FALSE(URIUtils::IsHostOnLAN("168.219.34.129", ANY_PRIVATE_SUBNET));
  }
  {
    EXPECT_TRUE(URIUtils::IsHostOnLAN("192.168.0.3", ANY_PRIVATE_SUBNET));
    EXPECT_TRUE(URIUtils::IsHostOnLAN("172.16.3.2", ANY_PRIVATE_SUBNET));
    EXPECT_TRUE(URIUtils::IsHostOnLAN("10.0.0.9", ANY_PRIVATE_SUBNET));
  }
  /*
	 * The following checks cannot be reliable tested on build machines
	 * These tests pass for a class B network (172.16.0.0/16) but cannot be guarenteed to work under different network configurations

Review comment (suggestion):
Are those tests of any use? `URIUtils::IsOnLAN` is for URIs, it will always return `false` for `ANY_PRIVATE_SUBNET` if tested with invalid input. E.g. change `URIUtils::IsOnLAN("192.168.0.3", ANY_PRIVATE_SUBNET)` into `URIUtils::IsOnLAN("http://192.168.0.3", ANY_PRIVATE_SUBNET)` and it returns the expected `true`.

Code after:
  EXPECT_STRCASEEQ(ref.c_str(), URIUtils::GetRealPath("zip://%2fpath%2fto%2fsome%2f..%2fzip/subpath/to/file").c_str());

  // test zip/zip path
  ref ="zip://zip%3a%2f%2f%252Fpath%252Fto%252Fzip%2fpath%2fto%2fzip/subpath/to/file";
  EXPECT_STRCASEEQ(ref.c_str(), URIUtils::GetRealPath("zip://zip%3a%2f%2f%252Fpath%252Fto%252Fsome%252F..%252Fzip%2fpath%2fto%2fsome%2f..%2fzip/subpath/to/some/../file").c_str());
}

TEST_F(TestURIUtils, UpdateUrlEncoding)
{
  std::string oldUrl = "stack://zip://%2fpath%2fto%2farchive%2fsome%2darchive%2dfile%2eCD1%2ezip/video.avi , zip://%2fpath%2fto%2farchive%2fsome%2darchive%2dfile%2eCD2%2ezip/video.avi";
  std::string newUrl = "stack://zip://%2fpath%2fto%2farchive%2fsome-archive-file.CD1.zip/video.avi , zip://%2fpath%2fto%2farchive%2fsome-archive-file.CD2.zip/video.avi";

  EXPECT_TRUE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "zip://%2fpath%2fto%2farchive%2fsome%2darchive%2efile%2ezip/video.avi";
  newUrl = "zip://%2fpath%2fto%2farchive%2fsome-archive.file.zip/video.avi";

  EXPECT_TRUE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "/path/to/some/long%2dnamed%2efile";
  newUrl = "/path/to/some/long%2dnamed%2efile";

  EXPECT_FALSE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());

  oldUrl = "/path/to/some/long-named.file";
  newUrl = "/path/to/some/long-named.file";

  EXPECT_FALSE(URIUtils::UpdateUrlEncoding(oldUrl));
  EXPECT_STRCASEEQ(newUrl.c_str(), oldUrl.c_str());
}

TEST_F(TestURIUtils, ContainersEncodeHostnamePaths)
{
  CURL curl("/path/thing");

  EXPECT_EQ("zip://%2fpath%2fthing/my/archived/path",
            URIUtils::CreateArchivePath("zip", curl, "/my/archived/path").Get());

  EXPECT_EQ("apk://%2fpath%2fthing/my/archived/path",
            URIUtils::CreateArchivePath("apk", curl, "/my/archived/path").Get());

  EXPECT_EQ("udf://%2fpath%2fthing/my/archived/path",
            URIUtils::CreateArchivePath("udf", curl, "/my/archived/path").Get());

  EXPECT_EQ("iso9660://%2fpath%2fthing/my/archived/path",
            URIUtils::CreateArchivePath("iso9660", curl, "/my/archived/path").Get());

  EXPECT_EQ("rar://%2fpath%2fthing/my/archived/path",