|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//go:build !windows && !openbsd |
| 19 | +// +build !windows,!openbsd |
| 20 | + |
| 21 | +// Test for openbsd are excluded here as info.GID() returns 0 instead of the actual value |
| 22 | +// As the code does not seem to be used in any of the beats, this should be ok |
| 23 | +// Still it would be interesting to know why it returns 0. |
| 24 | + |
| 25 | +package file_test |
| 26 | + |
| 27 | +import ( |
| 28 | + "io/ioutil" |
| 29 | + "os" |
| 30 | + "path/filepath" |
| 31 | + "testing" |
| 32 | + |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | + |
| 35 | + "github.com/elastic/elastic-agent-libs/file" |
| 36 | +) |
| 37 | + |
| 38 | +func TestStat(t *testing.T) { |
| 39 | + f, err := ioutil.TempFile("", "teststat") |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + defer os.Remove(f.Name()) |
| 44 | + |
| 45 | + link := filepath.Join(os.TempDir(), "teststat-link") |
| 46 | + if err := os.Symlink(f.Name(), link); err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + defer os.Remove(link) |
| 50 | + |
| 51 | + info, err := file.Stat(link) |
| 52 | + if err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + |
| 56 | + require.True(t, info.Mode().IsRegular()) |
| 57 | + |
| 58 | + uid, err := info.UID() |
| 59 | + if err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + require.EqualValues(t, os.Geteuid(), uid) |
| 63 | + |
| 64 | + gid, err := info.GID() |
| 65 | + if err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + require.EqualValues(t, os.Getegid(), gid) |
| 69 | +} |
| 70 | + |
| 71 | +func TestLstat(t *testing.T) { |
| 72 | + link := filepath.Join(os.TempDir(), "link") |
| 73 | + if err := os.Symlink("dummy", link); err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + defer os.Remove(link) |
| 77 | + |
| 78 | + info, err := file.Lstat(link) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + require.True(t, info.Mode()&os.ModeSymlink > 0) |
| 84 | + |
| 85 | + uid, err := info.UID() |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + require.EqualValues(t, os.Geteuid(), uid) |
| 90 | + |
| 91 | + gid, err := info.GID() |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + require.EqualValues(t, os.Getegid(), gid) |
| 96 | +} |
0 commit comments