github.com/XiaoMi/Gaea@v1.2.5/proxy/server/manager_test.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package server
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/XiaoMi/Gaea/models"
    20  	"github.com/XiaoMi/Gaea/mysql"
    21  )
    22  
    23  type userinfo struct {
    24  	username string
    25  	password string
    26  }
    27  
    28  type usercase struct {
    29  	username  string
    30  	password  string
    31  	namespace string
    32  }
    33  
    34  func TestCreateUserManagerFromNamespaceConfigs(t *testing.T) {
    35  	nsCfg := prepareNamespaceUsers()
    36  	_, err := CreateUserManager(nsCfg)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  }
    41  
    42  func TestUserManager_CheckUser(t *testing.T) {
    43  	nsCfg := prepareNamespaceUsers()
    44  	userManager, err := CreateUserManager(nsCfg)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	tests := []struct {
    50  		username string
    51  		valid    bool
    52  	}{
    53  		{username: "user1", valid: true},
    54  		{username: "user2", valid: true},
    55  		{username: "user3", valid: false},
    56  		{username: "", valid: false},
    57  	}
    58  	for _, test := range tests {
    59  		t.Run(test.username, func(t *testing.T) {
    60  			actualValid := userManager.CheckUser(test.username)
    61  			if actualValid != test.valid {
    62  				t.Errorf("CheckUser error, username: %s, expect: %t, actual: %t", test.username, test.valid, actualValid)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func TestUserManager_GetNamespaceByUser(t *testing.T) {
    69  	nsCfg := prepareNamespaceUsers()
    70  	userManager, err := CreateUserManager(nsCfg)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	tests := []usercase{
    76  		{username: "", password: "", namespace: ""},
    77  		{username: "", password: "pwd", namespace: ""},
    78  		{username: "user", password: "", namespace: ""},
    79  		{username: "user", password: "pwd", namespace: ""},
    80  		{username: "user1", password: "pwd1", namespace: "namespace1"},
    81  		{username: "user1", password: "pwd2", namespace: "namespace1"},
    82  		{username: "user2", password: "pwd1", namespace: "namespace1"},
    83  		{username: "user2", password: "pwd2", namespace: "namespace2"},
    84  		{username: "user2", password: "pwd3", namespace: "namespace2"},
    85  		{username: "user1", password: "pwd3", namespace: "namespace2"},
    86  		{username: "user1", password: "pwd4", namespace: ""},
    87  		{username: "user2", password: "pwd4", namespace: ""},
    88  	}
    89  	for _, test := range tests {
    90  		t.Run(test.username, func(t *testing.T) {
    91  			actualNamespace := userManager.GetNamespaceByUser(test.username, test.password)
    92  			if actualNamespace != test.namespace {
    93  				t.Errorf("GetNamespaceByUser error, username: %s, password: %s, expect: %s, actual: %s", test.username, test.password, test.namespace, actualNamespace)
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestUserManager_ClearNamespaceUsers_Namespace0(t *testing.T) {
   100  	nsCfg := prepareNamespaceUsers()
   101  	userManager, err := CreateUserManager(nsCfg)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	userManager.ClearNamespaceUsers("namespace0")
   106  
   107  	tests := []usercase{
   108  		{username: "", password: "", namespace: ""},
   109  		{username: "", password: "pwd", namespace: ""},
   110  		{username: "user", password: "", namespace: ""},
   111  		{username: "user", password: "pwd", namespace: ""},
   112  		{username: "user1", password: "pwd1", namespace: "namespace1"},
   113  		{username: "user1", password: "pwd2", namespace: "namespace1"},
   114  		{username: "user2", password: "pwd1", namespace: "namespace1"},
   115  		{username: "user2", password: "pwd2", namespace: "namespace2"},
   116  		{username: "user2", password: "pwd3", namespace: "namespace2"},
   117  		{username: "user1", password: "pwd3", namespace: "namespace2"},
   118  		{username: "user1", password: "pwd4", namespace: ""},
   119  		{username: "user2", password: "pwd4", namespace: ""},
   120  	}
   121  	for _, test := range tests {
   122  		t.Run(test.username, func(t *testing.T) {
   123  			actualNamespace := userManager.GetNamespaceByUser(test.username, test.password)
   124  			if actualNamespace != test.namespace {
   125  				t.Errorf("GetNamespaceByUser error, username: %s, password: %s, expect: %s, actual: %s", test.username, test.password, test.namespace, actualNamespace)
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func TestUserManager_ClearNamespaceUsers_Namespace1(t *testing.T) {
   132  	nsCfg := prepareNamespaceUsers()
   133  	userManager, err := CreateUserManager(nsCfg)
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  	userManager.ClearNamespaceUsers("namespace1")
   138  
   139  	tests := []usercase{
   140  		{username: "", password: "", namespace: ""},
   141  		{username: "", password: "pwd", namespace: ""},
   142  		{username: "user", password: "", namespace: ""},
   143  		{username: "user", password: "pwd", namespace: ""},
   144  		{username: "user1", password: "pwd1", namespace: ""},
   145  		{username: "user1", password: "pwd2", namespace: ""},
   146  		{username: "user2", password: "pwd1", namespace: ""},
   147  		{username: "user2", password: "pwd2", namespace: "namespace2"},
   148  		{username: "user2", password: "pwd3", namespace: "namespace2"},
   149  		{username: "user1", password: "pwd3", namespace: "namespace2"},
   150  		{username: "user1", password: "pwd4", namespace: ""},
   151  		{username: "user2", password: "pwd4", namespace: ""},
   152  	}
   153  	for _, test := range tests {
   154  		t.Run(test.username, func(t *testing.T) {
   155  			actualNamespace := userManager.GetNamespaceByUser(test.username, test.password)
   156  			if actualNamespace != test.namespace {
   157  				t.Errorf("GetNamespaceByUser error, username: %s, password: %s, expect: %s, actual: %s", test.username, test.password, test.namespace, actualNamespace)
   158  			}
   159  		})
   160  	}
   161  }
   162  
   163  func TestUserManager_ClearNamespaceUsers_Namespace2(t *testing.T) {
   164  	nsCfg := prepareNamespaceUsers()
   165  	userManager, err := CreateUserManager(nsCfg)
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  	userManager.ClearNamespaceUsers("namespace2")
   170  
   171  	tests := []usercase{
   172  		{username: "", password: "", namespace: ""},
   173  		{username: "", password: "pwd", namespace: ""},
   174  		{username: "user", password: "", namespace: ""},
   175  		{username: "user", password: "pwd", namespace: ""},
   176  		{username: "user1", password: "pwd1", namespace: "namespace1"},
   177  		{username: "user1", password: "pwd2", namespace: "namespace1"},
   178  		{username: "user2", password: "pwd1", namespace: "namespace1"},
   179  		{username: "user2", password: "pwd2", namespace: ""},
   180  		{username: "user2", password: "pwd3", namespace: ""},
   181  		{username: "user1", password: "pwd3", namespace: ""},
   182  		{username: "user1", password: "pwd4", namespace: ""},
   183  		{username: "user2", password: "pwd4", namespace: ""},
   184  	}
   185  	for _, test := range tests {
   186  		t.Run(test.username, func(t *testing.T) {
   187  			actualNamespace := userManager.GetNamespaceByUser(test.username, test.password)
   188  			if actualNamespace != test.namespace {
   189  				t.Errorf("GetNamespaceByUser error, username: %s, password: %s, expect: %s, actual: %s", test.username, test.password, test.namespace, actualNamespace)
   190  			}
   191  		})
   192  	}
   193  }
   194  
   195  func TestUserManager_RebuildNamespaceUsers_Namespace1(t *testing.T) {
   196  	nsCfg := prepareNamespaceUsers()
   197  	userManager, err := CreateUserManager(nsCfg)
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  
   202  	ns := "namespace1"
   203  	user1 := &userinfo{username: "user1", password: "pwd1"}
   204  	user2 := &userinfo{username: "user1", password: "pwd4"}
   205  	user3 := &userinfo{username: "user3", password: "pwd1"}
   206  
   207  	newNamespace := createNamespaceUsers(ns, []*userinfo{user1, user2, user3})
   208  	userManager.RebuildNamespaceUsers(newNamespace)
   209  
   210  	tests := []usercase{
   211  		{username: "", password: "", namespace: ""},
   212  		{username: "", password: "pwd", namespace: ""},
   213  		{username: "user", password: "", namespace: ""},
   214  		{username: "user", password: "pwd", namespace: ""},
   215  		{username: "user1", password: "pwd1", namespace: "namespace1"},
   216  		{username: "user1", password: "pwd2", namespace: ""},
   217  		{username: "user1", password: "pwd4", namespace: "namespace1"},
   218  		{username: "user3", password: "pwd1", namespace: "namespace1"},
   219  		{username: "user2", password: "pwd1", namespace: ""},
   220  		{username: "user2", password: "pwd2", namespace: "namespace2"},
   221  		{username: "user2", password: "pwd3", namespace: "namespace2"},
   222  		{username: "user1", password: "pwd3", namespace: "namespace2"},
   223  	}
   224  	for _, test := range tests {
   225  		t.Run(test.username, func(t *testing.T) {
   226  			actualNamespace := userManager.GetNamespaceByUser(test.username, test.password)
   227  			if actualNamespace != test.namespace {
   228  				t.Errorf("GetNamespaceByUser error, username: %s, password: %s, expect: %s, actual: %s", test.username, test.password, test.namespace, actualNamespace)
   229  			}
   230  		})
   231  	}
   232  }
   233  
   234  func TestUserManager_RebuildNamespaceUsers_Namespace2(t *testing.T) {
   235  	nsCfg := prepareNamespaceUsers()
   236  	userManager, err := CreateUserManager(nsCfg)
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  
   241  	ns := "namespace2"
   242  	user1 := &userinfo{username: "user5", password: "pwd1"}
   243  
   244  	newNamespace := createNamespaceUsers(ns, []*userinfo{user1})
   245  	userManager.RebuildNamespaceUsers(newNamespace)
   246  
   247  	tests := []usercase{
   248  		{username: "", password: "", namespace: ""},
   249  		{username: "", password: "pwd", namespace: ""},
   250  		{username: "user", password: "", namespace: ""},
   251  		{username: "user", password: "pwd", namespace: ""},
   252  		{username: "user1", password: "pwd1", namespace: "namespace1"},
   253  		{username: "user1", password: "pwd2", namespace: "namespace1"},
   254  		{username: "user2", password: "pwd1", namespace: "namespace1"},
   255  		{username: "user2", password: "pwd2", namespace: ""},
   256  		{username: "user2", password: "pwd3", namespace: ""},
   257  		{username: "user1", password: "pwd3", namespace: ""},
   258  		{username: "user5", password: "pwd1", namespace: "namespace2"},
   259  	}
   260  	for _, test := range tests {
   261  		t.Run(test.username, func(t *testing.T) {
   262  			actualNamespace := userManager.GetNamespaceByUser(test.username, test.password)
   263  			if actualNamespace != test.namespace {
   264  				t.Errorf("GetNamespaceByUser error, username: %s, password: %s, expect: %s, actual: %s", test.username, test.password, test.namespace, actualNamespace)
   265  			}
   266  		})
   267  	}
   268  }
   269  
   270  func TestUserManager_CheckPassword(t *testing.T) {
   271  	nsCfg := prepareNamespaceUsers()
   272  	userManager, err := CreateUserManager(nsCfg)
   273  	if err != nil {
   274  		t.Fatal(err)
   275  	}
   276  	salt := []byte("abcdefg_?!")
   277  
   278  	tests := []struct {
   279  		username string
   280  		password string
   281  		valid    bool
   282  	}{
   283  		{username: "user1", password: "pwd1", valid: true},
   284  		{username: "user1", password: "pwd2", valid: true},
   285  		{username: "user1", password: "pwd4", valid: false},
   286  		{username: "user2", password: "pwd1", valid: true},
   287  		{username: "user2", password: "pwd4", valid: false},
   288  		{username: "user3", password: "pwd", valid: false},
   289  	}
   290  	for _, test := range tests {
   291  		t.Run(test.username, func(t *testing.T) {
   292  			auth := mysql.CalcPassword(salt, []byte(test.password))
   293  			actualValid, actualPassword := userManager.CheckPassword(test.username, salt, auth)
   294  			if actualValid == test.valid {
   295  				if actualValid && (actualPassword != test.password) {
   296  					t.Errorf("password not equal, expect: %v, acutal: %t, %s", test, actualValid, actualPassword)
   297  				}
   298  			} else {
   299  				t.Errorf("valid not equal, expect: %v, acutal: %t, %s", test, actualValid, actualPassword)
   300  			}
   301  		})
   302  	}
   303  }
   304  
   305  func prepareNamespaceUsers() map[string]*models.Namespace {
   306  	nsMap := make(map[string]*models.Namespace)
   307  	ns1 := "namespace1"
   308  	ns1user1 := &userinfo{username: "user1", password: "pwd1"}
   309  	ns1user2 := &userinfo{username: "user1", password: "pwd2"}
   310  	ns1user3 := &userinfo{username: "user2", password: "pwd1"}
   311  	namespace1 := createNamespaceUsers(ns1, []*userinfo{ns1user1, ns1user2, ns1user3})
   312  	nsMap[ns1] = namespace1
   313  
   314  	ns2 := "namespace2"
   315  	ns2user1 := &userinfo{username: "user2", password: "pwd2"}
   316  	ns2user2 := &userinfo{username: "user2", password: "pwd3"}
   317  	ns2user3 := &userinfo{username: "user1", password: "pwd3"}
   318  	namespace2 := createNamespaceUsers(ns2, []*userinfo{ns2user1, ns2user2, ns2user3})
   319  	nsMap[ns2] = namespace2
   320  
   321  	return nsMap
   322  }
   323  
   324  func createNamespaceUsers(ns string, users []*userinfo) *models.Namespace {
   325  	var userList []*models.User
   326  	for _, user := range users {
   327  		u := &models.User{
   328  			UserName: user.username,
   329  			Password: user.password,
   330  		}
   331  		userList = append(userList, u)
   332  	}
   333  	return &models.Namespace{
   334  		Name:  ns,
   335  		Users: userList,
   336  	}
   337  }