Skip to content

Commit dc55ed0

Browse files
oschwaldclaude
andcommitted
Extract shared MMDB test writer helpers into mmdb_test_writer.h
bad_epoch_t.c, overflow_bounds_t.c, and bad_data_size_t.c all had identical copies of write_map, write_string, write_uint16, write_uint32, write_uint64, write_meta_key, and the METADATA_MARKER defines. Move these into a shared static-inline header. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e124a04 commit dc55ed0

5 files changed

Lines changed: 67 additions & 151 deletions

File tree

t/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ libmmdbtest_la_SOURCES = maxminddb_test_helper.c maxminddb_test_helper.h
1313

1414
EXTRA_DIST = compile_c++_t.pl external_symbols_t.pl mmdblookup_t.pl \
1515
libtap/COPYING libtap/INSTALL libtap/Makefile libtap/README.md \
16-
libtap/tap.c libtap/tap.h maxmind-db
16+
libtap/tap.c libtap/tap.h maxmind-db mmdb_test_writer.h
1717

1818
check_PROGRAMS = \
1919
bad_pointers_t bad_databases_t bad_data_size_t bad_epoch_t bad_indent_t \

t/bad_data_size_t.c

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,7 @@
11
#include "maxminddb_test_helper.h"
2+
#include "mmdb_test_writer.h"
23
#include <stdlib.h>
34

