func (self *Converter) WriteBlob(input []byte) interface{} { if input == nil { panic("Expecting blob but value is missing") } if self.BlobAsBinary { return input } else { return encoding.Base64Encode(input) } }
func (self *Converter) WriteValue(input types.Array) interface{} { if input == nil { panic("Expecting value but value is missing") } if self.ValueAsBinary { return input.ToBytesArray() } else { arrayOfStrings := make([]string, len(input)) for index, element := range input { arrayOfStrings[index] = encoding.Base64Encode([]byte(element)) } return arrayOfStrings } }
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)) } }