|
| 1 | +/** |
| 2 | +* Copyright 2020-present Facebook. All Rights Reserved. |
| 3 | +* |
| 4 | +* This program file is free software; you can redistribute it and/or modify it |
| 5 | +* under the terms of the GNU General Public License as published by the |
| 6 | +* Free Software Foundation; version 2 of the License. |
| 7 | +* |
| 8 | +* This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 11 | +* for more details. |
| 12 | +* |
| 13 | +* You should have received a copy of the GNU General Public License |
| 14 | +* along with this program in a file named COPYING; if not, write to the |
| 15 | +* Free Software Foundation, Inc., |
| 16 | +* 51 Franklin Street, Fifth Floor, |
| 17 | +* Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package common |
| 21 | + |
| 22 | +import ( |
| 23 | + "github.com/facebook/openbmc/tools/flashy/lib/step" |
| 24 | + "github.com/facebook/openbmc/tools/flashy/lib/utils" |
| 25 | + "github.com/pkg/errors" |
| 26 | + "testing" |
| 27 | + "unsafe" |
| 28 | +) |
| 29 | + |
| 30 | +var mockMemInfo = &utils.MemInfo{ |
| 31 | + MemTotal: 120 * 1024 * 1024, |
| 32 | + MemFree: 50 * 1024 * 1024, |
| 33 | +} |
| 34 | + |
| 35 | +func TestMemtestGoodHW(t *testing.T) { |
| 36 | + memInfoOrig := utils.GetMemInfo |
| 37 | + memchekOrig := checkMemtest |
| 38 | + |
| 39 | + utils.GetMemInfo = func() (*utils.MemInfo, error) { |
| 40 | + return mockMemInfo, nil |
| 41 | + } |
| 42 | + |
| 43 | + got := runMemtestSuite(step.StepParams{}) |
| 44 | + step.CompareTestExitErrors(nil, got, t) |
| 45 | + |
| 46 | + utils.GetMemInfo = memInfoOrig |
| 47 | + checkMemtest = memchekOrig |
| 48 | +} |
| 49 | + |
| 50 | +func TestMemtestBadHW(t *testing.T) { |
| 51 | + memInfoOrig := utils.GetMemInfo |
| 52 | + memchekOrig := checkMemtest |
| 53 | + |
| 54 | + utils.GetMemInfo = func() (*utils.MemInfo, error) { |
| 55 | + return mockMemInfo, nil |
| 56 | + } |
| 57 | + |
| 58 | + checkMemtest = func(pattern byte, memtestSize uint64, wordSize uintptr, scratchpadPointer unsafe.Pointer) step.StepExitError { |
| 59 | + *(*byte)(unsafe.Pointer(uintptr(scratchpadPointer) + (uintptr(1234) * wordSize))) = 0xbb |
| 60 | + return memchekOrig(pattern, memtestSize, wordSize, scratchpadPointer) |
| 61 | + } |
| 62 | + |
| 63 | + got := runMemtestSuite(step.StepParams{}) |
| 64 | + wantMsg := errors.Errorf("Wrote pattern 0x0 to index 1234 of test buffer, read value was 0xbb. Possible memory corruption") |
| 65 | + want := step.ExitUnsafeToReboot{Err: wantMsg} |
| 66 | + step.CompareTestExitErrors(want, got, t) |
| 67 | + |
| 68 | + utils.GetMemInfo = memInfoOrig |
| 69 | + checkMemtest = memchekOrig |
| 70 | +} |
| 71 | + |
| 72 | +func TestMemtestBadHWOnLastLoop(t *testing.T) { |
| 73 | + memInfoOrig := utils.GetMemInfo |
| 74 | + memchekOrig := checkMemtest |
| 75 | + |
| 76 | + utils.GetMemInfo = func() (*utils.MemInfo, error) { |
| 77 | + return mockMemInfo, nil |
| 78 | + } |
| 79 | + |
| 80 | + checkMemtest = func(pattern byte, memtestSize uint64, wordSize uintptr, scratchpadPointer unsafe.Pointer) step.StepExitError { |
| 81 | + if pattern == 0xa5 { |
| 82 | + *(*byte)(unsafe.Pointer(uintptr(scratchpadPointer) + (uintptr(1234) * wordSize))) = 0xbb |
| 83 | + } |
| 84 | + return memchekOrig(pattern, memtestSize, wordSize, scratchpadPointer) |
| 85 | + } |
| 86 | + |
| 87 | + got := runMemtestSuite(step.StepParams{}) |
| 88 | + wantMsg := errors.Errorf("Wrote pattern 0xa5 to index 1234 of test buffer, read value was 0xbb. Possible memory corruption") |
| 89 | + want := step.ExitUnsafeToReboot{Err: wantMsg} |
| 90 | + step.CompareTestExitErrors(want, got, t) |
| 91 | + |
| 92 | + utils.GetMemInfo = memInfoOrig |
| 93 | + checkMemtest = memchekOrig |
| 94 | +} |
| 95 | + |
| 96 | +func TestMemtestNoMemory(t *testing.T) { |
| 97 | + memInfoOrig := utils.GetMemInfo |
| 98 | + memchekOrig := checkMemtest |
| 99 | + |
| 100 | + utils.GetMemInfo = func() (*utils.MemInfo, error) { |
| 101 | + return &utils.MemInfo{ |
| 102 | + MemTotal: 120 * 1024 * 1024, |
| 103 | + MemFree: 2 * 1024 * 1024, |
| 104 | + }, nil |
| 105 | + } |
| 106 | + |
| 107 | + got := runMemtestSuite(step.StepParams{}) |
| 108 | + step.CompareTestExitErrors(nil, got, t) |
| 109 | + |
| 110 | + utils.GetMemInfo = memInfoOrig |
| 111 | + checkMemtest = memchekOrig |
| 112 | +} |
0 commit comments