...

Source file src/golang.conradwood.net/tests/ctx/client.go

Documentation: golang.conradwood.net/tests/ctx

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	au "golang.conradwood.net/apis/auth"
     7  	"golang.conradwood.net/go-easyops/auth"
     8  	"golang.conradwood.net/go-easyops/authremote"
     9  	"golang.conradwood.net/go-easyops/cmdline"
    10  	"golang.conradwood.net/go-easyops/ctx"
    11  	"golang.conradwood.net/go-easyops/utils"
    12  	"os"
    13  	"time"
    14  )
    15  
    16  var (
    17  	timeout    = time.Duration(10) * time.Second
    18  	me_user    *au.SignedUser
    19  	me_service *au.SignedUser
    20  )
    21  
    22  func client() {
    23  	fmt.Printf("testing context...\n")
    24  
    25  	// first check that new and old context carry the same user/service information once created.
    26  	ctx_def := authremote.Context()
    27  	fmt.Printf("context with version %d:\n", cmdline.GetContextBuilderVersion())
    28  	printContext(ctx_def)
    29  	me_user = auth.GetSignedUser(ctx_def)
    30  	me_service = auth.GetSignedService(ctx_def)
    31  	if me_user == nil && me_service == nil {
    32  		fmt.Printf("failed no service and no user in context. cannot proceed\n")
    33  		os.Exit(10)
    34  	}
    35  
    36  	cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
    37  	nctx := authremote.Context()
    38  	fmt.Printf("context with version %d:\n", cmdline.GetContextBuilderVersion())
    39  	printContext(nctx)
    40  	utils.Bail("(1) ctx assertion", AssertEqualContexts(ctx_def, nctx))
    41  
    42  	m := map[string]string{"foo": "bar"}
    43  	nctx_derived := authremote.DerivedContextWithRouting(nctx, m, true)
    44  	//	printContext(nctx)
    45  	utils.Bail("(2) ctx assertion", AssertEqualContexts(nctx, nctx_derived))
    46  
    47  	// check simple, new functions.
    48  	cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
    49  	ctx1 := authremote.Context()
    50  	s, err := ctx.SerialiseContextToString(ctx1)
    51  	utils.Bail("(1) failed to serialise context to string", err)
    52  	fmt.Printf("Serialised to: %s\n", utils.HexStr([]byte(s)))
    53  	_, err = ctx.DeserialiseContextFromString(s)
    54  	utils.Bail("(1) failed to deserialise context to string", err)
    55  	_, err = ctx.DeserialiseContext([]byte(s))
    56  	utils.Bail("(2) failed to deserialise context to string", err)
    57  	_, err = auth.RecreateContextWithTimeout(time.Duration(10)*time.Second, []byte(s))
    58  	utils.Bail("(3) failed to deserialise context to string", err)
    59  
    60  	checkFile("/tmp/context.env")
    61  	b, err := auth.SerialiseContext(ctx1)
    62  	utils.Bail("failed to serialise", err)
    63  	//fmt.Println(utils.Hexdump("Context: ", b))
    64  	ctx2, err := auth.RecreateContextWithTimeout(timeout, b)
    65  	utils.Bail("failed to deserialise", err)
    66  	mustBeSame(ctx1, ctx2)
    67  
    68  	cmdline.SetContextBuilderVersion(OLD_CONTEXT_VERSION)
    69  	fmt.Printf("Checking 'derived' context with routing...\n")
    70  	ctx1 = authremote.Context()
    71  	ctx2 = authremote.DerivedContextWithRouting(ctx1, make(map[string]string), true)
    72  	mustBeSame(ctx1, ctx2)
    73  	cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
    74  	fmt.Printf("Checking 'derived' context with routing...\n")
    75  	ctx1 = authremote.Context()
    76  	ctx2 = authremote.DerivedContextWithRouting(ctx1, make(map[string]string), true)
    77  	mustBeSame(ctx1, ctx2)
    78  
    79  	// now check all the various methods of deserialising
    80  	cmdline.SetContextBuilderVersion(OLD_CONTEXT_VERSION)
    81  	ctx1 = authremote.Context()
    82  	checkSer(ctx1)
    83  
    84  	cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
    85  	ctx1 = authremote.Context()
    86  	checkSer(ctx1)
    87  
    88  	cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
    89  	ctx1 = authremote.Context()
    90  	cmdline.SetContextBuilderVersion(OLD_CONTEXT_VERSION)
    91  	checkSer(ctx1)
    92  
    93  	cmdline.SetContextBuilderVersion(OLD_CONTEXT_VERSION)
    94  	ctx1 = authremote.Context()
    95  	cmdline.SetContextBuilderVersion(NEW_CONTEXT_VERSION)
    96  	checkSer(ctx1)
    97  
    98  }
    99  
   100  // check if serialisation and deserialisation arrives at the same context as the one passed in
   101  func checkSer(ctx1 context.Context) {
   102  	cmdline.SetEnvContext("")
   103  	b, err := auth.SerialiseContext(ctx1)
   104  	utils.Bail("failed to serialise", err)
   105  	fmt.Printf("Serialised to: %s\n", utils.HexStr(b))
   106  	ctx2, err := auth.RecreateContextWithTimeout(timeout, b)
   107  	utils.Bail("failed to deserialise", err)
   108  	mustBeSame(ctx1, ctx2)
   109  
   110  	cmdline.SetEnvContext(string(b))
   111  	ctx2 = authremote.Context()
   112  	mustBeSame(ctx1, ctx2)
   113  	cmdline.SetEnvContext("")
   114  
   115  	s, err := auth.SerialiseContextToString(ctx1)
   116  	utils.Bail("failed to serialise", err)
   117  	cmdline.SetEnvContext(s)
   118  	ctx2 = authremote.Context()
   119  	mustBeSame(ctx1, ctx2)
   120  
   121  	_, err = auth.RecreateContextWithTimeout(time.Duration(10)*time.Second, []byte(s))
   122  	if err != nil {
   123  		utils.PrintStack("deser string fail")
   124  	}
   125  	utils.Bail("failed to deserialise string", err)
   126  	cmdline.SetEnvContext("")
   127  
   128  }
   129  
   130  func printContext(ictx context.Context) {
   131  	u := auth.GetUser(ictx)
   132  	s := auth.GetService(ictx)
   133  	fmt.Printf("User   : %s\n", auth.UserIDString(u))
   134  	fmt.Printf("Service: %s\n", auth.UserIDString(s))
   135  	fmt.Printf("ctx2str: %s\n", ctx.Context2String(ictx))
   136  }
   137  func mustBeSame(ctx1, ctx2 context.Context) {
   138  	u1 := auth.GetUser(ctx1)
   139  	u2 := auth.GetUser(ctx2)
   140  	if auth.UserIDString(u1) != auth.UserIDString(u2) {
   141  		panic(fmt.Sprintf("users do not match (%s vs %s)", auth.UserIDString(u1), auth.UserIDString(u2)))
   142  	}
   143  	s1 := auth.GetService(ctx1)
   144  	s2 := auth.GetService(ctx2)
   145  	if auth.UserIDString(s1) != auth.UserIDString(s2) {
   146  		panic("services do not match")
   147  	}
   148  	//fmt.Printf("Contexts match\n")
   149  }
   150  
   151  func checkFile(filename string) {
   152  	b, err := utils.ReadFile(filename)
   153  	if err != nil {
   154  		fmt.Printf("ignoring file \"%s\", could not read it\n", filename)
   155  		return
   156  	}
   157  	ctx1, err := ctx.DeserialiseContext(b)
   158  	if err != nil {
   159  		fmt.Printf("Failed to deserialise context in file \"%s\": %s\n", filename, err)
   160  		os.Exit(10)
   161  	}
   162  	fmt.Printf("Serialised Context from file %s:\n", filename)
   163  	printContext(ctx1)
   164  }
   165  

View as plain text