Skip to content

Commit

Permalink
userhash: don't care about SQL behavior
Browse files Browse the repository at this point in the history
We no longer use database/sql for SQL, so we don't need to implement
sql/driver.Valuer or to test how it works in SQLite.

This removes the last dependency on mattn/go-sqlite3.
  • Loading branch information
zephyrtronium committed Aug 7, 2024
1 parent acf4ea0 commit e7cfbf8
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 68 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ require (
github.com/dgraph-io/badger/v4 v4.2.0
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/urfave/cli/v3 v3.0.0-alpha9
gitlab.com/zephyrtronium/pick v1.0.0
gitlab.com/zephyrtronium/sq v1.0.1
gitlab.com/zephyrtronium/tmi v0.0.0-20240325132202-7adf62e91c49
golang.org/x/crypto v0.21.0
golang.org/x/oauth2 v0.18.0
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLA
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -97,8 +95,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/zephyrtronium/pick v1.0.0 h1:69HZtpcbLu9gl5UoKl9YcwdiSuZ3BIuGQkHp2NmG5hI=
gitlab.com/zephyrtronium/pick v1.0.0/go.mod h1:jWZNNgIzAdXRkR0LFbpYckLo4ikhoQRXNm/vJuXUmVI=
gitlab.com/zephyrtronium/sq v1.0.1 h1:adEgr3pNIrpeUTn8pf4l5bzCZU3P+lZ71shhQpTtU3k=
gitlab.com/zephyrtronium/sq v1.0.1/go.mod h1:1PixOalEEwM8B2bR0JHkH0kqE4eaqG47DRFn4mWOtaQ=
gitlab.com/zephyrtronium/tmi v0.0.0-20240325132202-7adf62e91c49 h1:o9bho6E5B8uhpSSvyGnM/ds52dzJowGNJESJ9EQn5X0=
gitlab.com/zephyrtronium/tmi v0.0.0-20240325132202-7adf62e91c49/go.mod h1:OnGsPoEWvq0Kh20CPT2Aoa9bxmzX5imtWpZ+9Qiltik=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
Expand Down
6 changes: 0 additions & 6 deletions userhash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package userhash

import (
"crypto/hmac"
"database/sql/driver"
"encoding/binary"
"errors"
"hash"
Expand Down Expand Up @@ -54,11 +53,6 @@ func (h *Hash) Scan(src any) error {
return nil
}

// Value implements sql/driver.Valuer.
func (h *Hash) Value() (driver.Value, error) {
return h[:], nil
}

// A Hasher creates Hash values.
type Hasher struct {
// mac is the HMAC hasher.
Expand Down
56 changes: 0 additions & 56 deletions userhash/hash_test.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,12 @@
package userhash_test

import (
"context"
"testing"
"time"

"gitlab.com/zephyrtronium/sq"

"github.com/zephyrtronium/robot/userhash"

_ "github.com/mattn/go-sqlite3" // driver
)

// TestHashSQL tests that userhashes round-trip in an SQL database.
func TestHashSQL(t *testing.T) {
cases := []struct {
driver string
dsn string
create string
}{
{
driver: "sqlite3",
dsn: ":memory:",
create: `CREATE TABLE test (x BLOB) STRICT`,
},
}
for _, c := range cases {
c := c
t.Run(c.driver, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
db, err := sq.Open(c.driver, c.dsn)
if err != nil {
t.Fatal(err)
}
if err := db.Ping(ctx); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(ctx, c.create); err != nil {
t.Fatal(err)
}
in := &userhash.Hash{}
for i := range in {
in[i] = 0x55
}
r, err := db.Exec(ctx, `INSERT INTO test (x) VALUES (?)`, in)
if err != nil {
t.Errorf("couldn't insert userhash: %v", err)
}
n, _ := r.RowsAffected()
if n != 1 {
t.Errorf("wrong number of rows affected: want 1, got %d", n)
}
var out userhash.Hash
if err := db.QueryRow(ctx, `SELECT x FROM test`).Scan(&out); err != nil {
t.Errorf("couldn't scan userhash: %v", err)
}
if out != *in {
t.Errorf("wrong scan result: want %x, got %x", *in, out)
}
})
}
}

func TestHasher(t *testing.T) {
t.Parallel()
// Every combination of key, time, user, and location must produce a
Expand Down

0 comments on commit e7cfbf8

Please sign in to comment.