github.com/ethersphere/bee/v2@v2.2.0/pkg/manifest/mantaray/walker_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 mantaray_test
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/ethersphere/bee/v2/pkg/manifest/mantaray"
    14  )
    15  
    16  func TestWalkNode(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	for _, tc := range []struct {
    20  		name     string
    21  		toAdd    [][]byte
    22  		expected [][]byte
    23  	}{
    24  		{
    25  			name: "simple",
    26  			toAdd: [][]byte{
    27  				[]byte("index.html.backup"),
    28  				[]byte("index.html"),
    29  				[]byte("img/test/oho.png"),
    30  				[]byte("img/test/old/test.png.backup"),
    31  				[]byte("img/test/old/test.png"),
    32  				[]byte("img/2.png"),
    33  				[]byte("img/1.png"),
    34  				[]byte("robots.txt"),
    35  			},
    36  			expected: [][]byte{
    37  				[]byte(""),
    38  				[]byte("i"),
    39  				[]byte("img/"),
    40  				[]byte("img/1.png"),
    41  				[]byte("img/2.png"),
    42  				[]byte("img/test/o"),
    43  				[]byte("img/test/oho.png"),
    44  				[]byte("img/test/old/test.png"),
    45  				[]byte("img/test/old/test.png.backup"),
    46  				[]byte("index.html"),
    47  				[]byte("index.html.backup"),
    48  				[]byte("robots.txt"),
    49  			},
    50  		},
    51  	} {
    52  		ctx := context.Background()
    53  		tc := tc
    54  
    55  		createTree := func(t *testing.T, toAdd [][]byte) *mantaray.Node {
    56  			t.Helper()
    57  
    58  			n := mantaray.New()
    59  
    60  			for i := 0; i < len(toAdd); i++ {
    61  				c := toAdd[i]
    62  				e := append(make([]byte, 32-len(c)), c...)
    63  				err := n.Add(ctx, c, e, nil, nil)
    64  				if err != nil {
    65  					t.Fatalf("expected no error, got %v", err)
    66  				}
    67  			}
    68  			return n
    69  		}
    70  
    71  		pathExistsInRightSequence := func(found []byte, expected [][]byte, walkedCount int) bool {
    72  			rightPathInSequence := false
    73  
    74  			c := expected[walkedCount]
    75  			if bytes.Equal(found, c) {
    76  				rightPathInSequence = true
    77  			}
    78  
    79  			return rightPathInSequence
    80  		}
    81  
    82  		t.Run(tc.name, func(t *testing.T) {
    83  			t.Parallel()
    84  
    85  			n := createTree(t, tc.toAdd)
    86  
    87  			walkedCount := 0
    88  
    89  			walker := func(path []byte, node *mantaray.Node, err error) error {
    90  
    91  				if !pathExistsInRightSequence(path, tc.expected, walkedCount) {
    92  					return fmt.Errorf("walkFn returned unexpected path: %s", path)
    93  				}
    94  				walkedCount++
    95  				return nil
    96  			}
    97  
    98  			// Expect no errors.
    99  			err := n.WalkNode(ctx, []byte{}, nil, walker)
   100  			if err != nil {
   101  				t.Fatalf("no error expected, found: %s", err)
   102  			}
   103  
   104  			if len(tc.expected) != walkedCount {
   105  				t.Errorf("expected %d nodes, got %d", len(tc.expected), walkedCount)
   106  			}
   107  		})
   108  
   109  		t.Run(tc.name+"/with load save", func(t *testing.T) {
   110  			t.Parallel()
   111  
   112  			n := createTree(t, tc.toAdd)
   113  
   114  			ls := newMockLoadSaver()
   115  
   116  			err := n.Save(ctx, ls)
   117  			if err != nil {
   118  				t.Fatal(err)
   119  			}
   120  
   121  			n2 := mantaray.NewNodeRef(n.Reference())
   122  
   123  			walkedCount := 0
   124  
   125  			walker := func(path []byte, node *mantaray.Node, err error) error {
   126  
   127  				if !pathExistsInRightSequence(path, tc.expected, walkedCount) {
   128  					return fmt.Errorf("walkFn returned unexpected path: %s", path)
   129  				}
   130  				walkedCount++
   131  				return nil
   132  			}
   133  
   134  			// Expect no errors.
   135  			err = n2.WalkNode(ctx, []byte{}, ls, walker)
   136  			if err != nil {
   137  				t.Fatalf("no error expected, found: %s", err)
   138  			}
   139  
   140  			if len(tc.expected) != walkedCount {
   141  				t.Errorf("expected %d nodes, got %d", len(tc.expected), walkedCount)
   142  			}
   143  		})
   144  	}
   145  }