github.com/astaxie/beego@v1.12.3/session/sess_file_test.go (about)

     1  // Copyright 2014 beego Author. 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  
    15  package session
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"sync"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  const sid = "Session_id"
    26  const sidNew = "Session_id_new"
    27  const sessionPath = "./_session_runtime"
    28  
    29  var (
    30  	mutex sync.Mutex
    31  )
    32  
    33  func TestFileProvider_SessionInit(t *testing.T) {
    34  	mutex.Lock()
    35  	defer mutex.Unlock()
    36  	os.RemoveAll(sessionPath)
    37  	defer os.RemoveAll(sessionPath)
    38  	fp := &FileProvider{}
    39  
    40  	_ = fp.SessionInit(180, sessionPath)
    41  	if fp.maxlifetime != 180 {
    42  		t.Error()
    43  	}
    44  
    45  	if fp.savePath != sessionPath {
    46  		t.Error()
    47  	}
    48  }
    49  
    50  func TestFileProvider_SessionExist(t *testing.T) {
    51  	mutex.Lock()
    52  	defer mutex.Unlock()
    53  	os.RemoveAll(sessionPath)
    54  	defer os.RemoveAll(sessionPath)
    55  	fp := &FileProvider{}
    56  
    57  	_ = fp.SessionInit(180, sessionPath)
    58  
    59  	if fp.SessionExist(sid) {
    60  		t.Error()
    61  	}
    62  
    63  	_, err := fp.SessionRead(sid)
    64  	if err != nil {
    65  		t.Error(err)
    66  	}
    67  
    68  	if !fp.SessionExist(sid) {
    69  		t.Error()
    70  	}
    71  }
    72  
    73  func TestFileProvider_SessionExist2(t *testing.T) {
    74  	mutex.Lock()
    75  	defer mutex.Unlock()
    76  	os.RemoveAll(sessionPath)
    77  	defer os.RemoveAll(sessionPath)
    78  	fp := &FileProvider{}
    79  
    80  	_ = fp.SessionInit(180, sessionPath)
    81  
    82  	if fp.SessionExist(sid) {
    83  		t.Error()
    84  	}
    85  
    86  	if fp.SessionExist("") {
    87  		t.Error()
    88  	}
    89  
    90  	if fp.SessionExist("1") {
    91  		t.Error()
    92  	}
    93  }
    94  
    95  func TestFileProvider_SessionRead(t *testing.T) {
    96  	mutex.Lock()
    97  	defer mutex.Unlock()
    98  	os.RemoveAll(sessionPath)
    99  	defer os.RemoveAll(sessionPath)
   100  	fp := &FileProvider{}
   101  
   102  	_ = fp.SessionInit(180, sessionPath)
   103  
   104  	s, err := fp.SessionRead(sid)
   105  	if err != nil {
   106  		t.Error(err)
   107  	}
   108  
   109  	_ = s.Set("sessionValue", 18975)
   110  	v := s.Get("sessionValue")
   111  
   112  	if v.(int) != 18975 {
   113  		t.Error()
   114  	}
   115  }
   116  
   117  func TestFileProvider_SessionRead1(t *testing.T) {
   118  	mutex.Lock()
   119  	defer mutex.Unlock()
   120  	os.RemoveAll(sessionPath)
   121  	defer os.RemoveAll(sessionPath)
   122  	fp := &FileProvider{}
   123  
   124  	_ = fp.SessionInit(180, sessionPath)
   125  
   126  	_, err := fp.SessionRead("")
   127  	if err == nil {
   128  		t.Error(err)
   129  	}
   130  
   131  	_, err = fp.SessionRead("1")
   132  	if err == nil {
   133  		t.Error(err)
   134  	}
   135  }
   136  
   137  func TestFileProvider_SessionAll(t *testing.T) {
   138  	mutex.Lock()
   139  	defer mutex.Unlock()
   140  	os.RemoveAll(sessionPath)
   141  	defer os.RemoveAll(sessionPath)
   142  	fp := &FileProvider{}
   143  
   144  	_ = fp.SessionInit(180, sessionPath)
   145  
   146  	sessionCount := 546
   147  
   148  	for i := 1; i <= sessionCount; i++ {
   149  		_, err := fp.SessionRead(fmt.Sprintf("%s_%d", sid, i))
   150  		if err != nil {
   151  			t.Error(err)
   152  		}
   153  	}
   154  
   155  	if fp.SessionAll() != sessionCount {
   156  		t.Error()
   157  	}
   158  }
   159  
   160  func TestFileProvider_SessionRegenerate(t *testing.T) {
   161  	mutex.Lock()
   162  	defer mutex.Unlock()
   163  	os.RemoveAll(sessionPath)
   164  	defer os.RemoveAll(sessionPath)
   165  	fp := &FileProvider{}
   166  
   167  	_ = fp.SessionInit(180, sessionPath)
   168  
   169  	_, err := fp.SessionRead(sid)
   170  	if err != nil {
   171  		t.Error(err)
   172  	}
   173  
   174  	if !fp.SessionExist(sid) {
   175  		t.Error()
   176  	}
   177  
   178  	_, err = fp.SessionRegenerate(sid, sidNew)
   179  	if err != nil {
   180  		t.Error(err)
   181  	}
   182  
   183  	if fp.SessionExist(sid) {
   184  		t.Error()
   185  	}
   186  
   187  	if !fp.SessionExist(sidNew) {
   188  		t.Error()
   189  	}
   190  }
   191  
   192  func TestFileProvider_SessionDestroy(t *testing.T) {
   193  	mutex.Lock()
   194  	defer mutex.Unlock()
   195  	os.RemoveAll(sessionPath)
   196  	defer os.RemoveAll(sessionPath)
   197  	fp := &FileProvider{}
   198  
   199  	_ = fp.SessionInit(180, sessionPath)
   200  
   201  	_, err := fp.SessionRead(sid)
   202  	if err != nil {
   203  		t.Error(err)
   204  	}
   205  
   206  	if !fp.SessionExist(sid) {
   207  		t.Error()
   208  	}
   209  
   210  	err = fp.SessionDestroy(sid)
   211  	if err != nil {
   212  		t.Error(err)
   213  	}
   214  
   215  	if fp.SessionExist(sid) {
   216  		t.Error()
   217  	}
   218  }
   219  
   220  func TestFileProvider_SessionGC(t *testing.T) {
   221  	mutex.Lock()
   222  	defer mutex.Unlock()
   223  	os.RemoveAll(sessionPath)
   224  	defer os.RemoveAll(sessionPath)
   225  	fp := &FileProvider{}
   226  
   227  	_ = fp.SessionInit(1, sessionPath)
   228  
   229  	sessionCount := 412
   230  
   231  	for i := 1; i <= sessionCount; i++ {
   232  		_, err := fp.SessionRead(fmt.Sprintf("%s_%d", sid, i))
   233  		if err != nil {
   234  			t.Error(err)
   235  		}
   236  	}
   237  
   238  	time.Sleep(2 * time.Second)
   239  
   240  	fp.SessionGC()
   241  	if fp.SessionAll() != 0 {
   242  		t.Error()
   243  	}
   244  }
   245  
   246  func TestFileSessionStore_Set(t *testing.T) {
   247  	mutex.Lock()
   248  	defer mutex.Unlock()
   249  	os.RemoveAll(sessionPath)
   250  	defer os.RemoveAll(sessionPath)
   251  	fp := &FileProvider{}
   252  
   253  	_ = fp.SessionInit(180, sessionPath)
   254  
   255  	sessionCount := 100
   256  	s, _ := fp.SessionRead(sid)
   257  	for i := 1; i <= sessionCount; i++ {
   258  		err := s.Set(i, i)
   259  		if err != nil {
   260  			t.Error(err)
   261  		}
   262  	}
   263  }
   264  
   265  func TestFileSessionStore_Get(t *testing.T) {
   266  	mutex.Lock()
   267  	defer mutex.Unlock()
   268  	os.RemoveAll(sessionPath)
   269  	defer os.RemoveAll(sessionPath)
   270  	fp := &FileProvider{}
   271  
   272  	_ = fp.SessionInit(180, sessionPath)
   273  
   274  	sessionCount := 100
   275  	s, _ := fp.SessionRead(sid)
   276  	for i := 1; i <= sessionCount; i++ {
   277  		_ = s.Set(i, i)
   278  
   279  		v := s.Get(i)
   280  		if v.(int) != i {
   281  			t.Error()
   282  		}
   283  	}
   284  }
   285  
   286  func TestFileSessionStore_Delete(t *testing.T) {
   287  	mutex.Lock()
   288  	defer mutex.Unlock()
   289  	os.RemoveAll(sessionPath)
   290  	defer os.RemoveAll(sessionPath)
   291  	fp := &FileProvider{}
   292  
   293  	_ = fp.SessionInit(180, sessionPath)
   294  
   295  	s, _ := fp.SessionRead(sid)
   296  	s.Set("1", 1)
   297  
   298  	if s.Get("1") == nil {
   299  		t.Error()
   300  	}
   301  
   302  	s.Delete("1")
   303  
   304  	if s.Get("1") != nil {
   305  		t.Error()
   306  	}
   307  }
   308  
   309  func TestFileSessionStore_Flush(t *testing.T) {
   310  	mutex.Lock()
   311  	defer mutex.Unlock()
   312  	os.RemoveAll(sessionPath)
   313  	defer os.RemoveAll(sessionPath)
   314  	fp := &FileProvider{}
   315  
   316  	_ = fp.SessionInit(180, sessionPath)
   317  
   318  	sessionCount := 100
   319  	s, _ := fp.SessionRead(sid)
   320  	for i := 1; i <= sessionCount; i++ {
   321  		_ = s.Set(i, i)
   322  	}
   323  
   324  	_ = s.Flush()
   325  
   326  	for i := 1; i <= sessionCount; i++ {
   327  		if s.Get(i) != nil {
   328  			t.Error()
   329  		}
   330  	}
   331  }
   332  
   333  func TestFileSessionStore_SessionID(t *testing.T) {
   334  	mutex.Lock()
   335  	defer mutex.Unlock()
   336  	os.RemoveAll(sessionPath)
   337  	defer os.RemoveAll(sessionPath)
   338  	fp := &FileProvider{}
   339  
   340  	_ = fp.SessionInit(180, sessionPath)
   341  
   342  	sessionCount := 85
   343  
   344  	for i := 1; i <= sessionCount; i++ {
   345  		s, err := fp.SessionRead(fmt.Sprintf("%s_%d", sid, i))
   346  		if err != nil {
   347  			t.Error(err)
   348  		}
   349  		if s.SessionID() != fmt.Sprintf("%s_%d", sid, i) {
   350  			t.Error(err)
   351  		}
   352  	}
   353  }
   354  
   355  func TestFileSessionStore_SessionRelease(t *testing.T) {
   356  	mutex.Lock()
   357  	defer mutex.Unlock()
   358  	os.RemoveAll(sessionPath)
   359  	defer os.RemoveAll(sessionPath)
   360  	fp := &FileProvider{}
   361  
   362  	_ = fp.SessionInit(180, sessionPath)
   363  	filepder.savePath = sessionPath
   364  	sessionCount := 85
   365  
   366  	for i := 1; i <= sessionCount; i++ {
   367  		s, err := fp.SessionRead(fmt.Sprintf("%s_%d", sid, i))
   368  		if err != nil {
   369  			t.Error(err)
   370  		}
   371  
   372  		s.Set(i, i)
   373  		s.SessionRelease(nil)
   374  	}
   375  
   376  	for i := 1; i <= sessionCount; i++ {
   377  		s, err := fp.SessionRead(fmt.Sprintf("%s_%d", sid, i))
   378  		if err != nil {
   379  			t.Error(err)
   380  		}
   381  
   382  		if s.Get(i).(int) != i {
   383  			t.Error()
   384  		}
   385  	}
   386  }