1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package interactive
import (
"io"
"os"
"strings"
"testing"
"github.com/sylabs/singularity/v4/internal/pkg/test"
)
func generateQuestionInput(t *testing.T, input string) (*os.File, *os.File) {
// Each line of the string represents a virtual different answer from a user
testBytes := []byte(input)
// we create a temporary file that will act as Stdin
testFile, err := os.CreateTemp("", "inputTest")
if err != nil {
t.Fatalf("failed to create temporary file: %s", err)
}
// Write the data that we will later on need
_, err = testFile.Write(testBytes)
if err != nil {
testFile.Close()
os.Remove(testFile.Name())
t.Fatalf("failed to write to %s: %s", testFile.Name(), err)
}
// Reposition to the beginning of file to ensure there is something to read
_, err = testFile.Seek(0, io.SeekStart)
if err != nil {
testFile.Close()
os.Remove(testFile.Name())
t.Fatalf("failed to seek to beginning of file %s: %s", testFile.Name(), err)
}
// Redirect Stdin
savedStdin := os.Stdin
os.Stdin = testFile
return testFile, savedStdin
}
func TestAskNYQuestion(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
const (
defaultAnswer = "default"
)
tests := []struct {
name string
input string
expectedOutput string
shallPass bool
}{
{
name: "y",
input: "y",
expectedOutput: "y",
shallPass: true,
},
{
name: "yes",
input: "yes",
expectedOutput: "y",
shallPass: true,
},
{
name: "n",
input: "n",
expectedOutput: "n",
shallPass: true,
},
{
name: "no",
input: "no",
expectedOutput: "n",
shallPass: true,
},
{
name: "default",
input: "",
expectedOutput: defaultAnswer,
shallPass: true,
},
{
name: "invalid",
input: "abc",
expectedOutput: "",
shallPass: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempFile, savedStdin := generateQuestionInput(t, tt.input)
defer tempFile.Close()
defer os.RemoveAll(tempFile.Name())
defer func() {
os.Stdin = savedStdin
}()
res, err := AskYNQuestion(defaultAnswer, "")
if tt.shallPass == true && (err != nil || res != tt.expectedOutput) {
t.Fatalf("test %s failed while expected to pass", tt.name)
}
if tt.shallPass == false && (err == nil || res != tt.expectedOutput) {
t.Fatalf("test %s passed while expected to fail", tt.name)
}
})
}
}
func TestAskNumberInRange(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
rangeStart := 0
rangeEnd := 10
tests := []struct {
name string
input string
expectedOutput int
shallPass bool
}{
{
name: "in-range number",
input: "5",
expectedOutput: 5,
shallPass: true,
},
{
name: "out of range number",
input: "12",
expectedOutput: 0,
shallPass: false,
},
{
name: "invalid type (string)",
input: "abc",
expectedOutput: 0,
shallPass: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempFile, savedStdin := generateQuestionInput(t, tt.input)
defer tempFile.Close()
defer os.RemoveAll(tempFile.Name())
defer func() {
os.Stdin = savedStdin
}()
res, err := AskNumberInRange(rangeStart, rangeEnd, "")
if tt.shallPass == true && (err != nil || res != tt.expectedOutput) {
t.Fatalf("test %s failed while expected to pass", tt.name)
}
if tt.shallPass == false && (err == nil || res != tt.expectedOutput) {
t.Fatalf("test %s passed while expected to fail", tt.name)
}
})
}
}
//nolint:dupword
func TestAskQuestion(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
// Each line of the string represents a virtual different answer from a user
testStr := "test test test\ntest2 test2\n\ntest3"
tempFile, savedStdin := generateQuestionInput(t, testStr)
defer tempFile.Close()
defer os.RemoveAll(tempFile.Name())
defer func() {
os.Stdin = savedStdin
}()
// Actual test, run the test with the first line
output, err := AskQuestion("Question test: ")
if err != nil {
t.Fatalf("failed to get response from AskQuestion(): %s", err)
}
// We analyze the result. We always make sure we do not get the '\n'
firstAnswer := testStr[:strings.Index(testStr, "\n")]
restAnswer := testStr[len(firstAnswer)+1:]
if output != firstAnswer {
t.Fatal("AskQuestion() returned", output, "instead of", firstAnswer)
}
// Test with the second line
output, err = AskQuestion("Question test 2: ")
if err != nil {
t.Fatalf("failed to get response: %s", err)
}
// We analyze the result
secondAnswer := restAnswer[:strings.Index(restAnswer, "\n")]
if output != secondAnswer {
t.Fatalf("AskQuestion() returned: %s instead of: %s", output, secondAnswer)
}
// Test with the third line
output, err = AskQuestion("Question test 3: ")
if err != nil {
t.Fatalf("failed to get response: %s", err)
}
// We analyze the result
if output != "" {
t.Fatalf("AskQuestion() returned: %s instead of an empty string", output)
}
// Test with the final line
output, err = AskQuestion("Question test 4: ")
if err != nil {
t.Fatalf("failed to get response: %s", err)
}
finalAnswer := restAnswer[len(secondAnswer)+2:] // We have to account for 2 "\n"
if output != finalAnswer {
t.Fatalf("AskQuestion() returned: %s instead of: %s", output, finalAnswer)
}
}
//nolint:dupword
func TestAskQuestionNoEcho(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
testStr := "test test\ntest2 test2 test2\n\ntest3 test3 test3 test3"
tempFile, savedStdin := generateQuestionInput(t, testStr)
defer tempFile.Close()
defer os.RemoveAll(tempFile.Name())
defer func() {
os.Stdin = savedStdin
}()
// Test AskQuestionNoEcho(), starting with the first line
output, err := AskQuestionNoEcho("Test question 1: ")
if err != nil {
t.Fatalf("failed to get output from AskQuestionNoEcho(): %s", err)
}
// Analyze the result
firstAnswer := testStr[:strings.Index(testStr, "\n")]
restAnswer := testStr[len(firstAnswer)+1:] // Ignore "\n"
if output != firstAnswer {
t.Fatalf("AskQuestionNoEcho() returned %s instead of %s", output, firstAnswer)
}
// Test with the second line
output, err = AskQuestionNoEcho("Test question 2: ")
if err != nil {
t.Fatalf("failed to get output from AskQuestionNoEcho(): %s", err)
}
// We analyze the answer
secondAnswer := restAnswer[:strings.Index(restAnswer, "\n")]
if output != secondAnswer {
t.Fatalf("AskQuestionNoEcho() returned %s instead of %s", output, secondAnswer)
}
// Test with third line
output, err = AskQuestionNoEcho("Test question 3: ")
if err != nil {
t.Fatalf("failed to get output from AskQuestionNoEcho(): %s", err)
}
// We analyze the answer
if output != "" {
t.Fatalf("AskQuestionNoEcho() returned %s instead of an empty string", output)
}
// Test with the final line
output, err = AskQuestionNoEcho("Test question 4: ")
if err != nil {
t.Fatalf("failed to get output from AskQuestionNoEcho(): %s", err)
}
finalAnswer := restAnswer[len(secondAnswer)+2:] // We have to account for 2 "\n"
if output != finalAnswer {
t.Fatalf("AskQuestionNoEcho() returned %s instead of %s", output, finalAnswer)
}
}
func TestGetPassphrase(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
tests := []struct {
name string
input string
shallPass bool
}{
{
name: "valid case",
input: "mypassphrase\nmypassphrase\n",
shallPass: true,
},
{
name: "unmatching passphrases",
input: "mypassphrase\nsomethingelse\n",
shallPass: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary file that will act as input from stdin
tempFile, savedStdin := generateQuestionInput(t, tt.input)
defer tempFile.Close()
defer os.RemoveAll(tempFile.Name())
defer func() {
os.Stdin = savedStdin
}()
pass, err := GetPassphrase("Test: ", 1)
if tt.shallPass && (err != nil || pass != "mypassphrase") {
t.Fatalf("test %s is expected to succeed but failed: %s", tt.name, err)
}
if !tt.shallPass && err == nil {
t.Fatalf("invalid case %s succeeded", tt.name)
}
})
}
}
|