66 "testing"
77 "time"
88
9- "github.com/fearful-symmetry/gomsr "
9+ "github.com/micgor32/go-msr "
1010 "github.com/u-root/cpuid"
1111)
1212
@@ -28,7 +28,15 @@ func TestReadMSR(t *testing.T) {
2828 if os .Getenv ("RUN_IN_QEMU" ) != "TRUE" {
2929 t .Skip ("Not running on QEMU" )
3030 }
31- _ , err := gomsr .MSR (0 )
31+ err := msr .MSR (0 , func (dev msr.MSRDev ) error {
32+ // This closure is dummy, and left here only to
33+ // satisfy msr.MSR function definition. We can
34+ // safelu return nil here, as the function won't
35+ // ever reach the point of returning the clousure
36+ // if the caller permissions are not sufficient to
37+ // read /dev/cpu/n/msr
38+ return nil
39+ })
3240 if err != nil {
3341 t .Skip ("Not enough permissions to do test" )
3442 }
@@ -39,67 +47,85 @@ func TestReadMSR(t *testing.T) {
3947 {"IA32_PLATFORM_ID" , MsrPlatformID },
4048 }
4149 for _ , test := range tests {
42- vals := h .ReadMSR (test .msr )
43-
44- for iterator , value := range vals {
45- if iterator < len (vals ) && value != vals [iterator + 1 ] {
46- if value != vals [iterator + 1 ] {
47- t .Errorf ("MSR value are not the same for all cores. Core: %d, Value: 0x%x, Previous value: 0x%x" , iterator , value , vals [iterator + 1 ])
48- }
49- if value == 0 || vals [iterator + 1 ] == 0xffffffffffffffff {
50- t .Errorf ("ReadMSR got unexpected value for MSR %s %v" , test .name , vals )
51- }
52- }
50+ val := h .ReadMSR (test .msr )
51+
52+ if val == 0xff {
53+ t .Errorf ("MSR value for %s is not being read correctly." , test .name )
5354 }
55+
56+ // We can't check the consistency since ReadMSR reads only from core 0,
57+ // but we can check whether SOME value other than 0xff is being read.
58+ // Left for previous test for reference.
59+ // for iterator, value := range vals {
60+ // if iterator < len(vals) && value != vals[iterator+1] {
61+ // if value != vals[iterator+1] {
62+ // t.Errorf("MSR value are not the same for all cores. Core: %d, Value: 0x%x, Previous value: 0x%x", iterator, value, vals[iterator+1])
63+ // }
64+ // if value == 0 || vals[iterator+1] == 0xffffffffffffffff {
65+ // t.Errorf("ReadMSR got unexpected value for MSR %s %v", test.name, vals)
66+ // }
67+ // }
68+ // }
5469 }
5570
5671}
5772
5873func TestReadMSRTimeStampCounter (t * testing.T ) {
5974 h := GetAPI ()
60- _ , err := gomsr .MSR (0 )
75+ err := msr .MSR (0 , func (dev msr.MSRDev ) error {
76+ return nil
77+ })
78+
6179 if err != nil {
6280 t .Skip ("Not enough permissions to do test" )
6381 }
6482 if runtime .GOARCH == "amd64" && cpuid .HasFeature (cpuid .TSC ) {
6583 timestamp1 := h .ReadMSR (TimeStampCounter )
6684
67- if timestamp1 [ 0 ] == 0 {
85+ if timestamp1 == 0 {
6886 t .Errorf ("ReadMSR got unexpected value for MSR IA32_TIMESTAMP_COUNTER: %v" , timestamp1 )
6987 }
7088 time .Sleep (time .Millisecond )
7189 timestamp2 := h .ReadMSR (TimeStampCounter )
72- if timestamp2 [ 0 ] == 0 {
90+ if timestamp2 == 0 {
7391 t .Errorf ("ReadMSR got unexpected value for MSR IA32_TIMESTAMP_COUNTER: %v" , timestamp2 )
7492 }
75- if timestamp1 [ 0 ] == timestamp2 [ 0 ] {
93+ if timestamp1 == timestamp2 {
7694 t .Errorf ("Timestamps are equal, but shouldn't be" )
7795 }
7896 }
7997}
8098
8199func TestReadMSREFER (t * testing.T ) {
82100 h := GetAPI ()
83- _ , err := gomsr .MSR (0 )
101+
102+ err := msr .MSR (0 , func (dev msr.MSRDev ) error {
103+ return nil
104+ })
84105 if err != nil {
85106 t .Skip ("Not enough permissions to do test" )
86107 }
87108 if runtime .GOARCH == "amd64" {
88- vals := h .ReadMSR (Ia32Efer )
89-
90- for iterator , value := range vals {
91- if iterator < len (vals ) && value != vals [iterator + 1 ] {
92- t .Errorf ("MSR value are not the same for all cores. Core: %d, Value: 0x%x, Next value: 0x%x" , iterator , value , vals [iterator + 1 ])
93- }
109+ val := h .ReadMSR (Ia32Efer )
110+ if val == 0xff {
111+ t .Error ("MSR value is not being read correctly." )
94112 }
113+
114+ // Left for previous test for reference.
115+ // for iterator, value := range vals {
116+ // if iterator < len(vals) && value != vals[iterator+1] {
117+ // t.Errorf("MSR value are not the same for all cores. Core: %d, Value: 0x%x, Next value: 0x%x", iterator, value, vals[iterator+1])
118+ // }
119+ // }
95120 }
96121}
97122
98123func TestSMRR (t * testing.T ) {
99-
100124 h := GetAPI ()
101125
102- _ , err := gomsr .MSR (0 )
126+ err := msr .MSR (0 , func (dev msr.MSRDev ) error {
127+ return nil
128+ })
103129 if err != nil {
104130 t .Skip ("Not enough permissions to do test" )
105131 }
0 commit comments