Skip to content

Commit

Permalink
Harden UTF8 length test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 10, 2024
1 parent 57c58f6 commit 8f9becc
Showing 1 changed file with 85 additions and 5 deletions.
90 changes: 85 additions & 5 deletions packages/lexicon/tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,26 +567,106 @@ describe('Record validation', () => {
})

it('Applies string length constraint', () => {
// Shorter than two UTF8 characters
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '',
}),
).toThrow('Record/string must not be shorter than 2 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'a',
}),
).toThrow('Record/string must not be shorter than 2 characters')

// Two to four UTF8 characters
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'ab',
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '\u0301', // Combining acute accent (2 bytes)
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'a\u0301', // 'a' + combining acute accent (1 + 2 bytes = 3 bytes)
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'aé', // 'a' (1 byte) + 'é' (2 bytes) = 3 bytes
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'abc',
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '一', // CJK character (3 bytes)
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '\uD83D', // Unpaired high surrogate (3 bytes)
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '123',
string: 'abcd',
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'éé', // 'é' + 'é' (2 + 2 bytes = 4 bytes)
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'aaé', // 1 + 1 + 2 = 4 bytes
})
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '👋', // 4 bytes
})

expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '1',
string: 'abcde',
}),
).toThrow('Record/string must not be shorter than 2 characters')
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '12345',
string: 'a\u0301\u0301', // 1 + (2 * 2) = 5 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '👨‍👩‍👧‍👧',
string: '\uD83D\uD83D', // Two unpaired high surrogates (3 * 2 = 6 bytes)
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: 'ééé', // 2 + 2 + 2 bytes = 6 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '👋a', // 4 + 1 bytes = 5 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '👨👨', // 4 + 4 = 8 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
expect(() =>
lex.assertValidRecord('com.example.stringLength', {
$type: 'com.example.stringLength',
string: '👨‍👩‍👧‍👧', // 4 emojis × 4 bytes + 3 ZWJs × 3 bytes = 25 bytes
}),
).toThrow('Record/string must not be longer than 4 characters')
})
Expand Down

0 comments on commit 8f9becc

Please sign in to comment.