gitee.com/mysnapcore/mysnapd@v0.1.0/polkit/pid_start_time_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 //go:build linux 3 // +build linux 4 5 /* 6 * Copyright (C) 2017 Canonical Ltd 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 3 as 10 * published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * 20 */ 21 22 package polkit 23 24 import ( 25 "io/ioutil" 26 "os" 27 "path/filepath" 28 "syscall" 29 "testing" 30 31 "gopkg.in/check.v1" 32 ) 33 34 func Test(t *testing.T) { check.TestingT(t) } 35 36 type polkitSuite struct{} 37 38 var _ = check.Suite(&polkitSuite{}) 39 40 func (s *polkitSuite) TestGetStartTime(c *check.C) { 41 pid := os.Getpid() 42 43 startTime, err := getStartTimeForPid(int32(pid)) 44 c.Assert(err, check.IsNil) 45 c.Check(startTime, check.Not(check.Equals), uint64(0)) 46 } 47 48 func (s *polkitSuite) TestGetStartTimeBadPid(c *check.C) { 49 // Find an unused process ID by checking for errors from Kill. 50 pid := 2 51 for { 52 if err := syscall.Kill(pid, 0); err == syscall.ESRCH { 53 break 54 } 55 pid += 1 56 } 57 58 startTime, err := getStartTimeForPid(int32(pid)) 59 c.Assert(err, check.ErrorMatches, "open .*: no such file or directory") 60 c.Check(startTime, check.Equals, uint64(0)) 61 } 62 63 func (s *polkitSuite) TestProcStatParsing(c *check.C) { 64 filename := filepath.Join(c.MkDir(), "stat") 65 contents := []byte("18433 (cat) R 9732 18433 9732 34818 18433 4194304 96 0 1 0 0 0 0 0 20 0 1 0 123104764 7602176 182 18446744073709551615 94902526107648 94902526138492 140734457666896 0 0 0 0 0 0 0 0 0 17 5 0 0 0 0 0 94902528236168 94902528237760 94902542680064 140734457672267 140734457672287 140734457672287 140734457675759 0") 66 err := ioutil.WriteFile(filename, contents, 0644) 67 c.Assert(err, check.IsNil) 68 69 startTime, err := getStartTimeForProcStatFile(filename) 70 c.Assert(err, check.IsNil) 71 c.Check(startTime, check.Equals, uint64(123104764)) 72 }