File: EmbDataTest.cc

package info (click to toggle)
steghide 0.5.1-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,440 kB
  • sloc: cpp: 13,302; ansic: 6,184; sh: 3,960; makefile: 384; yacc: 316; perl: 228; sed: 16
file content (121 lines) | stat: -rw-r--r-- 3,832 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * steghide 0.5.1 - a steganography program
 * Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <vector>

#include "BitString.h"

#include "EmbDataTest.h"
#include "TestCategoryCaller.h"
#include "utcommon.h"

EmbDataTest::EmbDataTest (TestSuite* s)
	: UnitTest ("EmbData", s)
{
	ADDTESTCATEGORY (EmbDataTest, testEmbedding) ;
	ADDTESTCATEGORY (EmbDataTest, testExtracting) ;
}

void EmbDataTest::testEmbedding ()
{
	{
		std::vector<BYTE> data (1) ;
		EmbData emb (EmbData::EMBED, "passphrase not used", "test.txt") ;
		emb.setEncAlgo (EncryptionAlgorithm (EncryptionAlgorithm::NONE)) ;
		emb.setEncMode (EncryptionMode (EncryptionMode::ECB)) ;
		emb.setCompression (0) ;
		emb.setChecksum (false) ;
		emb.setData (data) ;

		BitString shouldbe ;
		shouldbe.append (EmbData::Magic, EmbData::NBitsMagic) ; // magic
		shouldbe.append (false) ; // version
		shouldbe.append ((BYTE) 0, 5) ; // encalgo
		shouldbe.append ((BYTE) 0, 3) ; // encmode
		shouldbe.append ((UWORD32) 82, 32) ; // nplainbits
		shouldbe.append (false) ; // compression
		shouldbe.append (false) ; // checksum
		shouldbe.append (std::string("test.txt")) ; // filename
		shouldbe.append ((BYTE) 0, 8) ; // end of filename
		shouldbe.append ((BYTE) 0, 8) ; // data

		addTestResult (genericTestEmbedding (emb, shouldbe)) ;
	}
}

void EmbDataTest::testExtracting ()
{
	{
		BitString bs ;
		bs.append (EmbData::Magic, EmbData::NBitsMagic) ; // magic
		bs.append (false) ; // version
		bs.append ((BYTE) 0, 5) ; // encalgo
		bs.append ((BYTE) 1, 3) ; // encmode
		bs.append ((UWORD32) 82, 32) ; // nplainbits
		bs.append (false) ; // compression
		bs.append (false) ; // checksum
		bs.append (std::string("testfile")) ; // filename
		bs.append ((BYTE) 0, 8) ; // end of filename
		bs.append ((BYTE) 0xff, 8) ; // data

		EmbData emb (EmbData::EXTRACT, "passphrase not used") ;
		bool ok = feed_to (bs, emb) ;

		ok = (emb.getEncAlgo() == EncryptionAlgorithm(EncryptionAlgorithm::NONE)) && ok ;
		ok = (emb.getEncMode() == EncryptionMode(EncryptionMode::CBC)) && ok ;
		ok = (emb.getCompression() == 0) && ok ;
		ok = (emb.getChecksum() == false) && ok ;
		ok = (emb.getFileName() == "testfile") && ok ;
		ok = (emb.getData().size() == 1 && emb.getData()[0] == 0xff) && ok ;

		addTestResult (ok) ;
	}
}

bool EmbDataTest::genericTestEmbedding (EmbData e, BitString shouldbe)
{
	BitString res = e.getBitString() ;
	bool retval = (res == shouldbe) ;
	if (ArgVerbose && !retval) {
		std::cerr << std::endl << "---- FAILED: genericTestEmbedding ----" << std::endl ;
		std::cerr << "result:" << std::endl ;
		res.print() ;
		std::cerr << "should be:" << std::endl ;
		shouldbe.print() ;
		std::cerr << "-------------------------------------" << std::endl ;
	}
	return retval ;
}

bool EmbDataTest::feed_to (const BitString& bs, EmbData& emb)
{
	UWORD32 idx = 0 ;
	while (!emb.finished()) {
		UWORD32 bitsrequested = emb.getNumBitsRequested() ;
		if (idx + bitsrequested > bs.getLength()) {
			return false ;
		}

		emb.addBits (bs.getBits (idx, bitsrequested)) ;
		idx += bitsrequested ;
	}

	return (idx == bs.getLength()) ;
}