| 1 | package inspect |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "testing" |
| 8 | |
| 9 | "filippo.io/age/internal/format" |
| 10 | "filippo.io/age/internal/stream" |
| 11 | ) |
| 12 | |
| 13 | // buildFile serializes a header with the given stanza types, |
| 14 | // followed by the minimal valid encrypted payload (a 16-byte stream nonce |
| 15 | // and a single empty ChaCha20-Poly1305 chunk). |
| 16 | func buildFile(t *testing.T, stanzaTypes ...string) []byte { |
| 17 | t.Helper() |
| 18 | hdr := &format.Header{MAC: make([]byte, 32)} |
| 19 | for _, stanzaType := range stanzaTypes { |
| 20 | hdr.Recipients = append(hdr.Recipients, &format.Stanza{Type: stanzaType}) |
| 21 | } |
| 22 | buf := &bytes.Buffer{} |
| 23 | if err := hdr.Marshal(buf); err != nil { |
| 24 | t.Fatalf("Header.Marshal: %v", err) |
| 25 | } |
| 26 | // Append nonce (16 bytes) + poly1305 tag for empty chunk (16 bytes). |
| 27 | buf.Write(make([]byte, 16+16)) |
| 28 | return buf.Bytes() |
| 29 | } |
| 30 | |
| 31 | func TestInspectTagStanzas(t *testing.T) { |
| 32 | tests := []struct { |
| 33 | stanzaType string |
| 34 | want string |
| 35 | }{ |
| 36 | {stanzaType: "p256tag", want: "no"}, |
| 37 | {stanzaType: "mlkem768p256tag", want: "yes"}, |
| 38 | } |
| 39 | for _, tt := range tests { |
| 40 | t.Run(tt.stanzaType, func(t *testing.T) { |
| 41 | f := buildFile(t, tt.stanzaType) |
| 42 | md, err := Inspect(bytes.NewReader(f), int64(len(f))) |
| 43 | if err != nil { |
| 44 | t.Fatalf("Inspect: %v", err) |
| 45 | } |
| 46 | if got := md.Postquantum; got != tt.want { |
| 47 | t.Errorf("Postquantum = %q, want %q", got, tt.want) |
| 48 | } |
| 49 | if len(md.StanzaTypes) != 1 || md.StanzaTypes[0] != tt.stanzaType { |
| 50 | t.Errorf("StanzaTypes = %v, want [%q]", md.StanzaTypes, tt.stanzaType) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestInspectPostquantum(t *testing.T) { |
| 57 | tests := []struct { |
| 58 | name string |
| 59 | stanzaTypes []string |
| 60 | want string |
| 61 | }{ |
| 62 | {"postquantum", []string{"mlkem768x25519"}, "yes"}, |
| 63 | {"classical", []string{"X25519"}, "no"}, |
| 64 | {"both", []string{"mlkem768x25519", "X25519"}, "no"}, |
| 65 | {"unrecognized", []string{"tpm-ecc"}, "unknown"}, |
| 66 | {"postquantum and unrecognized", []string{"mlkem768x25519", "tpm-ecc"}, "unknown"}, |
| 67 | {"unrecognized and postquantum", []string{"tpm-ecc", "mlkem768x25519"}, "unknown"}, |
| 68 | {"classical and unrecognized", []string{"X25519", "tpm-ecc"}, "no"}, |
| 69 | {"unrecognized and classical", []string{"tpm-ecc", "X25519"}, "no"}, |
| 70 | } |
| 71 | for _, tt := range tests { |
| 72 | t.Run(tt.name, func(t *testing.T) { |
| 73 | f := buildFile(t, tt.stanzaTypes...) |
| 74 | md, err := Inspect(bytes.NewReader(f), int64(len(f))) |
| 75 | if err != nil { |
| 76 | t.Fatalf("Inspect: %v", err) |
| 77 | } |
| 78 | if md.Postquantum != tt.want { |
| 79 | t.Errorf("Postquantum = %q, want %q", md.Postquantum, tt.want) |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // readAfterEOFReader returns io.EOF along with the last of its data, and then |
| 86 | // more data from subsequent Reads, like a terminal that received Ctrl-D |
| 87 | // followed by more input. |
| 88 | type readAfterEOFReader struct { |
| 89 | data []byte |
| 90 | eof bool |
| 91 | } |
| 92 | |
| 93 | func (r *readAfterEOFReader) Read(p []byte) (int, error) { |
| 94 | if r.eof { |
| 95 | return copy(p, "\n"), nil |
| 96 | } |
| 97 | n := copy(p, r.data) |
| 98 | r.data = r.data[n:] |
| 99 | if len(r.data) == 0 { |
| 100 | r.eof = true |
| 101 | return n, io.EOF |
| 102 | } |
| 103 | return n, nil |
| 104 | } |
| 105 | |
| 106 | func TestInspectReadAfterEOF(t *testing.T) { |
| 107 | f := buildFile(t, "X25519") |
| 108 | r := &readAfterEOFReader{data: f} |
| 109 | md, err := Inspect(r, -1) |
| 110 | if err != nil { |
| 111 | t.Fatalf("Inspect: %v", err) |
| 112 | } |
| 113 | if md.Version != "age-encryption.org/v1" { |
| 114 | t.Errorf("Version = %q, want age-encryption.org/v1", md.Version) |
| 115 | } |
| 116 | // If the reads after EOF were counted towards the file size, the extra |
| 117 | // bytes would show up in the payload size. |
| 118 | if md.Sizes.MinPayload != 0 { |
| 119 | t.Errorf("MinPayload = %d, want 0", md.Sizes.MinPayload) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestStreamOverhead(t *testing.T) { |
| 124 | tests := []struct { |
| 125 | payloadSize int64 |
| 126 | want int64 |
| 127 | wantErr bool |
| 128 | }{ |
| 129 | {payloadSize: 0, wantErr: true}, |
| 130 | {payloadSize: 15, wantErr: true}, |
| 131 | {payloadSize: 16, wantErr: true}, |
| 132 | {payloadSize: 16 + 15, wantErr: true}, |
| 133 | {payloadSize: 16 + 16, want: 16 + 16}, // empty plaintext |
| 134 | {payloadSize: 16 + 1 + 16, want: 16 + 16}, |
| 135 | {payloadSize: 16 + stream.ChunkSize + 16, want: 16 + 16}, |
| 136 | {payloadSize: 16 + stream.ChunkSize + 16 + 1, wantErr: true}, |
| 137 | {payloadSize: 16 + stream.ChunkSize + 16 + 15, wantErr: true}, |
| 138 | {payloadSize: 16 + stream.ChunkSize + 16 + 16, wantErr: true}, // empty final chunk |
| 139 | {payloadSize: 16 + stream.ChunkSize + 16 + 1 + 16, want: 16 + 16 + 16}, |
| 140 | } |
| 141 | for _, tt := range tests { |
| 142 | name := "payloadSize=" + fmt.Sprint(tt.payloadSize) |
| 143 | t.Run(name, func(t *testing.T) { |
| 144 | got, gotErr := streamOverhead(tt.payloadSize) |
| 145 | if gotErr != nil { |
| 146 | if !tt.wantErr { |
| 147 | t.Errorf("streamOverhead() failed: %v", gotErr) |
| 148 | } |
| 149 | return |
| 150 | } |
| 151 | if tt.wantErr { |
| 152 | t.Fatal("streamOverhead() succeeded unexpectedly") |
| 153 | } |
| 154 | if got != tt.want { |
| 155 | t.Errorf("streamOverhead() = %v, want %v", got, tt.want) |
| 156 | } |
| 157 | }) |
| 158 | } |
| 159 | } |