<commit_before>from __future__ import nested_scopes
from twisted.internet import reactor
import socket, unittest
from twistedsnmp import agent, agentprotocol, twinetables, agentproxy
from twistedsnmp import snmpprotocol, massretriever
from twistedsnmp.test import basetestcase
from twistedsnmp.pysnmpproto import v2c,v1, error, oid

class GetRetrieverV1( basetestcase.BaseTestCase ):
	version = 'v1'
	oidsForTesting = [
		(oid.OID(key),value)
		for key,value in [
			('.1.3.6.1.2.1.1.1.0', 'Hello world!'),
			('.1.3.6.1.2.1.1.2.0', 32),
			('.1.3.6.1.2.1.1.3.0', v1.IpAddress('127.0.0.1')),
			('.1.3.6.1.2.1.1.4.0', v1.OctetString('From Octet String')),
			('.1.3.6.1.2.1.2.1.0', 'Hello world!'),
			('.1.3.6.1.2.1.2.2.0', 32),
			('.1.3.6.1.2.1.2.3.0', v1.IpAddress('127.0.0.1')),
			('.1.3.6.1.2.1.2.4.0', v1.OctetString('From Octet String')),
		] + [
			('.1.3.6.1.2.1.3.%s.0'%i, 32)
			for i in xrange( 512 )
		] + [
			('.1.3.6.2.1.0', 'Hello world!'),
			('.1.3.6.2.2.0', 32),
			('.1.3.6.2.3.0', v1.IpAddress('127.0.0.1')),
			('.1.3.6.2.4.0', v1.OctetString('From Octet String')),
		]
	]
	#good
	def test_simpleGet( self ):
		"""Can retrieve a single simple value?"""
		d = self.client.get( [
			'.1.3.6.1.2.1.1.1.0',
		] )
		self.doUntilFinish( d )

		assert self.success, self.response
		assert isinstance( self.response, dict ), self.response
		assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1.1.0') ), self.response
		assert self.response[oid.OID('.1.3.6.1.2.1.1.1.0') ] == 'Hello world!', self.response

	#good
	def test_tableGet( self ):
		"""Can retrieve a tabular value?"""
		d = self.client.getTable( [
			'.1.3.6.1.2.1.1'
		] )
		self.doUntilFinish( d )

		assert self.success, self.response
		assert isinstance( self.response, dict ), self.response
		assert self.response.has_key(
			oid.OID('.1.3.6.1.2.1.1')
		), (self.response,self)
		tableData = self.response[oid.OID('.1.3.6.1.2.1.1') ]
		assert isinstance(tableData, dict)
		assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData
	def test_tableGetWithStart( self ):
		"""Can retrieve a tabular value?"""
		d = self.client.getTable( 
			[
				'.1.3.6.1.2.1.1'
			],
			startOIDs = [
				'.1.3.6.1.2.1.1.3.0'
			],
		)
		self.doUntilFinish( d )

		assert self.success, self.response
		assert isinstance( self.response, dict ), self.response
		assert self.response.has_key(
			oid.OID('.1.3.6.1.2.1.1')
		), (self.response,self)
		tableData = self.response[oid.OID('.1.3.6.1.2.1.1') ]
		assert isinstance(tableData, dict)
		# won't have this key because started later...
		assert not tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData
		# should (only) have this key because started at 3...
		assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.4.0')), tableData
		assert len(tableData) == 1

	#good
	def test_tableGetMissing( self ):
		"""Does tabular retrieval ignore non-existent oid-sets?"""
		d = self.client.getTable( [
			'.1.3.6.1.2.1.5'
		] )
		self.doUntilFinish( d )
		assert self.success, self.response
		assert self.response == {}, self.response

	#good
	def test_tableGetAll( self ):
		"""Does tabular retrieval work specifying a distant parent (e.g. .1.3.6)?"""
		d = self.client.getTable( [
			'.1.3.6'
		] )
		self.doUntilFinish( d )
		assert self.success, self.response
		assert self.response == {
			oid.OID('.1.3.6'):dict( self.oidsForTesting )
		}, self.response
	#bad
	def test_multiTableGet( self ):
		oids = [
			'.1.3.6.1.2.1.1',
			'.1.3.6.1.2.1.2',
			'.1.3.6.2',
		]
		d = self.client.getTable( oids )
		self.doUntilFinish( d )
		if not self.success:
			raise self.response.value
		else:
			for oidObject in oids:
				assert self.response.has_key( oid.OID(oidObject) )
	#good
	def test_multiTableGetBad( self ):
		oids = [
			'.1.3.6.1.2.1.1',
			'.1.3.6.1.2.1.2',
			'.1.3.6.2',
			'.1.3.6.3',
		]
		d = self.client.getTable( oids )
		self.doUntilFinish( d )
		for oidObject in oids[:-1]:
			assert self.response.has_key( oid.OID(oidObject) )
		assert not self.response.has_key( oid.OID(oids[-1]) ), self.response

	#good
	def test_socketFailure( self ):
		"""Test whether socket failure on send is caught properly

		Previous versions have not caught failure-on-send conditions,
		which can wind up having unexpected consequences, particularly
		with things such as mass-retriever, which build on top of the
		basic AgentProxy.
		"""
		import socket
		def mockSend( message ):
			raise socket.error(65, 'No route to host')
		self.client.send = mockSend
		d = self.client.get( [
			'.1.3.6.1.2.1.1.1.0',
		] )
		self.doUntilFinish( d )<commit_msg>Add test to be sure that errors in areWeDone are reported as errors in table retrieval<commit_after>from __future__ import nested_scopes
