Example #1
0
// Uploads something and then downloads
func testUploadDownload(t btesting.T, length int) {
	// First create a blon bucket
	var bucketId typing.BucketId
	operations.CreateBucket(t, typing.TypeId_BlobStore, &bucketId)
	if t.Failed() {
		return
	}

	// Create a buffer and fill that with random data
	buf := make([]byte, length)
	rand.Reader.Read(buf)
	reader := bytes.NewReader(buf)

	hash := Upload(t, bucketId, reader)
	if t.Failed() {
		return
	}

	// Now download
	writer := &bytes.Buffer{}
	resultingLength := Download(t, bucketId, hash, writer)
	if t.Failed() {
		return
	}

	// See if the length is the same as the upload length
	if resultingLength != uint64(length) {
		t.Errorf("Got different lengths, expecting %v but have %v", length, resultingLength)
		return
	}

	// See if the length is the same as the upload length
	if len(writer.Bytes()) != length {
		t.Errorf("Got different lengths, expecting %v but have %v in the buffer",
			length, len(writer.Bytes()))
		return
	}

	// Now compare the two buffers
	if bytes.Compare(buf, writer.Bytes()) != 0 {
		t.Errorf("Downloaded different data than the data just uploaded")
		return
	}
}
Example #2
0
// 1. Creates a bucket (storage)
// 2. Sees what metadata can be found for hat bucket.
func testScanMetadata(t btesting.T) {
	// The haser bucket is the simplest one, it currently only has one single metadata:
	// 'const.system.type_id'

	// First create a bucket
	var bucketId typing.BucketId
	operations.CreateBucket(t, typing.TypeId_Store, &bucketId)
	if t.Failed() {
		return
	}

	var keyAsBinary []byte
	t.Request(btesting.Request{
		Input: btesting.Object{
			"Operation": "Scan",
			"Data": btesting.Object{
				"BucketId": fmt.Sprintf(":meta-id:%v", bucketId.ToBase32String()),
				"FromKey":  "",
				"Limit":    1,
			},
		},
		Expecting: btesting.Object{
			"Code": eval.RetcodeOk(),
			"Data": btesting.Object{
				"HasMore": false,
				"Results": btesting.Array{
					btesting.Object{
						"Key": btesting.Array{eval.IsAnyBinaryB32(&keyAsBinary)},
					},
				},
			},
		},
	})
	if t.Failed() {
		return
	}
	keyAsString := string(keyAsBinary)
	if keyAsString != "const.system.type_id" {
		t.Errorf("Expecting to have the metadata const.system.type_id but have %v",
			keyAsString)
		return
	}
}
Example #3
0
func uploadDirectoryAndGet(t btesting.T) {
	// First create the blob bucket (that's where the directories are stored, and the data too)
	var blobBucketId typing.BucketId
	operations.CreateBucket(t, typing.TypeId_BlobStore, &blobBucketId)
	if t.Failed() {
		return
	}

	// Now create the directory bucket and connect that to the blob bucket
	var bucketId typing.BucketId
	var blobBucketIdCBor []byte
	blobBucketIdCBor, err := encoding.Cbor().Encode([]byte(blobBucketId))
	if err != nil {
		t.Errorf("error cbor encoding: %v", err)
		return
	}
	operations.CreateBucketWithMetadata(t, typing.TypeId_Directory, map[string][]byte{
		"const.forwarder.blob": blobBucketIdCBor,
	}, &bucketId)
	if t.Failed() {
		return
	}

	// Both buckets now exist and are connected
	dir, err := createDemoDirectory()
	if err != nil {
		t.Errorf("Error creating directory: %v\n", err)
	}

	// Now upload the directory to the blob storage
	dirReader := bytes.NewReader(dir)
	hash := blob.Upload(t, blobBucketId, dirReader)
	if t.Failed() {
		return
	}

	// The directory is now in the blob storage - the directory bucket can now index it
	cborHash, err := encoding.Cbor().Encode(hash)
	operations.Put(t, bucketId, typing.Key{[]byte("index")}, typing.Value{cborHash})
	if t.Failed() {
		return
	}

	// Get some files
	operations.Get(t, bucketId, typing.Key{hash, []byte("file_1.txt")}, true)
	if t.Failed() {
		return
	}
	operations.Get(t, bucketId, typing.Key{hash, []byte("another_file.jpeg")}, true)
	if t.Failed() {
		return
	}

	// Now some files that do not exist
	operations.Get(t, bucketId, typing.Key{hash, []byte("<UNKNOWN_FILE>.exe")}, false)
	if t.Failed() {
		return
	}
	operations.Get(t, bucketId, typing.Key{hash, []byte("no_is_not_in_directory.com")}, false)
	if t.Failed() {
		return
	}
}