gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

testing_common.go (4714B)


      1 package exif
      2 
      3 import (
      4 	"path"
      5 	"reflect"
      6 	"testing"
      7 
      8 	"io/ioutil"
      9 
     10 	"github.com/dsoprea/go-logging"
     11 
     12 	"github.com/dsoprea/go-exif/v3/common"
     13 )
     14 
     15 var (
     16 	testExifData []byte
     17 )
     18 
     19 func getExifSimpleTestIb() *IfdBuilder {
     20 	defer func() {
     21 		if state := recover(); state != nil {
     22 			err := log.Wrap(state.(error))
     23 			log.Panic(err)
     24 		}
     25 	}()
     26 
     27 	im := exifcommon.NewIfdMapping()
     28 
     29 	err := exifcommon.LoadStandardIfds(im)
     30 	log.PanicIf(err)
     31 
     32 	ti := NewTagIndex()
     33 	ib := NewIfdBuilder(im, ti, exifcommon.IfdStandardIfdIdentity, exifcommon.TestDefaultByteOrder)
     34 
     35 	err = ib.AddStandard(0x000b, "asciivalue")
     36 	log.PanicIf(err)
     37 
     38 	err = ib.AddStandard(0x00ff, []uint16{0x1122})
     39 	log.PanicIf(err)
     40 
     41 	err = ib.AddStandard(0x0100, []uint32{0x33445566})
     42 	log.PanicIf(err)
     43 
     44 	err = ib.AddStandard(0x013e, []exifcommon.Rational{{Numerator: 0x11112222, Denominator: 0x33334444}})
     45 	log.PanicIf(err)
     46 
     47 	return ib
     48 }
     49 
     50 func getExifSimpleTestIbBytes() []byte {
     51 	defer func() {
     52 		if state := recover(); state != nil {
     53 			err := log.Wrap(state.(error))
     54 			log.Panic(err)
     55 		}
     56 	}()
     57 
     58 	im := exifcommon.NewIfdMapping()
     59 
     60 	err := exifcommon.LoadStandardIfds(im)
     61 	log.PanicIf(err)
     62 
     63 	ti := NewTagIndex()
     64 	ib := NewIfdBuilder(im, ti, exifcommon.IfdStandardIfdIdentity, exifcommon.TestDefaultByteOrder)
     65 
     66 	err = ib.AddStandard(0x000b, "asciivalue")
     67 	log.PanicIf(err)
     68 
     69 	err = ib.AddStandard(0x00ff, []uint16{0x1122})
     70 	log.PanicIf(err)
     71 
     72 	err = ib.AddStandard(0x0100, []uint32{0x33445566})
     73 	log.PanicIf(err)
     74 
     75 	err = ib.AddStandard(0x013e, []exifcommon.Rational{{Numerator: 0x11112222, Denominator: 0x33334444}})
     76 	log.PanicIf(err)
     77 
     78 	ibe := NewIfdByteEncoder()
     79 
     80 	exifData, err := ibe.EncodeToExif(ib)
     81 	log.PanicIf(err)
     82 
     83 	return exifData
     84 }
     85 
     86 func validateExifSimpleTestIb(exifData []byte, t *testing.T) {
     87 	defer func() {
     88 		if state := recover(); state != nil {
     89 			err := log.Wrap(state.(error))
     90 			log.Panic(err)
     91 		}
     92 	}()
     93 
     94 	im := exifcommon.NewIfdMapping()
     95 
     96 	err := exifcommon.LoadStandardIfds(im)
     97 	log.PanicIf(err)
     98 
     99 	ti := NewTagIndex()
    100 
    101 	eh, index, err := Collect(im, ti, exifData)
    102 	log.PanicIf(err)
    103 
    104 	if eh.ByteOrder != exifcommon.TestDefaultByteOrder {
    105 		t.Fatalf("EXIF byte-order is not correct: %v", eh.ByteOrder)
    106 	} else if eh.FirstIfdOffset != ExifDefaultFirstIfdOffset {
    107 		t.Fatalf("EXIF first IFD-offset not correct: (0x%02x)", eh.FirstIfdOffset)
    108 	}
    109 
    110 	if len(index.Ifds) != 1 {
    111 		t.Fatalf("There wasn't exactly one IFD decoded: (%d)", len(index.Ifds))
    112 	}
    113 
    114 	ifd := index.RootIfd
    115 
    116 	if ifd.ByteOrder() != exifcommon.TestDefaultByteOrder {
    117 		t.Fatalf("IFD byte-order not correct.")
    118 	} else if ifd.ifdIdentity.UnindexedString() != exifcommon.IfdStandardIfdIdentity.UnindexedString() {
    119 		t.Fatalf("IFD name not correct.")
    120 	} else if ifd.ifdIdentity.Index() != 0 {
    121 		t.Fatalf("IFD index not zero: (%d)", ifd.ifdIdentity.Index())
    122 	} else if ifd.Offset() != uint32(0x0008) {
    123 		t.Fatalf("IFD offset not correct.")
    124 	} else if len(ifd.Entries()) != 4 {
    125 		t.Fatalf("IFD number of entries not correct: (%d)", len(ifd.Entries()))
    126 	} else if ifd.nextIfdOffset != uint32(0) {
    127 		t.Fatalf("Next-IFD offset is non-zero.")
    128 	} else if ifd.nextIfd != nil {
    129 		t.Fatalf("Next-IFD pointer is non-nil.")
    130 	}
    131 
    132 	// Verify the values by using the actual, original types (this is awesome).
    133 
    134 	expected := []struct {
    135 		tagId uint16
    136 		value interface{}
    137 	}{
    138 		{tagId: 0x000b, value: "asciivalue"},
    139 		{tagId: 0x00ff, value: []uint16{0x1122}},
    140 		{tagId: 0x0100, value: []uint32{0x33445566}},
    141 		{tagId: 0x013e, value: []exifcommon.Rational{{Numerator: 0x11112222, Denominator: 0x33334444}}},
    142 	}
    143 
    144 	for i, ite := range ifd.Entries() {
    145 		if ite.TagId() != expected[i].tagId {
    146 			t.Fatalf("Tag-ID for entry (%d) not correct: (0x%02x) != (0x%02x)", i, ite.TagId(), expected[i].tagId)
    147 		}
    148 
    149 		value, err := ite.Value()
    150 		log.PanicIf(err)
    151 
    152 		if reflect.DeepEqual(value, expected[i].value) != true {
    153 			t.Fatalf("Value for entry (%d) not correct: [%v] != [%v]", i, value, expected[i].value)
    154 		}
    155 	}
    156 }
    157 
    158 func getTestImageFilepath() string {
    159 	assetsPath := exifcommon.GetTestAssetsPath()
    160 	testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg")
    161 	return testImageFilepath
    162 }
    163 
    164 func getTestExifData() []byte {
    165 	if testExifData == nil {
    166 		assetsPath := exifcommon.GetTestAssetsPath()
    167 		filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
    168 
    169 		var err error
    170 
    171 		testExifData, err = ioutil.ReadFile(filepath)
    172 		log.PanicIf(err)
    173 	}
    174 
    175 	return testExifData
    176 }
    177 
    178 func getTestGpsImageFilepath() string {
    179 	assetsPath := exifcommon.GetTestAssetsPath()
    180 	testGpsImageFilepath := path.Join(assetsPath, "gps.jpg")
    181 	return testGpsImageFilepath
    182 }
    183 
    184 func getTestGeotiffFilepath() string {
    185 	assetsPath := exifcommon.GetTestAssetsPath()
    186 	testGeotiffFilepath := path.Join(assetsPath, "geotiff_example.tif")
    187 	return testGeotiffFilepath
    188 }