Skip to content

Commit

Permalink
Tests for string escaping sequences.
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Feb 15, 2024
1 parent 0d073ec commit 199059f
Showing 1 changed file with 74 additions and 24 deletions.
98 changes: 74 additions & 24 deletions src/repository/__tests__/repository.spec.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,153 @@
import { stringToHexstring, ReedSalomonAddressDecode, assertNotUndefined, assertNotEqual, assertExpression, deepCopy, parseDecimalNumber } from '../repository'

const dummyLine = '0:0'

describe('Strings to hexstring', () => {
it('should convert: simple string ( <= 0x7f)', () => {
const str = 'Simple'
const hexstring = '00 00 65 6c 70 6d 69 53'
const result = stringToHexstring(str)
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert: string sucessfully ( > 0x7f and < 0x800)', () => {
const str = 'Até'
const hexstring = '00 00 00 00 a9 c3 74 41'
const result = stringToHexstring(str)
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert: utf split between longs', () => {
const str = 'aaaaaaéa'
const hexstring = ' 00 00 00 00 00 00 00 61 a9 c3 61 61 61 61 61 61'
const result = stringToHexstring(str)
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert string sucessfully (chinese)', () => {
const str = '中华人民共和国'
const hexstring = '000000bd9be58c92e5b185e591b0e6babae48e8de5adb8e4'
const result = stringToHexstring(str)
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert string sucessfully (empty string)', () => {
const str = ''
const hexstring = '00 00 00 00 00 00 00 00'
const result = stringToHexstring(str)
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert string sucessfully (between 0xdfff and 0x10000 )', () => {
const str = '#>'
const hexstring = '00 00 9e bc ef 83 bc ef'
const result = stringToHexstring(str)
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert string sucessfully (over 0x10000)', () => {
const str = '🨁'
const hexstring = '00 00 00 00 81 a8 9f f0'
const result = stringToHexstring(str)
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
test('should throw: wrong string data', () => {
expect(() => {
const str = '🨁🨁🨁'.substr(0, 5)
stringToHexstring(str, dummyLine)
}).toThrowError(/^At line/)
})
test('should throw: wrong string data', () => {
expect(() => {
const str = '🨁🨁🨁'.substr(0, 5) + 'SS'
stringToHexstring(str, dummyLine)
}).toThrowError(/^At line/)
})
it('should convert: simple escape codes', () => {
const str = '3\\n3\\r3\\t3\\\\3\\\'3\\"3'
const hexstring = '00 00 00 33 22 33 27 33 5c 33 09 33 0d 33 0a 33'
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert wrong string data removing skipping invalid data', () => {
const str = '🨁🨁🨁'.substr(0, 5)
const hexstring = '81 a8 9f f0 81 a8 9f f0'
const result = stringToHexstring(str)
test('should throw: wrong simple escape code', () => {
expect(() => {
const str = '33\\c33'
stringToHexstring(str, dummyLine)
}).toThrowError(/^At line/)
})
it('should convert: escaped hexadecimal right', () => {
const str = 'a\\x01a'
const hexstring = '00 00 00 00 00 61 01 61'
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert wrong string data removing skipping invalid data', () => {
const str = '🨁🨁🨁'.substr(0, 5) + 'SS'
const hexstring = '00 00 00 00 00 00 53 53 81 a8 9f f0 81 a8 9f f0'
const result = stringToHexstring(str)
test('should throw: wrong escaped hexadecimal', () => {
expect(() => {
const str = '33\\xfg33'
stringToHexstring(str, dummyLine)
}).toThrowError(/^At line/)
})
test('should throw: wrong escaped hexadecimal', () => {
expect(() => {
const str = '33\\xf'
stringToHexstring(str, dummyLine)
}).toThrowError(/^At line/)
})
it('should convert: escaped unicode right', () => {
const str = 'a\\u668ba'
const hexstring = '00 00 00 61 8b 9a e6 61'
const result = stringToHexstring(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
test('should throw: wrong escaped unicode', () => {
expect(() => {
const str = '33\\u3fgh33'
stringToHexstring(str, dummyLine)
}).toThrowError(/^At line/)
})
test('should throw: wrong escaped unicode', () => {
expect(() => {
const str = '33\\u3f3'
stringToHexstring(str, dummyLine)
}).toThrowError(/^At line/)
})
})

describe('Reed-Salomon decode', () => {
it('should convert: EYCN-TQE9-K5RV-GQZFF', () => {
const str = 'EYCN-TQE9-K5RV-GQZFF'
const hexstring = 'e6 b7 f6 cd 98 76 79 54'
const result = ReedSalomonAddressDecode(str, 0)
const result = ReedSalomonAddressDecode(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert: 2222-2622-Y8GQ-22222', () => {
const str = '2222-2622-Y8GQ-22222'
const hexstring = '00 00 00 01 00 00 00 00'
const result = ReedSalomonAddressDecode(str, 0)
const result = ReedSalomonAddressDecode(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert: 2222-2222-2222-22222 (id zero)', () => {
const str = '2222-2222-2222-22222'
const hexstring = '00 00 00 00 00 00 00 00'
const result = ReedSalomonAddressDecode(str, 0)
const result = ReedSalomonAddressDecode(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
it('should convert: ZZZZ-ZZZZ-QY2K-HZZZZ (last valid id)', () => {
const str = 'ZZZZ-ZZZZ-QY2K-HZZZZ'
const hexstring = 'ff ff ff ff ff ff ff ff'
const result = ReedSalomonAddressDecode(str, 0)
const result = ReedSalomonAddressDecode(str, dummyLine)
expect(result).toBe(hexstring.replace(/ /g, ''))
})
test('should throw: Address overflow (id >= 2^64)', () => {
expect(() => {
const code = '2223-2222-AUZT-J2222'
ReedSalomonAddressDecode(code, 0)
ReedSalomonAddressDecode(code, dummyLine)
}).toThrowError(/^At line/)
})
test('should throw: wrong address', () => {
expect(() => {
const code = 'LQSJ-DXPH-8HZG-CZXQC'
ReedSalomonAddressDecode(code, 0)
ReedSalomonAddressDecode(code, dummyLine)
}).toThrowError(/^At line/)
})
test('should throw: wrong address', () => {
expect(() => {
const code = 'LQSJ-DXPH-HHZG-CZXQH'
ReedSalomonAddressDecode(code, 0)
ReedSalomonAddressDecode(code, dummyLine)
}).toThrowError(/^At line/)
})
})
Expand Down Expand Up @@ -185,12 +235,12 @@ describe('assert/deepcopy functions', () => {
describe('parseDecimal error', () => {
test('should throw: two decimal points', () => {
expect(() => {
parseDecimalNumber('2.234.33', -1)
parseDecimalNumber('2.234.33', dummyLine)
}).toThrowError(/^At line/)
})
test('should throw: more than 8 decimals', () => {
expect(() => {
parseDecimalNumber('2.123456789', -1)
parseDecimalNumber('2.123456789', dummyLine)
}).toThrowError(/^At line/)
})
})

0 comments on commit 199059f

Please sign in to comment.