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: ...@@ -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}" 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 - 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}" 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 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 ...@@ -6,6 +6,7 @@ package api
import ( import (
"bytes" "bytes"
"encoding/binary"
"errors" "errors"
"fmt" "fmt"
"io" "io"
...@@ -81,9 +82,13 @@ func (s *server) chunkUploadHandler(w http.ResponseWriter, r *http.Request) { ...@@ -81,9 +82,13 @@ func (s *server) chunkUploadHandler(w http.ResponseWriter, r *http.Request) {
s.Logger.Error("bzz-chunk: read chunk data error") s.Logger.Error("bzz-chunk: read chunk data error")
jsonhttp.InternalServerError(w, "cannot read chunk data") jsonhttp.InternalServerError(w, "cannot read chunk data")
return 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)) seen, err := s.Storer.Put(ctx, storage.ModePutUpload, swarm.NewChunk(address, data))
if err != nil { if err != nil {
s.Logger.Debugf("bzz-chunk: chunk write error: %v, addr %s", err, address) 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) { ...@@ -141,5 +146,5 @@ func (s *server) chunkGetHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Set("Content-Type", "binary/octet-stream") 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 ...@@ -6,6 +6,7 @@ package api_test
import ( import (
"bytes" "bytes"
"encoding/binary"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -32,7 +33,7 @@ func TestChunkUploadDownload(t *testing.T) { ...@@ -32,7 +33,7 @@ func TestChunkUploadDownload(t *testing.T) {
invalidHash = swarm.MustParseHexAddress("bbccdd") invalidHash = swarm.MustParseHexAddress("bbccdd")
validContent = []byte("bbaatt") validContent = []byte("bbaatt")
invalidContent = []byte("bbaattss") invalidContent = []byte("bbaattss")
mockValidator = validator.NewMockValidator(validHash, validContent) mockValidator = validator.NewMockValidator(validHash, append(newSpan(uint64(len(validContent))), validContent...))
tag = tags.NewTags() tag = tags.NewTags()
mockValidatingStorer = mock.NewValidatingStorer(mockValidator, tag) mockValidatingStorer = mock.NewValidatingStorer(mockValidator, tag)
client = newTestServer(t, testServerOptions{ client = newTestServer(t, testServerOptions{
...@@ -136,3 +137,9 @@ func request(t *testing.T, client *http.Client, method string, resource string, ...@@ -136,3 +137,9 @@ func request(t *testing.T, client *http.Client, method string, resource string,
} }
return resp 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) { ...@@ -28,7 +28,7 @@ func TestTags(t *testing.T) {
tagResourceUUid = func(uuid uint64) string { return "/bzz-tag/uuid/" + strconv.FormatUint(uuid, 10) } tagResourceUUid = func(uuid uint64) string { return "/bzz-tag/uuid/" + strconv.FormatUint(uuid, 10) }
validHash = swarm.MustParseHexAddress("aabbcc") validHash = swarm.MustParseHexAddress("aabbcc")
validContent = []byte("bbaatt") validContent = []byte("bbaatt")
mockValidator = validator.NewMockValidator(validHash, validContent) mockValidator = validator.NewMockValidator(validHash, append(newSpan(uint64(len(validContent))), validContent...))
tag = tags.NewTags() tag = tags.NewTags()
mockValidatingStorer = mock.NewValidatingStorer(mockValidator, tag) mockValidatingStorer = mock.NewValidatingStorer(mockValidator, tag)
mockPusher = mp.NewMockPusher(tag) mockPusher = mp.NewMockPusher(tag)
...@@ -98,7 +98,7 @@ func TestTags(t *testing.T) { ...@@ -98,7 +98,7 @@ func TestTags(t *testing.T) {
// Add asecond valid contentto validator // Add asecond valid contentto validator
secondValidHash := swarm.MustParseHexAddress("deadbeaf") secondValidHash := swarm.MustParseHexAddress("deadbeaf")
secondValidContent := []byte("123456") secondValidContent := []byte("123456")
mockValidator.AddPair(secondValidHash, secondValidContent) mockValidator.AddPair(secondValidHash, append(newSpan(uint64(len(secondValidContent))), secondValidContent...))
sentHheaders = make(http.Header) sentHheaders = make(http.Header)
sentHheaders.Set(api.TagHeaderUid, strconv.FormatUint(uint64(ta.Uid), 10)) sentHheaders.Set(api.TagHeaderUid, strconv.FormatUint(uint64(ta.Uid), 10))
......
...@@ -6,6 +6,7 @@ package debugapi_test ...@@ -6,6 +6,7 @@ package debugapi_test
import ( import (
"bytes" "bytes"
"encoding/binary"
"net/http" "net/http"
"testing" "testing"
...@@ -26,7 +27,7 @@ func TestPinChunkHandler(t *testing.T) { ...@@ -26,7 +27,7 @@ func TestPinChunkHandler(t *testing.T) {
resource := func(addr swarm.Address) string { return "/bzz-chunk/" + addr.String() } resource := func(addr swarm.Address) string { return "/bzz-chunk/" + addr.String() }
hash := swarm.MustParseHexAddress("aabbcc") hash := swarm.MustParseHexAddress("aabbcc")
data := []byte("bbaatt") data := []byte("bbaatt")
mockValidator := validator.NewMockValidator(hash, data) mockValidator := validator.NewMockValidator(hash, append(newSpan(uint64(len(data))), data...))
tag := tags.NewTags() tag := tags.NewTags()
mockValidatingStorer := mock.NewValidatingStorer(mockValidator, tag) mockValidatingStorer := mock.NewValidatingStorer(mockValidator, tag)
debugTestServer := newTestServer(t, testServerOptions{ debugTestServer := newTestServer(t, testServerOptions{
...@@ -148,7 +149,7 @@ func TestPinChunkHandler(t *testing.T) { ...@@ -148,7 +149,7 @@ func TestPinChunkHandler(t *testing.T) {
// post another chunk // post another chunk
hash2 := swarm.MustParseHexAddress("ddeeff") hash2 := swarm.MustParseHexAddress("ddeeff")
data2 := []byte("eagle") 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{ jsonhttptest.ResponseDirect(t, bzzTestServer, http.MethodPost, resource(hash2), bytes.NewReader(data2), http.StatusOK, jsonhttp.StatusResponse{
Message: http.StatusText(http.StatusOK), Message: http.StatusText(http.StatusOK),
Code: http.StatusOK, Code: http.StatusOK,
...@@ -172,3 +173,9 @@ func TestPinChunkHandler(t *testing.T) { ...@@ -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