Repository navigation
fix: Luhn check digit for card numbers and a new card type on every call - #105
Conversation
CCNumber and the cc_number tag made a prefix and random digits, so only 1 number in 10 passed the Luhn check that a shop or a form runs on a card number. The last digit is the check digit now. cctype kept the first card type it picked in a package variable and never let it go, so CCType returned the same type on every call (go-faker#24) and every struct got the same one. It also read the creditCards map in its random order, so the same seed could pick another first type. Now every call picks a type from the keys in a fixed order, and a struct gets a card of its own: its cc_type and cc_number fields still agree, the next struct gets another. The variable is behind a mutex, so parallel FakeData calls do not write it at the same time. Fixes go-faker#24
|
Hey, package faker
import (
"sync"
"sync/atomic"
"testing"
)
func TestZZConcurrentMismatch(t *testing.T) {
type C struct {
T string `faker:"cc_type"`
N string `faker:"cc_number"`
}
var bad, total atomic.Int64
var wg sync.WaitGroup
for range 8 {
wg.Add(1)
go func() {
defer wg.Done()
for range 5000 {
var c C
_ = FakeData(&c)
total.Add(1)
if !cardNumberMatches(c.N, c.T) {
bad.Add(1)
}
}
}()
}
wg.Wait()
if bad.Load() > 0 {
t.Errorf("cc_type and cc_number mismatched in %d of %d structs", bad.Load(), total.Load())
}
}It fails on concurrent, the data is not matched. |
| defer cacheCreditCardMu.Unlock() | ||
|
|
||
| outer := cacheCreditCard | ||
| cacheCreditCard = "" |
There was a problem hiding this comment.
I think the issue is here this is on package-level but reset here while another goroutine's struct is still using it.
There was a problem hiding this comment.
Maybe the solution is to keep the card per FakeData call instead of a package global?
getFakedValueForStruct (
Line 586 in eb9c7e5
opts down, so maybe it can live there. Then no mutex needed.
…ariable The card of a struct was kept in a package variable behind a mutex. The mutex kept the writes from racing, but not the pairing: a goroutine that started another struct put the variable back to empty while this struct was half filled, so its cc_number got another card than its cc_type. With 8 goroutines that happened in 29,809 of 40,000 structs. getFakedValueForStruct keeps the card in a variable of its own now, one per struct, picked by the first of its cc_type and cc_number fields. A struct inside a struct has a card of its own, and a struct that is filled at the same time in another goroutine does not share it. There is no package state and no mutex left.
|
Thanks, you're right: the mutex stopped the writes from racing, but not the pairing. I ran your test on my commit and it failed in 29,809 of 40,000 structs. I pushed a new commit: the card is a variable of |
|
Thanks, this is better now One thing I found: since faker.RemoveProvider("cc_type")
faker.AddProvider("cc_type", func(v reflect.Value) (any, error) { return "MY-CARD", nil })On master, a struct field with |
…struct A struct filled its cc_type and cc_number fields from the card of the struct without asking the registry, so a provider that a caller put in place of the built-in one (RemoveProvider, then AddProvider) was ignored there, and a removed provider did not give the unsupported tag error. mapperTag marks the providers that the package registers itself. The fields of a struct get the card of the struct only while the provider of their tag is still that one. A provider of the caller fills the field like it does for every other tag, and with no provider left the tag is an error again.
|
Good catch, thanks. I pushed a new commit: the card of the struct is used for New test One thing to know: if a caller registers the built-in function itself ( |
CCNumber()and thecc_numbertag only made a prefix and random digits, so about 9 in 10 numbers fail the Luhn check that a shop or a form runs on a card number, andCCType()returned the same type on every call after the first (#24).Why
ccnumbertook a prefix of the card type and filled the rest with random digits, with no check digit. The last digit is random, so exactly 1 in 10 numbers passes (60,236 of 600,000 in a copy of the rule; in the new test on master 5,375 of 6,000 fail).cctypekept the first type it picked in the package variablecacheCreditCardand never reset it, soCCType()is one value for the rest of the program and every struct you fill gets the same card type. The variable is there so thecc_typeand thecc_numberof one struct are the same card, which is worth keeping.cctypealso made its list of types by ranging over thecreditCardsmap, which Go reads in a random order, so the same seed (SetRandomSource) could pick another first type.What changed
creditCardNumberends a number with the Luhn check digit (luhnCheckDigit). Lengths and prefixes are as before, so it has one random digit less.CCType()andCCNumber()pick a card type from the sorted keys ofcreditCardson every call, so a seed gives the same sequence. The methods ofGetPayment()do the same outside of a struct.cc_typeandcc_numberfields still get one card, in either order.getFakedValueForStructstarts a card for each struct and puts the outer one back when it is done, so the elements of a slice get different cards, and a struct inside a struct has a card of its own. The variable is behind a mutex.Fixes #24
How it was tested
payment_test.go.luhnCheckDigit: the six card numbers that the payment providers publish for tests, and every prefix of 4 and of 5 digits (exactly one check digit passes and it is the one returned). The numbers ofCCNumber()pass the Luhn check,CCType()gives all six types in 600 calls and the same ones for the same seed, and a struct (in either field order) and the elements of a slice get a number that fits the type of their own card (length, prefix, check digit). Six of the tests fail on master.isLuhnNumberof validator.js, andisCreditCardaccepts them with the provider of their type, except the Diners Club ones that start with 39 (3,382 of 10,111): its pattern for that brand does not know the prefix, I left the prefixes alone.go test ./...,go vetandgofmtpass. I could not run-raceor golangci-lint here (no C compiler and no linter installed), CI does.