Commit 10ee9ab7 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

Fix bzz chunk api (#262)

Co-authored-by: default avatarsvetomir <svetomirsmiljkovic@gmail.com>
parent 0b7f1c63
......@@ -48,5 +48,7 @@ jobs:
run: ./beekeeper check fullconnectivity --api-scheme http --debug-api-scheme http --disable-namespace --debug-api-domain localhost --api-domain localhost --node-count "${REPLICA}"
- name: Test pingpong
run: ./beekeeper check pingpong --api-scheme http --debug-api-scheme http --disable-namespace --debug-api-domain localhost --api-domain localhost --node-count "${REPLICA}"
- name: Test pushsync
- name: Test pushsync (bzz API)
run: ./beekeeper check pushsync --api-scheme http --debug-api-scheme http --disable-namespace --debug-api-domain localhost --api-domain localhost --node-count "${REPLICA}" --upload-node-count "${REPLICA}" --chunks-per-node 3
- name: Test pushsync (bzz-chunk API)
run: ./beekeeper check pushsync --bzz-chunk --api-scheme http --debug-api-scheme http --disable-namespace --debug-api-domain localhost --api-domain localhost --node-count "${REPLICA}" --upload-node-count "${REPLICA}" --chunks-per-node 3
......@@ -6,6 +6,7 @@ package api
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
......@@ -81,9 +82,13 @@ func (s *server) chunkUploadHandler(w http.ResponseWriter, r *http.Request) {
s.Logger.Error("bzz-chunk: read chunk data error")
jsonhttp.InternalServerError(w, "cannot read chunk data")
return
}
span := len(data)
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(span))
data = append(b, data...)
seen, err := s.Storer.Put(ctx, storage.ModePutUpload, swarm.NewChunk(address, data))
if err != nil {
s.Logger.Debugf("bzz-chunk: chunk write error: %v, addr %s", err, address)
......@@ -141,5 +146,5 @@ func (s *server) chunkGetHandler(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set("Content-Type", "binary/octet-stream")
_, _ = io.Copy(w, bytes.NewReader(chunk.Data()))
_, _ = io.Copy(w, bytes.NewReader(chunk.Data()[8:]))
}
......@@ -6,6 +6,7 @@ package api_test
import (
"bytes"
"encoding/binary"
"io"
"io/ioutil"
"net/http"
......@@ -32,7 +33,7 @@ func TestChunkUploadDownload(t *testing.T) {
invalidHash = swarm.MustParseHexAddress("bbccdd")
validContent = []byte("bbaatt")
invalidContent = []byte("bbaattss")
mockValidator = validator.NewMockValidator(validHash, validContent)
mockValidator = validator.NewMockValidator(validHash, append(newSpan(uint64(len(validContent))), validContent...))
tag = tags.NewTags()
mockValidatingStorer = mock.NewValidatingStorer(mockValidator, tag)
client = newTestServer(t, testServerOptions{
......@@ -136,3 +137,9 @@ func request(t *testing.T, client *http.Client, method string, resource string,
}
return resp
}
func newSpan(size uint64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, size)
return b
}
......@@ -28,7 +28,7 @@ func TestTags(t *testing.T) {
tagResourceUUid = func(uuid uint64) string { return "/bzz-tag/uuid/" + strconv.FormatUint(uuid, 10) }
validHash = swarm.MustParseHexAddress("aabbcc")
validContent = []byte("bbaatt")
mockValidator = validator.NewMockValidator(validHash, validContent)
mockValidator = validator.NewMockValidator(validHash, append(newSpan(uint64(len(validContent))), validContent...))
tag = tags.NewTags()
mockValidatingStorer = mock.NewValidatingStorer(mockValidator, tag)
mockPusher = mp.NewMockPusher(tag)
......@@ -98,7 +98,7 @@ func TestTags(t *testing.T) {
// Add asecond valid contentto validator
secondValidHash := swarm.MustParseHexAddress("deadbeaf")
secondValidContent := []byte("123456")
mockValidator.AddPair(secondValidHash, secondValidContent)
mockValidator.AddPair(secondValidHash, append(newSpan(uint64(len(secondValidContent))), secondValidContent...))
sentHheaders = make(http.Header)
sentHheaders.Set(api.TagHeaderUid, strconv.FormatUint(uint64(ta.Uid), 10))
......
......@@ -6,6 +6,7 @@ package debugapi_test
import (
"bytes"
"encoding/binary"
"net/http"
"testing"
......@@ -26,7 +27,7 @@ func TestPinChunkHandler(t *testing.T) {
resource := func(addr swarm.Address) string { return "/bzz-chunk/" + addr.String() }
hash := swarm.MustParseHexAddress("aabbcc")
data := []byte("bbaatt")
mockValidator := validator.NewMockValidator(hash, data)
mockValidator := validator.NewMockValidator(hash, append(newSpan(uint64(len(data))), data...))
tag := tags.NewTags()
mockValidatingStorer := mock.NewValidatingStorer(mockValidator, tag)
debugTestServer := newTestServer(t, testServerOptions{
......@@ -148,7 +149,7 @@ func TestPinChunkHandler(t *testing.T) {
// post another chunk
hash2 := swarm.MustParseHexAddress("ddeeff")
data2 := []byte("eagle")
mockValidator.AddPair(hash2, data2)
mockValidator.AddPair(hash2, append(newSpan(uint64(len(data2))), data2...))
jsonhttptest.ResponseDirect(t, bzzTestServer, http.MethodPost, resource(hash2), bytes.NewReader(data2), http.StatusOK, jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK),
Code: http.StatusOK,
......@@ -172,3 +173,9 @@ func TestPinChunkHandler(t *testing.T) {
})
})
}
func newSpan(size uint64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, size)
return b
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment