github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/paperkey_phrase_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  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestPaperKeyPhraseBasics(t *testing.T) {
    12  	p, err := MakePaperKeyPhrase(0)
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	q := NewPaperKeyPhrase(p.String())
    17  	version, err := q.Version()
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	if version != 0 {
    22  		t.Errorf("version: %d, expected 0", version)
    23  	}
    24  }
    25  
    26  func TestPaperKeyPhraseTypos(t *testing.T) {
    27  	p, err := MakePaperKeyPhrase(0)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	equivs := []string{
    33  		p.String(),
    34  		"   " + p.String(),
    35  		p.String() + "  ",
    36  		" " + p.String() + " ",
    37  		"\t" + p.String() + "  ",
    38  		" " + p.String() + "\t",
    39  		strings.Join(strings.Split(p.String(), " "), "   "),
    40  		strings.ToTitle(p.String()),
    41  		strings.ToUpper(p.String()),
    42  	}
    43  
    44  	for _, s := range equivs {
    45  		q := NewPaperKeyPhrase(s)
    46  		version, err := q.Version()
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  		if version != 0 {
    51  			t.Errorf("input: %q => version: %d, expected 0", s, version)
    52  		}
    53  		if q.String() != p.String() {
    54  			t.Errorf("input: %q => phrase %q, expected %q", s, q.String(), p.String())
    55  		}
    56  		if len(q.InvalidWords()) > 0 {
    57  			t.Errorf("input: %q => phrase %q, contains invalid words %v", s, q.String(), q.InvalidWords())
    58  		}
    59  	}
    60  
    61  	// make a typo in one of the words
    62  	w := strings.Fields(p.String())
    63  	w[0] += "qx"
    64  	x := strings.Join(w, " ")
    65  	q := NewPaperKeyPhrase(x)
    66  
    67  	// version should still be ok
    68  	version, err := q.Version()
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	if version != 0 {
    73  		t.Errorf("input: %q => version: %d, expected 0", x, version)
    74  	}
    75  
    76  	// but InvalidWords should return the first word as invalid
    77  	if len(q.InvalidWords()) == 0 {
    78  		t.Fatalf("input: %q => all words valid, expected %s to be invalid", x, w[0])
    79  	}
    80  
    81  	if q.InvalidWords()[0] != w[0] {
    82  		t.Errorf("input: %q => invalid words %v, expected %s", x, q.InvalidWords(), w[0])
    83  	}
    84  }