github.com/ethersphere/bee/v2@v2.2.0/pkg/pss/trojan_test.go (about) 1 // Copyright 2020 The Swarm 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 pss_test 6 7 import ( 8 "bytes" 9 "context" 10 "testing" 11 12 "github.com/ethersphere/bee/v2/pkg/crypto" 13 "github.com/ethersphere/bee/v2/pkg/pss" 14 "github.com/ethersphere/bee/v2/pkg/swarm" 15 ) 16 17 func TestWrap(t *testing.T) { 18 t.Parallel() 19 20 topic := pss.NewTopic("topic") 21 msg := []byte("some payload") 22 key, err := crypto.GenerateSecp256k1Key() 23 if err != nil { 24 t.Fatal(err) 25 } 26 pubkey := &key.PublicKey 27 depth := 1 28 targets := newTargets(4, depth) 29 30 chunk, err := pss.Wrap(context.Background(), topic, msg, pubkey, targets) 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 contains := pss.Contains(targets, chunk.Address().Bytes()[0:depth]) 36 if !contains { 37 t.Fatal("trojan address was expected to match one of the targets with prefix") 38 } 39 40 if len(chunk.Data()) != swarm.ChunkWithSpanSize { 41 t.Fatalf("expected trojan data size to be %d, was %d", swarm.ChunkWithSpanSize, len(chunk.Data())) 42 } 43 } 44 45 func TestUnwrap(t *testing.T) { 46 t.Parallel() 47 48 topic := pss.NewTopic("topic") 49 msg := []byte("some payload") 50 key, err := crypto.GenerateSecp256k1Key() 51 if err != nil { 52 t.Fatal(err) 53 } 54 pubkey := &key.PublicKey 55 depth := 1 56 targets := newTargets(4, depth) 57 58 chunk, err := pss.Wrap(context.Background(), topic, msg, pubkey, targets) 59 if err != nil { 60 t.Fatal(err) 61 } 62 63 topic1 := pss.NewTopic("topic-1") 64 topic2 := pss.NewTopic("topic-2") 65 66 unwrapTopic, unwrapMsg, err := pss.Unwrap(context.Background(), key, chunk, []pss.Topic{topic1, topic2, topic}) 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 if !bytes.Equal(msg, unwrapMsg) { 72 t.Fatalf("message mismatch: expected %x, got %x", msg, unwrapMsg) 73 } 74 75 if !bytes.Equal(topic[:], unwrapTopic[:]) { 76 t.Fatalf("topic mismatch: expected %x, got %x", topic[:], unwrapTopic[:]) 77 } 78 } 79 80 func TestUnwrapTopicEncrypted(t *testing.T) { 81 t.Parallel() 82 83 topic := pss.NewTopic("topic") 84 msg := []byte("some payload") 85 86 privk := crypto.Secp256k1PrivateKeyFromBytes(topic[:]) 87 pubkey := privk.PublicKey 88 89 depth := 1 90 targets := newTargets(4, depth) 91 92 chunk, err := pss.Wrap(context.Background(), topic, msg, &pubkey, targets) 93 if err != nil { 94 t.Fatal(err) 95 } 96 97 key, err := crypto.GenerateSecp256k1Key() 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 topic1 := pss.NewTopic("topic-1") 103 topic2 := pss.NewTopic("topic-2") 104 105 unwrapTopic, unwrapMsg, err := pss.Unwrap(context.Background(), key, chunk, []pss.Topic{topic1, topic2, topic}) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 if !bytes.Equal(msg, unwrapMsg) { 111 t.Fatalf("message mismatch: expected %x, got %x", msg, unwrapMsg) 112 } 113 114 if !bytes.Equal(topic[:], unwrapTopic[:]) { 115 t.Fatalf("topic mismatch: expected %x, got %x", topic[:], unwrapTopic[:]) 116 } 117 }