github.com/fid/gofid@v0.0.0-20160726141414-f2913285a804/gofid_test.go (about)

     1  package gofid
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  const (
    11  	specTimeKeyLength = 9
    12  	testVendorSecret  = "bad_secret"
    13  	testVendor        = "FID"
    14  	testApp           = "TE"
    15  	testType          = "ES"
    16  )
    17  
    18  // TestGenerationOfNewID tests that the full ID generation output is as expected
    19  func TestGenerationOfNewID(t *testing.T) {
    20  	inputTime := time.Now()
    21  	t.Log("Testing full ID generation")
    22  	id, err := Generate(IndicatorEntity, testVendor, testApp, testType, "", "")
    23  	if err != nil || id == "" || len(id) != idLength {
    24  		t.Errorf("Error generating ID")
    25  	}
    26  
    27  	re := regexp.MustCompile(idRegex)
    28  	match := re.FindStringSubmatch(id)
    29  	if len(match) != 1 {
    30  		t.Errorf("Generated ID did not match expected format: " + id)
    31  	}
    32  
    33  	if result, err := Verify(id, ""); result == false {
    34  		t.Errorf("Generated ID was invalid: " + err.Error())
    35  	}
    36  
    37  	description, err := Describe(id)
    38  	if err != nil {
    39  		t.Errorf("Error with description " + err.Error())
    40  	}
    41  
    42  	keyTime := description.Time
    43  
    44  	if keyTime.Day() != inputTime.Day() {
    45  		t.Errorf("Timekey in ID is an unexpected value %d : %d", keyTime.Unix(), inputTime.Unix())
    46  	}
    47  }
    48  
    49  // TestGenerationOfNewID tests that the full ID generation output is as expected
    50  func TestGenerationOfNewIDWithVendorSecret(t *testing.T) {
    51  	inputTime := time.Now()
    52  	t.Log("Testing full ID generation with vendor secret")
    53  	id, err := Generate(IndicatorEntity, testVendor, testApp, testType, "", testVendorSecret)
    54  	if err != nil || id == "" || len(id) != idLength {
    55  		t.Errorf("Error generating ID")
    56  	}
    57  
    58  	re := regexp.MustCompile(idRegex)
    59  	match := re.FindStringSubmatch(id)
    60  	if len(match) != 1 {
    61  		t.Errorf("Generated ID did not match expected format: " + id)
    62  	}
    63  
    64  	if result, err := Verify(id, testVendorSecret); result == false {
    65  		t.Errorf("Generated ID was invalid: " + err.Error())
    66  	}
    67  
    68  	description, err := Describe(id)
    69  	if err != nil {
    70  		t.Errorf("Error with description " + err.Error())
    71  	}
    72  
    73  	keyTime := description.Time
    74  
    75  	if keyTime.Day() != inputTime.Day() {
    76  		t.Errorf("Timekey in ID is an unexpected value")
    77  	}
    78  
    79  	if result, _ := Verify(id, testVendorSecret+"qweqwe"); result == true {
    80  		t.Errorf("ID passed verification with invalid vendor secret")
    81  	}
    82  }
    83  
    84  // TestBase36KeyGeneration tests generation of the base36 time key
    85  func TestBase36KeyGeneration(t *testing.T) {
    86  	t.Log("Testing base 36 key generation")
    87  	key, err := getBase36TimeKey(time.Now())
    88  
    89  	if err != nil {
    90  		t.Errorf("Error generating ID time key")
    91  	}
    92  
    93  	if len(key) != specTimeKeyLength {
    94  		t.Errorf("Time key generated was not of specification length")
    95  	}
    96  }
    97  
    98  // TestRandomStringGeneration tests generation of the random string componenet
    99  func TestRandomStringGeneration(t *testing.T) {
   100  	t.Log("Testing random string generation")
   101  	randLength := 32
   102  	strRan := getRandString(randLength)
   103  	if len(strRan) != randLength {
   104  		t.Errorf("Random string generated with unexpected length")
   105  	}
   106  
   107  	if strRan != strings.ToUpper(strRan) {
   108  		t.Errorf("Random string contains unexpected characters")
   109  	}
   110  }
   111  
   112  const validIndicator = IndicatorEntity
   113  const invalidIndicator = TypeIndicator("Z")
   114  
   115  // TestTypeIndicatorValidation tests that Type Indicator validation correctly identifies valid Indicators
   116  func TestTypeIndicatorValidation(t *testing.T) {
   117  	t.Log("Testing type indicator validation")
   118  	if isValidIndicator(string(validIndicator)) != true {
   119  		t.Errorf("Valid type indicator flagged as invalid")
   120  	}
   121  
   122  	if isValidIndicator(string(invalidIndicator)) != false {
   123  		t.Errorf("Invalid type indicator flagged as valid")
   124  	}
   125  }
   126  
   127  const testValidID1 = "EFORTKPS-55QRHT4ET-USC1B-XRAQPPD"
   128  const testValidID2 = "EFIOUSAC-IP41IR5M=-MISCR-9JQLO0F"
   129  const testInvalidID = "EFOTKPS-55QRHT4E-USC1B-39H6POWT"
   130  
   131  // TestIDValidation tests IDs validate correctly
   132  func TestIDValidation(t *testing.T) {
   133  	t.Log("Testing ID Validation")
   134  	if result, err := Verify(testValidID1, ""); result == false {
   135  		t.Errorf("Valid ID failed verification" + err.Error())
   136  	}
   137  
   138  	if result, err := Verify(testValidID2, ""); result == false {
   139  		t.Errorf("Valid ID failed verification" + err.Error())
   140  	}
   141  
   142  	if result, err := Verify(testInvalidID, ""); result == true {
   143  		t.Errorf("Invalid ID did verify" + err.Error())
   144  	}
   145  }
   146  
   147  // TestValidationFromExternalSource tests externally generated IDs
   148  func TestValidationFromExternalSource(t *testing.T) {
   149  	fids := []string{
   150  		"EABCCDEF-IPIH7MHX=-MISCR-OI35356"}
   151  
   152  	for _, fid := range fids {
   153  		result, err := Verify(fid, "")
   154  		if result == false {
   155  			t.Errorf("Input fid failed verification %s", fid)
   156  		}
   157  
   158  		if err != nil {
   159  			t.Errorf("Error when validating fid")
   160  		}
   161  	}
   162  }
   163  
   164  // TestGetDescription generates a description and compares it against input data
   165  func TestGetDescription(t *testing.T) {
   166  	timeKey := "55QRHT4ET"
   167  	systemIndicator := IndicatorLog
   168  	vendorKey := "FOR"
   169  	app := "TE"
   170  	ntype := "ST"
   171  	location := "USC1B"
   172  	rand := "XRAQPPD"
   173  	inputID := string(systemIndicator) + vendorKey + app + ntype + delimitChar + timeKey + delimitChar + location + delimitChar + rand
   174  
   175  	description, err := Describe(inputID)
   176  	if err != nil {
   177  		t.Errorf("Error when describing FID")
   178  	}
   179  
   180  	if description.TimeKey != timeKey {
   181  		t.Errorf("Description time key %s does not match input %s", description.TimeKey, timeKey)
   182  	}
   183  
   184  	if description.Indicator != systemIndicator {
   185  		t.Errorf("Description indicator %s does not match input %s", description.Indicator, systemIndicator)
   186  	}
   187  
   188  	if description.VendorKey != vendorKey {
   189  		t.Errorf("Description vendor key %s does not match input %s", description.VendorKey, vendorKey)
   190  	}
   191  
   192  	if description.App != app {
   193  		t.Errorf("Description app %s does not match input %s", description.Type, app)
   194  	}
   195  
   196  	if description.Type != ntype {
   197  		t.Errorf("Description type %s does not match input %s", description.Type, ntype)
   198  	}
   199  
   200  	if description.Location != location {
   201  		t.Errorf("Description location %s does not match input %s", description.Location, location)
   202  	}
   203  
   204  	if description.RandomString != rand {
   205  		t.Errorf("Description random string %s does not match input %s", description.TimeKey, timeKey)
   206  	}
   207  }
   208  
   209  // Benchmark single fid generation
   210  func BenchmarkSingleFidGeneration(b *testing.B) {
   211  	for n := 0; n < 20; n++ {
   212  		Generate(IndicatorEntity, testVendor, testApp, testType, "", "")
   213  	}
   214  }
   215  
   216  // Benchmark to generate 1 million FIDs (12.7 seconds on mid 2015 Macbook pro)
   217  func BenchmarkOneMillionFidGeneration(b *testing.B) {
   218  	for i := 0; i < 1000000; i++ {
   219  		Generate(IndicatorEntity, testVendor, testApp, testType, "", "")
   220  	}
   221  }