add write to state store

This commit is contained in:
Fran Bull committed 2024-03-14 12:27:33 -07:00
1 parent cf8948da5f
commit d0dd8fd38d
2 files changed
+91

No files matched your search

+19
View File
@@ -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 {
+72
View File
@@ -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")
}
}