mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-11-04 04:28:08 +00:00 
			
		
		
		
	* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
		
			
				
	
	
		
			128 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			128 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright (c) HashiCorp, Inc.
 | 
						|
// SPDX-License-Identifier: BUSL-1.1
 | 
						|
 | 
						|
package transit
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"encoding/base64"
 | 
						|
	"encoding/hex"
 | 
						|
	"fmt"
 | 
						|
	"reflect"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/hashicorp/vault/helper/random"
 | 
						|
	"github.com/hashicorp/vault/sdk/logical"
 | 
						|
)
 | 
						|
 | 
						|
func TestTransit_Random(t *testing.T) {
 | 
						|
	var b *backend
 | 
						|
	sysView := logical.TestSystemView()
 | 
						|
	storage := &logical.InmemStorage{}
 | 
						|
	sysView.CachingDisabledVal = true
 | 
						|
 | 
						|
	b, _ = Backend(context.Background(), &logical.BackendConfig{
 | 
						|
		StorageView: storage,
 | 
						|
		System:      sysView,
 | 
						|
	})
 | 
						|
 | 
						|
	req := &logical.Request{
 | 
						|
		Storage:   storage,
 | 
						|
		Operation: logical.UpdateOperation,
 | 
						|
		Path:      "random",
 | 
						|
		Data:      map[string]interface{}{},
 | 
						|
	}
 | 
						|
 | 
						|
	doRequest := func(req *logical.Request, errExpected bool, format string, numBytes int) {
 | 
						|
		getResponse := func() []byte {
 | 
						|
			resp, err := b.HandleRequest(context.Background(), req)
 | 
						|
			if err != nil && !errExpected {
 | 
						|
				t.Fatal(err)
 | 
						|
			}
 | 
						|
			if resp == nil {
 | 
						|
				t.Fatal("expected non-nil response")
 | 
						|
			}
 | 
						|
			if errExpected {
 | 
						|
				if !resp.IsError() {
 | 
						|
					t.Fatalf("bad: got error response: %#v", *resp)
 | 
						|
				}
 | 
						|
				return nil
 | 
						|
			}
 | 
						|
			if resp.IsError() {
 | 
						|
				t.Fatalf("bad: got error response: %#v", *resp)
 | 
						|
			}
 | 
						|
			if _, ok := resp.Data["random_bytes"]; !ok {
 | 
						|
				t.Fatal("no random_bytes found in response")
 | 
						|
			}
 | 
						|
 | 
						|
			outputStr := resp.Data["random_bytes"].(string)
 | 
						|
			var outputBytes []byte
 | 
						|
			switch format {
 | 
						|
			case "base64":
 | 
						|
				outputBytes, err = base64.StdEncoding.DecodeString(outputStr)
 | 
						|
			case "hex":
 | 
						|
				outputBytes, err = hex.DecodeString(outputStr)
 | 
						|
			default:
 | 
						|
				t.Fatal("unknown format")
 | 
						|
			}
 | 
						|
			if err != nil {
 | 
						|
				t.Fatal(err)
 | 
						|
			}
 | 
						|
 | 
						|
			return outputBytes
 | 
						|
		}
 | 
						|
 | 
						|
		rand1 := getResponse()
 | 
						|
		// Expected error
 | 
						|
		if rand1 == nil {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		rand2 := getResponse()
 | 
						|
		if len(rand1) != numBytes || len(rand2) != numBytes {
 | 
						|
			t.Fatal("length of output random bytes not what is expected")
 | 
						|
		}
 | 
						|
		if reflect.DeepEqual(rand1, rand2) {
 | 
						|
			t.Fatal("found identical ouputs")
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	for _, source := range []string{"", "platform", "seal", "all"} {
 | 
						|
		req.Data["source"] = source
 | 
						|
		req.Data["bytes"] = 32
 | 
						|
		req.Data["format"] = "base64"
 | 
						|
		req.Path = "random"
 | 
						|
		// Test defaults
 | 
						|
		doRequest(req, false, "base64", 32)
 | 
						|
 | 
						|
		// Test size selection in the path
 | 
						|
		req.Path = "random/24"
 | 
						|
		req.Data["format"] = "hex"
 | 
						|
		doRequest(req, false, "hex", 24)
 | 
						|
 | 
						|
		if source != "" {
 | 
						|
			// Test source selection in the path
 | 
						|
			req.Path = fmt.Sprintf("random/%s", source)
 | 
						|
			req.Data["format"] = "hex"
 | 
						|
			doRequest(req, false, "hex", 32)
 | 
						|
 | 
						|
			req.Path = fmt.Sprintf("random/%s/24", source)
 | 
						|
			req.Data["format"] = "hex"
 | 
						|
			doRequest(req, false, "hex", 24)
 | 
						|
		}
 | 
						|
 | 
						|
		// Test bad input/format
 | 
						|
		req.Path = "random"
 | 
						|
		req.Data["format"] = "base92"
 | 
						|
		doRequest(req, true, "", 0)
 | 
						|
 | 
						|
		req.Data["format"] = "hex"
 | 
						|
		req.Data["bytes"] = -1
 | 
						|
		doRequest(req, true, "", 0)
 | 
						|
 | 
						|
		req.Data["format"] = "hex"
 | 
						|
		req.Data["bytes"] = random.APIMaxBytes + 1
 | 
						|
 | 
						|
		doRequest(req, true, "", 0)
 | 
						|
	}
 | 
						|
}
 |