-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f981ef
commit f87f992
Showing
5 changed files
with
229 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestEncryptDecrypt(t *testing.T) { | ||
tests := []struct { | ||
password string | ||
plaintext []byte | ||
}{ | ||
{ | ||
password: "correcthorsebatterystaple", | ||
plaintext: []byte("This is a test message."), | ||
}, | ||
{ | ||
password: "password123", | ||
plaintext: []byte("Another test message!"), | ||
}, | ||
{ | ||
password: "password123", | ||
plaintext: []byte("Short"), | ||
}, | ||
{ | ||
password: "password123", | ||
plaintext: []byte(""), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("password=%s", tt.password), func(t *testing.T) { | ||
// Encrypt | ||
ciphertext, err := Encrypt(tt.password, tt.plaintext) | ||
if err != nil { | ||
t.Fatalf("Encryption failed: %v", err) | ||
} | ||
|
||
// Decrypt | ||
decryptedText, err := Decrypt(tt.password, ciphertext) | ||
if err != nil { | ||
t.Fatalf("Decryption failed: %v", err) | ||
} | ||
|
||
// Check if decrypted text matches original plaintext | ||
if !bytes.Equal(decryptedText, tt.plaintext) { | ||
t.Errorf("Decrypted text does not match original plaintext. Expected: %s, got: %s", tt.plaintext, decryptedText) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDecryptWithIncorrectPassword(t *testing.T) { | ||
plaintext := []byte("Test for incorrect password") | ||
password := "correctpassword" | ||
incorrectPassword := "wrongpassword" | ||
|
||
// Encrypt with the correct password | ||
ciphertext, err := Encrypt(password, plaintext) | ||
if err != nil { | ||
t.Fatalf("Encryption failed: %v", err) | ||
} | ||
|
||
// Decrypt with incorrect password | ||
_, err = Decrypt(incorrectPassword, ciphertext) | ||
if err == nil { | ||
t.Errorf("Expected decryption to fail with incorrect password") | ||
} | ||
} | ||
|
||
func TestDecryptWithCorruptedCiphertext(t *testing.T) { | ||
plaintext := []byte("Test for corrupted ciphertext") | ||
password := "correctpassword" | ||
|
||
// Encrypt with the correct password | ||
ciphertext, err := Encrypt(password, plaintext) | ||
if err != nil { | ||
t.Fatalf("Encryption failed: %v", err) | ||
} | ||
|
||
// Corrupt the ciphertext by changing some bytes | ||
corruptedCiphertext := append([]byte{}, ciphertext...) | ||
corruptedCiphertext[10] = 0xFF | ||
|
||
// Decrypt the corrupted ciphertext | ||
_, err = Decrypt(password, corruptedCiphertext) | ||
if err == nil { | ||
t.Errorf("Expected decryption to fail with corrupted ciphertext") | ||
} | ||
} | ||
|
||
func TestEmptyPlaintext(t *testing.T) { | ||
// Test with empty plaintext | ||
plaintext := []byte("") | ||
password := "password123" | ||
|
||
// Encrypt with the correct password | ||
ciphertext, err := Encrypt(password, plaintext) | ||
if err != nil { | ||
t.Fatalf("Encryption failed: %v", err) | ||
} | ||
|
||
// Decrypt the ciphertext | ||
decryptedText, err := Decrypt(password, ciphertext) | ||
if err != nil { | ||
t.Fatalf("Decryption failed: %v", err) | ||
} | ||
|
||
// Check if decrypted text is also empty | ||
if len(decryptedText) != 0 { | ||
t.Errorf("Expected empty decrypted text, got: %s", decryptedText) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/go-audio/audio" | ||
"testing" | ||
) | ||
|
||
func TestEmbedDataWithDepthAudio(t *testing.T) { | ||
tests := []struct { | ||
data []byte | ||
bitDepth uint8 | ||
}{ | ||
{ | ||
data: []byte("Hello, world!"), | ||
bitDepth: 1, | ||
}, | ||
{ | ||
data: []byte("Another test data"), | ||
bitDepth: 2, | ||
}, | ||
{ | ||
data: []byte("Small"), | ||
bitDepth: 3, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("bitDepth=%d", tt.bitDepth), func(t *testing.T) { | ||
// Create a dummy audio buffer with arbitrary values | ||
buffer := &audio.IntBuffer{ | ||
Data: make([]int, len(tt.data)*8000), | ||
Format: &audio.Format{ | ||
SampleRate: 44100, | ||
NumChannels: 1, // Adjusted to use NumChannels instead of SampleWidth | ||
}, | ||
} | ||
|
||
// Embed data in audio buffer with bit depth | ||
embededBuff, _ := EmbedDataWithDepthAudio(buffer, tt.data, tt.bitDepth) | ||
|
||
// Extract data from the audio buffer to verify embedding worked | ||
extractedData := ExtractDataWithDepthAudio(embededBuff, tt.bitDepth) | ||
|
||
// Compare extracted data with original data | ||
if !bytes.Contains(extractedData, tt.data) { | ||
t.Errorf("Extracted data does not match original data. Expected: %s, got: %s", tt.data, extractedData) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestExtractDataWithDepthAudio(t *testing.T) { | ||
// Create a dummy audio buffer with embedded data | ||
data := []byte("Embed this data with bit depth") | ||
bitDepth := uint8(2) | ||
buffer := &audio.IntBuffer{ | ||
Data: make([]int, len(data)*8), | ||
Format: &audio.Format{ | ||
SampleRate: 44100, | ||
NumChannels: 1, // Adjusted to use NumChannels instead of SampleWidth | ||
}, | ||
} | ||
|
||
// Embed data in audio buffer with bit depth | ||
embededBuffer, _ := EmbedDataWithDepthAudio(buffer, data, bitDepth) | ||
|
||
// Extract data from the audio buffer with specific bit depth | ||
extractedData := ExtractDataWithDepthAudio(embededBuffer, bitDepth) | ||
|
||
// Compare extracted data with original data | ||
if !bytes.Contains(extractedData, data) { | ||
t.Errorf("Extracted data does not match original data. Expected: %s, got: %s", data, extractedData) | ||
} | ||
} | ||
|
||
func TestEmptyDataEmbedding(t *testing.T) { | ||
// Test with empty data input | ||
data := []byte("") | ||
depth := uint8(2) | ||
buffer := &audio.IntBuffer{ | ||
Data: make([]int, 100), | ||
Format: &audio.Format{ | ||
SampleRate: 44100, | ||
NumChannels: 1, // Adjusted to use NumChannels instead of SampleWidth | ||
}, | ||
} | ||
|
||
// Embed empty data at depth | ||
_, err := EmbedDataAtDepthAudio(buffer, data, depth) | ||
if err == nil { | ||
t.Error("Embedded empty data instead of returning error") | ||
} | ||
} |