LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package os_test | 5 package os_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 } | 401 } |
402 defer Remove(from) | 402 defer Remove(from) |
403 tostat, err := Stat(to) | 403 tostat, err := Stat(to) |
404 if err != nil { | 404 if err != nil { |
405 t.Fatalf("stat %q failed: %v", to, err) | 405 t.Fatalf("stat %q failed: %v", to, err) |
406 } | 406 } |
407 fromstat, err := Stat(from) | 407 fromstat, err := Stat(from) |
408 if err != nil { | 408 if err != nil { |
409 t.Fatalf("stat %q failed: %v", from, err) | 409 t.Fatalf("stat %q failed: %v", from, err) |
410 } | 410 } |
411 » if !tostat.Same(fromstat) { | 411 » if !tostat.(*FileStat).SameFile(fromstat.(*FileStat)) { |
412 t.Errorf("link %q, %q did not create hard link", to, from) | 412 t.Errorf("link %q, %q did not create hard link", to, from) |
413 } | 413 } |
414 } | 414 } |
415 | 415 |
416 func TestSymLink(t *testing.T) { | 416 func TestSymLink(t *testing.T) { |
417 // Symlinks are not supported under windows or Plan 9. | 417 // Symlinks are not supported under windows or Plan 9. |
418 if syscall.OS == "windows" || syscall.OS == "plan9" { | 418 if syscall.OS == "windows" || syscall.OS == "plan9" { |
419 return | 419 return |
420 } | 420 } |
421 from, to := "symlinktestfrom", "symlinktestto" | 421 from, to := "symlinktestfrom", "symlinktestto" |
(...skipping 15 matching lines...) Expand all Loading... |
437 if err != nil { | 437 if err != nil { |
438 t.Fatalf("stat %q failed: %v", to, err) | 438 t.Fatalf("stat %q failed: %v", to, err) |
439 } | 439 } |
440 if tostat.Mode()&ModeSymlink != 0 { | 440 if tostat.Mode()&ModeSymlink != 0 { |
441 t.Fatalf("stat %q claims to have found a symlink", to) | 441 t.Fatalf("stat %q claims to have found a symlink", to) |
442 } | 442 } |
443 fromstat, err := Stat(from) | 443 fromstat, err := Stat(from) |
444 if err != nil { | 444 if err != nil { |
445 t.Fatalf("stat %q failed: %v", from, err) | 445 t.Fatalf("stat %q failed: %v", from, err) |
446 } | 446 } |
447 » if !tostat.Same(fromstat) { | 447 » if !tostat.(*FileStat).SameFile(fromstat.(*FileStat)) { |
448 t.Errorf("symlink %q, %q did not create symlink", to, from) | 448 t.Errorf("symlink %q, %q did not create symlink", to, from) |
449 } | 449 } |
450 fromstat, err = Lstat(from) | 450 fromstat, err = Lstat(from) |
451 if err != nil { | 451 if err != nil { |
452 t.Fatalf("lstat %q failed: %v", from, err) | 452 t.Fatalf("lstat %q failed: %v", from, err) |
453 } | 453 } |
454 if fromstat.Mode()&ModeSymlink == 0 { | 454 if fromstat.Mode()&ModeSymlink == 0 { |
455 t.Fatalf("symlink %q, %q did not create symlink", to, from) | 455 t.Fatalf("symlink %q, %q did not create symlink", to, from) |
456 } | 456 } |
457 fromstat, err = Stat(from) | 457 fromstat, err = Stat(from) |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 t.Fatalf("chmod %s 0456: %s", f.Name(), err) | 589 t.Fatalf("chmod %s 0456: %s", f.Name(), err) |
590 } | 590 } |
591 checkMode(t, f.Name(), 0456) | 591 checkMode(t, f.Name(), 0456) |
592 | 592 |
593 if err := f.Chmod(0123); err != nil { | 593 if err := f.Chmod(0123); err != nil { |
594 t.Fatalf("chmod %s 0123: %s", f.Name(), err) | 594 t.Fatalf("chmod %s 0123: %s", f.Name(), err) |
595 } | 595 } |
596 checkMode(t, f.Name(), 0123) | 596 checkMode(t, f.Name(), 0123) |
597 } | 597 } |
598 | 598 |
599 func checkUidGid(t *testing.T, path string, uid, gid int) { | |
600 dir, err := Stat(path) | |
601 if err != nil { | |
602 t.Fatalf("Stat %q (looking for uid/gid %d/%d): %s", path, uid, g
id, err) | |
603 } | |
604 sys := dir.(*FileStat).Sys | |
605 if int(sys.Uid) != uid { | |
606 t.Errorf("Stat %q: uid %d want %d", path, sys.Uid, uid) | |
607 } | |
608 if int(sys.Gid) != gid { | |
609 t.Errorf("Stat %q: gid %d want %d", path, sys.Gid, gid) | |
610 } | |
611 } | |
612 | |
613 func TestChown(t *testing.T) { | |
614 // Chown is not supported under windows or Plan 9. | |
615 // Plan9 provides a native ChownPlan9 version instead. | |
616 if syscall.OS == "windows" || syscall.OS == "plan9" { | |
617 return | |
618 } | |
619 // Use TempDir() to make sure we're on a local file system, | |
620 // so that the group ids returned by Getgroups will be allowed | |
621 // on the file. On NFS, the Getgroups groups are | |
622 // basically useless. | |
623 f := newFile("TestChown", t) | |
624 defer Remove(f.Name()) | |
625 defer f.Close() | |
626 dir, err := f.Stat() | |
627 if err != nil { | |
628 t.Fatalf("stat %s: %s", f.Name(), err) | |
629 } | |
630 | |
631 // Can't change uid unless root, but can try | |
632 // changing the group id. First try our current group. | |
633 gid := Getgid() | |
634 t.Log("gid:", gid) | |
635 if err = Chown(f.Name(), -1, gid); err != nil { | |
636 t.Fatalf("chown %s -1 %d: %s", f.Name(), gid, err) | |
637 } | |
638 sys := dir.(*FileStat).Sys | |
639 checkUidGid(t, f.Name(), int(sys.Uid), gid) | |
640 | |
641 // Then try all the auxiliary groups. | |
642 groups, err := Getgroups() | |
643 if err != nil { | |
644 t.Fatalf("getgroups: %s", err) | |
645 } | |
646 t.Log("groups: ", groups) | |
647 for _, g := range groups { | |
648 if err = Chown(f.Name(), -1, g); err != nil { | |
649 t.Fatalf("chown %s -1 %d: %s", f.Name(), g, err) | |
650 } | |
651 checkUidGid(t, f.Name(), int(sys.Uid), g) | |
652 | |
653 // change back to gid to test fd.Chown | |
654 if err = f.Chown(-1, gid); err != nil { | |
655 t.Fatalf("fchown %s -1 %d: %s", f.Name(), gid, err) | |
656 } | |
657 checkUidGid(t, f.Name(), int(sys.Uid), gid) | |
658 } | |
659 } | |
660 | |
661 func checkSize(t *testing.T, f *File, size int64) { | 599 func checkSize(t *testing.T, f *File, size int64) { |
662 dir, err := f.Stat() | 600 dir, err := f.Stat() |
663 if err != nil { | 601 if err != nil { |
664 t.Fatalf("Stat %q (looking for size %d): %s", f.Name(), size, er
r) | 602 t.Fatalf("Stat %q (looking for size %d): %s", f.Name(), size, er
r) |
665 } | 603 } |
666 if dir.Size() != size { | 604 if dir.Size() != size { |
667 t.Errorf("Stat %q: size %d want %d", f.Name(), dir.Size(), size) | 605 t.Errorf("Stat %q: size %d want %d", f.Name(), dir.Size(), size) |
668 } | 606 } |
669 } | 607 } |
670 | 608 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 defer Remove(f.Name()) | 651 defer Remove(f.Name()) |
714 defer f.Close() | 652 defer f.Close() |
715 | 653 |
716 f.Write([]byte("hello, world\n")) | 654 f.Write([]byte("hello, world\n")) |
717 f.Close() | 655 f.Close() |
718 | 656 |
719 st, err := Stat(f.Name()) | 657 st, err := Stat(f.Name()) |
720 if err != nil { | 658 if err != nil { |
721 t.Fatalf("Stat %s: %s", f.Name(), err) | 659 t.Fatalf("Stat %s: %s", f.Name(), err) |
722 } | 660 } |
723 » preStat := st.(*FileStat).Sys | 661 » preStat := st.(*FileStat) |
724 | 662 |
725 // Move access and modification time back a second | 663 // Move access and modification time back a second |
726 » at := TimespecToTime(preStat.Atimespec) | 664 » at := Atime(preStat) |
727 » mt := TimespecToTime(preStat.Mtimespec) | 665 » mt := preStat.ModTime() |
728 err = Chtimes(f.Name(), at.Add(-time.Second), mt.Add(-time.Second)) | 666 err = Chtimes(f.Name(), at.Add(-time.Second), mt.Add(-time.Second)) |
729 if err != nil { | 667 if err != nil { |
730 t.Fatalf("Chtimes %s: %s", f.Name(), err) | 668 t.Fatalf("Chtimes %s: %s", f.Name(), err) |
731 } | 669 } |
732 | 670 |
733 st, err = Stat(f.Name()) | 671 st, err = Stat(f.Name()) |
734 if err != nil { | 672 if err != nil { |
735 t.Fatalf("second Stat %s: %s", f.Name(), err) | 673 t.Fatalf("second Stat %s: %s", f.Name(), err) |
736 } | 674 } |
737 » postStat := st.(*FileStat).Sys | 675 » postStat := st.(*FileStat) |
738 | 676 |
739 /* Plan 9: | 677 /* Plan 9: |
740 Mtime is the time of the last change of content. Similarly, ati
me is set whenever the | 678 Mtime is the time of the last change of content. Similarly, ati
me is set whenever the |
741 contents are accessed; also, it is set whenever mtime is set. | 679 contents are accessed; also, it is set whenever mtime is set. |
742 */ | 680 */ |
743 » pat := TimespecToTime(postStat.Atimespec) | 681 » pat := Atime(postStat) |
744 » pmt := TimespecToTime(postStat.Mtimespec) | 682 » pmt := postStat.ModTime() |
745 if !pat.Before(at) && syscall.OS != "plan9" { | 683 if !pat.Before(at) && syscall.OS != "plan9" { |
746 t.Errorf("AccessTime didn't go backwards; was=%d, after=%d", at,
pat) | 684 t.Errorf("AccessTime didn't go backwards; was=%d, after=%d", at,
pat) |
747 } | 685 } |
748 | 686 |
749 if !pmt.Before(mt) { | 687 if !pmt.Before(mt) { |
750 t.Errorf("ModTime didn't go backwards; was=%d, after=%d", mt, pm
t) | 688 t.Errorf("ModTime didn't go backwards; was=%d, after=%d", mt, pm
t) |
751 } | 689 } |
752 } | 690 } |
753 | 691 |
754 func TestChdirAndGetwd(t *testing.T) { | 692 func TestChdirAndGetwd(t *testing.T) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 if !strings.HasSuffix(syscallErrStr, expectedErr
Str) { | 836 if !strings.HasSuffix(syscallErrStr, expectedErr
Str) { |
899 t.Errorf("Open(%q, %d) = _, %q; want suf
fix %q", tt.path, tt.mode, syscallErrStr, expectedErrStr) | 837 t.Errorf("Open(%q, %d) = _, %q; want suf
fix %q", tt.path, tt.mode, syscallErrStr, expectedErrStr) |
900 } | 838 } |
901 } else { | 839 } else { |
902 t.Errorf("Open(%q, %d) = _, %q; want %q", tt.pat
h, tt.mode, perr.Err.Error(), tt.error.Error()) | 840 t.Errorf("Open(%q, %d) = _, %q; want %q", tt.pat
h, tt.mode, perr.Err.Error(), tt.error.Error()) |
903 } | 841 } |
904 } | 842 } |
905 } | 843 } |
906 } | 844 } |
907 | 845 |
| 846 func TestOpenNoName(t *testing.T) { |
| 847 f, err := Open("") |
| 848 if err == nil { |
| 849 t.Fatal(`Open("") succeeded`) |
| 850 f.Close() |
| 851 } |
| 852 } |
| 853 |
908 func run(t *testing.T, cmd []string) string { | 854 func run(t *testing.T, cmd []string) string { |
909 // Run /bin/hostname and collect output. | 855 // Run /bin/hostname and collect output. |
910 r, w, err := Pipe() | 856 r, w, err := Pipe() |
911 if err != nil { | 857 if err != nil { |
912 t.Fatal(err) | 858 t.Fatal(err) |
913 } | 859 } |
914 p, err := StartProcess("/bin/hostname", []string{"hostname"}, &ProcAttr{
Files: []*File{nil, w, Stderr}}) | 860 p, err := StartProcess("/bin/hostname", []string{"hostname"}, &ProcAttr{
Files: []*File{nil, w, Stderr}}) |
915 if err != nil { | 861 if err != nil { |
916 t.Fatal(err) | 862 t.Fatal(err) |
917 } | 863 } |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1075 } | 1021 } |
1076 } | 1022 } |
1077 | 1023 |
1078 func TestNilWaitmsgString(t *testing.T) { | 1024 func TestNilWaitmsgString(t *testing.T) { |
1079 var w *Waitmsg | 1025 var w *Waitmsg |
1080 s := w.String() | 1026 s := w.String() |
1081 if s != "<nil>" { | 1027 if s != "<nil>" { |
1082 t.Errorf("(*Waitmsg)(nil).String() = %q, want %q", s, "<nil>") | 1028 t.Errorf("(*Waitmsg)(nil).String() = %q, want %q", s, "<nil>") |
1083 } | 1029 } |
1084 } | 1030 } |
LEFT | RIGHT |