提交 f5f5700d 作者: Juan Batiz-Benet

Merge pull request #712 from jbenet/cat-length-fix

cat length output now uses unixfs size
......@@ -68,17 +68,12 @@ func cat(node *core.IpfsNode, paths []string) ([]io.Reader, uint64, error) {
return nil, 0, err
}
nodeLength, err := dagnode.Size()
if err != nil {
return nil, 0, err
}
length += nodeLength
read, err := uio.NewDagReader(node.Context(), dagnode, node.DAG)
if err != nil {
return nil, 0, err
}
readers = append(readers, read)
length += uint64(read.Size())
}
return readers, length, nil
}
......@@ -100,6 +100,30 @@ test_wait_open_tcp_port_10_sec() {
return 1
}
# test_config_set helps us make sure _we really did set_ a config value.
# it sets it and then tests it. This became elaborate because ipfs config
# was setting really weird things and am not sure why.
test_config_set() {
# grab flags (like -bool in "ipfs config -bool")
test_cfg_flags="" # unset in case.
test "$#" = 3 && { test_cfg_flags=$1; shift; }
test_cfg_key=$1
test_cfg_val=$2
# when verbose, tell the user what config values are being set
test_cfg_cmd="ipfs config $test_cfg_flags \"$test_cfg_key\" \"$test_cfg_val\""
test "$TEST_VERBOSE" = 1 && echo "$test_cfg_cmd"
# ok try setting the config key/val pair.
ipfs config $test_cfg_flags "$test_cfg_key" "$test_cfg_val"
echo "$test_cfg_val" >cfg_set_expected
ipfs config "$test_cfg_key" >cfg_set_actual
test_cmp cfg_set_expected cfg_set_actual
}
test_init_ipfs() {
test_expect_success "ipfs init succeeds" '
......@@ -107,33 +131,45 @@ test_init_ipfs() {
ipfs init -b=1024 > /dev/null
'
test_expect_success "prepare config" '
test_expect_success "prepare config -- mounting and bootstrap rm" '
mkdir mountdir ipfs ipns &&
ipfs config Mounts.IPFS "$(pwd)/ipfs" &&
ipfs config Mounts.IPNS "$(pwd)/ipns" &&
ipfs bootstrap rm --all
test_config_set Mounts.IPFS "$(pwd)/ipfs" &&
test_config_set Mounts.IPNS "$(pwd)/ipns" &&
ipfs bootstrap rm --all ||
fsh cat "\"$IPFS_PATH/config\""
'
}
test_config_ipfs_gateway_readonly() {
test_expect_success "prepare config -- gateway readonly" '
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002
if test "$1" == ""; then
echo "must call with an address, for example:"
echo 'test_config_ipfs_gateway_readonly "/ip4/0.0.0.0/tcp/5002"'
return 1 # todo: find a better way to exit here.
fi
GWAY_ADDR=$1
test_expect_success "prepare config -- gateway address" '
test_config_set "Addresses.Gateway" "$GWAY_ADDR"
'
}
test_config_ipfs_gateway_writable() {
test_config_ipfs_gateway_readonly $1
test_expect_success "prepare config -- gateway writable" '
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/5002 &&
ipfs config -bool Gateway.Writable true
test_config_set -bool Gateway.Writable true ||
fsh cat "\"$IPFS_PATH/config\""
'
}
test_launch_ipfs_daemon() {
ADDR_API="/ip4/127.0.0.1/tcp/5001"
ADDR_GWAY=`ipfs config Addresses.Gateway`
NLINES="2"
# ADDR_GWAY will be set if the test_config_ipfs_gateway_* funcs were called.
if test "$ADDR_GWAY" != ""; then
NLINES="3"
fi
......@@ -146,22 +182,23 @@ test_launch_ipfs_daemon() {
# and we make sure there are no errors
test_expect_success "'ipfs daemon' is ready" '
IPFS_PID=$! &&
test_wait_output_n_lines_60_sec actual_daemon $NLINES &&
printf "" >empty && test_cmp daemon_err empty ||
test_wait_output_n_lines_60_sec actual_daemon $NLINES ||
fsh cat actual_daemon || fsh cat daemon_err
'
test_expect_success "'ipfs daemon' output includes API address" '
cat actual_daemon | grep "API server listening on $ADDR_API" ||
fsh cat actual_daemon ||
fsh "cat actual_daemon | grep \"API server listening on $ADDR_API\""
fsh "cat actual_daemon | grep \"API server listening on $ADDR_API\"" ||
fsh cat daemon_err
'
if test "$ADDR_GWAY" != ""; then
test_expect_success "'ipfs daemon' output includes Gateway address" '
cat actual_daemon | grep "Gateway server listening on $ADDR_GWAY" ||
fsh cat actual_daemon ||
fsh "cat actual_daemon | grep \"Gateway server listening on $ADDR_GWAY\""
fsh "cat actual_daemon | grep \"Gateway server listening on $ADDR_GWAY\"" ||
fsh cat daemon_err
'
fi
}
......
#!/bin/sh
test_description="Test config command"
. lib/test-lib.sh
# we use a function so that we can run it both offline + online
test_config_cmd_set() {
# flags (like -bool in "ipfs config -bool")
cfg_flags="" # unset in case.
test "$#" = 3 && { cfg_flags=$1; shift; }
cfg_key=$1
cfg_val=$2
test_expect_success "ipfs config succeeds" '
ipfs config $cfg_flags "$cfg_key" "$cfg_val"
'
test_expect_success "ipfs config output looks good" '
echo "$cfg_val" >expected &&
ipfs config "$cfg_key" >actual &&
test_cmp expected actual
'
# also test our lib function. it should work too.
cfg_key="Lib.$cfg_key"
test_expect_success "test_config_set succeeds" '
test_config_set $cfg_flags "$cfg_key" "$cfg_val"
'
test_expect_success "test_config_set value looks good" '
echo "$cfg_val" >expected &&
ipfs config "$cfg_key" >actual &&
test_cmp expected actual
'
}
test_config_cmd() {
test_config_cmd_set "beep" "boop"
test_config_cmd_set "beep1" "boop2"
test_config_cmd_set "beep1" "boop2"
test_config_cmd_set "-bool" "beep2" "true"
test_config_cmd_set "-bool" "beep2" "false"
}
test_init_ipfs
# should work offline
test_config_cmd
# should work online
test_launch_ipfs_daemon
test_config_cmd
test_kill_ipfs_daemon
test_done
......@@ -68,6 +68,7 @@ test_expect_success "ipfs get -a -C output looks good" '
test_cmp expected actual
'
# TODO(mappum)
test_expect_failure "gzipped tar archive output is valid" '
tar -zxf "$HASH".tar.gz &&
test_cmp "$HASH" data &&
......@@ -106,7 +107,8 @@ test_expect_success "ipfs get -a -C output looks good (directory)" '
test_cmp expected actual
'
test_expect_success "gzipped tar archive output is valid (directory)" '
# TODO(mappum)
test_expect_failure "gzipped tar archive output is valid (directory)" '
tar -zxf "$HASH2".tar.gz &&
test_cmp dir/a "$HASH2"/a &&
test_cmp dir/b/c "$HASH2"/b/c &&
......
......@@ -4,12 +4,12 @@
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="Test HTTP gateway"
test_description="Test HTTP Gateway"
. lib/test-lib.sh
test_init_ipfs
test_config_ipfs_gateway_readonly
test_config_ipfs_gateway_readonly "/ip4/0.0.0.0/tcp/5002"
test_launch_ipfs_daemon
# TODO check both 5001 and 5002.
......
......@@ -4,12 +4,12 @@
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="Test HTTP Gateway"
test_description="Test HTTP Gateway (Writable)"
. lib/test-lib.sh
test_init_ipfs
test_config_ipfs_gateway_writable
test_config_ipfs_gateway_writable "/ip4/0.0.0.0/tcp/5002"
test_launch_ipfs_daemon
test_expect_success "ipfs daemon listening to TCP port 5002" '
......
......@@ -54,7 +54,7 @@ type ReadSeekCloser interface {
// NewDagReader creates a new reader object that reads the data represented by the given
// node, using the passed in DAGService for data retreival
func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (ReadSeekCloser, error) {
func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (*DagReader, error) {
pb := new(ftpb.Data)
err := proto.Unmarshal(n.Data, pb)
if err != nil {
......@@ -65,6 +65,8 @@ func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (Read
case ftpb.Data_Directory:
// Dont allow reading directories
return nil, ErrIsDir
case ftpb.Data_Raw:
fallthrough
case ftpb.Data_File:
fctx, cancel := context.WithCancel(ctx)
promises := serv.GetDAG(fctx, n)
......@@ -77,9 +79,6 @@ func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (Read
cancel: cancel,
pbdata: pb,
}, nil
case ftpb.Data_Raw:
// Raw block will just be a single level, return a byte buffer
return NewRSNCFromBytes(pb.GetData()), nil
default:
return nil, ft.ErrUnrecognizedType
}
......@@ -123,6 +122,11 @@ func (dr *DagReader) precalcNextBuf() error {
}
}
// Size return the total length of the data from the DAG structured file.
func (dr *DagReader) Size() int64 {
return int64(dr.pbdata.GetFilesize())
}
// Read reads data from the DAG structured file
func (dr *DagReader) Read(b []byte) (int, error) {
// If no cached buffer, load one
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论