From d0dd8fd38dc797ea10a280ce75115126c0fc29c0 Mon Sep 17 00:00:00 2001 From: Fran Bull Date: Thu, 14 Mar 2024 12:27:33 -0700 Subject: [PATCH] add write to state store --- ipn/ipnlocal/profiles.go | 19 +++++++++ ipn/ipnlocal/profiles_test.go | 72 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/ipn/ipnlocal/profiles.go b/ipn/ipnlocal/profiles.go index a7c6ab944..e82945f2f 100644 --- a/ipn/ipnlocal/profiles.go +++ b/ipn/ipnlocal/profiles.go @@ -49,6 +49,25 @@ func (pm *profileManager) WriteState(id ipn.StateKey, val []byte) error { return ipn.WriteState(pm.store, id, val) } +func (pm *profileManager) WriteStateForCurrentProfile(id ipn.StateKey, val []byte) error { + var currentProfileKey ipn.StateKey + // TODO(fran) maybe we should error if the current profile is nil? + if pm.currentProfile != nil { + currentProfileKey = pm.currentProfile.Key + } + return ipn.WriteState(pm.store, currentProfileKey+"||"+id, val) +} + +func (pm *profileManager) ReadStateForCurrentProfile(id ipn.StateKey) ([]byte, error) { + var currentProfileKey ipn.StateKey + // TODO(fran) maybe we should error if the current profile is nil? + if pm.currentProfile != nil { + currentProfileKey = pm.currentProfile.Key + } + fmt.Println(currentProfileKey) + return pm.store.ReadState(currentProfileKey + "||" + id) +} + // CurrentUserID returns the current user ID. It is only non-empty on // Windows where we have a multi-user system. func (pm *profileManager) CurrentUserID() ipn.WindowsUserID { diff --git a/ipn/ipnlocal/profiles_test.go b/ipn/ipnlocal/profiles_test.go index 47f75baba..815b46af5 100644 --- a/ipn/ipnlocal/profiles_test.go +++ b/ipn/ipnlocal/profiles_test.go @@ -576,3 +576,75 @@ func TestProfileManagementWindows(t *testing.T) { t.Fatalf("CurrentUserID = %q; want %q", pm.CurrentUserID(), uid) } } + +func TestFran(t *testing.T) { + store := new(mem.Store) + + pm, err := newProfileManagerWithGOOS(store, logger.Discard, "linux") + if err != nil { + t.Fatal(err) + } + + err = pm.WriteStateForCurrentProfile("x", []byte("hello")) + if err != nil { + t.Fatal(err) + } + bs, err := pm.ReadStateForCurrentProfile("x") + if err != nil { + t.Fatal(err) + } + + s := string(bs) + fmt.Println(s) + if s != "hello" { + t.Fatal("oh") + } + + var id int + newProfile := func(t *testing.T, loginName string) ipn.PrefsView { + id++ + t.Helper() + pm.NewProfile() + p := pm.CurrentPrefs().AsStruct() + p.Persist = &persist.Persist{ + NodeID: tailcfg.StableNodeID(fmt.Sprint(id)), + PrivateNodeKey: key.NewNode(), + UserProfile: tailcfg.UserProfile{ + ID: tailcfg.UserID(id), + LoginName: loginName, + }, + } + if err := pm.SetPrefs(p.View(), ipn.NetworkProfile{}); err != nil { + t.Fatal(err) + } + return p.View() + } + + pm.SetCurrentUserID("user1") + newProfile(t, "user1") + + err = pm.WriteStateForCurrentProfile("x", []byte("hello user1")) + if err != nil { + t.Fatal(err) + } + bs, err = pm.ReadStateForCurrentProfile("x") + if err != nil { + t.Fatal(err) + } + s = string(bs) + fmt.Println(s) + if s != "hello user1" { + t.Fatal("oh") + } + + pm.SetCurrentUserID("") + bs, err = pm.ReadStateForCurrentProfile("x") + if err != nil { + t.Fatal(err) + } + s = string(bs) + fmt.Println(s) + if s != "hello" { + t.Fatal("oh") + } +}