1 /* libcmis
2  * Version: MPL 1.1 / GPLv2+ / LGPLv2+
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License or as specified alternatively below. You may obtain a copy of
7  * the License at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * Major Contributor(s):
15  * Copyright (C) 2011 SUSE <cbosdonnat@suse.com>
16  *
17  *
18  * All Rights Reserved.
19  *
20  * For minor contributions see the git repository.
21  *
22  * Alternatively, the contents of this file may be used under the terms of
23  * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
24  * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
25  * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
26  * instead of those above.
27  */
28 
29 #include "test-dummies.hxx"
30 
31 using namespace std;
32 using libcmis::PropertyPtrMap;
33 
34 bool isOutOfMemory = false;
35 
36 /// Ignore all tests results depending on this when running in valgrind
operator new(size_t requestedSize)37 void * operator new ( size_t requestedSize )
38 {
39     if ( isOutOfMemory )
40     {
41         throw bad_alloc( );
42     }
43 
44     return malloc( requestedSize );
45 }
46 
operator delete(void * ptr)47 void operator delete ( void* ptr ) throw ( )
48 {
49     free( ptr );
50 }
51 
52 #if __cplusplus > 201103L
operator delete(void * ptr,std::size_t)53 void operator delete ( void* ptr, std::size_t ) throw ( )
54 {
55     free( ptr );
56 }
57 #endif
58 
59 namespace dummies
60 {
Session()61     Session::Session( )
62     {
63     }
64 
~Session()65     Session::~Session( )
66     {
67     }
68 
getRepository()69     libcmis::RepositoryPtr Session::getRepository( )
70     {
71         libcmis::RepositoryPtr repo( new Repository( ) );
72         return repo;
73     }
74 
75 
setRepository(std::string)76     bool Session::setRepository( std::string )
77     {
78         return true;
79     }
80 
getRepositories()81     vector< libcmis::RepositoryPtr > Session::getRepositories( )
82     {
83         vector< libcmis::RepositoryPtr > repos;
84         libcmis::RepositoryPtr repo1( new Repository( ) );
85         libcmis::RepositoryPtr repo2( new Repository( ) );
86         repos.push_back( repo1 );
87         repos.push_back( repo2 );
88         return repos;
89     }
90 
getRootFolder()91     libcmis::FolderPtr Session::getRootFolder()
92     {
93         libcmis::FolderPtr root( new Folder( true, false ) );
94         return root;
95     }
96 
getObject(string id)97     libcmis::ObjectPtr Session::getObject( string id )
98     {
99         return getFolder( id );
100     }
101 
getObjectByPath(string path)102     libcmis::ObjectPtr Session::getObjectByPath( string path )
103     {
104         return getFolder( path );
105     }
106 
getFolder(string)107     libcmis::FolderPtr Session::getFolder( string )
108     {
109         libcmis::FolderPtr result( new Folder( false, false ) );
110         return result;
111     }
112 
getType(string)113     libcmis::ObjectTypePtr Session::getType( string )
114     {
115         libcmis::ObjectTypePtr type( new ObjectType( true, false ) );
116         return type;
117     }
118 
getBaseTypes()119     vector< libcmis::ObjectTypePtr > Session::getBaseTypes( )
120     {
121         vector< libcmis::ObjectTypePtr > types;
122         libcmis::ObjectTypePtr type( new ObjectType( true, false ) );
123         types.push_back( type );
124         return types;
125     }
126 
getRefreshToken()127     std::string Session::getRefreshToken( )
128     {
129         return string( );
130     }
131 
Repository()132     Repository::Repository( ) :
133         libcmis::Repository( )
134     {
135         m_id = string( "Repository::Id" );
136         m_name = string( "Repository::Name" );
137         m_description = string( "Repository::Description" );
138         m_vendorName = string( "Repository::VendorName" );
139         m_productName = string( "Repository::ProductName" );
140         m_productVersion = string( "Repository::ProductVersion" );
141         m_rootId = string( "Repository::RootId" );
142         m_cmisVersionSupported = string( "Repository::CmisVersionSupported" );
143         m_thinClientUri.reset( new string( "Repository::ThinClientUri" ) );
144         m_principalAnonymous.reset( new string( "Repository::PrincipalAnonymous" ) );
145         m_principalAnyone.reset( new string( "Repository::PrincipalAnyone" ) );
146     }
147 
~Repository()148     Repository::~Repository( )
149     {
150     }
151 
PropertyType(string id,string xmlType)152     PropertyType::PropertyType( string id, string xmlType ) :
153         libcmis::PropertyType( )
154     {
155         setId( id );
156         setLocalName( string( "PropertyType::LocalName" ) );
157         setLocalNamespace( string( "PropertyType::LocalNamespace" ) );
158         setDisplayName( string( "PropertyType::DisplayName" ) );
159         setQueryName( string( "PropertyType::QueryName" ) );
160         setTypeFromXml( xmlType );
161 
162         // Setting true for the tests to see a difference with
163         // the default false result of the tested functions
164         setMultiValued( true );
165         setUpdatable( true );
166         setInherited( true );
167         setRequired( true );
168         setQueryable( true );
169         setOrderable( true );
170         setOpenChoice( true );
171     }
172 
~PropertyType()173     PropertyType::~PropertyType( )
174     {
175     }
176 
AllowableActions()177     AllowableActions::AllowableActions( ) :
178         libcmis::AllowableActions( )
179     {
180         m_states.insert( pair< libcmis::ObjectAction::Type, bool >( libcmis::ObjectAction::GetProperties, true ) );
181         m_states.insert( pair< libcmis::ObjectAction::Type, bool >( libcmis::ObjectAction::GetFolderParent, false ) );
182     }
183 
~AllowableActions()184     AllowableActions::~AllowableActions( )
185     {
186     }
187 
ObjectType()188     ObjectType::ObjectType( ) :
189         libcmis::ObjectType( ),
190         m_typeId( ),
191         m_childrenIds( ),
192         m_triggersFaults( false )
193     {
194     }
195 
ObjectType(bool rootType,bool triggersFaults)196     ObjectType::ObjectType( bool rootType, bool triggersFaults ) :
197         libcmis::ObjectType( ),
198         m_typeId( ),
199         m_childrenIds( ),
200         m_triggersFaults( triggersFaults )
201     {
202         if ( rootType )
203             m_typeId = "RootType";
204         else
205         {
206             m_typeId = "ObjectType";
207             m_parentTypeId = "ParentType";
208             m_childrenIds.push_back( "ChildType1" );
209             m_childrenIds.push_back( "ChildType2" );
210         }
211 
212         m_baseTypeId = "RootType";
213         libcmis::PropertyTypePtr propType1( new PropertyType( "Property1", "string" ) );
214         m_propertiesTypes.insert( pair< string, libcmis::PropertyTypePtr >( propType1->getId( ), propType1 ) );
215         libcmis::PropertyTypePtr propType2( new PropertyType( "Property2", "string" ) );
216         m_propertiesTypes.insert( pair< string, libcmis::PropertyTypePtr >( propType2->getId( ), propType2 ) );
217         libcmis::PropertyTypePtr propType3( new PropertyType( "Property3", "string" ) );
218         m_propertiesTypes.insert( pair< string, libcmis::PropertyTypePtr >( propType3->getId( ), propType3 ) );
219 
220         initMembers( );
221     }
222 
initMembers()223     void ObjectType::initMembers( )
224     {
225 
226         m_id = m_typeId + "::Id";
227         m_localName = m_typeId + "::LocalName";
228         m_localNamespace = m_typeId + "::LocalNamespace";
229         m_displayName = m_typeId + "::DisplayName";
230         m_queryName = m_typeId + "::QueryName";
231         m_description = m_typeId + "::Description";
232 
233         m_creatable = true;
234         m_fileable = true;
235         m_queryable = true;
236         m_fulltextIndexed = true;
237         m_includedInSupertypeQuery = true;
238         m_controllablePolicy = true;
239         m_controllableAcl = true;
240         m_versionable = true;
241         m_contentStreamAllowed = libcmis::ObjectType::Allowed;
242     }
243 
~ObjectType()244     ObjectType::~ObjectType( )
245     {
246     }
247 
getParentType()248     libcmis::ObjectTypePtr  ObjectType::getParentType( )
249     {
250         if ( m_triggersFaults )
251             throw libcmis::Exception( "Fault triggered" );
252 
253         ObjectType* parent = NULL;
254         if ( !m_parentTypeId.empty( ) )
255         {
256             parent = new ObjectType( );
257             parent->m_typeId = m_parentTypeId;
258             parent->m_parentTypeId = m_baseTypeId;
259             parent->m_baseTypeId = m_baseTypeId;
260             parent->m_childrenIds.push_back( m_id );
261             parent->m_triggersFaults = m_triggersFaults;
262             parent->m_propertiesTypes = m_propertiesTypes;
263 
264             parent->initMembers( );
265         }
266 
267         libcmis::ObjectTypePtr result( parent );
268         return result;
269     }
270 
getBaseType()271     libcmis::ObjectTypePtr  ObjectType::getBaseType( )
272     {
273         if ( m_triggersFaults )
274             throw libcmis::Exception( "Fault triggered" );
275 
276         ObjectType* base = this;
277         if ( m_typeId != m_baseTypeId )
278         {
279             base = new ObjectType( );
280             base->m_typeId = m_baseTypeId;
281             base->m_baseTypeId = m_baseTypeId;
282             base->m_childrenIds.push_back( m_id );
283             base->m_triggersFaults = m_triggersFaults;
284             base->m_propertiesTypes = m_propertiesTypes;
285 
286             base->initMembers( );
287         }
288 
289         libcmis::ObjectTypePtr result( base );
290         return result;
291     }
292 
getChildren()293     vector< libcmis::ObjectTypePtr > ObjectType::getChildren( )
294     {
295         if ( m_triggersFaults )
296             throw libcmis::Exception( "Fault triggered" );
297 
298         vector< libcmis::ObjectTypePtr > children;
299 
300         for ( vector< string >::iterator it = m_childrenIds.begin( ); it != m_childrenIds.end( ); ++it )
301         {
302             ObjectType* child = new ObjectType( );
303             child->m_typeId = *it;
304             child->m_parentTypeId = m_typeId;
305             child->m_baseTypeId = m_baseTypeId;
306             child->m_triggersFaults = m_triggersFaults;
307             child->m_propertiesTypes = m_propertiesTypes;
308 
309             child->initMembers( );
310 
311             libcmis::ObjectTypePtr result( child );
312             children.push_back( result );
313         }
314 
315         return children;
316     }
317 
toString()318     string ObjectType::toString( )
319     {
320         return m_typeId + "::toString";
321     }
322 
Object(bool triggersFaults,string type)323     Object::Object( bool triggersFaults, string type ):
324         libcmis::Object( NULL ),
325         m_type( type ),
326         m_triggersFaults( triggersFaults )
327     {
328         libcmis::PropertyTypePtr propertyType( new PropertyType( "Property1", "string" ) );
329         vector< string > values;
330         values.push_back( "Value1" );
331         libcmis::PropertyPtr property( new libcmis::Property( propertyType, values ) );
332         m_properties.insert( pair< string, libcmis::PropertyPtr >( propertyType->getId( ), property ) );
333     }
334 
getId()335     string Object::getId( )
336     {
337         return m_type + "::Id";
338     }
339 
getName()340     string Object::getName( )
341     {
342         return m_type + "::Name";
343     }
344 
getPaths()345     vector< string > Object::getPaths( )
346     {
347         vector< string > paths;
348         paths.push_back( string( "/Path1/" ) );
349         paths.push_back( string( "/Path2/" ) );
350 
351         return paths;
352     }
353 
getBaseType()354     string Object::getBaseType( )
355     {
356         return m_type + "::BaseType";
357     }
358 
getType()359     string Object::getType( )
360     {
361         return m_type + "::Type";
362     }
363 
getCreationDate()364     boost::posix_time::ptime Object::getCreationDate( )
365     {
366         boost::posix_time::ptime now( boost::posix_time::second_clock::local_time( ) );
367         return now;
368     }
369 
getLastModificationDate()370     boost::posix_time::ptime Object::getLastModificationDate( )
371     {
372         boost::posix_time::ptime now( boost::posix_time::second_clock::local_time( ) );
373         return now;
374     }
375 
updateProperties(const PropertyPtrMap &)376     libcmis::ObjectPtr Object::updateProperties(
377            const PropertyPtrMap& )
378     {
379         if ( m_triggersFaults )
380             throw libcmis::Exception( "Fault triggered" );
381 
382         time( &m_refreshTimestamp );
383         libcmis::ObjectPtr result( new Object( false ) );
384         return result;
385     }
386 
getTypeDescription()387     libcmis::ObjectTypePtr Object::getTypeDescription( )
388     {
389         libcmis::ObjectTypePtr type( new ObjectType( false, m_triggersFaults ) );
390         return type;
391     }
392 
getAllowableActions()393     libcmis::AllowableActionsPtr Object::getAllowableActions( )
394     {
395         libcmis::AllowableActionsPtr allowableActions( new AllowableActions( ) );
396         return allowableActions;
397     }
398 
refresh()399     void Object::refresh( )
400     {
401         if ( m_triggersFaults )
402             throw libcmis::Exception( "Fault triggered" );
403 
404         time( &m_refreshTimestamp );
405     }
406 
remove(bool)407     void Object::remove( bool )
408     {
409         if ( m_triggersFaults )
410             throw libcmis::Exception( "Fault triggered" );
411 
412         time( &m_refreshTimestamp );
413     }
414 
move(libcmis::FolderPtr,libcmis::FolderPtr)415     void Object::move( libcmis::FolderPtr, libcmis::FolderPtr )
416     {
417         if ( m_triggersFaults )
418             throw libcmis::Exception( "Fault triggered" );
419 
420         time( &m_refreshTimestamp );
421     }
422 
toXml(xmlTextWriterPtr)423     void Object::toXml( xmlTextWriterPtr )
424     {
425     }
426 
Folder(bool isRoot,bool triggersFaults)427     Folder::Folder( bool isRoot, bool triggersFaults ) :
428         libcmis::Object( NULL ),
429         libcmis::Folder( NULL ),
430         dummies::Object( triggersFaults, "Folder" ),
431         m_isRoot( isRoot )
432     {
433     }
434 
getFolderParent()435     libcmis::FolderPtr Folder::getFolderParent( )
436     {
437         if ( m_triggersFaults )
438             throw libcmis::Exception( "Fault triggered" );
439 
440         libcmis::FolderPtr parent;
441 
442         if ( !m_isRoot )
443             parent.reset( new Folder( true, m_triggersFaults ) );
444 
445         return parent;
446     }
447 
getChildren()448     vector< libcmis::ObjectPtr > Folder::getChildren( )
449     {
450         if ( m_triggersFaults )
451             throw libcmis::Exception( "Fault triggered" );
452 
453         vector< libcmis::ObjectPtr > children;
454 
455         libcmis::ObjectPtr child1( new Object( m_triggersFaults ) );
456         children.push_back( child1 );
457         libcmis::ObjectPtr child2( new Object( m_triggersFaults ) );
458         children.push_back( child2 );
459 
460         return children;
461     }
462 
getPath()463     string Folder::getPath( )
464     {
465         return string( "/Path/" );
466     }
467 
isRootFolder()468     bool Folder::isRootFolder( )
469     {
470         return m_isRoot;
471     }
472 
createFolder(const PropertyPtrMap &)473     libcmis::FolderPtr Folder::createFolder( const PropertyPtrMap& )
474     {
475         if ( m_triggersFaults )
476             throw libcmis::Exception( "Fault triggered" );
477 
478         libcmis::FolderPtr created( new Folder( true, m_triggersFaults ) );
479         return created;
480     }
481 
createDocument(const PropertyPtrMap & properties,boost::shared_ptr<ostream> os,string contentType,string filename)482     libcmis::DocumentPtr Folder::createDocument( const PropertyPtrMap& properties,
483                             boost::shared_ptr< ostream > os, string contentType, string filename )
484     {
485         if ( m_triggersFaults )
486             throw libcmis::Exception( "Fault triggered" );
487 
488         dummies::Document* document = new dummies::Document( true, false );
489 
490         PropertyPtrMap propertiesCopy( properties );
491         document->getProperties( ).swap( propertiesCopy );
492         document->setContentStream( os, contentType, filename );
493 
494         libcmis::DocumentPtr created( document );
495         return created;
496     }
497 
removeTree(bool,libcmis::UnfileObjects::Type,bool)498     vector< string > Folder::removeTree( bool, libcmis::UnfileObjects::Type,
499                             bool )
500     {
501         if ( m_triggersFaults )
502             throw libcmis::Exception( "Fault triggered" );
503 
504         time( &m_refreshTimestamp );
505 
506         vector< string > failed;
507         failed.push_back( "failed 1" );
508         return failed;
509     }
510 
Document(bool isFiled,bool triggersFaults)511     Document::Document( bool isFiled, bool triggersFaults ) :
512         libcmis::Object( NULL ),
513         libcmis::Document( NULL ),
514         dummies::Object( triggersFaults, "Document" ),
515         m_isFiled( isFiled ),
516         m_contentString( "Document::ContentStream" )
517     {
518     }
519 
getParents()520     vector< libcmis::FolderPtr > Document::getParents( )
521     {
522         if ( m_triggersFaults )
523             throw libcmis::Exception( "Fault triggered" );
524 
525         vector< libcmis::FolderPtr > parents;
526         if ( m_isFiled )
527         {
528             libcmis::FolderPtr parent1( new Folder( true, m_triggersFaults ) );
529             parents.push_back( parent1 );
530             libcmis::FolderPtr parent2( new Folder( false, m_triggersFaults ) );
531             parents.push_back( parent2 );
532         }
533 
534         return parents;
535     }
536 
getContentStream(string)537     boost::shared_ptr< istream > Document::getContentStream( string /*streamId*/ )
538     {
539         if ( m_triggersFaults )
540             throw libcmis::Exception( "Fault triggered" );
541 
542         bool oldOutOfMem = isOutOfMemory;
543         isOutOfMemory = false;
544         boost::shared_ptr< istream > stream( new stringstream( m_contentString ) );
545         isOutOfMemory = oldOutOfMem;
546         return stream;
547     }
548 
setContentStream(boost::shared_ptr<ostream> os,string,string,bool)549     void Document::setContentStream( boost::shared_ptr< ostream > os, string, string, bool )
550     {
551         if ( m_triggersFaults )
552             throw libcmis::Exception( "Fault triggered" );
553 
554         istream is( os->rdbuf( ) );
555         stringstream out;
556         is.seekg( 0 );
557         int bufSize = 2048;
558         char* buf = new char[ bufSize ];
559         while ( !is.eof( ) )
560         {
561             is.read( buf, bufSize );
562             size_t read = is.gcount( );
563             out.write( buf, read );
564         }
565         delete[] buf;
566 
567         m_contentString = out.str( );
568 
569         time( &m_refreshTimestamp );
570     }
571 
getContentType()572     string Document::getContentType( )
573     {
574         return "Document::ContentType";
575     }
576 
getContentFilename()577     string Document::getContentFilename( )
578     {
579         return "Document::ContentFilename";
580     }
581 
getContentLength()582     long Document::getContentLength( )
583     {
584         return long( 12345 );
585     }
586 
checkOut()587     libcmis::DocumentPtr Document::checkOut( )
588     {
589         if ( m_triggersFaults )
590             throw libcmis::Exception( "Fault triggered" );
591 
592         time( &m_refreshTimestamp );
593 
594         libcmis::DocumentPtr result( new Document( true, m_triggersFaults ) );
595         return result;
596     }
597 
cancelCheckout()598     void Document::cancelCheckout( )
599     {
600         if ( m_triggersFaults )
601             throw libcmis::Exception( "Fault triggered" );
602 
603         time( &m_refreshTimestamp );
604     }
605 
checkIn(bool,string,const PropertyPtrMap & properties,boost::shared_ptr<ostream> os,string contentType,string filename)606     libcmis::DocumentPtr Document::checkIn( bool, string, const PropertyPtrMap& properties,
607                   boost::shared_ptr< ostream > os, string contentType, string filename )
608     {
609         if ( m_triggersFaults )
610             throw libcmis::Exception( "Fault triggered" );
611 
612         m_properties = properties;
613         setContentStream( os, contentType, filename );
614         time( &m_refreshTimestamp );
615 
616         return libcmis::DocumentPtr( new Document( true, false ) );
617     }
618 
getAllVersions()619     vector< libcmis::DocumentPtr > Document::getAllVersions( )
620     {
621         if ( m_triggersFaults )
622             throw libcmis::Exception( "Fault triggered" );
623 
624         vector< libcmis::DocumentPtr > versions;
625 
626         libcmis::DocumentPtr version1( new Document( true, false ) );
627         versions.push_back( version1 );
628         libcmis::DocumentPtr version2( new Document( true, false ) );
629         versions.push_back( version2 );
630 
631         return versions;
632     }
633 }
634