github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/tracing/traced_bcrypt_test.go (about) 1 package tracing_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/ory/x/tracing" 10 ) 11 12 func TestCompare(t *testing.T) { 13 workfactor := 10 14 hasher := &tracing.TracedBCrypt{ 15 WorkFactor: workfactor, 16 } 17 18 expectedPassword := "hello world" 19 expectedPasswordHash, err := hasher.Hash(context.TODO(), []byte(expectedPassword)) 20 assert.NoError(t, err) 21 assert.NotNil(t, expectedPasswordHash) 22 23 expectedTagsSuccess := map[string]interface{}{ 24 tracing.BCryptWorkFactorTagName: int(workfactor), 25 } 26 27 expectedTagsError := map[string]interface{}{ 28 tracing.BCryptWorkFactorTagName: int(workfactor), 29 "error": true, 30 } 31 32 testCases := []struct { 33 testDescription string 34 providedPassword string 35 expectedTags map[string]interface{} 36 shouldError bool 37 }{ 38 { 39 testDescription: "should not return an error if hash of provided password matches hash of expected password", 40 providedPassword: expectedPassword, 41 expectedTags: expectedTagsSuccess, 42 shouldError: false, 43 }, 44 { 45 testDescription: "should return an error if hash of provided password does not match hash of expected password", 46 providedPassword: "some invalid password", 47 expectedTags: expectedTagsError, 48 shouldError: true, 49 }, 50 } 51 52 for _, test := range testCases { 53 t.Run(test.testDescription, func(t *testing.T) { 54 hash, err := hasher.Hash(context.TODO(), []byte(test.providedPassword)) 55 assert.NoError(t, err) 56 assert.NotNil(t, hash) 57 58 mockedTracer.Reset() 59 60 err = hasher.Compare(context.TODO(), expectedPasswordHash, []byte(test.providedPassword)) 61 if test.shouldError { 62 assert.Error(t, err) 63 } else { 64 assert.NoError(t, err) 65 } 66 67 spans := mockedTracer.FinishedSpans() 68 assert.Len(t, spans, 1) 69 span := spans[0] 70 71 assert.Equal(t, tracing.BCryptCompareOpName, span.OperationName) 72 assert.Equal(t, test.expectedTags, span.Tags()) 73 }) 74 } 75 } 76 77 func TestHashCreatesSpanWithCorrectTags(t *testing.T) { 78 validWorkFactor := 10 79 invalidWorkFactor := 1000 // this is an invalid work factor that will cause the call to Hash to fail! 80 password := []byte("bar") 81 82 expectedTagsSuccess := map[string]interface{}{ 83 tracing.BCryptWorkFactorTagName: int(validWorkFactor), 84 } 85 86 expectedTagsError := map[string]interface{}{ 87 tracing.BCryptWorkFactorTagName: int(invalidWorkFactor), 88 "error": true, 89 } 90 91 testCases := []struct { 92 testDescription string 93 expectedTags map[string]interface{} 94 workFactor int 95 shouldError bool 96 }{ 97 { 98 testDescription: "tests expected tags are created when call to Hash succeeds", 99 expectedTags: expectedTagsSuccess, 100 workFactor: validWorkFactor, 101 shouldError: false, 102 }, 103 { 104 testDescription: "tests expected tags are created when call to Hash fails", 105 expectedTags: expectedTagsError, 106 workFactor: invalidWorkFactor, 107 shouldError: true, 108 }, 109 } 110 111 for _, test := range testCases { 112 t.Run(test.testDescription, func(t *testing.T) { 113 mockedTracer.Reset() 114 hasher := &tracing.TracedBCrypt{ 115 WorkFactor: test.workFactor, 116 } 117 118 _, err := hasher.Hash(context.TODO(), password) 119 120 if test.shouldError { 121 assert.Error(t, err) 122 } else { 123 assert.NoError(t, err) 124 } 125 126 spans := mockedTracer.FinishedSpans() 127 assert.Len(t, spans, 1) 128 span := spans[0] 129 130 assert.Equal(t, tracing.BCryptHashOpName, span.OperationName) 131 assert.Equal(t, test.expectedTags, span.Tags()) 132 }) 133 } 134 }