-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy paththrift-field-id-validation.test.ts
More file actions
86 lines (71 loc) · 2.85 KB
/
thrift-field-id-validation.test.ts
File metadata and controls
86 lines (71 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import * as fs from 'fs';
import * as path from 'path';
import { expect } from 'chai';
/**
* Validates that all Thrift-generated classes comply with field ID constraints.
*
* Field IDs in Thrift must stay below 3329 to avoid conflicts with reserved ranges and ensure
* compatibility with various Thrift implementations and protocols.
*/
describe('Thrift Field ID Validation', () => {
const MAX_ALLOWED_FIELD_ID = 3329;
const THRIFT_DIR = path.join(__dirname, '../../thrift');
it('should ensure all Thrift field IDs are within allowed range', () => {
const violations: string[] = [];
// Get all JavaScript files in the thrift directory
const thriftFiles = fs
.readdirSync(THRIFT_DIR)
.filter((file) => file.endsWith('.js'))
.map((file) => path.join(THRIFT_DIR, file));
expect(thriftFiles.length).to.be.greaterThan(0, 'No Thrift JavaScript files found');
for (const filePath of thriftFiles) {
const fileName = path.basename(filePath);
const fileContent = fs.readFileSync(filePath, 'utf8');
// Extract field IDs from both read and write functions
const fieldIds = extractFieldIds(fileContent);
for (const fieldId of fieldIds) {
if (fieldId >= MAX_ALLOWED_FIELD_ID) {
violations.push(
`${fileName}: Field ID ${fieldId} exceeds maximum allowed value of ${MAX_ALLOWED_FIELD_ID - 1}`,
);
}
}
}
if (violations.length > 0) {
const errorMessage = [
`Found Thrift field IDs that exceed the maximum allowed value of ${MAX_ALLOWED_FIELD_ID - 1}.`,
'This can cause compatibility issues and conflicts with reserved ID ranges.',
'Violations found:',
...violations.map((v) => ` - ${v}`),
].join('\n');
throw new Error(errorMessage);
}
});
});
/**
* Extracts all field IDs from the given Thrift JavaScript file content.
* Looks for field IDs in both read functions (case statements) and write functions (writeFieldBegin calls).
*/
function extractFieldIds(fileContent: string): number[] {
const fieldIds = new Set<number>();
// Pattern 1: Extract field IDs from case statements in read functions
// Example: case 1281:
const casePattern = /case\s+(\d+):/g;
let match;
while ((match = casePattern.exec(fileContent)) !== null) {
const fieldId = parseInt(match[1], 10);
if (!isNaN(fieldId)) {
fieldIds.add(fieldId);
}
}
// Pattern 2: Extract field IDs from writeFieldBegin calls in write functions
// Example: output.writeFieldBegin('errorDetailsJson', Thrift.Type.STRING, 1281);
const writeFieldPattern = /writeFieldBegin\([^,]+,\s*[^,]+,\s*(\d+)\)/g;
while ((match = writeFieldPattern.exec(fileContent)) !== null) {
const fieldId = parseInt(match[1], 10);
if (!isNaN(fieldId)) {
fieldIds.add(fieldId);
}
}
return Array.from(fieldIds).sort((a, b) => a - b);
}