4-
/* MMDB binary format constants */
5-
#define METADATA_MARKER "\xab\xcd\xefMaxMind.com"
6-
#define METADATA_MARKER_LEN 14
7-
#define DATA_SEPARATOR_SIZE 16
8-
9-
static int write_map(uint8_t *buf, uint32_t size) {
10-
buf[0] = (7 << 5) | (size & 0x1f);
11-
return 1;
12-
}
13-
14-
static int write_string(uint8_t *buf, const char *str, uint32_t len) {
15-
buf[0] = (2 << 5) | (len & 0x1f);
16-
memcpy(buf + 1, str, len);
17-
return 1 + len;
18-
}
19-
20-
static int write_uint16(uint8_t *buf, uint16_t value) {
21-
buf[0] = (5 << 5) | 2;
22-
buf[1] = (value >> 8) & 0xff;
23-
buf[2] = value & 0xff;
24-
return 3;
25-
}
26-
27-
static int write_uint32(uint8_t *buf, uint32_t value) {
28-
buf[0] = (6 << 5) | 4;
29-
buf[1] = (value >> 24) & 0xff;
30-
buf[2] = (value >> 16) & 0xff;
31-
buf[3] = (value >> 8) & 0xff;
32-
buf[4] = value & 0xff;
33-
return 5;
34-
}
35-
36-
static int write_uint64(uint8_t *buf, uint64_t value) {
37-
buf[0] = (0 << 5) | 8;
38-
buf[1] = 2; /* extended type: 7 + 2 = 9 (uint64) */
39-
buf[2] = (value >> 56) & 0xff;
40-
buf[3] = (value >> 48) & 0xff;
41-
buf[4] = (value >> 40) & 0xff;
42-
buf[5] = (value >> 32) & 0xff;
43-
buf[6] = (value >> 24) & 0xff;
44-
buf[7] = (value >> 16) & 0xff;
45-
buf[8] = (value >> 8) & 0xff;
46-
buf[9] = value & 0xff;
47-
return 10;
48-
}
49-
50-
static int write_meta_key(uint8_t *buf, const char *key) {
51-
return write_string(buf, key, strlen(key));
52-
}
53-
545
/*
556
* Write a map control byte with a large size using case-31 encoding.
567
* Type 7 (map) fits in the base types: control byte = (7 << 5) | size_marker.

t/bad_epoch_t.c

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,7 @@
11
#include "maxminddb_test_helper.h"
2+
#include "mmdb_test_writer.h"
23
#include <stdlib.h>
34

4-
/* MMDB binary format constants */
5-
#define METADATA_MARKER "\xab\xcd\xefMaxMind.com"
6-
#define METADATA_MARKER_LEN 14
7-
#define DATA_SEPARATOR_SIZE 16
8-
9-
static int write_map(uint8_t *buf, uint32_t size) {
10-
buf[0] = (7 << 5) | (size & 0x1f);
11-
return 1;
12-
}
13-
14-
static int write_string(uint8_t *buf, const char *str, uint32_t len) {
15-
buf[0] = (2 << 5) | (len & 0x1f);
16-
memcpy(buf + 1, str, len);
17-
return 1 + len;
18-
}
19-
20-
static int write_uint16(uint8_t *buf, uint16_t value) {
21-
buf[0] = (5 << 5) | 2;
22-
buf[1] = (value >> 8) & 0xff;
23-
buf[2] = value & 0xff;
24-
return 3;
25-
}
26-
27-
static int write_uint32(uint8_t *buf, uint32_t value) {
28-
buf[0] = (6 << 5) | 4;
29-
buf[1] = (value >> 24) & 0xff;
30-
buf[2] = (value >> 16) & 0xff;
31-
buf[3] = (value >> 8) & 0xff;
32-
buf[4] = value & 0xff;
33-
return 5;
34-
}
35-
36-
static int write_uint64(uint8_t *buf, uint64_t value) {
37-
buf[0] = (0 << 5) | 8;
38-
buf[1] = 2; /* extended type: 7 + 2 = 9 (uint64) */
39-
buf[2] = (value >> 56) & 0xff;
40-
buf[3] = (value >> 48) & 0xff;
41-
buf[4] = (value >> 40) & 0xff;
42-
buf[5] = (value >> 32) & 0xff;
43-
buf[6] = (value >> 24) & 0xff;
44-
buf[7] = (value >> 16) & 0xff;
45-
buf[8] = (value >> 8) & 0xff;
46-
buf[9] = value & 0xff;
47-
return 10;
48-
}
49-
50-
static int write_meta_key(uint8_t *buf, const char *key) {
51-
return write_string(buf, key, strlen(key));
52-
}
53-
545
static void create_bad_epoch_db(const char *path) {
556
uint32_t node_count = 1;
567
uint32_t record_size = 24;

t/mmdb_test_writer.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef MMDB_TEST_WRITER_H
2+
#define MMDB_TEST_WRITER_H
3+
4+
/*
5+
* Shared helpers for tests that construct MMDB files in memory.
6+
* Each function writes one MMDB data-format element into buf and returns
7+
* the number of bytes written.
8+
*/
9+
10+
#include <stdint.h>
11+
#include <string.h>
12+
13+
/* MMDB binary format constants */
14+
#define METADATA_MARKER "\xab\xcd\xefMaxMind.com"
15+
#define METADATA_MARKER_LEN 14
16+
#define DATA_SEPARATOR_SIZE 16
17+
18+
static inline int write_map(uint8_t *buf, uint32_t size) {
19+
buf[0] = (7 << 5) | (size & 0x1f);
20+
return 1;
21+
}
22+
23+
static inline int write_string(uint8_t *buf, const char *str, uint32_t len) {
24+
buf[0] = (2 << 5) | (len & 0x1f);
25+
memcpy(buf + 1, str, len);
26+
return 1 + len;
27+
}
28+
29+
static inline int write_uint16(uint8_t *buf, uint16_t value) {
30+
buf[0] = (5 << 5) | 2;
31+
buf[1] = (value >> 8) & 0xff;
32+
buf[2] = value & 0xff;
33+
return 3;
34+
}
35+
36+
static inline int write_uint32(uint8_t *buf, uint32_t value) {
37+
buf[0] = (6 << 5) | 4;
38+
buf[1] = (value >> 24) & 0xff;
39+
buf[2] = (value >> 16) & 0xff;
40+
buf[3] = (value >> 8) & 0xff;
41+
buf[4] = value & 0xff;
42+
return 5;
43+
}
44+
45+
static inline int write_uint64(uint8_t *buf, uint64_t value) {
46+
buf[0] = (0 << 5) | 8;
47+
buf[1] = 2; /* extended type: 7 + 2 = 9 (uint64) */
48+
buf[2] = (value >> 56) & 0xff;
49+
buf[3] = (value >> 48) & 0xff;
50+
buf[4] = (value >> 40) & 0xff;
51+
buf[5] = (value >> 32) & 0xff;
52+
buf[6] = (value >> 24) & 0xff;
53+
buf[7] = (value >> 16) & 0xff;
54+
buf[8] = (value >> 8) & 0xff;
55+
buf[9] = value & 0xff;
56+
return 10;
57+
}
58+
59+
static inline int write_meta_key(uint8_t *buf, const char *key) {
60+
return write_string(buf, key, strlen(key));
61+
}
62+
63+
#endif /* MMDB_TEST_WRITER_H */

t/overflow_bounds_t.c

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,7 @@
11
#include "maxminddb_test_helper.h"
2+
#include "mmdb_test_writer.h"
23
#include <stdlib.h>
34

4-
/* MMDB binary format constants */
5-
#define METADATA_MARKER "\xab\xcd\xefMaxMind.com"
6-
#define METADATA_MARKER_LEN 14
7-
#define DATA_SEPARATOR_SIZE 16
8-
9-
static int write_map(uint8_t *buf, uint32_t size) {
10-
buf[0] = (7 << 5) | (size & 0x1f);
11-
return 1;
12-
}
13-
14-
static int write_string(uint8_t *buf, const char *str, uint32_t len) {
15-
buf[0] = (2 << 5) | (len & 0x1f);
16-
memcpy(buf + 1, str, len);
17-
return 1 + len;
18-
}
19-
20-
static int write_uint16(uint8_t *buf, uint16_t value) {
21-
buf[0] = (5 << 5) | 2;
22-
buf[1] = (value >> 8) & 0xff;
23-
buf[2] = value & 0xff;
24-
return 3;
25-
}
26-
27-
static int write_uint32(uint8_t *buf, uint32_t value) {
28-
buf[0] = (6 << 5) | 4;
29-
buf[1] = (value >> 24) & 0xff;
30-
buf[2] = (value >> 16) & 0xff;
31-
buf[3] = (value >> 8) & 0xff;
32-
buf[4] = value & 0xff;
33-
return 5;
34-
}
35-
36-
static int write_uint64(uint8_t *buf, uint64_t value) {
37-
buf[0] = (0 << 5) | 8;
38-
buf[1] = 2; /* extended type: 7 + 2 = 9 (uint64) */
39-
buf[2] = (value >> 56) & 0xff;
40-
buf[3] = (value >> 48) & 0xff;
41-
buf[4] = (value >> 40) & 0xff;
42-
buf[5] = (value >> 32) & 0xff;
43-
buf[6] = (value >> 24) & 0xff;
44-
buf[7] = (value >> 16) & 0xff;
45-
buf[8] = (value >> 8) & 0xff;
46-
buf[9] = value & 0xff;
47-
return 10;
48-
}
49-
50-
static int write_meta_key(uint8_t *buf, const char *key) {
51-
return write_string(buf, key, strlen(key));
52-
}
53-
545
/*
556
* Test that the bounds check in find_address_in_search_tree uses 64-bit
567
* arithmetic. We can't realistically create a database large enough to

0 commit comments

Comments
 (0)