from twisted.internet import reactor
import socket, unittest
from twistedsnmp import agent, agentprotocol, twinetables, agentproxy
from twistedsnmp import snmpprotocol, massretriever, tableretriever
from twistedsnmp.test import basetestcase
from twistedsnmp.pysnmpproto import v2c,v1, error, oid

class GetRetrieverV1( basetestcase.BaseTestCase ):
	version = 'v1'
	oidsForTesting = [
		(oid.OID(key),value)
		for key,value in [
			('.1.3.6.1.2.1.1.1.0', 'Hello world!'),
			('.1.3.6.1.2.1.1.2.0', 32),
			('.1.3.6.1.2.1.1.3.0', v1.IpAddress('127.0.0.1')),
			('.1.3.6.1.2.1.1.4.0', v1.OctetString('From Octet String')),
			('.1.3.6.1.2.1.2.1.0', 'Hello world!'),
			('.1.3.6.1.2.1.2.2.0', 32),
			('.1.3.6.1.2.1.2.3.0', v1.IpAddress('127.0.0.1')),
			('.1.3.6.1.2.1.2.4.0', v1.OctetString('From Octet String')),
		] + [
			('.1.3.6.1.2.1.3.%s.0'%i, 32)
			for i in xrange( 512 )
		] + [
			('.1.3.6.2.1.0', 'Hello world!'),
			('.1.3.6.2.2.0', 32),
			('.1.3.6.2.3.0', v1.IpAddress('127.0.0.1')),
			('.1.3.6.2.4.0', v1.OctetString('From Octet String')),
		]
	]
	#good
	def test_simpleGet( self ):
		"""Can retrieve a single simple value?"""
		d = self.client.get( [
			'.1.3.6.1.2.1.1.1.0',
		] )
		self.doUntilFinish( d )

		assert self.success, self.response
		assert isinstance( self.response, dict ), self.response
		assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1.1.0') ), self.response
		assert self.response[oid.OID('.1.3.6.1.2.1.1.1.0') ] == 'Hello world!', self.response

	#good
	def test_tableGet( self ):
		"""Can retrieve a tabular value?"""
		d = self.client.getTable( [
			'.1.3.6.1.2.1.1'
		] )
		self.doUntilFinish( d )

		assert self.success, self.response
		assert isinstance( self.response, dict ), self.response
		assert self.response.has_key(
			oid.OID('.1.3.6.1.2.1.1')
		), (self.response,self)
		tableData = self.response[oid.OID('.1.3.6.1.2.1.1') ]
		assert isinstance(tableData, dict)
		assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData
	def test_tableGetWithStart( self ):
		"""Can retrieve a tabular value?"""
		d = self.client.getTable( 
			[
				'.1.3.6.1.2.1.1'
			],
			startOIDs = [
				'.1.3.6.1.2.1.1.3.0'
			],
		)
		self.doUntilFinish( d )

		assert self.success, self.response
		assert isinstance( self.response, dict ), self.response
		assert self.response.has_key(
			oid.OID('.1.3.6.1.2.1.1')
		), (self.response,self)
		tableData = self.response[oid.OID('.1.3.6.1.2.1.1') ]
		assert isinstance(tableData, dict)
		# won't have this key because started later...
		assert not tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData
		# should (only) have this key because started at 3...
		assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.4.0')), tableData
		assert len(tableData) == 1

	#good
	def test_tableGetMissing( self ):
		"""Does tabular retrieval ignore non-existent oid-sets?"""
		d = self.client.getTable( [
			'.1.3.6.1.2.1.5'
		] )
		self.doUntilFinish( d )
		assert self.success, self.response
		assert self.response == {}, self.response

	#good
	def test_tableGetAll( self ):
		"""Does tabular retrieval work specifying a distant parent (e.g. .1.3.6)?"""
		d = self.client.getTable( [
			'.1.3.6'
		] )
		self.doUntilFinish( d )
		assert self.success, self.response
		assert self.response == {
			oid.OID('.1.3.6'):dict( self.oidsForTesting )
		}, self.response
	#bad
	def test_multiTableGet( self ):
		oids = [
			'.1.3.6.1.2.1.1',
			'.1.3.6.1.2.1.2',
			'.1.3.6.2',
		]
		d = self.client.getTable( oids )
		self.doUntilFinish( d )
		if not self.success:
			raise self.response.value
		else:
			for oidObject in oids:
				assert self.response.has_key( oid.OID(oidObject) )
	#good
	def test_multiTableGetBad( self ):
		oids = [
			'.1.3.6.1.2.1.1',
			'.1.3.6.1.2.1.2',
			'.1.3.6.2',
			'.1.3.6.3',
		]
		d = self.client.getTable( oids )
		self.doUntilFinish( d )
		for oidObject in oids[:-1]:
			assert self.response.has_key( oid.OID(oidObject) )
		assert not self.response.has_key( oid.OID(oids[-1]) ), self.response
	#good
	def test_tableGetErrorReported( self ):
		oids = [
			'.1.3.6.1.2.1.1',
			'.1.3.6.1.2.1.2',
			'.1.3.6.2',
			'.1.3.6.3',
		]
		originalAreWeDone = tableretriever.TableRetriever.areWeDone
		def raiseError( *args, **named ):
			raise TypeError( """Blah""" )
		tableretriever.TableRetriever.areWeDone = raiseError
		try:
			d = self.client.getTable( oids )
			self.doUntilFinish( d )
		finally:
			tableretriever.TableRetriever.areWeDone = originalAreWeDone
		assert not self.success
		assert isinstance( self.response.value, TypeError )
		assert self.response.value.args == ('Blah',)

	#good
	def test_socketFailure( self ):
		"""Test whether socket failure on send is caught properly

		Previous versions have not caught failure-on-send conditions,
		which can wind up having unexpected consequences, particularly
		with things such as mass-retriever, which build on top of the
		basic AgentProxy.
		"""
		import socket
		def mockSend( message ):
			raise socket.error(65, 'No route to host')
		self.client.send = mockSend
		d = self.client.get( [
			'.1.3.6.1.2.1.1.1.0',
		] )
		self.doUntilFinish( d )
{'fraction_non_alphanumeric': 0.18781157798351816, 'fraction_numerical': 0.06714823481534235, 'mean_word_length': 2.9749292357460573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 112, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4286504', 'n_tokens_mistral': 4545, 'n_tokens_neox': 4080, 'n_words': 787}