gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/cmd/gofer_test.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path"
    22  	"path/filepath"
    23  	"testing"
    24  )
    25  
    26  func tmpDir() string {
    27  	if dir, ok := os.LookupEnv("TEST_TMPDIR"); ok {
    28  		return dir
    29  	}
    30  	return "/tmp"
    31  }
    32  
    33  type dir struct {
    34  	rel  string
    35  	link string
    36  }
    37  
    38  func construct(root string, dirs []dir) error {
    39  	for _, d := range dirs {
    40  		p := path.Join(root, d.rel)
    41  		if d.link == "" {
    42  			if err := os.MkdirAll(p, 0755); err != nil {
    43  				return fmt.Errorf("error creating dir: %v", err)
    44  			}
    45  		} else {
    46  			if err := os.MkdirAll(path.Dir(p), 0755); err != nil {
    47  				return fmt.Errorf("error creating dir: %v", err)
    48  			}
    49  			if err := os.Symlink(d.link, p); err != nil {
    50  				return fmt.Errorf("error creating symlink: %v", err)
    51  			}
    52  		}
    53  	}
    54  	return nil
    55  }
    56  
    57  func TestResolveSymlinks(t *testing.T) {
    58  	root, err := ioutil.TempDir(tmpDir(), "root")
    59  	if err != nil {
    60  		t.Fatal("ioutil.TempDir() failed:", err)
    61  	}
    62  	dirs := []dir{
    63  		{"dir1/dir11/dir111/dir1111", ""}, // Just a boring dir
    64  		{"dir1/lnk12", "dir11"},           // Link to sibling
    65  		{"dir1/lnk13", "./dir11"},         // Link to sibling through self
    66  		{"dir1/lnk14", "../dir1/dir11"},   // Link to sibling through parent
    67  		{"dir1/dir15/lnk151", ".."},       // Link to parent
    68  		{"dir1/lnk16", "dir11/dir111"},    // Link to child
    69  		{"dir1/lnk17", "."},               // Link to self
    70  		{"dir1/lnk18", "lnk13"},           // Link to link
    71  		{"lnk2", "dir1/lnk13"},            // Link to link to link
    72  		{"dir3/dir21/lnk211", "../.."},    // Link to root relative
    73  		{"dir3/lnk22", "/"},               // Link to root absolute
    74  		{"dir3/lnk23", "/dir1"},           // Link to dir absolute
    75  		{"dir3/lnk24", "/dir1/lnk12"},     // Link to link absolute
    76  		{"lnk5", "../../.."},              // Link outside root
    77  	}
    78  	if err := construct(root, dirs); err != nil {
    79  		t.Fatal("construct failed:", err)
    80  	}
    81  
    82  	tests := []struct {
    83  		name        string
    84  		rel         string
    85  		want        string
    86  		compareHost bool
    87  	}{
    88  		{name: "root", rel: "/", want: "/", compareHost: true},
    89  		{name: "basic dir", rel: "/dir1/dir11/dir111", want: "/dir1/dir11/dir111", compareHost: true},
    90  		{name: "dot 1", rel: "/dir1/dir11/./dir111", want: "/dir1/dir11/dir111", compareHost: true},
    91  		{name: "dot 2", rel: "/dir1/././dir11/./././././dir111/.", want: "/dir1/dir11/dir111", compareHost: true},
    92  		{name: "dotdot 1", rel: "/dir1/dir11/../dir15", want: "/dir1/dir15", compareHost: true},
    93  		{name: "dotdot 2", rel: "/dir1/dir11/dir1111/../..", want: "/dir1", compareHost: true},
    94  
    95  		{name: "link sibling", rel: "/dir1/lnk12", want: "/dir1/dir11", compareHost: true},
    96  		{name: "link sibling + dir", rel: "/dir1/lnk12/dir111", want: "/dir1/dir11/dir111", compareHost: true},
    97  		{name: "link sibling through self", rel: "/dir1/lnk13", want: "/dir1/dir11", compareHost: true},
    98  		{name: "link sibling through parent", rel: "/dir1/lnk14", want: "/dir1/dir11", compareHost: true},
    99  
   100  		{name: "link parent", rel: "/dir1/dir15/lnk151", want: "/dir1", compareHost: true},
   101  		{name: "link parent + dir", rel: "/dir1/dir15/lnk151/dir11", want: "/dir1/dir11", compareHost: true},
   102  		{name: "link child", rel: "/dir1/lnk16", want: "/dir1/dir11/dir111", compareHost: true},
   103  		{name: "link child + dir", rel: "/dir1/lnk16/dir1111", want: "/dir1/dir11/dir111/dir1111", compareHost: true},
   104  		{name: "link self", rel: "/dir1/lnk17", want: "/dir1", compareHost: true},
   105  		{name: "link self + dir", rel: "/dir1/lnk17/dir11", want: "/dir1/dir11", compareHost: true},
   106  
   107  		{name: "link^2", rel: "/dir1/lnk18", want: "/dir1/dir11", compareHost: true},
   108  		{name: "link^2 + dir", rel: "/dir1/lnk18/dir111", want: "/dir1/dir11/dir111", compareHost: true},
   109  		{name: "link^3", rel: "/lnk2", want: "/dir1/dir11", compareHost: true},
   110  		{name: "link^3 + dir", rel: "/lnk2/dir111", want: "/dir1/dir11/dir111", compareHost: true},
   111  
   112  		{name: "link abs", rel: "/dir3/lnk23", want: "/dir1"},
   113  		{name: "link abs + dir", rel: "/dir3/lnk23/dir11", want: "/dir1/dir11"},
   114  		{name: "link^2 abs", rel: "/dir3/lnk24", want: "/dir1/dir11"},
   115  		{name: "link^2 abs + dir", rel: "/dir3/lnk24/dir111", want: "/dir1/dir11/dir111"},
   116  
   117  		{name: "root link rel", rel: "/dir3/dir21/lnk211", want: "/", compareHost: true},
   118  		{name: "root link abs", rel: "/dir3/lnk22", want: "/"},
   119  		{name: "root contain link", rel: "/lnk5/dir1", want: "/dir1"},
   120  		{name: "root contain dotdot", rel: "/dir1/dir11/../../../../../../../..", want: "/"},
   121  
   122  		{name: "crazy", rel: "/dir3/dir21/lnk211/dir3/lnk22/dir1/dir11/../../lnk5/dir3/../dir3/lnk24/dir111/dir1111/..", want: "/dir1/dir11/dir111"},
   123  	}
   124  	for _, tst := range tests {
   125  		t.Run(tst.name, func(t *testing.T) {
   126  			got, err := resolveSymlinks(root, tst.rel)
   127  			if err != nil {
   128  				t.Errorf("resolveSymlinks(root, %q) failed: %v", tst.rel, err)
   129  			}
   130  			want := path.Join(root, tst.want)
   131  			if got != want {
   132  				t.Errorf("resolveSymlinks(root, %q) got: %q, want: %q", tst.rel, got, want)
   133  			}
   134  			if tst.compareHost {
   135  				// Check that host got to the same end result.
   136  				host, err := filepath.EvalSymlinks(path.Join(root, tst.rel))
   137  				if err != nil {
   138  					t.Errorf("path.EvalSymlinks(root, %q) failed: %v", tst.rel, err)
   139  				}
   140  				if host != got {
   141  					t.Errorf("resolveSymlinks(root, %q) got: %q, want: %q", tst.rel, host, got)
   142  				}
   143  			}
   144  		})
   145  	}
   146  }
   147  
   148  func TestResolveSymlinksLoop(t *testing.T) {
   149  	root, err := ioutil.TempDir(tmpDir(), "root")
   150  	if err != nil {
   151  		t.Fatal("ioutil.TempDir() failed:", err)
   152  	}
   153  	dirs := []dir{
   154  		{"loop1", "loop2"},
   155  		{"loop2", "loop1"},
   156  	}
   157  	if err := construct(root, dirs); err != nil {
   158  		t.Fatal("construct failed:", err)
   159  	}
   160  	if _, err := resolveSymlinks(root, "loop1"); err == nil {
   161  		t.Errorf("resolveSymlinks() should have failed")
   162  	}
   163  }