1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
|
//
// System.IO.Path Test Cases
//
// Authors:
// Marcin Szczepanski (marcins@zipworld.com.au)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
// Ben Maurer (bmaurer@users.sf.net)
// Gilles Freart (gfr@skynet.be)
// Atsushi Enomoto (atsushi@ximian.com)
// Sebastien Pouliot <sebastien@ximian.com>
//
// (c) Marcin Szczepanski
// (c) 2002 Ximian, Inc. (http://www.ximian.com)
// (c) 2003 Ben Maurer
// (c) 2003 Gilles Freart
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
using NUnit.Framework;
using System.IO;
using System;
using System.Text;
namespace MonoTests.System.IO
{
enum OsType {
Windows,
Unix,
Mac
}
[TestFixture]
public class PathTest : Assertion
{
static string path1;
static string path2;
static string path3;
static OsType OS;
static char DSC = Path.DirectorySeparatorChar;
[SetUp]
public void SetUp ()
{
if ('/' == DSC) {
OS = OsType.Unix;
path1 = "/foo/test.txt";
path2 = "/etc";
path3 = "init.d";
} else if ('\\' == DSC) {
OS = OsType.Windows;
path1 = "c:\\foo\\test.txt";
path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
path3 = "system32";
} else {
OS = OsType.Mac;
//FIXME: For Mac. figure this out when we need it
path1 = "foo:test.txt";
path2 = "foo";
path3 = "bar";
}
}
bool Windows
{
get {
return OS == OsType.Windows;
}
}
bool Unix
{
get {
return OS == OsType.Unix;
}
}
bool Mac
{
get {
return OS == OsType.Mac;
}
}
public void TestChangeExtension ()
{
string [] files = new string [3];
files [(int) OsType.Unix] = "/foo/test.doc";
files [(int) OsType.Windows] = "c:\\foo\\test.doc";
files [(int) OsType.Mac] = "foo:test.doc";
string testPath = Path.ChangeExtension (path1, "doc");
AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
testPath = Path.ChangeExtension ("", ".extension");
AssertEquals ("ChangeExtension #02", String.Empty, testPath);
testPath = Path.ChangeExtension (null, ".extension");
AssertEquals ("ChangeExtension #03", null, testPath);
testPath = Path.ChangeExtension ("path", null);
AssertEquals ("ChangeExtension #04", "path", testPath);
testPath = Path.ChangeExtension ("path.ext", "doc");
AssertEquals ("ChangeExtension #05", "path.doc", testPath);
testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl");
AssertEquals ("ChangeExtension #07", "hogehoge.xsl", testPath);
testPath = Path.ChangeExtension ("hogehoge", ".xsl");
AssertEquals ("ChangeExtension #08", "hogehoge.xsl", testPath);
testPath = Path.ChangeExtension ("hogehoge.xml", "xsl");
AssertEquals ("ChangeExtension #09", "hogehoge.xsl", testPath);
testPath = Path.ChangeExtension ("hogehoge", "xsl");
AssertEquals ("ChangeExtension #10", "hogehoge.xsl", testPath);
testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty);
AssertEquals ("ChangeExtension #11", "hogehoge.", testPath);
testPath = Path.ChangeExtension ("hogehoge", String.Empty);
AssertEquals ("ChangeExtension #12", "hogehoge.", testPath);
testPath = Path.ChangeExtension ("hogehoge.", null);
AssertEquals ("ChangeExtension #13", "hogehoge", testPath);
testPath = Path.ChangeExtension ("hogehoge", null);
AssertEquals ("ChangeExtension #14", "hogehoge", testPath);
testPath = Path.ChangeExtension (String.Empty, null);
AssertEquals ("ChangeExtension #15", String.Empty, testPath);
testPath = Path.ChangeExtension (String.Empty, "bashrc");
AssertEquals ("ChangeExtension #16", String.Empty, testPath);
testPath = Path.ChangeExtension (String.Empty, ".bashrc");
AssertEquals ("ChangeExtension #17", String.Empty, testPath);
testPath = Path.ChangeExtension (null, null);
AssertNull ("ChangeExtension #18", testPath);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ChangeExtension_BadPath ()
{
if (!Windows) throw new ArgumentException ("Test Only On Windows");
Path.ChangeExtension ("<", ".extension");
}
[Test]
public void ChangeExtension_BadExtension ()
{
if (Windows) {
string fn = Path.ChangeExtension ("file.ext", "<");
AssertEquals ("Invalid filename", "file.<", fn);
}
}
public void TestCombine ()
{
string [] files = new string [3];
files [(int) OsType.Unix] = "/etc/init.d";
files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
files [(int) OsType.Mac] = "foo:bar";
string testPath = Path.Combine (path2, path3);
AssertEquals ("Combine #01", files [(int) OS], testPath);
testPath = Path.Combine ("one", "");
AssertEquals ("Combine #02", "one", testPath);
testPath = Path.Combine ("", "one");
AssertEquals ("Combine #03", "one", testPath);
string current = Directory.GetCurrentDirectory ();
testPath = Path.Combine (current, "one");
string expected = current + DSC + "one";
AssertEquals ("Combine #04", expected, testPath);
testPath = Path.Combine ("one", current);
// LAMESPEC noted in Path.cs
AssertEquals ("Combine #05", current, testPath);
testPath = Path.Combine (current, expected);
AssertEquals ("Combine #06", expected, testPath);
testPath = DSC + "one";
testPath = Path.Combine (testPath, "two" + DSC);
expected = DSC + "one" + DSC + "two" + DSC;
AssertEquals ("Combine #06", expected, testPath);
testPath = "one" + DSC;
testPath = Path.Combine (testPath, DSC + "two");
expected = DSC + "two";
AssertEquals ("Combine #06", expected, testPath);
testPath = "one" + DSC;
testPath = Path.Combine (testPath, "two" + DSC);
expected = "one" + DSC + "two" + DSC;
AssertEquals ("Combine #07", expected, testPath);
//TODO: Tests for UNC names
try {
testPath = Path.Combine ("one", null);
Fail ("Combine Fail #01");
} catch (Exception e) {
AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
}
try {
testPath = Path.Combine (null, "one");
Fail ("Combine Fail #02");
} catch (Exception e) {
AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
}
if (Windows) {
try {
testPath = Path.Combine ("a>", "one");
Fail ("Combine Fail #03");
} catch (Exception e) {
AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
}
try {
testPath = Path.Combine ("one", "aaa<");
Fail ("Combine Fail #04");
} catch (Exception e) {
AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
}
}
}
[Test]
[ExpectedException (typeof(ArgumentException))]
public void EmptyDirectoryName ()
{
string testDirName = Path.GetDirectoryName ("");
}
public void TestDirectoryName ()
{
string [] files = new string [3];
files [(int) OsType.Unix] = "/foo";
files [(int) OsType.Windows] = "c:\\foo";
files [(int) OsType.Mac] = "foo";
AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
string testDirName = Path.GetDirectoryName (path1);
AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
if (Windows) {
try {
testDirName = Path.GetDirectoryName ("aaa>");
Fail ("GetDirectoryName Fail #02");
} catch (Exception e) {
AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
}
}
try {
testDirName = Path.GetDirectoryName (" ");
Fail ("GetDirectoryName Fail #03");
} catch (Exception e) {
AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
}
if (Windows) {
AssertEquals ("GetDirectoryName #04", null, Path.GetDirectoryName ("C:"));
AssertEquals ("GetDirectoryName #05", null, Path.GetDirectoryName (@"C:\"));
AssertEquals ("GetDirectoryName #06", @"C:\", Path.GetDirectoryName (@"C:\dir"));
AssertEquals ("GetDirectoryName #07", @"C:\dir", Path.GetDirectoryName (@"C:\dir\"));
AssertEquals ("GetDirectoryName #08", @"C:\dir", Path.GetDirectoryName (@"C:\dir\dir"));
AssertEquals ("GetDirectoryName #09", @"C:\dir\dir", Path.GetDirectoryName (@"C:\dir\dir\"));
// UNC tests
AssertEquals ("GetDirectoryName #10", null, Path.GetDirectoryName (@"\\"));
AssertEquals ("GetDirectoryName #11", null, Path.GetDirectoryName (@"\\server"));
AssertEquals ("GetDirectoryName #12", null, Path.GetDirectoryName (@"\\server\share"));
AssertEquals ("GetDirectoryName #13", @"\\server\share", Path.GetDirectoryName (@"\\server\share\"));
AssertEquals ("GetDirectoryName #14", @"\\server\share", Path.GetDirectoryName (@"\\server\share\dir"));
AssertEquals ("GetDirectoryName #15", @"\\server\share\dir", Path.GetDirectoryName (@"\\server\share\dir\subdir"));
}
}
public void TestGetExtension ()
{
string testExtn = Path.GetExtension (path1);
AssertEquals ("GetExtension #01", ".txt", testExtn);
testExtn = Path.GetExtension (path2);
AssertEquals ("GetExtension #02", String.Empty, testExtn);
testExtn = Path.GetExtension (String.Empty);
AssertEquals ("GetExtension #03", String.Empty, testExtn);
testExtn = Path.GetExtension (null);
AssertEquals ("GetExtension #04", null, testExtn);
testExtn = Path.GetExtension (" ");
AssertEquals ("GetExtension #05", String.Empty, testExtn);
testExtn = Path.GetExtension (path1 + ".doc");
AssertEquals ("GetExtension #06", ".doc", testExtn);
testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
AssertEquals ("GetExtension #07", ".txt", testExtn);
testExtn = Path.GetExtension (".");
AssertEquals ("GetExtension #08", String.Empty, testExtn);
testExtn = Path.GetExtension ("end.");
AssertEquals ("GetExtension #09", String.Empty, testExtn);
testExtn = Path.GetExtension (".start");
AssertEquals ("GetExtension #10", ".start", testExtn);
testExtn = Path.GetExtension (".a");
AssertEquals ("GetExtension #11", ".a", testExtn);
testExtn = Path.GetExtension ("a.");
AssertEquals ("GetExtension #12", String.Empty, testExtn);
testExtn = Path.GetExtension ("a");
AssertEquals ("GetExtension #13", String.Empty, testExtn);
testExtn = Path.GetExtension ("makefile");
AssertEquals ("GetExtension #14", String.Empty, testExtn);
if (Windows) {
try {
testExtn = Path.GetExtension ("hi<there.txt");
Fail ("GetExtension Fail #01");
} catch (Exception e) {
AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
}
}
}
public void TestGetFileName ()
{
string testFileName = Path.GetFileName (path1);
AssertEquals ("GetFileName #01", "test.txt", testFileName);
testFileName = Path.GetFileName (null);
AssertEquals ("GetFileName #02", null, testFileName);
testFileName = Path.GetFileName (String.Empty);
AssertEquals ("GetFileName #03", String.Empty, testFileName);
testFileName = Path.GetFileName (" ");
AssertEquals ("GetFileName #04", " ", testFileName);
if (Windows) {
try {
testFileName = Path.GetFileName ("hi<");
Fail ("GetFileName Fail #01");
} catch (Exception e) {
AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
}
}
}
public void TestGetFileNameWithoutExtension ()
{
string testFileName = Path.GetFileNameWithoutExtension (path1);
AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
testFileName = Path.GetFileNameWithoutExtension (null);
AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
testFileName = Path.GetFileNameWithoutExtension (String.Empty);
AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
}
[Ignore("This does not work under windows. See ERROR comments below.")]
public void TestGetFullPath ()
{
string current = Directory.GetCurrentDirectory ();
string testFullPath = Path.GetFullPath ("foo.txt");
string expected = current + DSC + "foo.txt";
AssertEquals ("GetFullPath #01", expected, testFullPath);
testFullPath = Path.GetFullPath ("a//./.././foo.txt");
AssertEquals ("GetFullPath #02", expected, testFullPath);
string root = Windows ? "C:\\" : "/";
string [,] test = new string [,] {
{"root////././././././../root/././../root", "root"},
{"root/", "root/"},
{"root/./", "root/"},
{"root/./", "root/"},
{"root/../", ""},
{"root/../", ""},
{"root/../..", ""},
{"root/.hiddenfile", "root/.hiddenfile"},
{"root/. /", "root/. /"},
{"root/.. /", "root/.. /"},
{"root/..weirdname", "root/..weirdname"},
{"root/..", ""},
{"root/../a/b/../../..", ""},
{"root/./..", ""},
{"..", ""},
{".", ""},
{"root//dir", "root/dir"},
{"root/. /", "root/. /"},
{"root/.. /", "root/.. /"},
{"root/ . /", "root/ . /"},
{"root/ .. /", "root/ .. /"},
{"root/./", "root/"},
//ERROR! Paths are trimmed
{"root/.. /", "root/.. /"},
{".//", ""}
};
//ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
//... so only the first test was being done.
for (int i = 0; i < test.GetUpperBound (1); i++) {
AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
}
if (Windows) {
string uncroot = @"\\server\share\";
string [,] testunc = new string [,] {
{"root////././././././../root/././../root", "root"},
{"root/", "root/"},
{"root/./", "root/"},
{"root/./", "root/"},
{"root/../", ""},
{"root/../", ""},
{"root/../..", ""},
{"root/.hiddenfile", "root/.hiddenfile"},
{"root/. /", "root/. /"},
{"root/.. /", "root/.. /"},
{"root/..weirdname", "root/..weirdname"},
{"root/..", ""},
{"root/../a/b/../../..", ""},
{"root/./..", ""},
{"..", ""},
{".", ""},
{"root//dir", "root/dir"},
{"root/. /", "root/. /"},
{"root/.. /", "root/.. /"},
{"root/ . /", "root/ . /"},
{"root/ .. /", "root/ .. /"},
{"root/./", "root/"},
{"root/.. /", "root/.. /"},
{".//", ""}
};
for (int i = 0; i < test.GetUpperBound (1); i++) {
AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
}
}
try {
testFullPath = Path.GetFullPath (null);
Fail ("GetFullPath Fail #01");
} catch (Exception e) {
AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
}
try {
testFullPath = Path.GetFullPath (String.Empty);
Fail ("GetFullPath Fail #02");
} catch (Exception e) {
AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
}
}
public void TestGetFullPath2 ()
{
if (Windows) {
AssertEquals ("GetFullPath w#01", @"Z:\", Path.GetFullPath ("Z:"));
AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
// "\\\\" is not allowed
Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
// "//" is not allowed
Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
}
}
public void TestGetPathRoot ()
{
string current;
string expected;
if (!Windows){
current = Directory.GetCurrentDirectory ();
expected = current [0].ToString ();
} else {
current = @"J:\Some\Strange Directory\Name";
expected = "J:\\";
}
string pathRoot = Path.GetPathRoot (current);
AssertEquals ("GetPathRoot #01", expected, pathRoot);
}
[Test]
public void TestGetPathRoot2 ()
{
// note: this method doesn't call Directory.GetCurrentDirectory so it can be
// reused for partial trust unit tests in PathCas.cs
string pathRoot = Path.GetPathRoot ("hola");
AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
pathRoot = Path.GetPathRoot (null);
AssertEquals ("GetPathRoot #03", null, pathRoot);
if (Windows) {
AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
AssertEquals ("GetPathRoot w#15", @"C:\", Path.GetPathRoot (@"C:\"));
AssertEquals ("GetPathRoot w#16", @"C:\", Path.GetPathRoot (@"C:\\"));
AssertEquals ("GetPathRoot w#17", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def\\ghi"));
} else {
// TODO: Same tests for Unix.
}
}
public void TestGetTempPath ()
{
string getTempPath = Path.GetTempPath ();
Assert ("GetTempPath #01", getTempPath != String.Empty);
Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
}
public void TestGetTempFileName ()
{
string getTempFileName = null;
try {
getTempFileName = Path.GetTempFileName ();
Assert ("GetTempFileName #01", getTempFileName != String.Empty);
Assert ("GetTempFileName #02", File.Exists (getTempFileName));
} finally {
if (getTempFileName != null && getTempFileName != String.Empty){
File.Delete (getTempFileName);
}
}
}
public void TestHasExtension ()
{
AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
AssertEquals ("HasExtension #05", false, Path.HasExtension (null));
AssertEquals ("HasExtension #06", false, Path.HasExtension (String.Empty));
AssertEquals ("HasExtension #07", false, Path.HasExtension (" "));
AssertEquals ("HasExtension #08", false, Path.HasExtension ("."));
AssertEquals ("HasExtension #09", false, Path.HasExtension ("end."));
AssertEquals ("HasExtension #10", true, Path.HasExtension (".start"));
AssertEquals ("HasExtension #11", true, Path.HasExtension (".a"));
AssertEquals ("HasExtension #12", false, Path.HasExtension ("a."));
AssertEquals ("HasExtension #13", false, Path.HasExtension ("Makefile"));
}
public void TestRooted ()
{
Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
if (Windows)
Assert ("IsPathRooted #07", Path.IsPathRooted ("\\"));
else
Assert ("IsPathRooted #07", !Path.IsPathRooted ("\\"));
Assert ("IsPathRooted #08", Path.IsPathRooted ("//"));
if (Windows)
Assert ("IsPathRooted #09", Path.IsPathRooted ("\\\\"));
else
Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\\\"));
Assert ("IsPathRooted #10", !Path.IsPathRooted (":"));
if (Windows)
Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
else
Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
if (Windows) {
Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
// This looks MS BUG. It is treated as absolute path
Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
}
}
public void TestCanonicalizeDots ()
{
string current = Path.GetFullPath (".");
Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
string parent = Path.GetFullPath ("..");
Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
}
public void TestDirectoryNameBugs ()
{
if (Windows) {
AssertEquals ("Win #01", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
} else {
AssertEquals ("No win #01", "/etc", Path.GetDirectoryName ("/etc/hostname"));
}
}
public void TestGetFullPathUnix ()
{
if (Windows)
return;
AssertEquals ("#01", "/", Path.GetFullPath ("/"));
AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
Path.GetFullPath ("hey"));
}
[Test]
public void GetFullPath_EndingSeparator ()
{
string fp = Path.GetFullPath ("something/");
char end = fp[fp.Length - 1];
Assert (end == Path.DirectorySeparatorChar);
}
[Test]
public void WindowsSystem32_76191 ()
{
// check for Unix platforms - see FAQ for more details
// http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
int platform = (int) Environment.OSVersion.Platform;
if ((platform == 4) || (platform == 128))
return;
string curdir = Directory.GetCurrentDirectory ();
try {
string system = Environment.SystemDirectory;
Directory.SetCurrentDirectory (system);
string drive = system.Substring (0, 2);
AssertEquals ("current dir", system, Path.GetFullPath (drive));
}
finally {
Directory.SetCurrentDirectory (curdir);
}
}
[Test]
public void WindowsSystem32_77007 ()
{
// check for Unix platforms - see FAQ for more details
// http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
int platform = (int) Environment.OSVersion.Platform;
if ((platform == 4) || (platform == 128))
return;
string curdir = Directory.GetCurrentDirectory ();
try {
string system = Environment.SystemDirectory;
Directory.SetCurrentDirectory (system);
// e.g. C:dir (no backslash) will return CurrentDirectory + dir
string dir = system.Substring (0, 2) + "dir";
AssertEquals ("current dir", Path.Combine (system, "dir"), Path.GetFullPath (dir));
}
finally {
Directory.SetCurrentDirectory (curdir);
}
}
[Test]
public void WindowsDriveC14N_77058 ()
{
// check for Unix platforms - see FAQ for more details
// http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
int platform = (int) Environment.OSVersion.Platform;
if ((platform == 4) || (platform == 128))
return;
AssertEquals ("1", @"C:\Windows\dir", Path.GetFullPath (@"C:\Windows\System32\..\dir"));
AssertEquals ("2", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\dir"));
AssertEquals ("3", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\dir"));
AssertEquals ("4", @"C:\dir", Path.GetFullPath (@"C:\Windows\System32\..\..\..\..\dir"));
AssertEquals ("5", @"C:\dir\", Path.GetFullPath (@"C:\Windows\System32\..\.\..\.\..\dir\"));
}
[Test]
public void InvalidPathChars_Values ()
{
char[] invalid = Path.InvalidPathChars;
if (Windows) {
#if NET_2_0
AssertEquals ("Length", 36, invalid.Length);
#else
AssertEquals ("Length", 15, invalid.Length);
#endif
foreach (char c in invalid) {
int i = (int) c;
#if NET_2_0
if (i < 32)
continue;
#else
if ((i == 0) || (i == 8) || ((i > 15) && (i < 19)) || ((i > 19) && (i < 26)))
continue;
#endif
// in both 1.1 SP1 and 2.0
if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
continue;
Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
}
} else {
foreach (char c in invalid) {
int i = (int) c;
if (i == 0)
continue;
Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
}
}
}
[Test]
public void InvalidPathChars_Modify ()
{
char[] expected = Path.InvalidPathChars;
char[] invalid = Path.InvalidPathChars;
char original = invalid[0];
try {
invalid[0] = 'a';
// kind of scary
Assert ("expected", expected[0] == 'a');
AssertEquals ("readonly", expected[0], Path.InvalidPathChars[0]);
}
finally {
invalid[0] = original;
}
}
#if NET_2_0
[Test]
public void GetInvalidFileNameChars_Values ()
{
char[] invalid = Path.GetInvalidFileNameChars ();
if (Windows) {
AssertEquals (41, invalid.Length);
foreach (char c in invalid) {
int i = (int) c;
if (i < 32)
continue;
if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
continue;
// ':', '*', '?', '\', '/'
if ((i == 58) || (i == 42) || (i == 63) || (i == 92) || (i == 47))
continue;
Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
}
} else {
foreach (char c in invalid) {
int i = (int) c;
// null or '/'
if ((i == 0) || (i == 47))
continue;
Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
}
}
}
[Test]
public void GetInvalidFileNameChars_Modify ()
{
char[] expected = Path.GetInvalidFileNameChars ();
char[] invalid = Path.GetInvalidFileNameChars ();
invalid[0] = 'a';
Assert ("expected", expected[0] != 'a');
AssertEquals ("readonly", expected[0], Path.GetInvalidFileNameChars ()[0]);
}
[Test]
public void GetInvalidPathChars_Values ()
{
char[] invalid = Path.GetInvalidPathChars ();
if (Windows) {
AssertEquals (36, invalid.Length);
foreach (char c in invalid) {
int i = (int) c;
if (i < 32)
continue;
if ((i == 34) || (i == 60) || (i == 62) || (i == 124))
continue;
Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
}
} else {
foreach (char c in invalid) {
int i = (int) c;
if (i == 0)
continue;
Fail (String.Format ("'{0}' (#{1}) is invalid", c, i));
}
}
}
[Test]
public void GetInvalidPathChars_Modify ()
{
char[] expected = Path.GetInvalidPathChars ();
char[] invalid = Path.GetInvalidPathChars ();
invalid[0] = 'a';
Assert ("expected", expected[0] != 'a');
AssertEquals ("readonly", expected[0], Path.GetInvalidPathChars ()[0]);
}
[Test]
public void GetRandomFileName ()
{
string s = Path.GetRandomFileName ();
AssertEquals ("Length", 12, s.Length);
char[] invalid = Path.GetInvalidFileNameChars ();
for (int i=0; i < s.Length; i++) {
if (i == 8)
AssertEquals ("8", '.', s[i]);
else
Assert (i.ToString (), Array.IndexOf (invalid, s[i]) == -1);
}
}
#endif
}
}
|