marshal_test.go 6.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package mantaray

import (
	"bytes"
	"context"
	"encoding/hex"
11
	"math/rand"
12 13 14 15 16 17 18 19 20 21 22
	mrand "math/rand"
	"reflect"
	"testing"

	"golang.org/x/crypto/sha3"
)

const testMarshalOutput01 = "52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64950ac787fbce1061870e8d34e0a638bc7e812c7ca4ebd31d626a572ba47b06f6952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072102654f163f5f0fa0621d729566c74d10037c4d7bbb0407d1e2c64950fcd3072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64950f89d6640e3044f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64850ff9f642182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64b50fc98072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64a50ff99622182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64d"

const testMarshalOutput02 = "52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64905954fb18659339d0b25e0fb9723d3cd5d528fb3c8d495fd157bd7b7a210496952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072102654f163f5f0fa0621d729566c74d10037c4d7bbb0407d1e2c64940fcd3072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952e3872548ec012a6e123b60f9177017fb12e57732621d2c1ada267adbe8cc4350f89d6640e3044f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64850ff9f642182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64b50fc98072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64a50ff99622182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64952fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64d"

23
var testEntries = []NodeEntry{
24
	{
25 26
		Path: []byte("/"),
		Metadata: map[string]string{
27 28 29 30
			"index-document": "aaaaa",
		},
	},
	{
31
		Path: []byte("aaaaa"),
32 33
	},
	{
34
		Path: []byte("cc"),
35 36
	},
	{
37
		Path: []byte("d"),
38 39
	},
	{
40
		Path: []byte("ee"),
41 42 43
	},
}

44 45 46 47 48 49
type NodeEntry struct {
	Path     []byte
	Entry    []byte
	Metadata map[string]string
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
func init() {
	obfuscationKeyFn = mrand.Read
}

func TestVersion01(t *testing.T) {
	hasher := sha3.NewLegacyKeccak256()

	_, err := hasher.Write([]byte(version01String))
	if err != nil {
		t.Fatal(err)
	}
	sum := hasher.Sum(nil)

	sumHex := hex.EncodeToString(sum)

	if version01HashString != sumHex {
		t.Fatalf("expecting version hash '%s', got '%s'", version01String, sumHex)
	}
}

func TestVersion02(t *testing.T) {
	hasher := sha3.NewLegacyKeccak256()

	_, err := hasher.Write([]byte(version02String))
	if err != nil {
		t.Fatal(err)
	}
	sum := hasher.Sum(nil)

	sumHex := hex.EncodeToString(sum)

	if version02HashString != sumHex {
		t.Fatalf("expecting version hash '%s', got '%s'", version02String, sumHex)
	}
}

func TestUnmarshal01(t *testing.T) {
	input, _ := hex.DecodeString(testMarshalOutput01)
	n := &Node{}
	err := n.UnmarshalBinary(input)
	if err != nil {
		t.Fatalf("expected no error marshaling, got %v", err)
	}

	expEncrypted := testMarshalOutput01[128:192]
	// perform XOR decryption
	expEncryptedBytes, _ := hex.DecodeString(expEncrypted)
	expBytes := encryptDecrypt(expEncryptedBytes, n.obfuscationKey)
	exp := hex.EncodeToString(expBytes)

	if hex.EncodeToString(n.entry) != exp {
		t.Fatalf("expected %x, got %x", exp, n.entry)
	}
	if len(testEntries) != len(n.forks) {
		t.Fatalf("expected %d forks, got %d", len(testEntries), len(n.forks))
	}
	for _, entry := range testEntries {
107
		prefix := entry.Path
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		f := n.forks[prefix[0]]
		if f == nil {
			t.Fatalf("expected to have  fork on byte %x", prefix[:1])
		}
		if !bytes.Equal(f.prefix, prefix) {
			t.Fatalf("expected prefix for byte %x to match %s, got %s", prefix[:1], prefix, f.prefix)
		}
	}
}

func TestUnmarshal02(t *testing.T) {
	input, _ := hex.DecodeString(testMarshalOutput02)
	n := &Node{}
	err := n.UnmarshalBinary(input)
	if err != nil {
		t.Fatalf("expected no error marshaling, got %v", err)
	}

	expEncrypted := testMarshalOutput02[128:192]
	// perform XOR decryption
	expEncryptedBytes, _ := hex.DecodeString(expEncrypted)
	expBytes := encryptDecrypt(expEncryptedBytes, n.obfuscationKey)
	exp := hex.EncodeToString(expBytes)

	if hex.EncodeToString(n.entry) != exp {
		t.Fatalf("expected %x, got %x", exp, n.entry)
	}
	if len(testEntries) != len(n.forks) {
		t.Fatalf("expected %d forks, got %d", len(testEntries), len(n.forks))
	}
	for _, entry := range testEntries {
139
		prefix := entry.Path
140 141 142 143 144 145 146
		f := n.forks[prefix[0]]
		if f == nil {
			t.Fatalf("expected to have  fork on byte %x", prefix[:1])
		}
		if !bytes.Equal(f.prefix, prefix) {
			t.Fatalf("expected prefix for byte %x to match %s, got %s", prefix[:1], prefix, f.prefix)
		}
147 148 149
		if len(entry.Metadata) > 0 {
			if !reflect.DeepEqual(entry.Metadata, f.metadata) {
				t.Fatalf("expected metadata for byte %x to match %s, got %s", prefix[:1], entry.Metadata, f.metadata)
150 151 152 153 154 155
			}
		}
	}
}

func TestMarshal(t *testing.T) {
156
	rand.Seed(1)
157 158 159 160 161 162 163 164 165 166 167
	ctx := context.Background()
	n := New()
	defer func(r func(*fork) []byte) { refBytes = r }(refBytes)
	i := uint8(0)
	refBytes = func(*fork) []byte {
		b := make([]byte, 32)
		b[31] = i
		i++
		return b
	}
	for i := 0; i < len(testEntries); i++ {
168 169
		c := testEntries[i].Path
		e := testEntries[i].Entry
170 171 172
		if len(e) == 0 {
			e = append(make([]byte, 32-len(c)), c...)
		}
173
		m := testEntries[i].Metadata
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		err := n.Add(ctx, c, e, m, nil)
		if err != nil {
			t.Fatalf("expected no error, got %v", err)
		}
	}
	b, err := n.MarshalBinary()
	if err != nil {
		t.Fatalf("expected no error marshaling, got %v", err)
	}
	exp, _ := hex.DecodeString(testMarshalOutput02)
	if !bytes.Equal(b, exp) {
		t.Fatalf("expected marshalled output to match %x, got %x", exp, b)
	}
	// n = &Node{}
	// err = n.UnmarshalBinary(b)
	// if err != nil {
	// 	t.Fatalf("expected no error unmarshaling, got %v", err)
	// }

	// for j := 0; j < len(testCases); j++ {
	// 	d := testCases[j]
	// 	m, err := n.Lookup(d, nil)
	// 	if err != nil {
	// 		t.Fatalf("expected no error, got %v", err)
	// 	}
	// 	if !bytes.Equal(m, d) {
	// 		t.Fatalf("expected value %x, got %x", d, m)
	// 	}
	// }
}