mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-01 19:19:07 +02:00
Migrate android profile manager
This commit is contained in:
@@ -40,9 +40,9 @@ func generateProfileID() (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// isValidProfileFilenameStem reports whether s is safe to use as the stem
|
||||
// IsValidProfileFilenameStem reports whether s is safe to use as the stem
|
||||
// of a profile JSON filename.
|
||||
func isValidProfileFilenameStem(s string) bool {
|
||||
func IsValidProfileFilenameStem(s string) bool {
|
||||
if s == "" || len(s) > maxProfileIDLen {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func (p *Profile) FilePath() (string, error) {
|
||||
return DefaultConfigPath, nil
|
||||
}
|
||||
|
||||
if !isValidProfileFilenameStem(id) {
|
||||
if !IsValidProfileFilenameStem(id) {
|
||||
return "", fmt.Errorf("invalid profile ID: %q", id)
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func (pm *ProfileManager) GetActiveProfile() (*Profile, error) {
|
||||
// supplied handle to a concrete ID, so the value written here is always a
|
||||
// valid profile filename stem.
|
||||
func (pm *ProfileManager) SwitchProfile(id string) error {
|
||||
if id != defaultProfileName && !isValidProfileFilenameStem(id) {
|
||||
if id != defaultProfileName && !IsValidProfileFilenameStem(id) {
|
||||
return fmt.Errorf("invalid profile ID: %q", id)
|
||||
}
|
||||
|
||||
|
||||
@@ -105,7 +105,7 @@ func (a *ActiveProfileState) FilePath() (string, error) {
|
||||
return DefaultConfigPath, nil
|
||||
}
|
||||
|
||||
if !isValidProfileFilenameStem(a.ID) {
|
||||
if !IsValidProfileFilenameStem(a.ID) {
|
||||
return "", fmt.Errorf("invalid profile ID: %q", a.ID)
|
||||
}
|
||||
|
||||
@@ -266,7 +266,7 @@ func (s *ServiceManager) SetActiveProfileState(a *ActiveProfileState) error {
|
||||
return fmt.Errorf("username must be set for non-default profiles, got: %s", a.ID)
|
||||
}
|
||||
|
||||
if a.ID != defaultProfileName && !isValidProfileFilenameStem(a.ID) {
|
||||
if a.ID != defaultProfileName && !IsValidProfileFilenameStem(a.ID) {
|
||||
return fmt.Errorf("invalid profile ID: %q", a.ID)
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ func (s *ServiceManager) RemoveProfile(id, username string) error {
|
||||
if id == defaultProfileName {
|
||||
return fmt.Errorf("cannot remove profile with reserved name: %s", defaultProfileName)
|
||||
}
|
||||
if !isValidProfileFilenameStem(id) {
|
||||
if !IsValidProfileFilenameStem(id) {
|
||||
return fmt.Errorf("invalid profile ID: %q", id)
|
||||
}
|
||||
|
||||
@@ -408,7 +408,7 @@ func (s *ServiceManager) GetStatePath() string {
|
||||
return defaultStatePath
|
||||
}
|
||||
|
||||
if !isValidProfileFilenameStem(activeProf.ID) {
|
||||
if !IsValidProfileFilenameStem(activeProf.ID) {
|
||||
log.Warnf("invalid active profile ID %q, using default state path", activeProf.ID)
|
||||
return defaultStatePath
|
||||
}
|
||||
@@ -478,7 +478,7 @@ func (s *ServiceManager) loadAllProfiles(username string) ([]Profile, error) {
|
||||
// default lives at the top-level config dir, not under /<user>
|
||||
continue
|
||||
}
|
||||
if !isValidProfileFilenameStem(stem) {
|
||||
if !IsValidProfileFilenameStem(stem) {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join(configDir, base)
|
||||
@@ -498,8 +498,7 @@ func (s *ServiceManager) loadAllProfiles(username string) ([]Profile, error) {
|
||||
if fileProfiles[i].Name != fileProfiles[j].Name {
|
||||
return fileProfiles[i].Name < fileProfiles[j].Name
|
||||
}
|
||||
// Stable tie-break on ID so duplicate names always render in
|
||||
// the same order across calls.
|
||||
// Sort tie-break on ID so duplicate names always render in the same order.
|
||||
return fileProfiles[i].ID < fileProfiles[j].ID
|
||||
})
|
||||
profiles = append(profiles, fileProfiles...)
|
||||
|
||||
@@ -218,7 +218,7 @@ func TestIsValidProfileFilenameStem(t *testing.T) {
|
||||
{strings.Repeat("a", maxProfileIDLen+1), false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := isValidProfileFilenameStem(tc.in)
|
||||
got := IsValidProfileFilenameStem(tc.in)
|
||||
assert.Equal(t, tc.want, got, "case %q", tc.in)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ func (pm *ProfileManager) GetProfileState(id string) (*ProfileState, error) {
|
||||
return nil, fmt.Errorf("get config directory: %w", err)
|
||||
}
|
||||
|
||||
if id != defaultProfileName && !isValidProfileFilenameStem(id) {
|
||||
if id != defaultProfileName && !IsValidProfileFilenameStem(id) {
|
||||
return nil, fmt.Errorf("invalid profile ID: %q", id)
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func (pm *ProfileManager) SetActiveProfileState(state *ProfileState) error {
|
||||
}
|
||||
|
||||
id := activeProf.ID
|
||||
if id != defaultProfileName && !isValidProfileFilenameStem(id) {
|
||||
if id != defaultProfileName && !IsValidProfileFilenameStem(id) {
|
||||
return fmt.Errorf("invalid active profile ID: %q", id)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user