Skip to content

Commit

Permalink
fix: ensure BytesFactory does not produce padded array (#397)
Browse files Browse the repository at this point in the history
* ensure BytesFactory does not produce padded array

* add newline
  • Loading branch information
pfletcherhill authored Jan 9, 2023
1 parent ef03070 commit 6b3247c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ coverage/

# Turborepo
.turbo
dist
9 changes: 9 additions & 0 deletions packages/utils/src/factories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ describe('VerificationAddEthAddressBodyFactory', () => {
expect(verifiedAddress._unsafeUnwrap()).toEqual(body.addressArray());
});
});

describe('BytesFactory', () => {
describe('with length', () => {
test('succeeds with unpadded little endian', () => {
const bytes = Factories.Bytes.build({}, { transient: { length: 32 } });
expect(bytes[bytes.length - 1]).not.toEqual(0);
});
});
});
9 changes: 5 additions & 4 deletions packages/utils/src/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import { toFarcasterTime } from './time';
import { toTsHash } from './tsHash';
import { makeVerificationEthAddressClaim, VerificationEthAddressClaim } from './verifications';

/* eslint-disable security/detect-object-injection */
const BytesFactory = Factory.define<Uint8Array, { length?: number }>(({ transientParams }) => {
const length = transientParams.length ?? 8;
const length = transientParams.length ?? faker.datatype.number({ max: 64, min: 1 });
const bytes = new Uint8Array(length);
for (let i = 0; i < bytes.byteLength; i++) {
bytes[i] = faker.datatype.number({ max: 255, min: 0 });
for (let i = 0; i < bytes.length - 1; i++) {
bytes.set([faker.datatype.number({ max: 255, min: 0 })], i);
}
// Ensure that the most significant byte is not 0 (i.e. is not padding)
bytes.set([faker.datatype.number({ max: 255, min: 1 })], bytes.length - 1);
return bytes;
});

Expand Down

0 comments on commit 6b3247c

Please sign in to comment.