github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/pgp_enc_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"bytes"
     8  	"crypto/rand"
     9  	"io"
    10  
    11  	"strings"
    12  	"testing"
    13  	"testing/quick"
    14  
    15  	"github.com/keybase/go-crypto/openpgp"
    16  )
    17  
    18  // give a private key and a public key, test the encryption of a
    19  // message
    20  func TestPGPEncrypt(t *testing.T) {
    21  	tc := SetupTest(t, "pgp_encrypt", 1)
    22  	defer tc.Cleanup()
    23  	bundleSrc, err := tc.MakePGPKey("src@keybase.io")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	bundleDst, err := tc.MakePGPKey("dst@keybase.io")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	msg := "59 seconds"
    33  	sink := NewBufferCloser()
    34  	recipients := []*PGPKeyBundle{bundleSrc, bundleDst}
    35  	if err := PGPEncrypt(strings.NewReader(msg), sink, bundleSrc, recipients); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	out := sink.Bytes()
    39  	if len(out) == 0 {
    40  		t.Fatal("no output")
    41  	}
    42  
    43  	// check that each recipient can read the message
    44  	for _, recip := range recipients {
    45  		kr := openpgp.EntityList{recip.Entity}
    46  		emsg := bytes.NewBuffer(out)
    47  		md, err := openpgp.ReadMessage(emsg, kr, nil, nil)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  		text, err := io.ReadAll(md.UnverifiedBody)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		if string(text) != msg {
    56  			t.Errorf("message: %q, expected %q", string(text), msg)
    57  		}
    58  	}
    59  }
    60  
    61  func TestPGPEncryptString(t *testing.T) {
    62  	tc := SetupTest(t, "pgp_encrypt", 1)
    63  	defer tc.Cleanup()
    64  	bundleSrc, err := tc.MakePGPKey("src@keybase.io")
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	bundleDst, err := tc.MakePGPKey("dst@keybase.io")
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	msg := "59 seconds"
    74  	recipients := []*PGPKeyBundle{bundleSrc, bundleDst}
    75  	out, err := PGPEncryptString(msg, bundleSrc, recipients)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	if len(out) == 0 {
    81  		t.Fatal("no output")
    82  	}
    83  
    84  	// check that each recipient can read the message
    85  	for _, recip := range recipients {
    86  		kr := openpgp.EntityList{recip.Entity}
    87  		emsg := bytes.NewBuffer(out)
    88  		md, err := openpgp.ReadMessage(emsg, kr, nil, nil)
    89  		if err != nil {
    90  			t.Fatal(err)
    91  		}
    92  		text, err := io.ReadAll(md.UnverifiedBody)
    93  		if err != nil {
    94  			t.Fatal(err)
    95  		}
    96  		if string(text) != msg {
    97  			t.Errorf("message: %q, expected %q", string(text), msg)
    98  		}
    99  	}
   100  }
   101  
   102  func TestPGPEncryptQuick(t *testing.T) {
   103  	tc := SetupTest(t, "pgp_encrypt", 1)
   104  	defer tc.Cleanup()
   105  	bundleSrc, err := tc.MakePGPKey("src@keybase.io")
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	bundleDst, err := tc.MakePGPKey("dst@keybase.io")
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	f := func(msg []byte) bool {
   115  		sink := NewBufferCloser()
   116  		recipients := []*PGPKeyBundle{bundleSrc, bundleDst}
   117  		if err := PGPEncrypt(bytes.NewReader(msg), sink, bundleSrc, recipients); err != nil {
   118  			return false
   119  		}
   120  		out := sink.Bytes()
   121  		if len(out) == 0 {
   122  			return false
   123  		}
   124  
   125  		// check that each recipient can read the message
   126  		for _, recip := range recipients {
   127  			kr := openpgp.EntityList{recip.Entity}
   128  			emsg := bytes.NewBuffer(out)
   129  			md, err := openpgp.ReadMessage(emsg, kr, nil, nil)
   130  			if err != nil {
   131  				return false
   132  			}
   133  			data, err := io.ReadAll(md.UnverifiedBody)
   134  			if err != nil {
   135  				return false
   136  			}
   137  			if !bytes.Equal(data, msg) {
   138  				return false
   139  			}
   140  		}
   141  		return true
   142  	}
   143  
   144  	if err := quick.Check(f, nil); err != nil {
   145  		t.Error(err)
   146  	}
   147  }
   148  
   149  func TestPGPEncryptLong(t *testing.T) {
   150  	tc := SetupTest(t, "pgp_encrypt", 1)
   151  	defer tc.Cleanup()
   152  	bundleSrc, err := tc.MakePGPKey("src@keybase.io")
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  	bundleDst, err := tc.MakePGPKey("dst@keybase.io")
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  
   161  	msg := make([]byte, 1024*1024)
   162  
   163  	_, err = rand.Read(msg)
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  
   168  	tc.G.Log.Info("msg size: %d", len(msg))
   169  
   170  	sink := NewBufferCloser()
   171  	recipients := []*PGPKeyBundle{bundleSrc, bundleDst}
   172  	if err := PGPEncrypt(bytes.NewReader(msg), sink, bundleSrc, recipients); err != nil {
   173  		t.Fatal(err)
   174  	}
   175  
   176  	out := sink.Bytes()
   177  	if len(out) == 0 {
   178  		t.Fatal("no output")
   179  	}
   180  
   181  	// check that each recipient can read the message
   182  	for _, recip := range recipients {
   183  		kr := openpgp.EntityList{recip.Entity}
   184  		emsg := bytes.NewBuffer(out)
   185  		md, err := openpgp.ReadMessage(emsg, kr, nil, nil)
   186  		if err != nil {
   187  			t.Fatal(err)
   188  		}
   189  		text, err := io.ReadAll(md.UnverifiedBody)
   190  		if err != nil {
   191  			t.Fatal(err)
   192  		}
   193  		if string(text) != string(msg) {
   194  			t.Errorf("message: %q, expected %q", string(text), string(msg))
   195  		}
   196  	}
   197  }