Skip to content

Commit

Permalink
Reworked put-card to fail with ErrInvalidCard for card numbers 0,0xff…
Browse files Browse the repository at this point in the history
…ffffff and 0x00ffffff
  • Loading branch information
twystd committed Jun 3, 2024
1 parent 432a5b0 commit 61d4334
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
1. Reworked UT0311-L0x driver to use connected UDP sockets.
2. Renamed _master_ branch to _main_ branch.
3. Updated network stack to use modern Go IP constructs.
4. `put-card` fails with 'invalid card' for card numbers 0, 0xffffffff and 0x00ffffff (as per SDK).


## [0.8.8](https://github.com/uhppoted/uhppote-core/releases/tag/v0.8.8) - 2024-03-26
Expand Down
8 changes: 5 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

- [x] TCP/IP protocol (cf. https://github.com/uhppoted/uhppote-core/issues/17)
- [x] Add timezone to NewDevice
- (?) Replace (* UHPPOTE) in API functions with non-pointer version
- [ ] Rework any remaining Date/DateTime pointers to rather use IsZero/IsValid
- [x] `put-card`: error on bad card numbers (0,0xffffffff,0x00ffffff)
- [ ] Rework any remaining Date pointers to rather use IsZero/IsValid
- [ ] GetTimeProfile
- [ ] lib::encoding/tsv
- [ ] simulator get-device ReleaseDate
- [ ] uhppoted-app-wild-apricot types.Date
- [ ] uhppoted-rest acl.go
- [ ] `put-card`: error on bad card numbers (0,0xffffffff,0x00ffffff)
- [ ] Rework any remaining DateTime pointers to rather use IsZero/IsValid
- [ ] Rework any remaining Time pointers to rather use IsZero/IsValid
- (?) Replace (* UHPPOTE) in API functions with non-pointer version

## TODO

Expand Down
1 change: 1 addition & 0 deletions uhppote/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import (

var ErrInvalidListenerAddress = errors.New("invalid listener address")
var ErrIncorrectController = errors.New("response from incorrect controller")
var ErrInvalidCard = errors.New("invalid card")
4 changes: 4 additions & 0 deletions uhppote/put_card.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func (u *uhppote) PutCard(deviceID uint32, card types.Card, formats ...types.Car
return false, fmt.Errorf("invalid device ID (%v)", deviceID)
}

if card.CardNumber == 0 || card.CardNumber == 0xffffffff || card.CardNumber == 0x00ffffff {
return false, ErrInvalidCard
}

if !isCardNumberValid(card.CardNumber, formats...) {
return false, fmt.Errorf("invalid card number (%v)", card.CardNumber)
}
Expand Down
30 changes: 30 additions & 0 deletions uhppote/put_card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uhppote

import (
"bytes"
"errors"
"fmt"
"net"
"testing"
Expand Down Expand Up @@ -84,6 +85,35 @@ func TestPutCardWithInvalidDeviceID(t *testing.T) {
}
}

func TestPutCardWithInvalidCardNumber(t *testing.T) {
u := uhppote{}

cards := []uint32{
0,
0xffffffff,
0x00ffffff,
}

for _, v := range cards {
card := types.Card{
CardNumber: v,
From: types.ToDate(2023, time.January, 1),
To: types.ToDate(2023, time.December, 31),
Doors: map[uint8]uint8{
1: 1,
2: 0,
3: 29,
4: 1,
},
PIN: 54321,
}

if _, err := u.PutCard(405419896, card); err == nil || !errors.Is(err, ErrInvalidCard) {
t.Errorf("Expected 'invalid card' error, got %v", err)
}
}
}

func TestIsWiegand26(t *testing.T) {
tests := []struct {
card uint32
Expand Down

0 comments on commit 61d4334

Please sign in to comment.