git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/3] t: add t0016-oidmap.sh

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Jun 9, 2019, 21:21 UTC
Message-ID
<20190609212130.GC24208@szeder.dev>
In-Reply-To
<CAP8UFD2AD9NzOUcLfN+NuWp_9JzwdV9oUo9rGAPXt3EP95=_og@mail.gmail.com>
On Sun, Jun 09, 2019 at 10:24:55PM +0200, Christian Couder wrote:
Show 22 quoted lines
> On Sun, Jun 9, 2019 at 11:23 AM SZEDER Gábor <szeder.dev@gmail.com> wrote:
> >
> > On Sun, Jun 09, 2019 at 06:49:06AM +0200, Christian Couder wrote:
> > > +
> > > +test_oidmap() {
> > > +     echo "$1" | test-tool oidmap $3 > actual &&
> > > +     echo "$2" > expect &&
> >
> > Style nit: space between redirection op and filename.
> 
> Thanks for spotting this. It's fixed in my current version.
> 
> > > +test_oidhash() {
> > > +     git rev-parse "$1" | perl -ne 'print hex("$4$3$2$1") . "\n" if m/^(..)(..)(..)(..).*/;'
> >
> > New Perl dependencies always make Dscho sad... :)
> 
> Yeah, I was not sure how to do it properly in shell so I was hoping I
> would get suggestions about this. Thanks for looking at this!
> 
> I could have hardcoded the values as it is done in t0011-hashmap.sh,
> but I thought it was better to find a function that does he job.

Well, I'm fine with hardcoding the expected hash values (in network byte order) as well, because then we won't add another git process upstream of a pipe that would pop up during audit later...

Show 30 quoted lines
> > So, 'test oidmap' from the previous patch prints the value we want to
> > check with:
> >
> >     printf("%u\n", sha1hash(oid.hash));
> 
> Yeah, I did it this way because "test-hashmap.c" does the same kind of
> thing to print hashes:
> 
>             printf("%u %u %u %u\n",
>                    strhash(p1), memhash(p1, strlen(p1)),
>                    strihash(p1), memihash(p1, strlen(p1)));
> 
> > First, since object ids inherently make more sense as hex values, it
> > would be more appropriate to print that hash with the '%x' format
> > specifier,
> 
> I would be ok with that, but then I think it would make sense to also
> print hex values in "test-hashmap.c".
> 
> > and then we wouldn't need Perl's hex() anymore, and thus
> > could swap the order of the first four bytes in oidmap's hash without
> > relying on Perl, e.g. with:
> >
> >   sed -e 's/^\(..\)\(..\)\(..\)\(..\).*/\4\3\2\1/'
> >
> > Second, and more importantly, the need for swapping the byte order
> > indicates that this test would fail on big-endian systems, I'm afraid.
> > So I think we need an additional bswap32() on the printing side,
> 
> Ok, but then shouldn't we also use bswap32() in "test-hashmap.c"?

No. The two test scripts/helpers work with different hashes. t0011 and 'test-hashmap.c' uses the various FNV-1-based hash functions (strhash(), memhash(), ...) to calculate an unsigned int hash of the items stored in the hashmap, therefore their hashes will be the same regardless of endianness. In an oidmap, however, the hash is simply the first four bytes of the object id as an unsigned int as is, and look at how sha1hash() does it, and indeed at the last sentence of the comment in front of it:

 * [...] Note that
 * the results will be different on big-endian and little-endian
 * platforms, so they should not be stored or transferred over the net.
 */
static inline unsigned int sha1hash(const unsigned char *sha1)
{
        /*
         * Equivalent to 'return *(unsigned int *)sha1;', but safe on
         * platforms that don't support unaligned reads.
         */
        unsigned int hash;
        memcpy(&hash, sha1, sizeof(hash));
        return hash;
}
> By the way it seems that we use ntohl() or htonl() instead of
> bswap32() in the source code.
OK.
Show 31 quoted lines
> > and then could further simplify 'test_oidhash':
> >
> > diff --git a/t/helper/test-oidmap.c b/t/helper/test-oidmap.c
> > index 0ba122a264..4177912f9a 100644
> > --- a/t/helper/test-oidmap.c
> > +++ b/t/helper/test-oidmap.c
> > @@ -51,7 +51,7 @@ int cmd__oidmap(int argc, const char **argv)
> >
> >                         /* print hash of oid */
> >                         if (!get_oid(p1, &oid))
> > -                               printf("%u\n", sha1hash(oid.hash));
> > +                               printf("%x\n", bswap32(sha1hash(oid.hash)));
> >                         else
> >                                 printf("Unknown oid: %s\n", p1);
> >
> > diff --git a/t/t0016-oidmap.sh b/t/t0016-oidmap.sh
> > index 3a8e8bdb3d..9c0d88a316 100755
> > --- a/t/t0016-oidmap.sh
> > +++ b/t/t0016-oidmap.sh
> > @@ -22,10 +22,10 @@ test_expect_success 'setup' '
> >  '
> >
> >  test_oidhash() {
> > -       git rev-parse "$1" | perl -ne 'print hex("$4$3$2$1") . "\n" if m/^(..)(..)(..)(..).*/;'
> > +       git rev-parse "$1" | cut -c1-8
> >  }
> >
> > -test_expect_success PERL 'hash' '
> > +test_expect_success 'hash' '
> 
> Yeah, I agree that it seems better to me this way.
Previous: Christian CouderNext: Christian Couder
Message 6 of 14 in “Test oidmap”
  1. 0/3 Test oidmapChristian Couder, Jun 9, 2019
  2. 1/3 t/helper: add test-oidmap.cChristian Couder, Jun 9, 2019
  3. 2/3 t: add t0016-oidmap.shChristian Couder, Jun 9, 2019
  4. SZEDER GáborJun 9, 2019
  5. Christian CouderJun 9, 2019
  6. SZEDER GáborJun 9, 2019
  7. Christian CouderJun 9, 2019
  8. Junio C HamanoJun 10, 2019
  9. Jeff KingJun 13, 2019
  10. SZEDER GáborJun 13, 2019
  11. Jeff KingJun 13, 2019
  12. Junio C HamanoJun 13, 2019
  13. Christian CouderJun 14, 2019
  14. 3/3 oidmap: use sha1hash() instead of static hash() functionChristian Couder, Jun 9, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.