Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fuzzer should change objects with its public setters#439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Markoutte merged 3 commits intomainfrompelevin/289
Jul 12, 2022

Conversation

@Markoutte
Copy link
Collaborator

Description

Fuzzing now can create object with empty constructor and change that object with public field or field setter if exists.
Setter can have any return type but must have corresponding getter.

Fixes#289

Type of Change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Automated Testing

org.utbot.framework.plugin.api.ModelProviderTest#test complex object is created with setters

Manual Scenario

Try to generate test for methodequalsTo from this class:

publicclassMyJavaObject {publicstaticintpubStaticField;publicfinalintpubFinalField =0;publicintpubField;publicintpubFieldWithSetter;privateintprvField;privateintprvFieldWithSetter;publicintgetPubFieldWithSetter() {returnpubFieldWithSetter;    }publicvoidsetPubFieldWithSetter(intpubFieldWithSetter) {this.pubFieldWithSetter =pubFieldWithSetter;    }publicintgetPrvFieldWithSetter() {returnprvFieldWithSetter;    }publicvoidsetPrvFieldWithSetter(intprvFieldWithSetter) {this.prvFieldWithSetter =prvFieldWithSetter;    }publicbooleanequalsTo(MyJavaObjecto) {if (o.prvField >0) {}if (this ==o)returntrue;if (pubStaticField !=o.pubStaticField)returnfalse;if (pubFinalField !=o.pubFinalField)returnfalse;if (pubField !=o.pubField)returnfalse;if (pubFieldWithSetter !=o.pubFieldWithSetter)returnfalse;if (prvField !=o.prvField)returnfalse;if (prvFieldWithSetter !=o.prvFieldWithSetter)returnfalse;returntrue;    }}

Example of result test:

@Test@DisplayName("equalsTo: o = MyJavaObject() -> return true")publicvoidtestEqualsToReturnsTrue() {MyJavaObjectmyJavaObject =newMyJavaObject();MyJavaObjectmyJavaObject1 =newMyJavaObject();myJavaObject1.pubField =0;myJavaObject1.setPubFieldWithSetter(0);myJavaObject1.setPrvFieldWithSetter(0);booleanactual =myJavaObject.equalsTo(myJavaObject1);assertTrue(actual);}

Checklist (remove irrelevant options):

  • The change followed the style guidelines of the UTBot project
  • Self-review of the code is passed
  • The change contains enough commentaries, particularly in hard-to-understand areas
  • New documentation is provided or existed one is altered
  • No new warnings
  • All tests pass locally with my changes

fieldValues.asSequence().mapIndexedNotNull { index, value->
val field= fields[index]
when {
field.setter!=null->UtExecutableCallModel(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We prefer direct accessors inAssembleModelGenerator.chooseModificator

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

At the moment fuzzing depends on only framework-api module. Therefore,AssembleModelGenerator.chooseModificator can be accessed from fuzzing module.

FieldDescription(
field.name,
field.type.id,
field.isPublic&&!field.isFinal&&!field.isStatic,
Copy link
Collaborator

@EgorkaKulikovEgorkaKulikovJul 11, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why only public, not package private, it sometimes also can be set directly

null
}
}
privatevalField.isPublic
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why do you prefer to do all this logic on aField, not on aFieldId? Related to top level methods too.

it hasModifier.PUBLIC&&
it.name== setterName&&
it.parameterCount==1&&
it.parameterTypes[0]== field.type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Possibly we should enlargeStatementsStorage.isSetterOrDirectAccessor method with this type check.

privatevalField.isStatic
get()= has(Modifier.STATIC)

privateinfixfun Field.has(modifier:Int)= (modifiersand modifier)!=0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I do not understand what happens in this line and in the next one...

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The functionhas takesfield.modifiers as integer with bitmask for a concreate modifier flag (STATIC in this sample) and then compares it with 0. If it is 0 then the flag wasn't set in field.modifiers. In Java it looks like:

field.getModifiers() &Modifier.STATIC !=0

That checks that this field is static.

Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I replaced it with methodisAccessible(modifiers: Int, packageName: String?)

Copy link
Collaborator

@EgorkaKulikovEgorkaKulikov left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Left several comments, important moment are that direct accessor is now considered more important than setter and missed package-private fields in the lists of candidates for direct accessing.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@EgorkaKulikovEgorkaKulikovEgorkaKulikov approved these changes

@dtimdtimAwaiting requested review from dtim

@denis-fokindenis-fokinAwaiting requested review from denis-fokin

Assignees

No one assigned

Labels

None yet

Projects

Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Fuzzer should change objects which are created with empty constructor by its public setters

3 participants

@Markoutte@EgorkaKulikov

[8]ページ先頭

©2009-2025 Movatter.jp