github.com/tacerus/ldifdiff@v0.0.0-20181030102753-4dccbe38183b/input_test.go (about) 1 package ldifdiff 2 3 import ( 4 "os" 5 "strconv" 6 "strings" 7 "testing" 8 ) 9 10 func TestImportLdifFile(t *testing.T) { 11 12 entries, err := importLdifFile(testSourceLdifFile, testIgnoreAttr) 13 okLdifTests(t, entries, testIgnoreAttr, err) 14 15 _, err = importLdifFile(testInvalidLineContLdifFile, testIgnoreAttr) 16 invalidLineCont(t, err) 17 18 _, err = importLdifFile(testInvalidNoDnLdifFile, testIgnoreAttr) 19 invalidNoDn(t, err) 20 21 } 22 23 func TestConvertLdifStr(t *testing.T) { 24 entries, err := convertLdifStr(testSourceStr, testIgnoreAttr) 25 okLdifTests(t, entries, testIgnoreAttr, err) 26 27 _, err = convertLdifStr(testInvalidLineContStr, testIgnoreAttr) 28 invalidLineCont(t, err) 29 30 entries, err = convertLdifStr(testInvalidNoDnStr, testIgnoreAttr) 31 invalidNoDn(t, err) 32 } 33 34 func TestImportLdifFileBig(t *testing.T) { 35 if os.Getenv(testBigFilesEnv) != testBigFilesEnvValue { 36 t.Skip("Skipping big files test") 37 } 38 entries, err := importLdifFile(testSourceLdifFileBig, testIgnoreAttr) 39 okLdifTests(t, entries, testIgnoreAttr, err) 40 } 41 42 func TestConvertLdifStrBig(t *testing.T) { 43 if os.Getenv(testBigFilesEnv) != testBigFilesEnvValue { 44 t.Skip("Skipping big files test") 45 } 46 entries, err := convertLdifStr(testSourceStrBig, testIgnoreAttr) 47 okLdifTests(t, entries, testIgnoreAttr, err) 48 } 49 50 /* Helper test evaluation */ 51 func okLdifTests(t *testing.T, entries entries, ignoreAttr []string, err error) { 52 if err != nil { 53 t.Error("Expected values, got error: ", err) 54 } 55 if len(entries) != testSourceNrEntries { 56 t.Error("Expected", testSourceNrEntries, "entries, got", strconv.Itoa(len(entries))) 57 } 58 for dn, attributes := range entries { 59 if !strings.HasPrefix(dn, "dn:") { 60 t.Error("Invalid dn:", dn) 61 } 62 for _, attr := range attributes { 63 if strings.HasPrefix(attr, "#") { 64 t.Error("Invalid comment:", attr) 65 } 66 if strings.HasPrefix(attr, " ") { 67 t.Error("Line continuation not correctly appended:", attr) 68 } 69 if strings.IndexAny(attr, " :") < 1 { 70 t.Error("Invalid attribute line:", attr) 71 } 72 } 73 } 74 if len(ignoreAttr) > 0 { 75 for _, attributes := range entries { 76 for _, attr := range attributes { 77 for _, ignoredAttr := range ignoreAttr { 78 if strings.HasPrefix(attr, ignoredAttr+":") { 79 t.Error("Attributed not ignored as requested:", 80 ignoredAttr, attr) 81 } 82 } 83 } 84 } 85 } 86 } 87 88 func invalidLineCont(t *testing.T, err error) { 89 if err == nil { 90 t.Error("Error expected (line continuation), but none received") 91 } 92 } 93 94 func invalidNoDn(t *testing.T, err error) { 95 if err == nil { 96 t.Error("Error expected (no dn), but none received") 97 } 98 }