| 1 |
package jwt_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"math" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/golang-jwt/jwt/v4" |
| 10 |
) |
| 11 |
|
| 12 |
func TestNumericDate(t *testing.T) { |
| 13 |
var s struct { |
| 14 |
Iat jwt.NumericDate `json:"iat"` |
| 15 |
Exp jwt.NumericDate `json:"exp"` |
| 16 |
} |
| 17 |
|
| 18 |
oldPrecision := jwt.TimePrecision |
| 19 |
|
| 20 |
jwt.TimePrecision = time.Microsecond |
| 21 |
|
| 22 |
raw := `{"iat":1516239022.000000,"exp":1516239022.123450}` |
| 23 |
|
| 24 |
if err := json.Unmarshal([]byte(raw), &s); err != nil { |
| 25 |
t.Fatalf("Unexpected error: %s", err) |
| 26 |
} |
| 27 |
|
| 28 |
b, _ := json.Marshal(s) |
| 29 |
|
| 30 |
if raw != string(b) { |
| 31 |
t.Errorf("Serialized format of numeric date mismatch. Expecting: %s Got: %s", string(raw), string(b)) |
| 32 |
} |
| 33 |
|
| 34 |
jwt.TimePrecision = oldPrecision |
| 35 |
} |
| 36 |
|
| 37 |
func TestSingleArrayMarshal(t *testing.T) { |
| 38 |
jwt.MarshalSingleStringAsArray = false |
| 39 |
|
| 40 |
s := jwt.ClaimStrings{"test"} |
| 41 |
expected := `"test"` |
| 42 |
|
| 43 |
b, err := json.Marshal(s) |
| 44 |
|
| 45 |
if err != nil { |
| 46 |
t.Errorf("Unexpected error: %s", err) |
| 47 |
} |
| 48 |
|
| 49 |
if expected != string(b) { |
| 50 |
t.Errorf("Serialized format of string array mismatch. Expecting: %s Got: %s", string(expected), string(b)) |
| 51 |
} |
| 52 |
|
| 53 |
jwt.MarshalSingleStringAsArray = true |
| 54 |
|
| 55 |
expected = `["test"]` |
| 56 |
|
| 57 |
b, err = json.Marshal(s) |
| 58 |
|
| 59 |
if err != nil { |
| 60 |
t.Errorf("Unexpected error: %s", err) |
| 61 |
} |
| 62 |
|
| 63 |
if expected != string(b) { |
| 64 |
t.Errorf("Serialized format of string array mismatch. Expecting: %s Got: %s", string(expected), string(b)) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestNumericDate_MarshalJSON(t *testing.T) { |
| 69 |
// Do not run this test in parallel because it's changing |
| 70 |
// global state. |
| 71 |
oldPrecision := jwt.TimePrecision |
| 72 |
t.Cleanup(func() { |
| 73 |
jwt.TimePrecision = oldPrecision |
| 74 |
}) |
| 75 |
|
| 76 |
tt := []struct { |
| 77 |
in time.Time |
| 78 |
want string |
| 79 |
precision time.Duration |
| 80 |
}{ |
| 81 |
{time.Unix(5243700879, 0), "5243700879", time.Second}, |
| 82 |
{time.Unix(5243700879, 0), "5243700879.000", time.Millisecond}, |
| 83 |
{time.Unix(5243700879, 0), "5243700879.000000", time.Microsecond}, |
| 84 |
{time.Unix(5243700879, 0), "5243700879.000000000", time.Nanosecond}, |
| 85 |
// |
| 86 |
{time.Unix(4239425898, 0), "4239425898", time.Second}, |
| 87 |
{time.Unix(4239425898, 0), "4239425898.000", time.Millisecond}, |
| 88 |
{time.Unix(4239425898, 0), "4239425898.000000", time.Microsecond}, |
| 89 |
{time.Unix(4239425898, 0), "4239425898.000000000", time.Nanosecond}, |
| 90 |
// |
| 91 |
{time.Unix(253402271999, 0), "253402271999", time.Second}, |
| 92 |
{time.Unix(253402271999, 0), "253402271999.000", time.Millisecond}, |
| 93 |
{time.Unix(253402271999, 0), "253402271999.000000", time.Microsecond}, |
| 94 |
{time.Unix(253402271999, 0), "253402271999.000000000", time.Nanosecond}, |
| 95 |
// |
| 96 |
{time.Unix(0, 1644285000210402000), "1644285000", time.Second}, |
| 97 |
{time.Unix(0, 1644285000210402000), "1644285000.210", time.Millisecond}, |
| 98 |
{time.Unix(0, 1644285000210402000), "1644285000.210402", time.Microsecond}, |
| 99 |
{time.Unix(0, 1644285000210402000), "1644285000.210402000", time.Nanosecond}, |
| 100 |
// |
| 101 |
{time.Unix(0, 1644285315063096000), "1644285315", time.Second}, |
| 102 |
{time.Unix(0, 1644285315063096000), "1644285315.063", time.Millisecond}, |
| 103 |
{time.Unix(0, 1644285315063096000), "1644285315.063096", time.Microsecond}, |
| 104 |
{time.Unix(0, 1644285315063096000), "1644285315.063096000", time.Nanosecond}, |
| 105 |
// Maximum time that a go time.Time can represent |
| 106 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second}, |
| 107 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999", time.Millisecond}, |
| 108 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999", time.Microsecond}, |
| 109 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999999", time.Nanosecond}, |
| 110 |
// Strange precisions |
| 111 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second}, |
| 112 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854775756", time.Minute}, |
| 113 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854774016", time.Hour}, |
| 114 |
{time.Unix(math.MaxInt64, 999999999), "9223372036854745216", 24 * time.Hour}, |
| 115 |
} |
| 116 |
|
| 117 |
for i, tc := range tt { |
| 118 |
jwt.TimePrecision = tc.precision |
| 119 |
by, err := jwt.NewNumericDate(tc.in).MarshalJSON() |
| 120 |
if err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
if got := string(by); got != tc.want { |
| 124 |
t.Errorf("[%d]: failed encoding: got %q want %q", i, got, tc.want) |
| 125 |
} |
| 126 |
} |
| 127 |
} |