github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/ocminvitemanager/token_test.go (about)

     1  // Copyright 2018-2023 CERN
     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  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package ocminvitemanager
    20  
    21  import (
    22  	"sync"
    23  	"testing"
    24  	"time"
    25  
    26  	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    27  )
    28  
    29  func TestCreateToken(t *testing.T) {
    30  	user := userpb.User{
    31  		Id: &userpb.UserId{
    32  			Idp:      "http://localhost:20080",
    33  			OpaqueId: "4c510ada-c86b-4815-8820-42cdf82c3d51",
    34  			Type:     userpb.UserType_USER_TYPE_PRIMARY,
    35  		},
    36  		Username:     "",
    37  		Mail:         "",
    38  		MailVerified: false,
    39  		DisplayName:  "",
    40  		Groups:       nil,
    41  		Opaque:       nil,
    42  	}
    43  
    44  	token := CreateToken(24*time.Hour, user.GetId(), "")
    45  	if token == nil {
    46  		t.Errorf("CreateToken() got = %v", token)
    47  	}
    48  	if token.GetToken() == "" {
    49  		t.Errorf("CreateToken() got = %v", token)
    50  	}
    51  	if token.GetUserId() != user.GetId() {
    52  		t.Errorf("CreateToken() got = %v", token)
    53  	}
    54  }
    55  
    56  func TestCreateTokenCollision(t *testing.T) {
    57  	tokens := sync.Map{}
    58  
    59  	user := userpb.User{
    60  		Id: &userpb.UserId{
    61  			Idp:      "http://localhost:20080",
    62  			OpaqueId: "4c510ada-c86b-4815-8820-42cdf82c3d51",
    63  			Type:     userpb.UserType_USER_TYPE_PRIMARY,
    64  		},
    65  		Username:     "",
    66  		Mail:         "",
    67  		MailVerified: false,
    68  		DisplayName:  "",
    69  		Groups:       nil,
    70  		Opaque:       nil,
    71  	}
    72  
    73  	for i := 0; i < 1000000; i++ {
    74  		token := CreateToken(24*time.Hour, user.GetId(), "")
    75  
    76  		if token == nil {
    77  			t.Errorf("CreateToken() token = %v", token)
    78  		}
    79  
    80  		_, ok := tokens.Load(token.GetToken())
    81  		if ok == true {
    82  			t.Errorf("CreateToken() there are ID collision  = %v", token)
    83  		}
    84  
    85  		tokens.Store(token.GetToken(), token)
    86  	}
    87  }