Skip to content

Commit 94c0cee

Browse files
committed
fix: test encrypted
1 parent 077e763 commit 94c0cee

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

pkg/feeds/getter_test.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,40 @@ func TestGetWrappedChunk(t *testing.T) {
2828
t.Fatal("data mismatch")
2929
}
3030

31-
// old format (ts + ref)
32-
timestamp := make([]byte, 8)
33-
binary.BigEndian.PutUint64(timestamp, 1)
34-
ch = soctesting.GenerateMockSOC(t, append(timestamp, wch.Address().Bytes()...)).Chunk()
35-
36-
err = storer.Put(context.Background(), wch)
37-
if err != nil {
38-
t.Fatal(err)
31+
// old format
32+
tt := []struct {
33+
name string
34+
addr []byte
35+
}{
36+
{
37+
name: "unencrypted",
38+
addr: wch.Address().Bytes(),
39+
},
40+
{
41+
name: "encrypted",
42+
addr: append(wch.Address().Bytes(), wch.Address().Bytes()...),
43+
},
3944
}
4045

41-
wch, err = GetWrappedChunk(context.Background(), storer.ChunkStore(), ch)
42-
if err != nil {
43-
t.Fatal(err)
44-
}
46+
for _, tc := range tt {
47+
t.Run(tc.name, func(t *testing.T) {
48+
timestamp := make([]byte, 8)
49+
binary.BigEndian.PutUint64(timestamp, 1)
50+
ch = soctesting.GenerateMockSOC(t, append(timestamp, tc.addr...)).Chunk()
4551

46-
if !bytes.Equal(wch.Data()[8:], []byte("data")) {
47-
t.Fatal("data mismatch")
52+
err = storer.Put(context.Background(), wch)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
wch, err = GetWrappedChunk(context.Background(), storer.ChunkStore(), ch)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
62+
if !bytes.Equal(wch.Data()[8:], []byte("data")) {
63+
t.Fatal("data mismatch")
64+
}
65+
})
4866
}
4967
}

pkg/storage/inmemchunkstore/inmemchunkstore.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *ChunkStore) Get(_ context.Context, addr swarm.Address) (swarm.Chunk, er
3232
c.mu.Lock()
3333
defer c.mu.Unlock()
3434

35-
chunk, ok := c.chunks[addr.ByteString()]
35+
chunk, ok := c.chunks[c.key(addr)]
3636
if !ok {
3737
return nil, storage.ErrNotFound
3838
}
@@ -43,12 +43,12 @@ func (c *ChunkStore) Put(_ context.Context, ch swarm.Chunk) error {
4343
c.mu.Lock()
4444
defer c.mu.Unlock()
4545

46-
chunkCount, ok := c.chunks[ch.Address().ByteString()]
46+
chunkCount, ok := c.chunks[c.key(ch.Address())]
4747
if !ok {
4848
chunkCount.chunk = swarm.NewChunk(ch.Address(), ch.Data()).WithStamp(ch.Stamp())
4949
}
5050
chunkCount.count++
51-
c.chunks[ch.Address().ByteString()] = chunkCount
51+
c.chunks[c.key(ch.Address())] = chunkCount
5252

5353
return nil
5454
}
@@ -57,7 +57,7 @@ func (c *ChunkStore) Has(_ context.Context, addr swarm.Address) (bool, error) {
5757
c.mu.Lock()
5858
defer c.mu.Unlock()
5959

60-
_, exists := c.chunks[addr.ByteString()]
60+
_, exists := c.chunks[c.key(addr)]
6161

6262
return exists, nil
6363
}
@@ -66,12 +66,12 @@ func (c *ChunkStore) Delete(_ context.Context, addr swarm.Address) error {
6666
c.mu.Lock()
6767
defer c.mu.Unlock()
6868

69-
chunkCount := c.chunks[addr.ByteString()]
69+
chunkCount := c.chunks[c.key(addr)]
7070
chunkCount.count--
7171
if chunkCount.count <= 0 {
7272
delete(c.chunks, addr.ByteString())
7373
} else {
74-
c.chunks[addr.ByteString()] = chunkCount
74+
c.chunks[c.key(addr)] = chunkCount
7575
}
7676

7777
return nil
@@ -81,12 +81,12 @@ func (c *ChunkStore) Replace(_ context.Context, ch swarm.Chunk, emplace bool) er
8181
c.mu.Lock()
8282
defer c.mu.Unlock()
8383

84-
chunkCount := c.chunks[ch.Address().ByteString()]
84+
chunkCount := c.chunks[c.key(ch.Address())]
8585
chunkCount.chunk = ch
8686
if emplace {
8787
chunkCount.count++
8888
}
89-
c.chunks[ch.Address().ByteString()] = chunkCount
89+
c.chunks[c.key(ch.Address())] = chunkCount
9090

9191
return nil
9292
}
@@ -111,3 +111,10 @@ func (c *ChunkStore) Iterate(_ context.Context, fn storage.IterateChunkFn) error
111111
func (c *ChunkStore) Close() error {
112112
return nil
113113
}
114+
115+
func (c *ChunkStore) key(addr swarm.Address) string {
116+
if len(addr.Bytes()) < swarm.HashSize {
117+
return addr.ByteString()
118+
}
119+
return string(addr.Bytes()[:swarm.HashSize])
120+
}

0 commit comments

Comments
 (0)