| 1 | // Copyright 2023 The age Authors |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style |
| 4 | // license that can be found in the LICENSE file or at |
| 5 | // https://developers.google.com/open-source/licenses/bsd |
| 6 | |
| 7 | package plugin |
| 8 | |
| 9 | import ( |
| 10 | "bufio" |
| 11 | "bytes" |
| 12 | "errors" |
| 13 | "io" |
| 14 | "os" |
| 15 | "os/exec" |
| 16 | "path/filepath" |
| 17 | "runtime" |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "filippo.io/age" |
| 22 | "filippo.io/age/internal/bech32" |
| 23 | "filippo.io/age/internal/format" |
| 24 | ) |
| 25 | |
| 26 | func TestMain(m *testing.M) { |
| 27 | switch filepath.Base(os.Args[0]) { |
| 28 | case "age-plugin-test": |
| 29 | p, _ := New("test") |
| 30 | p.HandleRecipient(func(data []byte) (age.Recipient, error) { |
| 31 | return testRecipient{}, nil |
| 32 | }) |
| 33 | os.Exit(p.Main()) |
| 34 | case "age-plugin-testpqc": |
| 35 | p, _ := New("testpqc") |
| 36 | p.HandleRecipient(func(data []byte) (age.Recipient, error) { |
| 37 | return testPQCRecipient{}, nil |
| 38 | }) |
| 39 | os.Exit(p.Main()) |
| 40 | case "age-plugin-error": |
| 41 | p, _ := New("error") |
| 42 | p.HandleRecipient(func(data []byte) (age.Recipient, error) { |
| 43 | return nil, errors.New("oh my, an error occurred") |
| 44 | }) |
| 45 | p.HandleIdentity(func(data []byte) (age.Identity, error) { |
| 46 | return nil, errors.New("oh my, an error occurred") |
| 47 | }) |
| 48 | os.Exit(p.Main()) |
| 49 | default: |
| 50 | os.Exit(m.Run()) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | type testRecipient struct{} |
| 55 | |
| 56 | func (testRecipient) Wrap(fileKey []byte) ([]*age.Stanza, error) { |
| 57 | return []*age.Stanza{{Type: "test", Body: fileKey}}, nil |
| 58 | } |
| 59 | |
| 60 | type testPQCRecipient struct{} |
| 61 | |
| 62 | var _ age.RecipientWithLabels = testPQCRecipient{} |
| 63 | |
| 64 | func (testPQCRecipient) Wrap(fileKey []byte) ([]*age.Stanza, error) { |
| 65 | return []*age.Stanza{{Type: "test", Body: fileKey}}, nil |
| 66 | } |
| 67 | |
| 68 | func (testPQCRecipient) WrapWithLabels(fileKey []byte) ([]*age.Stanza, []string, error) { |
| 69 | return []*age.Stanza{{Type: "test", Body: fileKey}}, []string{"postquantum"}, nil |
| 70 | } |
| 71 | |
| 72 | func TestIdentityV1NegativeIndex(t *testing.T) { |
| 73 | p, err := New("test") |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | p.HandleIdentity(func([]byte) (age.Identity, error) { return nil, nil }) |
| 78 | stderr := &bytes.Buffer{} |
| 79 | p.SetIO(strings.NewReader("-> recipient-stanza -1 X25519\n\n"), io.Discard, stderr) |
| 80 | if code := p.IdentityV1(); code != 1 { |
| 81 | t.Errorf("exit code = %d, want 1", code) |
| 82 | } |
| 83 | want := "unexpected file index -1, previous was -1" |
| 84 | if got := stderr.String(); got != want { |
| 85 | t.Errorf("stderr = %q, want %q", got, want) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestPluginNameCase(t *testing.T) { |
| 90 | p, err := New("MixedCase") |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | if p.Name() != "mixedcase" { |
| 95 | t.Errorf("Plugin.Name = %q", p.Name()) |
| 96 | } |
| 97 | id, err := NewIdentityWithoutData("MixedCase", &ClientUI{}) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | if id.Name() != "mixedcase" { |
| 102 | t.Errorf("Identity.Name = %q", id.Name()) |
| 103 | } |
| 104 | if _, err := New("\u212a"); err == nil { |
| 105 | t.Error("New accepted a non-ASCII name") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestRequestValueEmpty(t *testing.T) { |
| 110 | p, err := New("test") |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | p.SetIO(nil, io.Discard, io.Discard) |
| 115 | p.sr = format.NewStanzaReader(bufio.NewReader(strings.NewReader("-> ok\n\n"))) |
| 116 | value, err := p.RequestValue("prompt", true) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if value != "" { |
| 121 | t.Errorf("value = %q", value) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestLabels(t *testing.T) { |
| 126 | if runtime.GOOS == "windows" { |
| 127 | t.Skip("Windows support is TODO") |
| 128 | } |
| 129 | temp := t.TempDir() |
| 130 | testOnlyPluginPath = temp |
| 131 | t.Cleanup(func() { testOnlyPluginPath = "" }) |
| 132 | ex, err := os.Executable() |
| 133 | if err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | if err := os.Link(ex, filepath.Join(temp, "age-plugin-test")); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | if err := os.Chmod(filepath.Join(temp, "age-plugin-test"), 0755); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | if err := os.Link(ex, filepath.Join(temp, "age-plugin-testpqc")); err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | if err := os.Chmod(filepath.Join(temp, "age-plugin-testpqc"), 0755); err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | |
| 149 | name, err := bech32.Encode("age1test", nil) |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | testPlugin, err := NewRecipient(name, &ClientUI{}) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | namePQC, err := bech32.Encode("age1testpqc", nil) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | testPluginPQC, err := NewRecipient(namePQC, &ClientUI{}) |
| 162 | if err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | |
| 166 | if _, err := age.Encrypt(io.Discard, testPluginPQC); err != nil { |
| 167 | t.Errorf("expected one pqc to work, got %v", err) |
| 168 | } |
| 169 | if _, err := age.Encrypt(io.Discard, testPluginPQC, testPluginPQC); err != nil { |
| 170 | t.Errorf("expected two pqc to work, got %v", err) |
| 171 | } |
| 172 | if _, err := age.Encrypt(io.Discard, testPluginPQC, testPlugin); err == nil { |
| 173 | t.Errorf("expected one pqc and one normal to fail") |
| 174 | } |
| 175 | if _, err := age.Encrypt(io.Discard, testPlugin, testPluginPQC); err == nil { |
| 176 | t.Errorf("expected one pqc and one normal to fail") |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func TestNotFound(t *testing.T) { |
| 181 | if runtime.GOOS == "windows" { |
| 182 | t.Skip("Windows support is TODO") |
| 183 | } |
| 184 | |
| 185 | r := EncodeRecipient("nonexistentplugin", nil) |
| 186 | t.Log(r) |
| 187 | testPluginRecipient, err := NewRecipient(r, &ClientUI{}) |
| 188 | if err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | var e *NotFoundError |
| 192 | if _, err := age.Encrypt(io.Discard, testPluginRecipient); err == nil { |
| 193 | t.Errorf("expected error for nonexistent plugin") |
| 194 | } else if !errors.As(err, &e) { |
| 195 | t.Errorf("expected NotFoundError, got %T: %v", err, err) |
| 196 | } else if e.Name != "nonexistentplugin" { |
| 197 | t.Errorf("expected NotFoundError.Name to be nonexistentplugin, got %q", e.Name) |
| 198 | } else if !errors.Is(err, exec.ErrNotFound) { |
| 199 | t.Errorf("expected error to wrap exec.ErrNotFound, got: %v", err) |
| 200 | } |
| 201 | |
| 202 | buf := &bytes.Buffer{} |
| 203 | id, err := age.GenerateHybridIdentity() |
| 204 | if err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | w, err := age.Encrypt(buf, id.Recipient()) |
| 208 | if err != nil { |
| 209 | t.Fatal(err) |
| 210 | } |
| 211 | w.Close() |
| 212 | |
| 213 | i := EncodeIdentity("nonexistentplugin", nil) |
| 214 | t.Log(i) |
| 215 | testPluginIdentity, err := NewIdentity(i, &ClientUI{}) |
| 216 | if err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | if _, err := age.Decrypt(buf, testPluginIdentity); err == nil { |
| 220 | t.Errorf("expected error for nonexistent plugin") |
| 221 | } else if errors.As(err, new(*age.NoIdentityMatchError)) { |
| 222 | t.Errorf("expected NotFoundError, got NoIdentityMatchError: %v", err) |
| 223 | } else if !errors.As(err, &e) { |
| 224 | t.Errorf("expected NotFoundError, got %T: %v", err, err) |
| 225 | } else if e.Name != "nonexistentplugin" { |
| 226 | t.Errorf("expected NotFoundError.Name to be nonexistentplugin, got %q", e.Name) |
| 227 | } else if !errors.Is(err, exec.ErrNotFound) { |
| 228 | t.Errorf("expected error to wrap exec.ErrNotFound, got: %v", err) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestPluginError(t *testing.T) { |
| 233 | if runtime.GOOS == "windows" { |
| 234 | t.Skip("Windows support is TODO") |
| 235 | } |
| 236 | temp := t.TempDir() |
| 237 | testOnlyPluginPath = temp |
| 238 | t.Cleanup(func() { testOnlyPluginPath = "" }) |
| 239 | ex, err := os.Executable() |
| 240 | if err != nil { |
| 241 | t.Fatal(err) |
| 242 | } |
| 243 | if err := os.Link(ex, filepath.Join(temp, "age-plugin-error")); err != nil { |
| 244 | t.Fatal(err) |
| 245 | } |
| 246 | if err := os.Chmod(filepath.Join(temp, "age-plugin-error"), 0755); err != nil { |
| 247 | t.Fatal(err) |
| 248 | } |
| 249 | |
| 250 | r := EncodeRecipient("error", nil) |
| 251 | testPluginRecipient, err := NewRecipient(r, &ClientUI{}) |
| 252 | if err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | if _, err := age.Encrypt(io.Discard, testPluginRecipient); err == nil { |
| 256 | t.Errorf("expected error from plugin") |
| 257 | } else if !strings.Contains(err.Error(), "oh my, an error occurred") { |
| 258 | t.Errorf("expected plugin error, got: %v", err) |
| 259 | } |
| 260 | |
| 261 | buf := &bytes.Buffer{} |
| 262 | id, err := age.GenerateHybridIdentity() |
| 263 | if err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | w, err := age.Encrypt(buf, id.Recipient()) |
| 267 | if err != nil { |
| 268 | t.Fatal(err) |
| 269 | } |
| 270 | w.Close() |
| 271 | |
| 272 | i := EncodeIdentity("error", nil) |
| 273 | testPluginIdentity, err := NewIdentity(i, &ClientUI{}) |
| 274 | if err != nil { |
| 275 | t.Fatal(err) |
| 276 | } |
| 277 | if _, err := age.Decrypt(buf, testPluginIdentity); err == nil { |
| 278 | t.Errorf("expected error from plugin") |
| 279 | } else if !strings.Contains(err.Error(), "oh my, an error occurred") { |
| 280 | t.Errorf("expected plugin error, got: %v", err) |
| 281 | } |
| 282 | } |