github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/rtc/rtc_linux_test.go (about) 1 // Copyright 2021 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rtc 6 7 import ( 8 "os" 9 "runtime" 10 "testing" 11 "time" 12 13 "github.com/mvdan/u-root-coreutils/pkg/testutil" 14 "golang.org/x/sys/unix" 15 ) 16 17 type testSyscalls struct{} 18 19 func (tsc testSyscalls) ioctlGetRTCTime(fd int) (*unix.RTCTime, error) { 20 return &unix.RTCTime{}, nil 21 } 22 23 func (tsc testSyscalls) ioctlSetRTCTime(fd int, time *unix.RTCTime) error { 24 return nil 25 } 26 27 func OpenMockRTC() (*RTC, error) { 28 var tsc testSyscalls 29 f, err := os.CreateTemp(os.TempDir(), "rtc-*") 30 if err != nil { 31 return nil, err 32 } 33 return &RTC{ 34 f, 35 tsc, 36 }, nil 37 } 38 39 func TestOpenRTC(t *testing.T) { 40 rtc, err := OpenMockRTC() 41 if err != nil { 42 t.Errorf("OpenRTC got: %v; want nil", err) 43 } 44 if err := rtc.Close(); err != nil { 45 t.Errorf("Failed to close RTC: %v", err) 46 } 47 } 48 49 func TestOpenRealRTC(t *testing.T) { 50 testutil.SkipIfNotRoot(t) 51 // The u-root amd64 VM does not seem to have a RTC device 52 if runtime.GOARCH == "amd64" { 53 t.Skip("Test not supported in amd64 Qemu") 54 } 55 rtc, err := OpenRTC() 56 if err != nil { 57 t.Errorf("OpenRTC got: %v; want nil", err) 58 } 59 if err := rtc.Close(); err != nil { 60 t.Errorf("Failed to close RTC: %v", err) 61 } 62 } 63 64 func TestSet(t *testing.T) { 65 rtc, err := OpenMockRTC() 66 if err != nil { 67 t.Errorf("Error opening RTC: %v", err) 68 } 69 if err := rtc.Set(time.Now()); err != nil { 70 t.Errorf("rtc.Set got: %v; want nil", err) 71 } 72 if err := rtc.Close(); err != nil { 73 t.Errorf("Failed to close RTC: %v", err) 74 } 75 } 76 77 func TestRead(t *testing.T) { 78 rtc, err := OpenMockRTC() 79 if err != nil { 80 t.Errorf("Error opening RTC: %v", err) 81 } 82 if _, err = rtc.Read(); err != nil { 83 t.Errorf("rtc.Read got: %v; want nil", err) 84 } 85 if err := rtc.Close(); err != nil { 86 t.Errorf("Failed to close RTC: %v", err) 87 } 88 }