aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/service/store/store_dht_test.go
blob: 263ac41a055666013890e7c4c81e2e76ac6b591a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix  >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package store

import (
	"encoding/hex"
	"gnunet/crypto"
	"gnunet/enums"
	"gnunet/service/dht/blocks"
	"gnunet/util"
	"math/rand"
	"os"
	"testing"
)

// test constants
const (
	fsNumBlocks = 10
)

// TestDHTFileStore generates 'fsNumBlocks' fully-random blocks
// and stores them under their SHA512 key. It than retrieves
// each block from storage and checks for matching hash.
func TestDHTFilesStore(t *testing.T) {
	// test configuration
	path := "/tmp/dht-store"
	defer func() {
		os.RemoveAll(path)
	}()

	cfg := make(util.ParameterSet)
	cfg["mode"] = "file"
	cfg["cache"] = false
	cfg["path"] = path
	cfg["maxGB"] = 10

	// create file store
	if _, err := os.Stat(path); err != nil {
		if err = os.MkdirAll(path, 0755); err != nil {
			t.Fatal(err)
		}
	}
	fs, err := NewDHTStore(cfg)
	if err != nil {
		t.Fatal(err)
	}
	// allocate keys
	keys := make([]blocks.Query, 0, fsNumBlocks)
	// create result filter
	rf := blocks.NewGenericResultFilter(128, 236742)

	// First round: save blocks
	btype := enums.BLOCK_TYPE_TEST
	expire := util.AbsoluteTimeNever()
	for i := 0; i < fsNumBlocks; i++ {
		// generate random block
		size := 1024 + rand.Intn(62000) //nolint:gosec // good enough for testing
		buf := make([]byte, size)
		if _, err = rand.Read(buf); err != nil { //nolint:gosec // good enough for testing
			t.Fatal(err)
		}
		var blk blocks.Block
		if blk, err = blocks.NewBlock(btype, expire, buf); err != nil {
			t.Fatal(err)
		}
		// generate associated key
		k := crypto.Hash(buf)
		key := blocks.NewGenericQuery(k, enums.BLOCK_TYPE_TEST, 0)

		// store entry
		val := &DHTEntry{
			Blk: blk,
		}
		if err := fs.Put(key, val); err != nil {
			t.Fatalf("[%d] %s", i, err)
		}
		// remember key
		keys = append(keys, key)
	}

	// Second round: retrieve blocks and check
	for i, key := range keys {
		// get block
		vals, err := fs.Get("test", key, rf)
		if err != nil {
			t.Fatalf("[%d] %s", i, err)
		}
		if len(vals) != 1 {
			t.Fatalf("[%d] only one result expected", i)
		}
		buf := vals[0].Blk.Bytes()

		// re-create key
		k := crypto.Hash(buf)

		// do the keys match?
		if !k.Equal(key.Key()) {
			t.Log(hex.EncodeToString(k.Data))
			t.Log(hex.EncodeToString(key.Key().Data))
			t.Fatal("key/value mismatch")
		}
	}
}

func TestDHTEntryStore(t *testing.T) {
	// pth, sender, local := path.GenerateTestPath(10)
}