diff --git a/brimfeather/feather_test.go b/brimfeather/feather_test.go new file mode 100644 index 0000000..e7fcb36 --- /dev/null +++ b/brimfeather/feather_test.go @@ -0,0 +1,55 @@ +package main + +import ( + cap2 "github.com/trimble-oss/tierceron-hat/cap" + captiplib "github.com/trimble-oss/tierceron-hat/captip/captiplib" + "os" + "sync" + "testing" +) + +func featherInterrupted(featherCtx *cap2.FeatherContext) error { + cap2.FeatherCtlEmit(featherCtx, string(cap2.MODE_PERCH), *featherCtx.SessionIdentifier, true) + os.Exit(-1) + return nil +} + +func TestGetSaltyGuardian(t *testing.T) { + cap2.TapInitCodeSaltGuard(func() string { return "ExtraSaltPlease" }) + + var serverStart sync.WaitGroup + serverStart.Add(1) + go func() { + go cap2.Feather("Som18vhjqa72935h", "1cx7v89as7df89", "127.0.0.1:1832", "ThisIsACode", func(int, string) bool { return true }) + + cap2.TapFeather("I think", "therefore I am.") + cap2.TapFeather("It is not enough to have a good mind.", "The main thing is to use it well.") + + cap2.TapFeather("Ponder", "a feather.") + + go cap2.TapServer("127.0.0.1:1534") + serverStart.Done() + }() + + serverStart.Wait() + localHostAddr := "localhost:1534" + encryptPass := "Som18vhjqa72935h" + encryptSalt := "1cx7v89as7df89" + hostAddr := "127.0.0.1:1832" + handshakeCode := "ThisIsACode" + sessionIdentifier := "FeatherSessionOne" + env := "SomeEnv" + + var interruptChan chan os.Signal = make(chan os.Signal, 5) + featherCtx := captiplib.FeatherCtlInit(interruptChan, &localHostAddr, &encryptPass, &encryptSalt, &hostAddr, &handshakeCode, &sessionIdentifier, &env, captiplib.AcceptRemote, featherInterrupted) + + expected := "therefore I am." + msg, err := captiplib.FeatherQueryCache(featherCtx, "I think") + if err != nil { + t.Fatalf("Expected '%s', got %s", expected, err.Error()) + } + + if msg != expected { + t.Fatalf("Expected '%s', got %s", expected, msg) + } +} diff --git a/cap/cap.go b/cap/cap.go index b6dda7e..f1c6dab 100644 --- a/cap/cap.go +++ b/cap/cap.go @@ -83,12 +83,12 @@ var penseFeatherPluckMap = cmap.New[bool]() var penseFeatherCtlCodeMap = cmap.New[string]() // CodeSaltGuardFn is expected to return a hex.EncodeToString encoded salt -type CodeSaltGuardFn func() string +type CodeSaltGuardFunc func() string -var codeSaltGuardFn CodeSaltGuardFn = nil +var CodeSaltGuardFn CodeSaltGuardFunc = nil -func TapInitCodeSaltGuard(csgFn CodeSaltGuardFn) { - codeSaltGuardFn = csgFn +func TapInitCodeSaltGuard(csgFn CodeSaltGuardFunc) { + CodeSaltGuardFn = csgFn } func TapServer(address string, opt ...grpc.ServerOption) { @@ -209,8 +209,8 @@ func handleMessage(handshakeCode string, conn *kcp.UDPSession, acceptRemote func messageParts := strings.Split(message, string(PROTOCOL_DELIM)) if messageParts[0] == handshakeCode { featherCode := messageParts[1] - if codeSaltGuardFn != nil { - codeSalt := codeSaltGuardFn() + if CodeSaltGuardFn != nil { + codeSalt := CodeSaltGuardFn() if len(codeSalt) > 0 { if strings.HasSuffix(featherCode, codeSalt) { featherCode = strings.TrimSuffix(featherCode, codeSalt) @@ -222,7 +222,7 @@ func handleMessage(handshakeCode string, conn *kcp.UDPSession, acceptRemote func } if len(featherCode) == 64 { - penseFeatherCodeMap.Set(messageParts[1], "") + penseFeatherCodeMap.Set(featherCode, "") } } } diff --git a/captip/captiplib/captiplib.go b/captip/captiplib/captiplib.go index f9956f1..1e1c95e 100644 --- a/captip/captiplib/captiplib.go +++ b/captip/captiplib/captiplib.go @@ -222,6 +222,7 @@ func FeatherQueryCache(featherCtx *cap.FeatherContext, pense string) (string, er penseCode := randomString(12 + rand.Intn(7)) penseArray := sha256.Sum256([]byte(penseCode)) penseSum := hex.EncodeToString(penseArray[:]) + penseSum = penseSum + cap.CodeSaltGuardFn() _, featherErr := cap.FeatherWriter(featherCtx, penseSum) if featherErr != nil {