github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/osutil/settime_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package osutil_test
    21  
    22  import (
    23  	"syscall"
    24  	"time"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/osutil"
    29  )
    30  
    31  type settimeTestSuite struct{}
    32  
    33  var _ = Suite(&settimeTestSuite{})
    34  
    35  func (s *settimeTestSuite) TestSetTime(c *C) {
    36  	timeIn, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
    37  	c.Assert(err, IsNil)
    38  
    39  	r := osutil.MockSyscallSettimeofday(func(t *syscall.Timeval) error {
    40  		c.Assert(int64(t.Sec), Equals, timeIn.Unix())
    41  		c.Assert(int64(t.Usec), Equals, timeIn.UnixNano()/1000%1000)
    42  		return nil
    43  	})
    44  	defer r()
    45  
    46  	err = osutil.SetTime(timeIn)
    47  	c.Assert(err, IsNil)
    48  }