Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(share): Folder not shared should fail to download #129

Merged
merged 13 commits into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions tests/cli_tests/zboxcli_file_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ func TestFileStats(t *testing.T) {
t.Run("get file stats for an allocation you dont own", func(t *testing.T) {
t.Parallel()

var stats map[string]climodel.FileStats

otherAllocationID := ""
remotepath := "/"
filesize := int64(533)
Expand All @@ -252,7 +250,7 @@ func TestFileStats(t *testing.T) {
}), true)
require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)

Expand Down Expand Up @@ -285,25 +283,10 @@ func TestFileStats(t *testing.T) {

require.Nil(t, err, strings.Join(output, "\n"))
require.Len(t, output, 1)

var stats map[string]climodel.FileStats
err = json.Unmarshal([]byte(output[0]), &stats)
require.Nil(t, err)

for _, data := range stats {
require.Equal(t, "", data.Name)
require.Equal(t, "", data.Path)
require.Equal(t, "", data.PathHash)
require.Equal(t, int64(0), data.Size)
require.Equal(t, int64(0), data.NumOfBlocks)
require.Equal(t, int64(0), data.NumOfBlockDownloads)
require.Equal(t, int64(0), data.NumOfChallenges)
require.Equal(t, int64(0), data.NumOfUpdates)
require.Equal(t, "", data.WriteMarkerTxn)
require.Equal(t, "", data.LastChallengeTxn)
require.Equal(t, "", data.BlobberID)
require.Equal(t, "", data.BlobberURL)
require.Equal(t, false, data.BlockchainAware)
}
require.Len(t, stats, 0)
})

t.Run("get file stats with no params supplied", func(t *testing.T) {
Expand Down
38 changes: 37 additions & 1 deletion tests/cli_tests/zboxcli_share_file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli_tests

import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
Expand All @@ -14,6 +15,7 @@ import (
climodel "github.com/0chain/system_test/internal/cli/model"
cliutils "github.com/0chain/system_test/internal/cli/util"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
)

func TestShareFile(t *testing.T) {
Expand Down Expand Up @@ -1208,6 +1210,9 @@ func TestShareFile(t *testing.T) {

output, err = registerWalletForName(t, configPath, receiverWallet)
require.Nil(t, err, "registering wallet failed", err, strings.Join(output, "\n"))
require.Len(t, output, 4, strings.Join(output, "\n"))
require.Equal(t, "Read pool created successfully", output[2], strings.Join(output, "\n"))
require.Equal(t, "Wallet registered", output[3], strings.Join(output, "\n"))

walletReceiver, err := getWalletForName(t, configPath, receiverWallet)
require.Nil(t, err)
Expand Down Expand Up @@ -1281,6 +1286,7 @@ func TestShareFile(t *testing.T) {
downloadParams := createParams(map[string]interface{}{
"localpath": file,
"authticket": authTicket,
"lookuphash": GetReferenceLookup(allocationID, file),
})
output, err = downloadFileForWallet(t, receiverWallet, configPath, downloadParams, false)
require.Nil(t, err, strings.Join(output, "\n"))
Expand Down Expand Up @@ -1341,6 +1347,9 @@ func TestShareFile(t *testing.T) {

output, err = registerWalletForName(t, configPath, receiverWallet)
require.Nil(t, err, "registering wallet failed", err, strings.Join(output, "\n"))
require.Len(t, output, 4, strings.Join(output, "\n"))
require.Equal(t, "Read pool created successfully", output[2], strings.Join(output, "\n"))
require.Equal(t, "Wallet registered", output[3], strings.Join(output, "\n"))

shareParams := map[string]interface{}{
"allocation": allocationID,
Expand Down Expand Up @@ -1406,6 +1415,7 @@ func TestShareFile(t *testing.T) {
downloadParams := createParams(map[string]interface{}{
"localpath": filename,
"authticket": authTicket,
"lookuphash": GetReferenceLookup(allocationID, filename),
})
output, err = downloadFileForWallet(t, receiverWallet, configPath, downloadParams, false)
require.Nil(t, err, strings.Join(output, "\n"))
Expand Down Expand Up @@ -1459,16 +1469,24 @@ func shareFileWithWallet(t *testing.T, wallet, cliConfigFilename string, param m
func registerAndCreateAllocation(t *testing.T, configPath, wallet string) (string, *climodel.Wallet) {
faucetTokens := 3.0
// First create a wallet and run faucet command
// Output:
// [0]:"ZCN wallet created"
// [1]:"Creating related read pool for storage smart-contract..."
// [2]:"Read pool created successfully"
// [3]:"Wallet registered"
output, err := registerWalletForName(t, configPath, wallet)
require.Nil(t, err, "registering wallet failed", err, strings.Join(output, "\n"))
require.Len(t, output, 4, strings.Join(output, "\n"))
require.Equal(t, "Read pool created successfully", output[2], strings.Join(output, "\n"))
require.Equal(t, "Wallet registered", output[3], strings.Join(output, "\n"))

output, err = executeFaucetWithTokensForWallet(t, wallet, configPath, faucetTokens)
require.Nil(t, err, "faucet execution failed", err, strings.Join(output, "\n"))

allocParam := createParams(map[string]interface{}{
"lock": 0.5,
"size": 10485760,
"expire": "1h",
"expire": "2h",
"parity": 1,
"data": 1,
})
Expand Down Expand Up @@ -1499,3 +1517,21 @@ func registerAndCreateAllocation(t *testing.T, configPath, wallet string) (strin

return allocationID, walletModel
}

// Hash - hash the given data and return the hash as hex string
func Hash(data string) string {
return hex.EncodeToString(RawHash(data))
}

// RawHash - Logic to hash the text and return the hash bytes
func RawHash(data string) []byte {
hash := sha3.New256()
hash.Write([]byte(data))
var buf []byte
return hash.Sum(buf)
}

// GetReferenceLookup hash(allocationID + ":" + path)
func GetReferenceLookup(allocationID, path string) string {
return Hash(allocationID + ":" + path)
}