github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/checkers_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  type checkerTest struct {
    13  	input string
    14  	valid bool
    15  }
    16  
    17  var usernameTests = []checkerTest{
    18  	{input: "a", valid: false},
    19  	{input: "az", valid: true},
    20  	{input: "0123456789abcdef", valid: true},
    21  	{input: "0123456789abcdefg", valid: false},
    22  	{input: "_foo", valid: false},
    23  	{input: "foo_bar_baz", valid: true},
    24  	{input: "foo__bar", valid: false},
    25  	{input: "Upper_Case", valid: true},
    26  }
    27  
    28  func TestCheckUsername(t *testing.T) {
    29  	for _, test := range usernameTests {
    30  		res := CheckUsername.F(test.input)
    31  		require.Equal(t, res, test.valid)
    32  	}
    33  }
    34  
    35  var deviceNameTests = []checkerTest{
    36  	{input: "home computer", valid: true},
    37  	{input: " home computer", valid: false},
    38  	{input: " home computer ", valid: false},
    39  	{input: "home computer ", valid: false},
    40  	{input: "home  computer", valid: false},
    41  	{input: "home - computer", valid: true},
    42  	{input: "Mike's computer", valid: true},
    43  	{input: "tab\tcomputer", valid: false},
    44  	{input: "foo -- computer", valid: false},
    45  	{input: "foo - _ - computer", valid: false},
    46  	{input: "home computer-", valid: false},
    47  	{input: "home computer+", valid: true},
    48  	{input: "home computer'", valid: false},
    49  	{input: "home computer_", valid: false},
    50  	{input: "not😂ascii", valid: false},
    51  	{input: "John’s iPhone", valid: true},
    52  	{input: "absolute@unit", valid: false},
    53  	{input: "absolute(unit", valid: false},
    54  }
    55  
    56  func TestCheckDeviceName(t *testing.T) {
    57  	for _, test := range deviceNameTests {
    58  		res := CheckDeviceName.F(test.input)
    59  		require.Equal(t, res, test.valid)
    60  	}
    61  }
    62  
    63  type normalizeTest struct {
    64  	name1 string
    65  	name2 string
    66  }
    67  
    68  var deviceNormalizeTests = []normalizeTest{
    69  	{name1: "home computer", name2: "homecomputer"},
    70  	{name1: "home - computer", name2: "homecomputer"},
    71  	{name1: "Mike's computer", name2: "mikescomputer"},
    72  	{name1: "John’s iPhone", name2: "johnsiphone"},
    73  }
    74  
    75  func TestNormalizeDeviceName(t *testing.T) {
    76  	for _, test := range deviceNormalizeTests {
    77  		require.Equal(t, test.name2, CheckDeviceName.Normalize(test.name1))
    78  	}
    79  }