...

Source file src/github.com/Microsoft/hcsshim/internal/jobcontainers/path_test.go

Documentation: github.com/Microsoft/hcsshim/internal/jobcontainers

     1  //go:build windows
     2  
     3  package jobcontainers
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func assertStr(t *testing.T, a string, b string) {
    13  	t.Helper()
    14  	if !strings.EqualFold(a, b) {
    15  		t.Fatalf("expected %s, got %s", a, b)
    16  	}
    17  }
    18  
    19  func TestSearchPath(t *testing.T) {
    20  	// Testing that relative paths work.
    21  	_, err := searchPathForExe("windows\\system32\\ping", "C:\\")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	_, err = searchPathForExe("system32\\ping", "C:\\windows")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	_, err = searchPathForExe("ping", "C:\\windows\\system32")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  }
    36  
    37  type config struct {
    38  	name                    string
    39  	commandLine             string
    40  	workDir                 string
    41  	pathEnv                 string
    42  	expectedApplicationName string
    43  	expectedCmdline         string
    44  }
    45  
    46  func runGetApplicationNameTests(t *testing.T, tests []*config) {
    47  	t.Helper()
    48  	for _, cfg := range tests {
    49  		t.Run(cfg.name, func(t *testing.T) {
    50  			path, cmdLine, err := getApplicationName(cfg.commandLine, cfg.workDir, cfg.pathEnv)
    51  			if err != nil {
    52  				t.Fatal(err)
    53  			}
    54  			assertStr(t, cfg.expectedCmdline, cmdLine)
    55  			assertStr(t, cfg.expectedApplicationName, path)
    56  		})
    57  	}
    58  }
    59  
    60  func TestGetApplicationNamePing(t *testing.T) {
    61  	expected := "C:\\WINDOWS\\system32\\ping.exe"
    62  
    63  	tests := []*config{
    64  		{
    65  			name:                    "Ping",
    66  			commandLine:             "ping",
    67  			workDir:                 "C:\\",
    68  			pathEnv:                 "C:\\windows\\system32",
    69  			expectedCmdline:         "ping",
    70  			expectedApplicationName: expected,
    71  		},
    72  		{
    73  			name:                    "Ping_Relative_Forward_Slash",
    74  			commandLine:             "system32/ping",
    75  			workDir:                 "C:\\windows\\",
    76  			pathEnv:                 "C:\\windows\\system32",
    77  			expectedCmdline:         "system32/ping",
    78  			expectedApplicationName: expected,
    79  		},
    80  		{
    81  			name:                    "Ping_Relative_Back_Slash",
    82  			commandLine:             "system32\\ping",
    83  			workDir:                 "C:\\windows",
    84  			pathEnv:                 "C:\\windows\\system32",
    85  			expectedCmdline:         "system32\\ping",
    86  			expectedApplicationName: expected,
    87  		},
    88  		{
    89  			name:                    "Ping_Cwd_Windows_Directory",
    90  			commandLine:             "system32\\ping",
    91  			workDir:                 "C:\\Windows",
    92  			pathEnv:                 "C:\\windows\\system32",
    93  			expectedCmdline:         "system32\\ping",
    94  			expectedApplicationName: expected,
    95  		},
    96  		{
    97  			name:                    "Ping_With_Cwd",
    98  			commandLine:             "cmd /c ping 127.0.0.1",
    99  			workDir:                 "C:\\",
   100  			pathEnv:                 "C:\\windows\\system32",
   101  			expectedCmdline:         "cmd /c ping 127.0.0.1",
   102  			expectedApplicationName: "C:\\windows\\system32\\cmd.exe",
   103  		},
   104  		{
   105  			name:                    "Ping_With_Cwd_Relative_Path",
   106  			commandLine:             "system32\\cmd /c ping 127.0.0.1",
   107  			workDir:                 "C:\\windows\\",
   108  			pathEnv:                 "C:\\windows\\system32",
   109  			expectedCmdline:         "system32\\cmd /c ping 127.0.0.1",
   110  			expectedApplicationName: "C:\\windows\\system32\\cmd.exe",
   111  		},
   112  		{
   113  			name:                    "Ping_With_Space",
   114  			commandLine:             "ping test",
   115  			workDir:                 "C:\\",
   116  			pathEnv:                 "C:\\windows\\system32",
   117  			expectedCmdline:         "ping test",
   118  			expectedApplicationName: expected,
   119  		},
   120  		{
   121  			name:                    "Ping_With_Quote",
   122  			commandLine:             "\"ping\" 127.0.0.1",
   123  			workDir:                 "C:\\",
   124  			pathEnv:                 "C:\\windows\\system32",
   125  			expectedCmdline:         "\"ping\" 127.0.0.1",
   126  			expectedApplicationName: expected,
   127  		},
   128  	}
   129  
   130  	runGetApplicationNameTests(t, tests)
   131  }
   132  
   133  func TestGetApplicationNameRandomBinary(t *testing.T) {
   134  	tempDir := t.TempDir()
   135  
   136  	// Create fake executables in a temporary directory to use for the below tests.
   137  	testExe := filepath.Join(tempDir, "test.exe")
   138  	f1, err := os.Create(testExe)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	t.Cleanup(func() { _ = f1.Close() })
   143  
   144  	test2Exe := filepath.Join(tempDir, "test 2.exe")
   145  	f2, err := os.Create(test2Exe)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	t.Cleanup(func() { _ = f2.Close() })
   150  
   151  	exeWithSpace := filepath.Join(tempDir, "exe with space.exe")
   152  	f3, err := os.Create(exeWithSpace)
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  	t.Cleanup(func() { _ = f3.Close() })
   157  
   158  	tests := []*config{
   159  		// See if we can successfully find "exe with space.exe" with no quoting, it should first try "exe.exe", then "exe with.exe" and then finally
   160  		// "exe with space.exe"
   161  		{
   162  			name:                    "Spaces_With_No_Quoting",
   163  			commandLine:             "exe with space.exe",
   164  			workDir:                 filepath.Dir(testExe),
   165  			pathEnv:                 "",
   166  			expectedCmdline:         "\"exe with space.exe\"",
   167  			expectedApplicationName: exeWithSpace,
   168  		},
   169  		// See if we can successfully find "exe with space.exe" with quoting, it should try "exe with space.exe" only.
   170  		{
   171  			name:                    "Spaces_With_Quoting",
   172  			commandLine:             "\"exe with space.exe\"",
   173  			workDir:                 filepath.Dir(testExe),
   174  			pathEnv:                 "",
   175  			expectedCmdline:         "\"exe with space.exe\"",
   176  			expectedApplicationName: exeWithSpace,
   177  		},
   178  		// Try a quoted commandline, so that we find the actual "C:\rest\of\the\path\test 2.exe" binary
   179  		{
   180  			name:                    "Test2_Binary_With_Quotes",
   181  			commandLine:             "\"test 2.exe\"",
   182  			workDir:                 filepath.Dir(test2Exe),
   183  			pathEnv:                 "",
   184  			expectedCmdline:         "\"test 2.exe\"",
   185  			expectedApplicationName: test2Exe,
   186  		},
   187  		// We should find the test.exe binary, and the 2 will be treated as an argument in this case
   188  		{
   189  			name:                    "Test2_Binary_No_Quotes",
   190  			commandLine:             "test 2",
   191  			workDir:                 filepath.Dir(test2Exe),
   192  			pathEnv:                 "",
   193  			expectedCmdline:         "test 2",
   194  			expectedApplicationName: testExe,
   195  		},
   196  		// Test finding test binary with no file extension
   197  		{
   198  			name:                    "Test_Binary_No_File_Extension",
   199  			commandLine:             testExe[0 : len(testExe)-4],
   200  			workDir:                 filepath.Dir(testExe),
   201  			pathEnv:                 "",
   202  			expectedCmdline:         testExe[0 : len(testExe)-4],
   203  			expectedApplicationName: testExe,
   204  		},
   205  		// Test finding the test binary with the PATH containing the directory it lives in.
   206  		{
   207  			name:                    "Test_Binary_With_Path_Containing_Location",
   208  			commandLine:             "test",
   209  			workDir:                 "C:\\",
   210  			pathEnv:                 filepath.Dir(testExe),
   211  			expectedCmdline:         "test",
   212  			expectedApplicationName: testExe,
   213  		},
   214  	}
   215  
   216  	runGetApplicationNameTests(t, tests)
   217  }
   218  

View as plain text