mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	Use null byte in filename to simulate 'unable to access' on both windows and linux
This commit is contained in:
		
							parent
							
								
									f122b3bbdf
								
							
						
					
					
						commit
						6af26e2306
					
				@ -7,7 +7,6 @@ import (
 | 
				
			|||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"runtime"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRoot(t *testing.T) {
 | 
					func TestRoot(t *testing.T) {
 | 
				
			||||||
@ -27,13 +26,13 @@ func TestRoot(t *testing.T) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
 | 
							t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer func () {
 | 
						defer func() {
 | 
				
			||||||
		existingFile.Close()
 | 
							existingFile.Close()
 | 
				
			||||||
		os.Remove(existingFile.Name())
 | 
							os.Remove(existingFile.Name())
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	unaccessiblePath := getInaccessibleOsDependentPath(existingFile.Name())
 | 
						inaccessiblePath := getInaccessiblePath(existingFile.Name())
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		input              string
 | 
							input              string
 | 
				
			||||||
		shouldErr          bool
 | 
							shouldErr          bool
 | 
				
			||||||
@ -52,7 +51,7 @@ func TestRoot(t *testing.T) {
 | 
				
			|||||||
			`root `, true, "", parseErrContent,
 | 
								`root `, true, "", parseErrContent,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			fmt.Sprintf(`root %s`, unaccessiblePath), true, "", unableToAccessErrContent,
 | 
								fmt.Sprintf(`root %s`, inaccessiblePath), true, "", unableToAccessErrContent,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			fmt.Sprintf(`root {
 | 
								fmt.Sprintf(`root {
 | 
				
			||||||
@ -103,11 +102,7 @@ func getTempDirPath() (string, error) {
 | 
				
			|||||||
	return tempDir, nil
 | 
						return tempDir, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getInaccessibleOsDependentPath(file string) string{
 | 
					func getInaccessiblePath(file string) string {
 | 
				
			||||||
	if runtime.GOOS == "windows"{
 | 
						// null byte in filename is not allowed on Windows AND unix
 | 
				
			||||||
		return filepath.Join("C:", "file\x00name")// 0 byte breaks the lstat syscall
 | 
						return filepath.Join("C:", "file\x00name")
 | 
				
			||||||
	} else {
 | 
					}
 | 
				
			||||||
		// TODO - check if the zero-byte filename works for linux only. If it does - use it instead
 | 
					 | 
				
			||||||
 	   return filepath.Join(file, "some_name")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user