| 1 | // Copyright 2019 The age Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package age_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "crypto/rand" |
| 10 | "io" |
| 11 | "testing" |
| 12 | |
| 13 | "filippo.io/age" |
| 14 | ) |
| 15 | |
| 16 | func TestX25519RoundTrip(t *testing.T) { |
| 17 | i, err := age.GenerateX25519Identity() |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | r := i.Recipient() |
| 22 | |
| 23 | if r1, err := age.ParseX25519Recipient(r.String()); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } else if r1.String() != r.String() { |
| 26 | t.Errorf("recipient did not round-trip through parsing: got %q, want %q", r1, r) |
| 27 | } |
| 28 | if i1, err := age.ParseX25519Identity(i.String()); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } else if i1.String() != i.String() { |
| 31 | t.Errorf("identity did not round-trip through parsing: got %q, want %q", i1, i) |
| 32 | } |
| 33 | |
| 34 | fileKey := make([]byte, 16) |
| 35 | if _, err := rand.Read(fileKey); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | stanzas, err := r.Wrap(fileKey) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | |
| 43 | out, err := i.Unwrap(stanzas) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | |
| 48 | if !bytes.Equal(fileKey, out) { |
| 49 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestHybridRoundTrip(t *testing.T) { |
| 54 | i, err := age.GenerateHybridIdentity() |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | r := i.Recipient() |
| 59 | |
| 60 | if r1, err := age.ParseHybridRecipient(r.String()); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } else if r1.String() != r.String() { |
| 63 | t.Errorf("recipient did not round-trip through parsing: got %q, want %q", r1, r) |
| 64 | } |
| 65 | if i1, err := age.ParseHybridIdentity(i.String()); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } else if i1.String() != i.String() { |
| 68 | t.Errorf("identity did not round-trip through parsing: got %q, want %q", i1, i) |
| 69 | } |
| 70 | |
| 71 | fileKey := make([]byte, 16) |
| 72 | if _, err := rand.Read(fileKey); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | stanzas, err := r.Wrap(fileKey) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | |
| 80 | out, err := i.Unwrap(stanzas) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | |
| 85 | if !bytes.Equal(fileKey, out) { |
| 86 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestHybridMixingRestrictions(t *testing.T) { |
| 91 | x25519, err := age.GenerateX25519Identity() |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | hybrid, err := age.GenerateHybridIdentity() |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | |
| 100 | // Hybrid recipients can be used together. |
| 101 | if _, err := age.Encrypt(io.Discard, hybrid.Recipient(), hybrid.Recipient()); err != nil { |
| 102 | t.Errorf("expected two hybrid recipients to work, got %v", err) |
| 103 | } |
| 104 | |
| 105 | // Hybrid and X25519 recipients cannot be mixed. |
| 106 | if _, err := age.Encrypt(io.Discard, hybrid.Recipient(), x25519.Recipient()); err == nil { |
| 107 | t.Error("expected hybrid mixed with X25519 to fail") |
| 108 | } |
| 109 | if _, err := age.Encrypt(io.Discard, x25519.Recipient(), hybrid.Recipient()); err == nil { |
| 110 | t.Error("expected X25519 mixed with hybrid to fail") |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestScryptRoundTrip(t *testing.T) { |
| 115 | password := "twitch.tv/filosottile" |
| 116 | |
| 117 | r, err := age.NewScryptRecipient(password) |
| 118 | if err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | r.SetWorkFactor(15) |
| 122 | i, err := age.NewScryptIdentity(password) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | |
| 127 | fileKey := make([]byte, 16) |
| 128 | if _, err := rand.Read(fileKey); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | stanzas, err := r.Wrap(fileKey) |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | |
| 136 | out, err := i.Unwrap(stanzas) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | |
| 141 | if !bytes.Equal(fileKey, out) { |
| 142 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 143 | } |
| 144 | } |