|
| 1 | +package prompter |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +// creates a fake runner so we can perform unit tests |
| 11 | +type FakePinentryRunner struct { |
| 12 | + HasRun bool |
| 13 | + FakeOutput string |
| 14 | + FakePinentryOutput string |
| 15 | + PassedInput string |
| 16 | +} |
| 17 | + |
| 18 | +func (f *FakePinentryRunner) Run(cmd string) (string, error) { |
| 19 | + f.PassedInput = cmd |
| 20 | + f.HasRun = true |
| 21 | + if f.FakeOutput != "" { |
| 22 | + return f.FakeOutput, nil |
| 23 | + } |
| 24 | + if f.FakePinentryOutput != "" { |
| 25 | + reader := strings.NewReader(f.FakePinentryOutput) |
| 26 | + return ParseResults(reader) |
| 27 | + } |
| 28 | + return f.FakeOutput, nil |
| 29 | +} |
| 30 | + |
| 31 | +// FakeDefaultPrompter is a Mock prompter |
| 32 | +type FakeDefaultPrompter struct { |
| 33 | + CalledRequestSecurityCode bool |
| 34 | + CalledChoose bool |
| 35 | + CalledChooseWithDefault bool |
| 36 | + CalledString bool |
| 37 | + CalledStringRequired bool |
| 38 | + CalledPassword bool |
| 39 | +} |
| 40 | + |
| 41 | +// all the functions to implement the Prompter interface |
| 42 | +func (f *FakeDefaultPrompter) RequestSecurityCode(p string) string { |
| 43 | + f.CalledRequestSecurityCode = true |
| 44 | + return "" |
| 45 | +} |
| 46 | +func (f *FakeDefaultPrompter) Choose(p string, option []string) int { |
| 47 | + f.CalledChoose = true |
| 48 | + return 0 |
| 49 | +} |
| 50 | +func (f *FakeDefaultPrompter) ChooseWithDefault(p string, d string, c []string) (string, error) { |
| 51 | + f.CalledChooseWithDefault = true |
| 52 | + return "", nil |
| 53 | +} |
| 54 | +func (f *FakeDefaultPrompter) String(p string, defaultValue string) string { |
| 55 | + f.CalledString = true |
| 56 | + return "" |
| 57 | +} |
| 58 | +func (f *FakeDefaultPrompter) StringRequired(p string) string { |
| 59 | + f.CalledStringRequired = true |
| 60 | + return "" |
| 61 | +} |
| 62 | +func (f *FakeDefaultPrompter) Password(p string) string { |
| 63 | + f.CalledPassword = true |
| 64 | + return "" |
| 65 | +} |
| 66 | + |
| 67 | +func TestValidateAndSetPrompterShouldFailWithWrongInput(t *testing.T) { |
| 68 | + |
| 69 | + // backing up the current prompters for the other tests |
| 70 | + oldPrompter := ActivePrompter |
| 71 | + defer func() { ActivePrompter = oldPrompter }() |
| 72 | + |
| 73 | + errPrompts := []string{"abcde", "invalid", "prompt", " ", "pinentryfake"} |
| 74 | + for _, errPrompt := range errPrompts { |
| 75 | + err := ValidateAndSetPrompter(errPrompt) |
| 76 | + assert.Error(t, err) |
| 77 | + } |
| 78 | + |
| 79 | +} |
| 80 | +func TestValidateAndSetPrompterShouldWorkWithInputForCli(t *testing.T) { |
| 81 | + |
| 82 | + // backing up the current prompters for the other tests |
| 83 | + oldPrompter := ActivePrompter |
| 84 | + defer func() { ActivePrompter = oldPrompter }() |
| 85 | + |
| 86 | + errPrompts := []string{"", "default", "survey"} |
| 87 | + for _, errPrompt := range errPrompts { |
| 88 | + err := ValidateAndSetPrompter(errPrompt) |
| 89 | + assert.NoError(t, err) |
| 90 | + assert.IsType(t, ActivePrompter, NewCli()) |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | +func TestValidateAndSetPrompterShouldWorkWithInputForPinentry(t *testing.T) { |
| 95 | + |
| 96 | + // backing up the current prompters for the other tests |
| 97 | + oldPrompter := ActivePrompter |
| 98 | + defer func() { ActivePrompter = oldPrompter }() |
| 99 | + |
| 100 | + errPrompts := []string{"pinentry", "pinentry-tty", "pinentry-mac", "pinentry-gnome3"} |
| 101 | + for _, errPrompt := range errPrompts { |
| 102 | + err := ValidateAndSetPrompter(errPrompt) |
| 103 | + assert.NoError(t, err) |
| 104 | + assert.IsType(t, ActivePrompter, &PinentryPrompter{}) |
| 105 | + } |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +func TestChecksPinentryPrompterDefault(t *testing.T) { |
| 110 | + p := &PinentryPrompter{} |
| 111 | + fakeDefaultPrompter := &FakeDefaultPrompter{} |
| 112 | + p.DefaultPrompter = fakeDefaultPrompter |
| 113 | + |
| 114 | + _ = p.Choose("random", []string{"1", "2"}) |
| 115 | + assert.True(t, fakeDefaultPrompter.CalledChoose) |
| 116 | + |
| 117 | + _, _ = p.ChooseWithDefault("random", "random", []string{"1", "2"}) |
| 118 | + assert.True(t, fakeDefaultPrompter.CalledChooseWithDefault) |
| 119 | + |
| 120 | + _ = p.String("random", "random") |
| 121 | + assert.True(t, fakeDefaultPrompter.CalledString) |
| 122 | + |
| 123 | + _ = p.StringRequired("random") |
| 124 | + assert.True(t, fakeDefaultPrompter.CalledStringRequired) |
| 125 | + |
| 126 | + _ = p.Password("random") |
| 127 | + assert.True(t, fakeDefaultPrompter.CalledPassword) |
| 128 | +} |
| 129 | + |
| 130 | +func TestChecksPinentryPrompterCallsPinentryForRequestSecurityCode(t *testing.T) { |
| 131 | + p := &PinentryPrompter{} |
| 132 | + runner := &FakePinentryRunner{} |
| 133 | + p.Runner = runner |
| 134 | + runner.FakeOutput = "random_code" |
| 135 | + pinentryOutput := p.RequestSecurityCode("000000") |
| 136 | + |
| 137 | + assert.True(t, runner.HasRun) |
| 138 | + assert.Equal(t, pinentryOutput, "random_code") |
| 139 | + assert.Equal(t, runner.PassedInput, "SETPROMPT Security token [000000]\nGETPIN\n") |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +func TestChecksPinentryPrompterReturnsRightCodeGivenFakePinentryOutput(t *testing.T) { |
| 144 | + p := &PinentryPrompter{} |
| 145 | + runner := &FakePinentryRunner{} |
| 146 | + p.Runner = runner |
| 147 | + runner.FakePinentryOutput = "OK This line should get ignored\nOK This line should too\nD 654321\nOK Final\n" |
| 148 | + pinentryOutput := p.RequestSecurityCode("000000") |
| 149 | + |
| 150 | + assert.True(t, runner.HasRun) |
| 151 | + assert.Equal(t, pinentryOutput, "654321") |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +func TestChecksPinentryPrompterReturnsNoCodeGivenErroneousFakePinentryOutput(t *testing.T) { |
| 156 | + p := &PinentryPrompter{} |
| 157 | + runner := &FakePinentryRunner{} |
| 158 | + p.Runner = runner |
| 159 | + runner.FakePinentryOutput = "OK This line should get ignored\nOK This line should too\nERR This is an error\nD 654321\nOK Final\n" |
| 160 | + pinentryOutput := p.RequestSecurityCode("000000") |
| 161 | + |
| 162 | + assert.True(t, runner.HasRun) |
| 163 | + assert.Equal(t, pinentryOutput, "") |
| 164 | +} |
| 165 | + |
| 166 | +func TestParseOutputShouldThrowError(t *testing.T) { |
| 167 | + |
| 168 | + input := strings.NewReader("OK Ignore this line\nERR This is an error\nD This result should be ignored\nOK this is irrelevant\n") |
| 169 | + output, err := ParseResults(input) |
| 170 | + |
| 171 | + assert.Error(t, err) |
| 172 | + assert.Equal(t, output, "") |
| 173 | +} |
| 174 | + |
| 175 | +func TestParseOutputShouldReturnCorrectOutput(t *testing.T) { |
| 176 | + |
| 177 | + input := strings.NewReader("OK Ignore this line\nD THISISTHERETURN\nOK this is irrelevant\n") |
| 178 | + output, err := ParseResults(input) |
| 179 | + |
| 180 | + assert.NoError(t, err) |
| 181 | + assert.Equal(t, output, "THISISTHERETURN") |
| 182 | +} |
0 commit comments