Ejemplo n.º 1
0
// ParseHistoryResponseForMultipleMessages unmarshalls the response of the history call to the
// pubnub api and compares the received messages to the sent messages. If the response match the
// test is successful.
//
// Parameters:
// returnChannel: channel to read the response from,
// t: a reference to *testing.T,
// channel: the pubnub channel to publish the messages,
// message: message to compare,
// testname: the test name form where this method is called,
// startMessagesFrom: the message identifer,
// numberOfMessages: number of messages to send,
// cipherKey: the cipher key if used. Can be empty.
func ParseHistoryResponseForMultipleMessages(returnChannel chan []byte, channel string, message string, testName string, startMessagesFrom int, numberOfMessages int, cipherKey string, responseChannel chan string) {
	for {
		value, ok := <-returnChannel
		if !ok {
			break
		}
		if string(value) != "[]" {
			data, _, _, err := messaging.ParseJSON(value, cipherKey)
			if err != nil {
				//t.Error("Test '" + testName + "': failed.")
				responseChannel <- "Test '" + testName + "': failed. Message: " + err.Error()
			} else {
				var arr []string
				err2 := json.Unmarshal([]byte(data), &arr)
				if err2 != nil {
					//t.Error("Test '" + testName + "': failed.");
					responseChannel <- "Test '" + testName + "': failed. Message: " + err2.Error()
				} else {
					messagesReceived := 0

					if len(arr) != numberOfMessages {
						responseChannel <- "Test '" + testName + "': failed."
						//t.Error("Test '" + testName + "': failed.");
						break
					}
					for i := 0; i < numberOfMessages; i++ {
						if arr[i] == message+strconv.Itoa(startMessagesFrom+i) {
							//fmt.Println("data:",arr[i])
							messagesReceived++
						}
					}
					if messagesReceived == numberOfMessages {
						fmt.Println("Test '" + testName + "': passed.")
						responseChannel <- "Test '" + testName + "': passed."
					} else {
						responseChannel <- "Test '" + testName + "': failed. Returned message mismatch"
						//t.Error("Test '" + testName + "': failed.");
					}
					break
				}
			}
		}
	}
}
Ejemplo n.º 2
0
// TestGermanDecryption tests the German decryption.
// Assumes that the input message is deserialized
// Decrypted string should match ÜÖ
func TestGermanDecryption(t *testing.T) {
	message := "stpgsG1DZZxb44J7mFNSzg=="
	//decrypt
	decrypted, decErr := messaging.DecryptString("enigma", message)
	if decErr != nil {
		t.Error("German decryption: failed.")
	} else {
		data, _, _, err := messaging.ParseJSON([]byte(decrypted.(string)), "")
		if err != nil {
			t.Error("German decryption: failed.", err)
		} else {
			if "ÜÖ" == data {
				fmt.Println("German decryption: passed.")
			} else {
				t.Error("German decryption: failed.")
			}
		}
	}
}
Ejemplo n.º 3
0
// TestUnicodeDecryption tests the Unicode decryption.
// Assumes that the input message is deserialized
// Decrypted string should match 漢語
func TestUnicodeDecryption(t *testing.T) {
	message := "+BY5/miAA8aeuhVl4d13Kg=="
	//decrypt
	decrypted, decErr := messaging.DecryptString("enigma", message)
	if decErr != nil {
		t.Error("Unicode decryption: failed.")
	} else {
		data, _, _, err := messaging.ParseJSON([]byte(decrypted.(string)), "")
		if err != nil {
			t.Error("Unicode decryption: failed.", err)
		} else {
			if "漢語" == data {
				fmt.Println("Unicode decryption: passed.")
			} else {
				t.Error("Unicode decryption: failed.")
			}
		}
	}
}