| 1 | package uuid |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestIsAsError(t *testing.T) { |
| 11 | tcs := []struct { |
| 12 | err error |
| 13 | expected string |
| 14 | expectedErr error |
| 15 | }{ |
| 16 | { |
| 17 | err: fmt.Errorf("%w sample error: %v", ErrInvalidVersion, 123), |
| 18 | expected: "uuid: sample error: 123", |
| 19 | expectedErr: ErrInvalidVersion, |
| 20 | }, |
| 21 | { |
| 22 | err: fmt.Errorf("%w", ErrInvalidFormat), |
| 23 | expected: "uuid: invalid UUID format", |
| 24 | expectedErr: ErrInvalidFormat, |
| 25 | }, |
| 26 | { |
| 27 | err: fmt.Errorf("%w %q", ErrIncorrectFormatInString, "test"), |
| 28 | expected: "uuid: incorrect UUID format in string \"test\"", |
| 29 | expectedErr: ErrIncorrectFormatInString, |
| 30 | }, |
| 31 | } |
| 32 | for i, tc := range tcs { |
| 33 | t.Run(fmt.Sprintf("Test case %d", i), func(t *testing.T) { |
| 34 | if tc.err.Error() != tc.expected { |
| 35 | t.Errorf("expected err.Error() to be '%s' but was '%s'", tc.expected, tc.err.Error()) |
| 36 | } |
| 37 | var uuidErr Error |
| 38 | if !errors.As(tc.err, &uuidErr) { |
| 39 | t.Error("expected errors.As() to work") |
| 40 | } |
| 41 | if !errors.Is(tc.err, tc.expectedErr) { |
| 42 | t.Errorf("expected error to be, or wrap, the %v sentinel error", tc.expectedErr) |
| 43 | } |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestParseErrors(t *testing.T) { |
| 49 | tcs := []struct { |
| 50 | function string |
| 51 | uuidStr string |
| 52 | expected string |
| 53 | }{ |
| 54 | { // 34 chars - With braces |
| 55 | function: "parse", |
| 56 | uuidStr: "..................................", |
| 57 | expected: "uuid: incorrect UUID format in string: invalid braces", |
| 58 | }, |
| 59 | { // 41 chars - urn:uuid: |
| 60 | function: "parse", |
| 61 | uuidStr: "123456789................................", |
| 62 | expected: "uuid: incorrect UUID format in string: invalid URN prefix", |
| 63 | }, |
| 64 | { // other |
| 65 | function: "parse", |
| 66 | uuidStr: "....", |
| 67 | expected: "uuid: incorrect UUID length", |
| 68 | }, |
| 69 | { // 36 chars - canonical, but not correct format |
| 70 | function: "parse", |
| 71 | uuidStr: "....................................", |
| 72 | expected: "uuid: incorrect UUID format in string: dashes were not in expected positions", |
| 73 | }, |
| 74 | { // 36 chars - canonical, invalid data |
| 75 | function: "parse", |
| 76 | uuidStr: "xx00ae9e-dae3-459f-ad0e-6b574be3f950", |
| 77 | expected: "uuid: invalid UUID format", |
| 78 | }, |
| 79 | { // Hash like |
| 80 | function: "parse", |
| 81 | uuidStr: "................................", |
| 82 | expected: "uuid: invalid UUID format", |
| 83 | }, |
| 84 | { // Hash like, invalid |
| 85 | function: "parse", |
| 86 | uuidStr: "xx00ae9edae3459fad0e6b574be3f950", |
| 87 | expected: "uuid: invalid UUID format", |
| 88 | }, |
| 89 | { // Hash like, invalid |
| 90 | function: "parse", |
| 91 | uuidStr: "xx00ae9edae3459fad0e6b574be3f950", |
| 92 | expected: "uuid: invalid UUID format", |
| 93 | }, |
| 94 | } |
| 95 | for i, tc := range tcs { |
| 96 | t.Run(fmt.Sprintf("Test case %d", i), func(t *testing.T) { |
| 97 | id := UUID{} |
| 98 | err := id.Parse(tc.uuidStr) |
| 99 | if err == nil { |
| 100 | t.Error("expected an error") |
| 101 | return |
| 102 | } |
| 103 | if err.Error() != tc.expected { |
| 104 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), tc.expected) |
| 105 | } |
| 106 | err = id.UnmarshalText([]byte(tc.uuidStr)) |
| 107 | if err == nil { |
| 108 | t.Error("expected an error") |
| 109 | return |
| 110 | } |
| 111 | if err.Error() != tc.expected { |
| 112 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), tc.expected) |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestUnmarshalBinaryError(t *testing.T) { |
| 119 | id := UUID{} |
| 120 | b := make([]byte, 33) |
| 121 | expectedErr := "uuid: UUID must be exactly 16 bytes long" |
| 122 | err := id.UnmarshalBinary([]byte(b)) |
| 123 | if err == nil { |
| 124 | t.Error("expected an error") |
| 125 | return |
| 126 | } |
| 127 | if err.Error() != expectedErr { |
| 128 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestScanError(t *testing.T) { |
| 133 | id := UUID{} |
| 134 | err := id.Scan(123) |
| 135 | if err == nil { |
| 136 | t.Error("expected an error") |
| 137 | return |
| 138 | } |
| 139 | expectedErr := "uuid: cannot convert int to UUID" |
| 140 | if err.Error() != expectedErr { |
| 141 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestUUIDVersionErrors(t *testing.T) { |
| 146 | // UUId V1 Version |
| 147 | id := FromStringOrNil("e86160d3-beff-443c-b9b5-1f8197ccb12e") |
| 148 | _, err := TimestampFromV1(id) |
| 149 | if err == nil { |
| 150 | t.Error("expected an error") |
| 151 | return |
| 152 | } |
| 153 | expectedErr := "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 1" |
| 154 | if err.Error() != expectedErr { |
| 155 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr) |
| 156 | } |
| 157 | |
| 158 | // UUId V2 Version |
| 159 | id = FromStringOrNil("e86160d3-beff-443c-b9b5-1f8197ccb12e") |
| 160 | _, err = TimestampFromV6(id) |
| 161 | if err == nil { |
| 162 | t.Error("expected an error") |
| 163 | return |
| 164 | } |
| 165 | expectedErr = "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 6" |
| 166 | if err.Error() != expectedErr { |
| 167 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr) |
| 168 | } |
| 169 | |
| 170 | // UUId V7 Version |
| 171 | id = FromStringOrNil("e86160d3-beff-443c-b9b5-1f8197ccb12e") |
| 172 | _, err = TimestampFromV7(id) |
| 173 | if err == nil { |
| 174 | t.Error("expected an error") |
| 175 | return |
| 176 | } |
| 177 | expectedErr = "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 7" |
| 178 | if err.Error() != expectedErr { |
| 179 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // This test cannot be run in parallel with other tests since it modifies the |
| 184 | // global state |
| 185 | func TestErrNoHwAddressFound(t *testing.T) { |
| 186 | netInterfaces = func() ([]net.Interface, error) { |
| 187 | return nil, nil |
| 188 | } |
| 189 | defer func() { |
| 190 | netInterfaces = net.Interfaces |
| 191 | }() |
| 192 | _, err := defaultHWAddrFunc() |
| 193 | if err == nil { |
| 194 | t.Error("expected an error") |
| 195 | return |
| 196 | } |
| 197 | expectedErr := "uuid: no HW address found" |
| 198 | if err.Error() != expectedErr { |
| 199 | t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr) |
| 200 | } |
| 201 | } |