| 175 | } |
| 176 | |
| 177 | func TestChecksumFileCRC32(t *testing.T) { |
| 178 | t.Parallel() |
| 179 | |
| 180 | t.Run("file exists", func(t *testing.T) { |
| 181 | t.Parallel() |
| 182 | |
| 183 | ctx := testutil.Context(t, testutil.WaitShort) |
| 184 | logger := testutil.Logger(t) |
| 185 | |
| 186 | tmpfile, err := os.CreateTemp("", "lockfile-*.hcl") |
| 187 | require.NoError(t, err) |
| 188 | defer os.Remove(tmpfile.Name()) |
| 189 | |
| 190 | content := []byte("provider \"aws\" { version = \"5.0.0\" }") |
| 191 | _, err = tmpfile.Write(content) |
| 192 | require.NoError(t, err) |
| 193 | tmpfile.Close() |
| 194 | |
| 195 | // Calculate checksum - expected value for this specific content |
| 196 | expectedChecksum := uint32(0x08f39f51) |
| 197 | checksum := checksumFileCRC32(ctx, logger, tmpfile.Name()) |
| 198 | require.Equal(t, expectedChecksum, checksum) |
| 199 | |
| 200 | // Modify file |
| 201 | err = os.WriteFile(tmpfile.Name(), []byte("modified content"), 0o600) |
| 202 | require.NoError(t, err) |
| 203 | |
| 204 | // Checksum should be different |
| 205 | modifiedChecksum := checksumFileCRC32(ctx, logger, tmpfile.Name()) |
| 206 | require.NotEqual(t, expectedChecksum, modifiedChecksum) |
| 207 | }) |
| 208 | |
| 209 | t.Run("file does not exist", func(t *testing.T) { |
| 210 | t.Parallel() |
| 211 | |
| 212 | ctx := testutil.Context(t, testutil.WaitShort) |
| 213 | logger := testutil.Logger(t) |
| 214 | |
| 215 | checksum := checksumFileCRC32(ctx, logger, "/nonexistent/file.hcl") |
| 216 | require.Zero(t, checksum) |
| 217 | }) |
| 218 | } |