...

Source file src/github.com/ory/x/randx/sequence_test.go

Documentation: github.com/ory/x/randx

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   */
    20  
    21  package randx
    22  
    23  import (
    24  	"regexp"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestRunePatterns(t *testing.T) {
    31  	for k, v := range []struct {
    32  		runes       []rune
    33  		shouldMatch string
    34  	}{
    35  		{Alpha, "[a-zA-Z]{52}"},
    36  		{AlphaLower, "[a-z]{26}"},
    37  		{AlphaUpper, "[A-Z]{26}"},
    38  		{AlphaNum, "[a-zA-Z0-9]{62}"},
    39  		{AlphaLowerNum, "[a-z0-9]{36}"},
    40  		{AlphaUpperNum, "[A-Z0-9]{36}"},
    41  		{Numeric, "[0-9]{10}"},
    42  	} {
    43  		valid, err := regexp.Match(v.shouldMatch, []byte(string(v.runes)))
    44  		assert.Nil(t, err, "Case %d", k)
    45  		assert.True(t, valid, "Case %d", k)
    46  	}
    47  }
    48  
    49  func TestRuneSequenceMatchesPattern(t *testing.T) {
    50  	for k, v := range []struct {
    51  		runes       []rune
    52  		shouldMatch string
    53  		length      int
    54  	}{
    55  		{Alpha, "[a-zA-Z]+", 25},
    56  		{AlphaLower, "[a-z]+", 46},
    57  		{AlphaUpper, "[A-Z]+", 21},
    58  		{AlphaNum, "[a-zA-Z0-9]+", 123},
    59  		{AlphaLowerNum, "[a-z0-9]+", 41},
    60  		{AlphaUpperNum, "[A-Z0-9]+", 94914},
    61  		{Numeric, "[0-9]+", 94914},
    62  	} {
    63  		seq, err := RuneSequence(v.length, v.runes)
    64  		assert.Nil(t, err, "case %d", k)
    65  		assert.Equal(t, v.length, len(seq), "case %d", k)
    66  
    67  		valid, err := regexp.Match(v.shouldMatch, []byte(string(seq)))
    68  		assert.Nil(t, err, "case %d", k)
    69  		assert.True(t, valid, "case %d\nrunes %s\nresult %s", k, v.runes, string(seq))
    70  	}
    71  }
    72  
    73  func TestRuneSequenceIsPseudoUnique(t *testing.T) {
    74  	if testing.Short() {
    75  		t.SkipNow()
    76  	}
    77  
    78  	times := 100
    79  	runes := []rune("ab")
    80  	length := 32
    81  	s := make(map[string]bool)
    82  
    83  	for i := 0; i < times; i++ {
    84  		k, err := RuneSequence(length, runes)
    85  		assert.Nil(t, err)
    86  		ks := string(k)
    87  		_, ok := s[ks]
    88  		assert.False(t, ok)
    89  		if ok {
    90  			return
    91  		}
    92  		s[ks] = true
    93  	}
    94  }
    95  
    96  func BenchmarkTestInt64(b *testing.B) {
    97  	length := 25
    98  	pattern := []rune("abcdefghijklmnopqrstuvwxyz")
    99  	for i := 0; i < b.N; i++ {
   100  		RuneSequence(length, pattern)
   101  	}
   102  }
   103  

View as plain text