| 1 | package seal |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func testDEK() []byte { |
| 9 | d := make([]byte, 32) |
| 10 | for i := range d { |
| 11 | d[i] = byte(i) |
| 12 | } |
| 13 | return d |
| 14 | } |
| 15 | |
| 16 | func TestSealOpenRoundTrip(t *testing.T) { |
| 17 | s, err := New(testDEK()) |
| 18 | if err != nil { |
| 19 | t.Fatalf("New: %v", err) |
| 20 | } |
| 21 | ct, err := s.Seal("signin.address", []byte("person@example.invalid")) |
| 22 | if err != nil { |
| 23 | t.Fatalf("Seal: %v", err) |
| 24 | } |
| 25 | if bytes.Contains(ct, []byte("example.invalid")) { |
| 26 | t.Fatal("ciphertext contains the plaintext") |
| 27 | } |
| 28 | pt, err := s.Open("signin.address", ct) |
| 29 | if err != nil { |
| 30 | t.Fatalf("Open: %v", err) |
| 31 | } |
| 32 | if string(pt) != "person@example.invalid" { |
| 33 | t.Fatalf("Open = %q", pt) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // A value sealed under one purpose must not open under another. In the |
| 38 | // home these are three different tables, so what this catches is a store |
| 39 | // handed the wrong purpose — invisible to the type system, and invisible |
| 40 | // to any test that only round-trips through the same purpose it wrote. |
| 41 | func TestWrongPurposeFails(t *testing.T) { |
| 42 | s, _ := New(testDEK()) |
| 43 | ct, _ := s.Seal("signin.address", []byte("secret")) |
| 44 | if _, err := s.Open("team.name", ct); err == nil { |
| 45 | t.Fatal("opened under the wrong purpose") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestRejectsShortDEK(t *testing.T) { |
| 50 | if _, err := New(make([]byte, 31)); err == nil { |
| 51 | t.Fatal("accepted a 31-byte DEK") |
| 52 | } |
| 53 | } |
| 54 | |