github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/fs/files/files_linux_test.go (about) 1 // Copyright (c) 2018, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package files 7 8 import ( 9 "bytes" 10 "io/ioutil" 11 "os" 12 "testing" 13 14 "github.com/sylabs/singularity/internal/pkg/test" 15 ) 16 17 func TestGroup(t *testing.T) { 18 test.DropPrivilege(t) 19 defer test.ResetPrivilege(t) 20 21 var gids []int 22 uid := os.Getuid() 23 24 _, err := Group("/fake", uid, gids) 25 if err == nil { 26 t.Errorf("should have failed with bad group file") 27 } 28 _, err = Group("/etc/group", uid, gids) 29 if err != nil { 30 t.Errorf("should have passed with correct group file") 31 } 32 // with an empty file 33 f, err := ioutil.TempFile("", "empty-group-") 34 if err != nil { 35 t.Error(err) 36 } 37 emptyGroup := f.Name() 38 defer os.Remove(emptyGroup) 39 f.Close() 40 41 _, err = Group(emptyGroup, uid, gids) 42 if err != nil { 43 t.Error(err) 44 } 45 } 46 47 func TestPasswd(t *testing.T) { 48 test.DropPrivilege(t) 49 defer test.ResetPrivilege(t) 50 51 uid := os.Getuid() 52 53 _, err := Passwd("/fake", "/fake", uid) 54 if err == nil { 55 t.Errorf("should have failed with bad passwd file") 56 } 57 _, err = Passwd("/etc/passwd", "/home", uid) 58 if err != nil { 59 t.Errorf("should have passed with correct passwd file") 60 } 61 // with an empty file 62 f, err := ioutil.TempFile("", "empty-passwd-") 63 if err != nil { 64 t.Error(err) 65 } 66 emptyPasswd := f.Name() 67 defer os.Remove(emptyPasswd) 68 f.Close() 69 70 _, err = Passwd(emptyPasswd, "/home", uid) 71 if err != nil { 72 t.Error(err) 73 } 74 } 75 76 func TestHostname(t *testing.T) { 77 test.DropPrivilege(t) 78 defer test.ResetPrivilege(t) 79 80 content, err := Hostname("") 81 if err == nil { 82 t.Errorf("should have failed with empty hostname") 83 } 84 content, err = Hostname("mycontainer") 85 if err != nil { 86 t.Errorf("should have passed with correct hostname") 87 } 88 if bytes.Compare(content, []byte("mycontainer\n")) != 0 { 89 t.Errorf("Hostname returns a bad content") 90 } 91 content, err = Hostname("bad|hostname") 92 if err == nil { 93 t.Errorf("should have failed with non valid hostname") 94 } 95 } 96 97 func TestResolvConf(t *testing.T) { 98 test.DropPrivilege(t) 99 defer test.ResetPrivilege(t) 100 101 content, err := ResolvConf([]string{}) 102 if err == nil { 103 t.Errorf("should have failed with empty dns") 104 } 105 content, err = ResolvConf([]string{"test"}) 106 if err == nil { 107 t.Errorf("should have failed with bad dns") 108 } 109 content, err = ResolvConf([]string{"8.8.8.8"}) 110 if err != nil { 111 t.Errorf("should have passed with valid dns") 112 } 113 if bytes.Compare(content, []byte("nameserver 8.8.8.8\n")) != 0 { 114 t.Errorf("ResolvConf returns a bad content") 115 } 116 }