213 lines
5.4 KiB
Go
213 lines
5.4 KiB
Go
package encryptedstring
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
)
|
|
|
|
var keyFilePath string
|
|
|
|
func startup() (err error) {
|
|
dir := getLocalPath()
|
|
if _, err := os.Stat(dir); err != nil {
|
|
os.MkdirAll(dir, os.ModePerm)
|
|
}
|
|
keyFilePath = path.Join(dir, EncryptionKeyFileName)
|
|
encryptionKey := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
|
doCreate := false
|
|
_, err = os.Stat(keyFilePath)
|
|
if err == nil {
|
|
err = os.Remove(keyFilePath)
|
|
doCreate = err == nil
|
|
} else {
|
|
doCreate = true
|
|
}
|
|
if doCreate {
|
|
err = os.WriteFile(keyFilePath, encryptionKey, os.ModePerm)
|
|
} else {
|
|
err = fmt.Errorf("unable to create custom keyfile")
|
|
}
|
|
return
|
|
}
|
|
|
|
func cleanup() {
|
|
_, err := os.Stat(keyFilePath)
|
|
if err == nil {
|
|
os.Remove(keyFilePath)
|
|
}
|
|
}
|
|
|
|
func TestEncryptedString(t *testing.T) {
|
|
if startup() != nil {
|
|
t.Log("test initialisation failed.\n")
|
|
cleanup()
|
|
t.FailNow()
|
|
}
|
|
|
|
// Basic tests
|
|
es := &EncryptedString{}
|
|
es.SetValue("testData")
|
|
if es.Value() != "testData" {
|
|
t.Log("value retrieval failed\n")
|
|
cleanup()
|
|
t.FailNow()
|
|
}
|
|
expected := "$!|7567707041677369090a010203040506"
|
|
if encrypted := es.String(); encrypted != expected {
|
|
t.Logf("Expected '%s' but got '%s'\n", expected, encrypted)
|
|
t.Fail()
|
|
}
|
|
|
|
// JSON Marshalling
|
|
marshalled, err := es.MarshalJSON()
|
|
if err == nil {
|
|
if string(marshalled) != "\""+expected+"\"" {
|
|
t.Logf("JSON Marshalling failed: Expected '%s' but got '%s'\n", "\""+expected+"\"", string(marshalled))
|
|
t.Fail()
|
|
}
|
|
} else {
|
|
t.Logf("JSON Marshalling failed: %s\n", err)
|
|
t.Fail()
|
|
}
|
|
if !t.Failed() {
|
|
marshalled[len(marshalled)-19]++
|
|
newValue := "testDatq"
|
|
if err := es.UnmarshalJSON(marshalled); err != nil {
|
|
t.Logf("JSON Unmarshalling failed: %s\n", err)
|
|
t.Fail()
|
|
} else if es.Value() != newValue {
|
|
t.Logf("JSON Unmarshalling failed: Expected '%s' but got '%s'", "\""+newValue+"\"\n", es.Value())
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
// XML Marshalling
|
|
tmp := New("testData")
|
|
es = &tmp
|
|
expectedXML := "<EncryptedString>" + expected + "</EncryptedString>"
|
|
marshalled, err = xml.Marshal(es)
|
|
if err != nil {
|
|
t.Logf("XML Marshalling failed: %s\n", err)
|
|
t.Fail()
|
|
} else if string(marshalled) != expectedXML {
|
|
t.Logf("XML Marshalling failed: Expected '%s' but got '%s'\n", expectedXML, string(marshalled))
|
|
t.Fail()
|
|
}
|
|
if !t.Failed() {
|
|
marshalled[34]++
|
|
newValue := "testDatq"
|
|
if err := xml.Unmarshal(marshalled, &es); err != nil {
|
|
t.Logf("JSON Unmarshalling failed: %s\n", err)
|
|
t.Fail()
|
|
} else if es.Value() != newValue {
|
|
t.Logf("JSON Unmarshalling failed: Expected '%s' but got '%s'\n", "\""+newValue+"\"", es.Value())
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
// Text Marshalling
|
|
es.SetValue("testData")
|
|
marshalled, err = es.MarshalText()
|
|
if err == nil {
|
|
if string(marshalled) != expected {
|
|
t.Logf("Text Marshalling failed: Expected '%s' but got '%s'\n", expected, string(marshalled))
|
|
t.Fail()
|
|
}
|
|
} else {
|
|
t.Logf("Text Marshalling failed: %s\n", err)
|
|
t.Fail()
|
|
}
|
|
if !t.Failed() {
|
|
marshalled[len(marshalled)-18]++
|
|
newValue := "testDatq"
|
|
if err := es.UnmarshalText(marshalled); err != nil {
|
|
t.Logf("Text Unmarshalling failed: %s\n", err)
|
|
t.Fail()
|
|
} else if es.Value() != newValue {
|
|
t.Logf("Text Unmarshalling failed: Expected '%s' but got '%s'\n", newValue, es.Value())
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
marshalled = []byte("unencrypted")
|
|
expected = "unencrypted"
|
|
if err := es.UnmarshalText(marshalled); err != nil {
|
|
t.Logf("Text Unmarshalling of unencrypted value failed: %s\n", err)
|
|
t.Fail()
|
|
} else if es.Value() != expected {
|
|
t.Logf("Text Unmarshalling of unencrypted value failed: Expected '%s' but got '%s'\n", expected, es.Value())
|
|
t.Fail()
|
|
}
|
|
if !es.WasUnencrypted() {
|
|
t.Logf("Text Unmarshalling of unencrypted value failed: was unencrypted flag is missing\n")
|
|
t.Fail()
|
|
}
|
|
|
|
cleanup()
|
|
}
|
|
|
|
func TestEncryptedStringFailures(t *testing.T) {
|
|
if startup() != nil {
|
|
t.Log("test initialisation failed.\n")
|
|
cleanup()
|
|
t.FailNow()
|
|
}
|
|
|
|
es := &EncryptedString{}
|
|
es.SetValue("testData")
|
|
expected := "$!|7567707041677369"
|
|
|
|
err := es.UnmarshalJSON([]byte(expected + "\""))
|
|
if err == nil {
|
|
t.Log("JSON Unmarshalling 1 succeeded unexpectedly\n")
|
|
t.Fail()
|
|
}
|
|
|
|
testData := "A" + expected[1:]
|
|
err = es.UnmarshalJSON([]byte("\"" + testData + "\""))
|
|
if err != nil {
|
|
t.Log("JSON Unmarshalling 2 failed unexpectedly\n")
|
|
t.Fail()
|
|
} else if result := es.Value(); result != testData {
|
|
t.Logf("JSON Unmarshalling 2 yielded '%s' instead of '%s'\n", result, testData)
|
|
t.Fail()
|
|
}
|
|
|
|
// testData = "<EncryptedString>" + expected + "</EncryptedString>"
|
|
testData = "<<EncryptedString>" + expected + "</EncryptedString>"
|
|
err = xml.Unmarshal([]byte(testData), es)
|
|
if err == nil {
|
|
t.Log("XML Unmarshalling 1 succeeded unexpectedly\n")
|
|
t.Fail()
|
|
}
|
|
|
|
testData = "<EncryptedString>" + expected + "</EncryptString>"
|
|
err = xml.Unmarshal([]byte(testData), es)
|
|
if err == nil {
|
|
t.Log("XML Unmarshalling 2 succeeded unexpectedly\n")
|
|
t.Fail()
|
|
}
|
|
|
|
testData = "<EncryptedString>" + expected[1:] + "</EncryptedString>"
|
|
err = xml.Unmarshal([]byte(testData), es)
|
|
if err != nil {
|
|
t.Log("XML Unmarshalling 3 failed unexpectedly\n")
|
|
t.Fail()
|
|
} else if result := es.Value(); result != expected[1:] {
|
|
t.Logf("XML Unmarshalling 3 yielded '%s' instead of '%s'\n", result, expected[1:])
|
|
t.Fail()
|
|
}
|
|
|
|
testData = "<EncryptedString><EncryptedString>" + expected + "</EncryptString></EncryptString>"
|
|
err = xml.Unmarshal([]byte(testData), es)
|
|
if err == nil {
|
|
t.Log("XML Unmarshalling 4 succeeded unexpectedly\n")
|
|
t.Fail()
|
|
}
|
|
|
|
cleanup()
|
|
}
|