package main import ( "strings" "testing" ) func TestTheManifestIsThePackageAndNothingElse(t *testing.T) { m := readManifest(t) holdsTheBundle(t, m, "xclip") if got := strings.Join(m.packages(), ","); got != "xclip" || len(m.Resources) != 1 { t.Errorf("packages %s, resources %v", got, m.Resources) } } // aSession gives the tools an X session on :1 with the account's cookie. func aSession(t *testing.T) { t.Helper() _, sockets := aMachine(t, map[string]string{"DISPLAY": ":1", "XAUTHORITY": "/home/op/.Xauthority"}) aSocket(t, sockets, "X1") } func TestCopyRunsDetachedInTheSessionWithTheTextOnItsInput(t *testing.T) { aSession(t) f := using(t, func(string, Cmd) Result { return ok("") }) got, err := Copy("hello", "clipboard", "") if err != nil || got.Bytes != 5 || got.Type != "UTF8_STRING" || got.Session.Display != ":1" { t.Fatalf("%+v %v", got, err) } c := f.asked[0] if f.lines()[0] != "xclip -selection clipboard -in" || c.Stdin != "hello" || !c.Detached { t.Errorf("%v %+v", f.lines(), c) } if strings.Join(c.Env, " ") != "DISPLAY=:1 XAUTHORITY=/home/op/.Xauthority" { t.Errorf("env %v", c.Env) } if _, err := Copy("x", "primary", "text/html"); err != nil || f.lines()[1] != "xclip -selection primary -in -t text/html" { t.Errorf("%v %v", f.lines(), err) } if _, err := Copy(strings.Repeat("a", MostCopy+1), "clipboard", ""); err == nil { t.Error("more than 1 MiB") } if _, err := Copy("x", "clipboard", "-o"); err == nil { t.Error("an option as a type") } } func TestPasteAnswersTextBase64OrEmpty(t *testing.T) { aSession(t) answer := Result{Stdout: "some text"} f := using(t, func(string, Cmd) Result { return answer }) got, err := Paste("clipboard", "") if err != nil || got.Text != "some text" || got.Bytes != 9 || got.Empty { t.Fatalf("%+v %v", got, err) } if f.lines()[0] != "xclip -selection clipboard -out" || f.asked[0].Detached { t.Errorf("%v", f.lines()) } answer = Result{Stdout: "\x89PNG\r\n\x1a\n\xff"} got, _ = Paste("clipboard", "image/png") if got.Text != "" || got.Base64 == "" { t.Errorf("binary: %+v", got) } answer = Result{Status: 1, Stderr: "Error: target UTF8_STRING not available\n"} got, err = Paste("clipboard", "") if err != nil || !got.Empty { t.Errorf("an empty clipboard is an answer: %+v %v", got, err) } } func TestADisplayThatRefusesIsSaidWithHowItWasFound(t *testing.T) { aSession(t) using(t, func(string, Cmd) Result { return Result{Status: 1, Stderr: "Error: Can't open display: :1\n"} }) _, err := Paste("clipboard", "") if err == nil || !strings.Contains(err.Error(), "refused the connection") || !strings.Contains(err.Error(), "environment") { t.Fatalf("%v", err) } } func TestWithoutASessionNothingRunsAndTheToolSaysWhy(t *testing.T) { aMachine(t, map[string]string{}) f := using(t, func(string, Cmd) Result { return ok("") }) if _, err := Copy("x", "clipboard", ""); err == nil || !strings.Contains(err.Error(), "no graphical session") { t.Fatalf("%v", err) } got, err := SessionCheck() if err != nil || got["found"] != false || !strings.Contains(got["why"].(string), "logged in") { t.Fatalf("%v %v", got, err) } if len(f.asked) != 0 { t.Errorf("nothing runs without a session: %v", f.lines()) } } func TestTargetsListWhatASelectionOffers(t *testing.T) { aSession(t) using(t, func(string, Cmd) Result { return ok("TIMESTAMP\nTARGETS\nUTF8_STRING\ntext/html\n") }) got, err := Targets("clipboard") if err != nil || len(got["targets"].([]string)) != 4 { t.Fatalf("%v %v", got, err) } s, _ := SessionCheck() if s["answers"] != true { t.Errorf("%v", s) } }