1// Copyright 2019 Google LLC 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 7package main 8 9import ( 10 "errors" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 "strings" 15 "testing" 16 17 "filippo.io/age" 18) 19 20func TestVectors(t *testing.T) { 21 defaultIDs, err := parseIdentitiesFile("testdata/default_key.txt") 22 if err != nil { 23 t.Fatal(err) 24 } 25 password, err := ioutil.ReadFile("testdata/default_password.txt") 26 if err == nil { 27 p := strings.TrimSpace(string(password)) 28 i, err := age.NewScryptIdentity(p) 29 if err != nil { 30 t.Fatal(err) 31 } 32 defaultIDs = append(defaultIDs, i) 33 } 34 35 files, _ := filepath.Glob("testdata/*.age") 36 for _, f := range files { 37 _, name := filepath.Split(f) 38 name = strings.TrimSuffix(name, ".age") 39 expectFailure := strings.HasPrefix(name, "fail_") 40 expectNoMatch := strings.HasPrefix(name, "nomatch_") 41 t.Run(name, func(t *testing.T) { 42 identities := defaultIDs 43 ids, err := parseIdentitiesFile("testdata/" + name + "_key.txt") 44 if err == nil { 45 identities = ids 46 } 47 password, err := ioutil.ReadFile("testdata/" + name + "_password.txt") 48 if err == nil { 49 p := strings.TrimSpace(string(password)) 50 i, err := age.NewScryptIdentity(p) 51 if err != nil { 52 t.Fatal(err) 53 } 54 identities = []age.Identity{i} 55 } 56 57 in, err := os.Open("testdata/" + name + ".age") 58 if err != nil { 59 t.Fatal(err) 60 } 61 r, err := age.Decrypt(in, identities...) 62 if expectFailure { 63 if err == nil { 64 t.Fatal("expected Decrypt failure") 65 } 66 if e := (&age.NoIdentityMatchError{}); errors.As(err, &e) { 67 t.Errorf("got ErrIncorrectIdentity, expected more specific error") 68 } 69 } else if expectNoMatch { 70 if err == nil { 71 t.Fatal("expected Decrypt failure") 72 } 73 if e := (&age.NoIdentityMatchError{}); !errors.As(err, &e) { 74 t.Errorf("expected ErrIncorrectIdentity, got %v", err) 75 } 76 } else { 77 if err != nil { 78 t.Fatal(err) 79 } 80 out, err := ioutil.ReadAll(r) 81 if err != nil { 82 t.Fatal(err) 83 } 84 t.Logf("%s", out) 85 } 86 }) 87 } 88} 89