github.com/cs3org/reva/v2@v2.27.7/pkg/ocm/invite/token/token_test.go (about)

     1  // Copyright 2018-2021 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 token
    20  
    21  import (
    22  	"sync"
    23  	"testing"
    24  
    25  	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    26  )
    27  
    28  func TestCreateToken(t *testing.T) {
    29  
    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, err := CreateToken("24h", user.GetId())
    45  	if err != nil {
    46  		t.Errorf("CreateToken() error = %v", err)
    47  	}
    48  	if token == nil {
    49  		t.Errorf("CreateToken() got = %v", token)
    50  	}
    51  	if token.GetToken() == "" {
    52  		t.Errorf("CreateToken() got = %v", token)
    53  	}
    54  	if token.GetUserId() != user.GetId() {
    55  		t.Errorf("CreateToken() got = %v", token)
    56  	}
    57  }
    58  
    59  func TestCreateTokenCollision(t *testing.T) {
    60  
    61  	tokens := sync.Map{}
    62  
    63  	user := userpb.User{
    64  		Id: &userpb.UserId{
    65  			Idp:      "http://localhost:20080",
    66  			OpaqueId: "4c510ada-c86b-4815-8820-42cdf82c3d51",
    67  			Type:     userpb.UserType_USER_TYPE_PRIMARY,
    68  		},
    69  		Username:     "",
    70  		Mail:         "",
    71  		MailVerified: false,
    72  		DisplayName:  "",
    73  		Groups:       nil,
    74  		Opaque:       nil,
    75  	}
    76  
    77  	for i := 0; i < 1000000; i++ {
    78  		token, err := CreateToken("24h", user.GetId())
    79  		if err != nil {
    80  			t.Errorf("CreateToken() error = %v", err)
    81  		}
    82  		if token == nil {
    83  			t.Errorf("CreateToken() token = %v", token)
    84  		}
    85  
    86  		_, ok := tokens.Load(token.GetToken())
    87  		if ok == true {
    88  			t.Errorf("CreateToken() there are ID collision  = %v", token)
    89  		}
    90  
    91  		tokens.Store(token.GetToken(), token)
    92  	}
    93  }