Пример #1
0
func (self *Converter) ReadBase64(input interface{}) []byte {
	if input == nil {
		panic("Expecting a value, but it's missing")
	}
	switch input := input.(type) {
	case string:
		byteVal, err := encoding.Base64Decode(input)
		if err != nil {
			panic(fmt.Sprintf("Expecting a base64 string but got %v", input))
		}
		return byteVal
	default:
		panic(fmt.Sprintf("Expecting a base64 string but got a %T", input))
	}
}
Пример #2
0
func Base64(payload string) (data []byte, err error) {
	return encoding.Base64Decode(payload)
}
Пример #3
0
func TestSimpleBlobStoreWrite(t *testing.T) {
	bucketId := createBucket(t, TypeId_BlobStore)
	arguments := make(map[string]interface{})

	// Put something
	// 'VGhpcyBpcyBhIHRlc3Q' = "This is a test"
	putToBlob := &testframework.JsonTest{
		Input: fmt.Sprintf(`{
			"Operation" : "Put",
			"Data" : {
				"BucketId" : "%v",
				"Key" : "incubation/test1/append",
				"Value" : "VGhpcyBpcyBhIHRlc3Q"
			}}`, bucketId),
		Expect: `{
			"Code" : "!(retcode_ok)!"
		}`,
	}
	runner().Run(t, putToBlob)

	// Now get the sha256 hash
	getSha256Hash := &testframework.JsonTest{
		Input: fmt.Sprintf(`{
			"Operation" : "Get",
			"Data" : {
				"BucketId" : "%v",
				"Key" : "incubation/test1/sum"
			}}`, bucketId),
		Expect: `{
			"Code" : "!(retcode_ok_found)!",
			"Data" : {
				"Value" : ["!(string_not_empty:cbor_hash)!",
					 "!(string_not_empty:cbor_length)!"]
			}
		}`,
		Arguments: arguments,
	}
	runner().Run(t, getSha256Hash)
	if t.Failed() {
		return
	}

	// See if the data we got is correct
	hashCborBase64 := arguments["cbor_hash"].(string)
	lengthCBorBase64 := arguments["cbor_length"].(string)
	hashCBor, err := encoding.Base64Decode(hashCborBase64)

	lengthCBor, err := encoding.Base64Decode(lengthCBorBase64)
	var hash []byte
	err = encoding.Cbor().Decode(hashCBor, &hash)
	var length uint64
	err = encoding.Cbor().Decode(lengthCBor, &length)
	if err != nil {
		t.Fatal(err)
	}

	// Length of "This is a test"
	if length != 14 {
		t.Fatalf("Expecting the length to be 14 but it's %v\n", length)
	}
	// Just check the hash length. TODO: Also check content
	if len(hash) != 32 {
		t.Fatalf("Since it's SHA256 (256 bits) the hash has to be 32 bytes,"+
			" but is %v\n", len(hash))
	}

	// Now finish it (will make it accessible to the public)
	putFinish := &testframework.JsonTest{
		Input: fmt.Sprintf(`{
			"Operation" : "Put",
			"Data" : {
				"BucketId" : "%v",
				"Key" : "incubation/test1/finish"
			}}`, bucketId),
		Expect: `{
			"Code" : "!(retcode_ok)!"
		}`,
	}
	runner().Run(t, putFinish)

	// It now should be possible to access the data using the hash

	// First get the length
	hashAsBase64 := encoding.Base64Encode(hash)
	getLength := &testframework.JsonTest{
		Input: fmt.Sprintf(`{
			"Operation" : "Get",
			"Data" : {
				"BucketId" : "%v",
				"Key" : "hash/:base64:%v/length"
			}}`, bucketId, hashAsBase64),
		Expect: `{
			"Code" : "!(retcode_ok_found)!",
			"Data" : {
				"Value" : ["!(string_not_empty:cbor_length)!"]
			}
		}`,
		Arguments: arguments,
	}
	runner().Run(t, getLength)

	// Again, check the length
	lengthCBorBase64 = arguments["cbor_length"].(string)
	lengthCBor, err = encoding.Base64Decode(lengthCBorBase64)
	err = encoding.Cbor().Decode(lengthCBor, &length)
	if err != nil {
		t.Fatal(err)
	}
	if length != 14 {
		t.Fatalf("The length should still be 14 but it's %v\n", length)
	}

	// Now read from it
	// Skip is set to 0
	// Limit is set to 14 (should read everything we have)
	getData := &testframework.JsonTest{
		Input: fmt.Sprintf(`{
			"Operation" : "Get",
			"Data" : {
				"BucketId" : "%v",
				"Key" : "hash/:base64:%v/content/:uvarint:0/:uvarint:14"
			}}`, bucketId, hashAsBase64),
		Expect: `{
			"Code" : "!(retcode_ok_found)!",
			"Data" : {
				"Value" : [
					"!(string_not_empty:data)!",
					"!(string_not_empty:cbor_length)!"
				]
			}
		}`,
		Arguments: arguments,
	}
	runner().Run(t, getData)

	// Ok, now check if we got the correct data
	dataAsBase64 := arguments["data"].(string)
	dataAsBinary, err := encoding.Base64Decode(dataAsBase64)
	if err != nil {
		t.Fatal(err)
	}
	// The data we originally added
	if string(dataAsBinary) != "This is a test" {
		t.Fatalf("Got wrong data '%v'. Expecting 'This is a test'",
			string(dataAsBinary))
	}
}